blob: 4e9bfabd5fd7841d4ba23f584f38f0ca5811c790 [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"
Jamie Madill570e04d2013-06-21 09:15:33 -040017#include "compiler/FlagStd140Structs.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000018
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000019#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000020#include <cfloat>
21#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000022
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000023namespace sh
24{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000025// Integer to TString conversion
26TString str(int i)
27{
28 char buffer[20];
kbr@chromium.orgddb6e8e2012-04-25 00:48:13 +000029 snprintf(buffer, sizeof(buffer), "%d", i);
daniel@transgaming.com005c7392010-04-15 20:45:27 +000030 return buffer;
31}
32
Nicolas Capense0ba27a2013-06-24 16:10:52 -040033TString OutputHLSL::TextureFunction::name() const
34{
35 TString name = "gl_texture";
36
37 if (sampler == EbtSampler2D ||
38 sampler == EbtISampler2D ||
Nicolas Capensfb50dff2013-06-24 16:16:23 -040039 sampler == EbtUSampler2D ||
40 sampler == EbtSampler2DArray ||
41 sampler == EbtISampler2DArray ||
42 sampler == EbtUSampler2DArray)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040043 {
44 name += "2D";
45 }
46 else if (sampler == EbtSampler3D ||
47 sampler == EbtISampler3D ||
48 sampler == EbtUSampler3D)
49 {
50 name += "3D";
51 }
52 else if (sampler == EbtSamplerCube ||
53 sampler == EbtISamplerCube ||
54 sampler == EbtUSamplerCube)
55 {
56 name += "Cube";
57 }
58 else UNREACHABLE();
59
60 if (proj)
61 {
62 name += "Proj";
63 }
64
65 switch(mipmap)
66 {
67 case IMPLICIT: break;
68 case BIAS: break;
69 case LOD: name += "Lod"; break;
70 case LOD0: name += "Lod0"; break;
71 default: UNREACHABLE();
72 }
73
74 return name + "(";
75}
76
77bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
78{
79 if (sampler < rhs.sampler) return true;
80 if (coords < rhs.coords) return true;
81 if (!proj && rhs.proj) return true;
82 if (mipmap < rhs.mipmap) return true;
83
84 return false;
85}
86
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +000087OutputHLSL::OutputHLSL(TParseContext &context, const ShBuiltInResources& resources, ShShaderOutput outputType)
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +000088 : TIntermTraverser(true, true, true), mContext(context), mOutputType(outputType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000089{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000090 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +000091 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000092
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +000093 mUsesFragColor = false;
94 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +000095 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +000096 mUsesFragCoord = false;
97 mUsesPointCoord = false;
98 mUsesFrontFacing = false;
99 mUsesPointSize = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000100 mUsesXor = false;
101 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000102 mUsesMod2v = false;
103 mUsesMod2f = false;
104 mUsesMod3v = false;
105 mUsesMod3f = false;
106 mUsesMod4v = false;
107 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000108 mUsesFaceforward1 = false;
109 mUsesFaceforward2 = false;
110 mUsesFaceforward3 = false;
111 mUsesFaceforward4 = false;
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000112
113 for (unsigned int col = 0; col <= 4; col++)
114 {
115 for (unsigned int row = 0; row <= 4; row++)
116 {
117 mUsesEqualMat[col][row] = false;
118 }
119 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000120 mUsesEqualVec2 = false;
121 mUsesEqualVec3 = false;
122 mUsesEqualVec4 = false;
123 mUsesEqualIVec2 = false;
124 mUsesEqualIVec3 = false;
125 mUsesEqualIVec4 = false;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +0000126 mUsesEqualUVec2 = false;
127 mUsesEqualUVec3 = false;
128 mUsesEqualUVec4 = false;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000129 mUsesEqualBVec2 = false;
130 mUsesEqualBVec3 = false;
131 mUsesEqualBVec4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000132 mUsesAtan2_1 = false;
133 mUsesAtan2_2 = false;
134 mUsesAtan2_3 = false;
135 mUsesAtan2_4 = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000136
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000137 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
138
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000139 mScopeDepth = 0;
140
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000141 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000142
143 mContainsLoopDiscontinuity = false;
144 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000145 mInsideDiscontinuousLoop = false;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000146
147 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000148
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000149 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000150 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000151 if (mContext.shaderType == SH_FRAGMENT_SHADER)
152 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000153 mUniformRegister = 3; // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000154 }
155 else
156 {
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000157 mUniformRegister = 2; // Reserve registers for dx_DepthRange and dx_ViewAdjust
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000158 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000159 }
160 else
161 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000162 mUniformRegister = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000163 }
164
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000165 mSamplerRegister = 0;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000166 mInterfaceBlockRegister = 2; // Reserve registers for the default uniform block and driver constants
Jamie Madill574d9dd2013-06-20 11:55:56 -0400167 mPaddingCounter = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000168}
169
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000170OutputHLSL::~OutputHLSL()
171{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000172 delete mUnfoldShortCircuit;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000173}
174
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000175void OutputHLSL::output()
176{
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +0000177 mContainsLoopDiscontinuity = mContext.shaderType == SH_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400178 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
179 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000180
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000181 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 +0000182 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000183
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000184 mContext.infoSink().obj << mHeader.c_str();
185 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000186}
187
Jamie Madill570e04d2013-06-21 09:15:33 -0400188void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
189{
190 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
191 {
192 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
193
194 // This will mark the necessary block elements as referenced
195 flaggedNode->traverse(this);
196 TString structName(mBody.c_str());
197 mBody.erase();
198
199 mFlaggedStructOriginalNames[flaggedNode] = structName;
200
201 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
202 {
203 structName.erase(pos, 1);
204 }
205
206 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
207 }
208}
209
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000210TInfoSinkBase &OutputHLSL::getBodyStream()
211{
212 return mBody;
213}
214
daniel@transgaming.com043da132012-12-20 21:12:22 +0000215const ActiveUniforms &OutputHLSL::getUniforms()
216{
217 return mActiveUniforms;
218}
219
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000220const ActiveInterfaceBlocks &OutputHLSL::getInterfaceBlocks() const
221{
222 return mActiveInterfaceBlocks;
223}
224
Jamie Madill46131a32013-06-20 11:55:50 -0400225const ActiveShaderVariables &OutputHLSL::getOutputVariables() const
226{
227 return mActiveOutputVariables;
228}
229
Jamie Madilldefb6742013-06-20 11:55:51 -0400230const ActiveShaderVariables &OutputHLSL::getAttributes() const
231{
232 return mActiveAttributes;
233}
234
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000235int OutputHLSL::vectorSize(const TType &type) const
236{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000237 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000238 int arraySize = type.isArray() ? type.getArraySize() : 1;
239
240 return elementSize * arraySize;
241}
242
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000243TString OutputHLSL::interfaceBlockUniformName(const TType &interfaceBlockType, const TType &uniformType)
244{
245 if (interfaceBlockType.hasInstanceName())
246 {
247 return interfaceBlockType.getTypeName() + "." + uniformType.getFieldName();
248 }
249 else
250 {
251 return uniformType.getFieldName();
252 }
253}
254
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000255TString OutputHLSL::decoratePrivate(const TString &privateText)
256{
257 return "dx_" + privateText;
258}
259
260TString OutputHLSL::interfaceBlockStructName(const TType &interfaceBlockType)
261{
262 return decoratePrivate(interfaceBlockType.getTypeName()) + "_type";
263}
264
265TString OutputHLSL::interfaceBlockInstanceString(const TType& interfaceBlockType, unsigned int arrayIndex)
266{
267 if (!interfaceBlockType.hasInstanceName())
268 {
269 return "";
270 }
271 else if (interfaceBlockType.isArray())
272 {
273 return decoratePrivate(interfaceBlockType.getInstanceName()) + "_" + str(arrayIndex);
274 }
275 else
276 {
277 return decorate(interfaceBlockType.getInstanceName());
278 }
279}
280
Jamie Madillc835df62013-06-21 09:15:32 -0400281TString OutputHLSL::interfaceBlockMemberTypeString(const TType &memberType, TLayoutBlockStorage blockStorage)
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000282{
Jamie Madill529077d2013-06-20 11:55:54 -0400283 const TLayoutMatrixPacking matrixPacking = memberType.getLayoutQualifier().matrixPacking;
284 ASSERT(matrixPacking != EmpUnspecified);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000285
286 if (memberType.isMatrix())
287 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400288 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill529077d2013-06-20 11:55:54 -0400289 const TString &matrixPackString = (matrixPacking == EmpRowMajor ? "column_major" : "row_major");
290 return matrixPackString + " " + typeString(memberType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000291 }
292 else if (memberType.getBasicType() == EbtStruct)
293 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400294 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madillc835df62013-06-21 09:15:32 -0400295 return structureTypeName(memberType, matrixPacking == EmpColumnMajor, blockStorage == EbsStd140);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000296 }
297 else
298 {
299 return typeString(memberType);
300 }
301}
302
Jamie Madill574d9dd2013-06-20 11:55:56 -0400303TString OutputHLSL::std140PrePaddingString(const TType &type, int *elementIndex)
304{
305 if (type.getBasicType() == EbtStruct || type.isMatrix() || type.isArray())
306 {
307 // no padding needed, HLSL will align the field to a new register
308 *elementIndex = 0;
309 return "";
310 }
311
312 const GLenum glType = glVariableType(type);
313 const int numComponents = gl::UniformComponentCount(glType);
314
315 if (numComponents >= 4)
316 {
317 // no padding needed, HLSL will align the field to a new register
318 *elementIndex = 0;
319 return "";
320 }
321
322 if (*elementIndex + numComponents > 4)
323 {
324 // no padding needed, HLSL will align the field to a new register
325 *elementIndex = numComponents;
326 return "";
327 }
328
329 TString padding;
330
331 const int alignment = numComponents == 3 ? 4 : numComponents;
332 const int paddingOffset = (*elementIndex % alignment);
333
334 if (paddingOffset != 0)
335 {
336 // padding is neccessary
337 for (int paddingIndex = paddingOffset; paddingIndex < alignment; paddingIndex++)
338 {
339 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
340 }
341
342 *elementIndex += (alignment - paddingOffset);
343 }
344
345 *elementIndex += numComponents;
346 *elementIndex %= 4;
347
348 return padding;
349}
350
Jamie Madille4075c92013-06-21 09:15:32 -0400351TString OutputHLSL::std140PostPaddingString(const TType &type, bool useHLSLRowMajorPacking)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400352{
Jamie Madillc835df62013-06-21 09:15:32 -0400353 if (!type.isMatrix() && !type.isArray() && type.getBasicType() != EbtStruct)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400354 {
355 return "";
356 }
357
Jamie Madill574d9dd2013-06-20 11:55:56 -0400358 int numComponents = 0;
359
360 if (type.isMatrix())
361 {
Jamie Madille4075c92013-06-21 09:15:32 -0400362 // This method can also be called from structureString, which does not use layout qualifiers.
363 // Thus, use the method parameter for determining the matrix packing.
364 //
365 // Note HLSL row major packing corresponds to GL API column-major, and vice-versa, since we
366 // wish to always transpose GL matrices to play well with HLSL's matrix array indexing.
367 //
368 const bool isRowMajorMatrix = !useHLSLRowMajorPacking;
Jamie Madillc835df62013-06-21 09:15:32 -0400369 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400370 numComponents = gl::MatrixComponentCount(glType, isRowMajorMatrix);
371 }
Jamie Madillc835df62013-06-21 09:15:32 -0400372 else if (type.getBasicType() == EbtStruct)
373 {
Jamie Madille4075c92013-06-21 09:15:32 -0400374 const TString &structName = structureTypeName(type, useHLSLRowMajorPacking, true);
375 numComponents = mStd140StructElementIndexes[structName];
376
377 if (numComponents == 0)
378 {
379 return "";
380 }
Jamie Madillc835df62013-06-21 09:15:32 -0400381 }
Jamie Madill574d9dd2013-06-20 11:55:56 -0400382 else
383 {
Jamie Madillc835df62013-06-21 09:15:32 -0400384 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400385 numComponents = gl::UniformComponentCount(glType);
386 }
387
388 TString padding;
389 for (int paddingOffset = numComponents; paddingOffset < 4; paddingOffset++)
390 {
391 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
392 }
393 return padding;
394}
395
396TString OutputHLSL::interfaceBlockMemberString(const TTypeList &typeList, TLayoutBlockStorage blockStorage)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000397{
398 TString hlsl;
399
Jamie Madill574d9dd2013-06-20 11:55:56 -0400400 int elementIndex = 0;
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000401
402 for (unsigned int typeIndex = 0; typeIndex < typeList.size(); typeIndex++)
403 {
404 const TType &memberType = *typeList[typeIndex].type;
Jamie Madill574d9dd2013-06-20 11:55:56 -0400405
406 if (blockStorage == EbsStd140)
407 {
Jamie Madillc835df62013-06-21 09:15:32 -0400408 // 2 and 3 component vector types in some cases need pre-padding
409 hlsl += std140PrePaddingString(memberType, &elementIndex);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400410 }
411
Jamie Madillc835df62013-06-21 09:15:32 -0400412 hlsl += " " + interfaceBlockMemberTypeString(memberType, blockStorage) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000413 " " + decorate(memberType.getFieldName()) + arrayString(memberType) + ";\n";
Jamie Madill574d9dd2013-06-20 11:55:56 -0400414
415 // must pad out after matrices and arrays, where HLSL usually allows itself room to pack stuff
416 if (blockStorage == EbsStd140)
417 {
Jamie Madille4075c92013-06-21 09:15:32 -0400418 const bool useHLSLRowMajorPacking = (memberType.getLayoutQualifier().matrixPacking == EmpColumnMajor);
419 hlsl += std140PostPaddingString(memberType, useHLSLRowMajorPacking);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400420 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000421 }
422
423 return hlsl;
424}
425
426TString OutputHLSL::interfaceBlockStructString(const TType &interfaceBlockType)
427{
428 const TTypeList &typeList = *interfaceBlockType.getStruct();
Jamie Madill574d9dd2013-06-20 11:55:56 -0400429 const TLayoutBlockStorage blockStorage = interfaceBlockType.getLayoutQualifier().blockStorage;
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000430
431 return "struct " + interfaceBlockStructName(interfaceBlockType) + "\n"
432 "{\n" +
Jamie Madill574d9dd2013-06-20 11:55:56 -0400433 interfaceBlockMemberString(typeList, blockStorage) +
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000434 "};\n\n";
435}
436
437TString OutputHLSL::interfaceBlockString(const TType &interfaceBlockType, unsigned int registerIndex, unsigned int arrayIndex)
438{
439 const TString &arrayIndexString = (arrayIndex != GL_INVALID_INDEX ? decorate(str(arrayIndex)) : "");
440 const TString &blockName = interfaceBlockType.getTypeName() + arrayIndexString;
441 TString hlsl;
442
443 hlsl += "cbuffer " + blockName + " : register(b" + str(registerIndex) + ")\n"
444 "{\n";
445
446 if (interfaceBlockType.hasInstanceName())
447 {
448 hlsl += " " + interfaceBlockStructName(interfaceBlockType) + " " + interfaceBlockInstanceString(interfaceBlockType, arrayIndex) + ";\n";
449 }
450 else
451 {
452 const TTypeList &typeList = *interfaceBlockType.getStruct();
Jamie Madill574d9dd2013-06-20 11:55:56 -0400453 const TLayoutBlockStorage blockStorage = interfaceBlockType.getLayoutQualifier().blockStorage;
454 hlsl += interfaceBlockMemberString(typeList, blockStorage);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000455 }
456
457 hlsl += "};\n\n";
458
459 return hlsl;
460}
461
Jamie Madill440dc742013-06-20 11:55:55 -0400462// Use the same layout for packed and shared
463void setBlockLayout(InterfaceBlock *interfaceBlock, BlockLayoutType newLayout)
464{
465 interfaceBlock->layout = newLayout;
466 interfaceBlock->blockInfo.clear();
467
468 switch (newLayout)
469 {
470 case BLOCKLAYOUT_SHARED:
471 case BLOCKLAYOUT_PACKED:
472 {
473 HLSLBlockEncoder hlslEncoder(&interfaceBlock->blockInfo);
474 hlslEncoder.encodeFields(interfaceBlock->activeUniforms);
475 interfaceBlock->dataSize = hlslEncoder.getBlockSize();
476 }
477 break;
478
479 case BLOCKLAYOUT_STANDARD:
480 {
481 Std140BlockEncoder stdEncoder(&interfaceBlock->blockInfo);
482 stdEncoder.encodeFields(interfaceBlock->activeUniforms);
483 interfaceBlock->dataSize = stdEncoder.getBlockSize();
484 }
485 break;
486
487 default:
488 UNREACHABLE();
489 break;
490 }
491}
492
Jamie Madill574d9dd2013-06-20 11:55:56 -0400493BlockLayoutType convertBlockLayoutType(TLayoutBlockStorage blockStorage)
494{
495 switch (blockStorage)
496 {
497 case EbsPacked: return BLOCKLAYOUT_PACKED;
498 case EbsShared: return BLOCKLAYOUT_SHARED;
499 case EbsStd140: return BLOCKLAYOUT_STANDARD;
500 default: UNREACHABLE(); return BLOCKLAYOUT_SHARED;
501 }
502}
503
Jamie Madill570e04d2013-06-21 09:15:33 -0400504TString OutputHLSL::structInitializerString(int indent, const TTypeList &structMembers, const TString &structName)
505{
506 TString init;
507
508 TString preIndentString;
509 TString fullIndentString;
510
511 for (int spaces = 0; spaces < (indent * 4); spaces++)
512 {
513 preIndentString += ' ';
514 }
515
516 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
517 {
518 fullIndentString += ' ';
519 }
520
521 init += preIndentString + "{\n";
522
523 for (unsigned int memberIndex = 0; memberIndex < structMembers.size(); memberIndex++)
524 {
525 const TType &memberType = *structMembers[memberIndex].type;
526 const TString &fieldName = decorate(memberType.getFieldName());
527
528 if (memberType.getBasicType() == EbtStruct)
529 {
530 const TTypeList &nestedStructMembers = *memberType.getStruct();
531 init += structInitializerString(indent + 1, nestedStructMembers, structName + "." + fieldName);
532 }
533 else
534 {
535 init += fullIndentString + structName + "." + fieldName + ",\n";
536 }
537 }
538
539 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
540
541 return init;
542}
543
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000544void OutputHLSL::header()
545{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000546 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000547
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000548 TString uniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000549 TString interfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000550 TString varyings;
551 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400552 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000553
554 for (ReferencedSymbols::const_iterator uniform = mReferencedUniforms.begin(); uniform != mReferencedUniforms.end(); uniform++)
555 {
556 const TType &type = uniform->second->getType();
557 const TString &name = uniform->second->getSymbol();
558
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000559 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture
560 {
561 int index = samplerRegister(mReferencedUniforms[name]);
562
563 uniforms += "uniform SamplerState sampler_" + decorateUniform(name, type) + arrayString(type) +
564 " : register(s" + str(index) + ");\n";
565
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000566 uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) +
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000567 " : register(t" + str(index) + ");\n";
568 }
569 else
570 {
571 uniforms += "uniform " + typeString(type) + " " + decorateUniform(name, type) + arrayString(type) +
572 " : register(" + registerString(mReferencedUniforms[name]) + ");\n";
573 }
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000574 }
575
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000576 for (ReferencedSymbols::const_iterator interfaceBlockIt = mReferencedInterfaceBlocks.begin(); interfaceBlockIt != mReferencedInterfaceBlocks.end(); interfaceBlockIt++)
577 {
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000578 const TType &nodeType = interfaceBlockIt->second->getType();
579 const TType &interfaceBlockType = nodeType.isInterfaceBlockMember() ? *nodeType.getInterfaceBlockType() : nodeType;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000580 const TString &blockName = interfaceBlockType.getTypeName();
581 const TTypeList &typeList = *interfaceBlockType.getStruct();
582
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000583 const unsigned int arraySize = interfaceBlockType.isArray() ? interfaceBlockType.getArraySize() : 0;
584 sh::InterfaceBlock interfaceBlock(blockName.c_str(), arraySize, mInterfaceBlockRegister);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000585 for (unsigned int typeIndex = 0; typeIndex < typeList.size(); typeIndex++)
586 {
587 const TType &memberType = *typeList[typeIndex].type;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000588 const TString &fullUniformName = interfaceBlockUniformName(interfaceBlockType, memberType);
589 declareUniformToList(memberType, fullUniformName, typeIndex, interfaceBlock.activeUniforms);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000590 }
591
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000592 mInterfaceBlockRegister += std::max(1u, interfaceBlock.arraySize);
593
Jamie Madill574d9dd2013-06-20 11:55:56 -0400594 BlockLayoutType blockLayoutType = convertBlockLayoutType(interfaceBlockType.getLayoutQualifier().blockStorage);
595 setBlockLayout(&interfaceBlock, blockLayoutType);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000596 mActiveInterfaceBlocks.push_back(interfaceBlock);
597
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000598 if (interfaceBlockType.hasInstanceName())
599 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000600 interfaceBlocks += interfaceBlockStructString(interfaceBlockType);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000601 }
602
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000603 if (arraySize > 0)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000604 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000605 for (unsigned int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
606 {
607 interfaceBlocks += interfaceBlockString(interfaceBlockType, interfaceBlock.registerIndex + arrayIndex, arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000608 }
609 }
610 else
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000611 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000612 interfaceBlocks += interfaceBlockString(interfaceBlockType, interfaceBlock.registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000613 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000614 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000615
Jamie Madill570e04d2013-06-21 09:15:33 -0400616 for (auto flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
617 {
618 TIntermTyped *structNode = flaggedStructIt->first;
619 const TString &mappedName = flaggedStructIt->second;
620 const TType &structType = structNode->getType();
621 const TTypeList &structMembers = *structType.getStruct();
622 const TString &originalName = mFlaggedStructOriginalNames[structNode];
623
624 flaggedStructs += "static " + decorate(structType.getTypeName()) + " " + mappedName + " =\n";
625 flaggedStructs += structInitializerString(0, structMembers, originalName);
626 flaggedStructs += "\n";
627 }
628
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000629 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
630 {
631 const TType &type = varying->second->getType();
632 const TString &name = varying->second->getSymbol();
633
634 // Program linking depends on this exact format
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +0000635 varyings += "static " + interpolationString(type.getQualifier()) + " " + typeString(type) + " " +
636 decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000637 }
638
639 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
640 {
641 const TType &type = attribute->second->getType();
642 const TString &name = attribute->second->getSymbol();
643
644 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400645
646 ShaderVariable shaderVar(glVariableType(type), glVariablePrecision(type), name.c_str(),
647 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
648 mActiveAttributes.push_back(shaderVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000649 }
650
Jamie Madill529077d2013-06-20 11:55:54 -0400651 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
652 {
653 out << *structDeclaration;
654 }
655
656 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
657 {
658 out << *constructor;
659 }
660
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400661 if (mContext.shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000662 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000663 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000664 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000665
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000666 out << "// Varyings\n";
667 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400668 out << "\n";
669
670 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000671 {
Jamie Madill46131a32013-06-20 11:55:50 -0400672 for (auto outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000673 {
Jamie Madill46131a32013-06-20 11:55:50 -0400674 const TString &variableName = outputVariableIt->first;
675 const TType &variableType = outputVariableIt->second->getType();
676 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
677
678 out << "static " + typeString(variableType) + " out_" + variableName + arrayString(variableType) +
679 " = " + initializer(variableType) + ";\n";
680
681 ShaderVariable outputVar(glVariableType(variableType), glVariablePrecision(variableType), variableName.c_str(),
682 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
683 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000684 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000685 }
Jamie Madill46131a32013-06-20 11:55:50 -0400686 else
687 {
688 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
689
690 out << "static float4 gl_Color[" << numColorValues << "] =\n"
691 "{\n";
692 for (unsigned int i = 0; i < numColorValues; i++)
693 {
694 out << " float4(0, 0, 0, 0)";
695 if (i + 1 != numColorValues)
696 {
697 out << ",";
698 }
699 out << "\n";
700 }
701
702 out << "};\n";
703 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000704
705 if (mUsesFragCoord)
706 {
707 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
708 }
709
710 if (mUsesPointCoord)
711 {
712 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
713 }
714
715 if (mUsesFrontFacing)
716 {
717 out << "static bool gl_FrontFacing = false;\n";
718 }
719
720 out << "\n";
721
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000722 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000723 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000724 out << "struct gl_DepthRangeParameters\n"
725 "{\n"
726 " float near;\n"
727 " float far;\n"
728 " float diff;\n"
729 "};\n"
730 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000731 }
732
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000733 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000734 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000735 out << "cbuffer DriverConstants : register(b1)\n"
736 "{\n";
737
738 if (mUsesDepthRange)
739 {
740 out << " float3 dx_DepthRange : packoffset(c0);\n";
741 }
742
743 if (mUsesFragCoord)
744 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000745 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000746 }
747
748 if (mUsesFragCoord || mUsesFrontFacing)
749 {
750 out << " float3 dx_DepthFront : packoffset(c2);\n";
751 }
752
753 out << "};\n";
754 }
755 else
756 {
757 if (mUsesDepthRange)
758 {
759 out << "uniform float3 dx_DepthRange : register(c0);";
760 }
761
762 if (mUsesFragCoord)
763 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000764 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000765 }
766
767 if (mUsesFragCoord || mUsesFrontFacing)
768 {
769 out << "uniform float3 dx_DepthFront : register(c2);\n";
770 }
771 }
772
773 out << "\n";
774
775 if (mUsesDepthRange)
776 {
777 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
778 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000779 }
780
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000781 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000782 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000783
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000784 if (!interfaceBlocks.empty())
785 {
786 out << interfaceBlocks;
787 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400788
789 if (!flaggedStructs.empty())
790 {
791 out << "// Std140 Structures accessed by value\n";
792 out << "\n";
793 out << flaggedStructs;
794 out << "\n";
795 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000796 }
797
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000798 if (usingMRTExtension && mNumRenderTargets > 1)
799 {
800 out << "#define GL_USES_MRT\n";
801 }
802
803 if (mUsesFragColor)
804 {
805 out << "#define GL_USES_FRAG_COLOR\n";
806 }
807
808 if (mUsesFragData)
809 {
810 out << "#define GL_USES_FRAG_DATA\n";
811 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000812 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000813 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000814 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000815 out << "// Attributes\n";
816 out << attributes;
817 out << "\n"
818 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
819
820 if (mUsesPointSize)
821 {
822 out << "static float gl_PointSize = float(1);\n";
823 }
824
825 out << "\n"
826 "// Varyings\n";
827 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000828 out << "\n";
829
830 if (mUsesDepthRange)
831 {
832 out << "struct gl_DepthRangeParameters\n"
833 "{\n"
834 " float near;\n"
835 " float far;\n"
836 " float diff;\n"
837 "};\n"
838 "\n";
839 }
840
841 if (mOutputType == SH_HLSL11_OUTPUT)
842 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000843 if (mUsesDepthRange)
844 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000845 out << "cbuffer DriverConstants : register(b1)\n"
846 "{\n"
847 " float3 dx_DepthRange : packoffset(c0);\n"
848 "};\n"
849 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000850 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000851 }
852 else
853 {
854 if (mUsesDepthRange)
855 {
856 out << "uniform float3 dx_DepthRange : register(c0);\n";
857 }
858
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000859 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000860 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000861 }
862
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000863 if (mUsesDepthRange)
864 {
865 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
866 "\n";
867 }
868
869 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000871
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000872 if (!interfaceBlocks.empty())
873 {
874 out << interfaceBlocks;
875 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400876
877 if (!flaggedStructs.empty())
878 {
879 out << "// Std140 Structures accessed by value\n";
880 out << "\n";
881 out << flaggedStructs;
882 out << "\n";
883 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000884 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400885 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000886
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400887 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
888 {
889 // Return type
890 switch(textureFunction->sampler)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000891 {
Nicolas Capensfb50dff2013-06-24 16:16:23 -0400892 case EbtSampler2D: out << "float4 "; break;
893 case EbtSampler3D: out << "float4 "; break;
894 case EbtSamplerCube: out << "float4 "; break;
895 case EbtSampler2DArray: out << "float4 "; break;
896 case EbtISampler2D: out << "int4 "; break;
897 case EbtISampler3D: out << "int4 "; break;
898 case EbtISamplerCube: out << "int4 "; break;
899 case EbtISampler2DArray: out << "int4 "; break;
900 case EbtUSampler2D: out << "uint4 "; break;
901 case EbtUSampler3D: out << "uint4 "; break;
902 case EbtUSamplerCube: out << "uint4 "; break;
903 case EbtUSampler2DArray: out << "uint4 "; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400904 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000905 }
906
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400907 // Function name
908 out << textureFunction->name();
909
910 // Argument list
911 int hlslCoords = 4;
912
913 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000914 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400915 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000916 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400917 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
918 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
919 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000920 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400921
922 switch(textureFunction->mipmap)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000923 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400924 case TextureFunction::IMPLICIT: break;
925 case TextureFunction::BIAS: hlslCoords = 4; break;
926 case TextureFunction::LOD: hlslCoords = 4; break;
927 case TextureFunction::LOD0: hlslCoords = 4; break;
928 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000929 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400930 }
931 else if (mOutputType == SH_HLSL11_OUTPUT)
932 {
933 switch(textureFunction->sampler)
934 {
Nicolas Capensfb50dff2013-06-24 16:16:23 -0400935 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
936 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
937 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
938 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 2; break;
939 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
940 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
941 case EbtISamplerCube: out << "TextureCube<int4> x, SamplerState s"; hlslCoords = 3; break;
942 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 2; break;
943 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
944 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
945 case EbtUSamplerCube: out << "TextureCube<uint4> x, SamplerState s"; hlslCoords = 3; break;
946 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 2; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400947 default: UNREACHABLE();
948 }
949 }
950 else UNREACHABLE();
951
952 switch(textureFunction->coords)
953 {
954 case 2: out << ", float2 t"; break;
955 case 3: out << ", float3 t"; break;
956 case 4: out << ", float4 t"; break;
957 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000958 }
959
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400960 switch(textureFunction->mipmap)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000961 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400962 case TextureFunction::IMPLICIT: break;
963 case TextureFunction::BIAS: out << ", float bias"; break;
964 case TextureFunction::LOD: out << ", float lod"; break;
965 case TextureFunction::LOD0: break;
966 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000967 }
968
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400969 out << ")\n"
970 "{\n"
971 " return ";
972
973 // HLSL intrinsic
974 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000975 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400976 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000977 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400978 case EbtSampler2D: out << "tex2D"; break;
979 case EbtSamplerCube: out << "texCUBE"; break;
980 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000981 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400982 }
983 else if (mOutputType == SH_HLSL11_OUTPUT)
984 {
985 out << "x.Sample";
986 }
987 else UNREACHABLE();
988
989 if (mOutputType == SH_HLSL9_OUTPUT)
990 {
991 switch(textureFunction->mipmap)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000992 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400993 case TextureFunction::IMPLICIT: out << "(s, "; break;
994 case TextureFunction::BIAS: out << "bias(s, "; break;
995 case TextureFunction::LOD: out << "lod(s, "; break;
996 case TextureFunction::LOD0: out << "lod(s, "; break;
997 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000998 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400999 }
1000 else if (mOutputType == SH_HLSL11_OUTPUT)
1001 {
1002 switch(textureFunction->mipmap)
1003 {
1004 case TextureFunction::IMPLICIT: out << "(s, "; break;
1005 case TextureFunction::BIAS: out << "Bias(s, "; break;
1006 case TextureFunction::LOD: out << "Level(s, "; break;
1007 case TextureFunction::LOD0: out << "Level(s, "; break;
1008 default: UNREACHABLE();
1009 }
1010 }
1011 else UNREACHABLE();
1012
1013 switch(hlslCoords)
1014 {
1015 case 2: out << "float2("; break;
1016 case 3: out << "float3("; break;
1017 case 4: out << "float4("; break;
1018 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +00001019 }
1020
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001021 TString proj = "";
1022
1023 if (textureFunction->proj)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001024 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001025 switch(textureFunction->coords)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001026 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001027 case 3: proj = " / t.z"; break;
1028 case 4: proj = " / t.w"; break;
1029 default: UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001030 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001031 }
1032
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001033 out << "t.x" + proj + ", t.y" + proj;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001034
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001035 if (mOutputType == SH_HLSL9_OUTPUT)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001036 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001037 if (hlslCoords >= 3)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001038 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001039 if (textureFunction->coords < 3)
1040 {
1041 out << ", 0";
1042 }
1043 else
1044 {
1045 out << ", t.z" + proj;
1046 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001047 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001048
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001049 if (hlslCoords == 4)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001050 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001051 switch(textureFunction->mipmap)
1052 {
1053 case TextureFunction::BIAS: out << ", bias"; break;
1054 case TextureFunction::LOD: out << ", lod"; break;
1055 case TextureFunction::LOD0: out << ", 0"; break;
1056 default: UNREACHABLE();
1057 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001058 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001059
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001060 out << "));\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +00001061 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001062 else if (mOutputType == SH_HLSL11_OUTPUT)
1063 {
1064 if (hlslCoords >= 3)
1065 {
1066 out << ", t.z" + proj;
1067 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001068
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001069 switch(textureFunction->mipmap)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001070 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001071 case TextureFunction::IMPLICIT: out << "));"; break;
1072 case TextureFunction::BIAS: out << "), bias);"; break;
1073 case TextureFunction::LOD: out << "), lod);"; break;
1074 case TextureFunction::LOD0: out << "), 0);"; break;
1075 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001076 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001077 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001078 else UNREACHABLE();
1079
1080 out << "}\n"
1081 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001082 }
1083
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001084 if (mUsesFragCoord)
1085 {
1086 out << "#define GL_USES_FRAG_COORD\n";
1087 }
1088
1089 if (mUsesPointCoord)
1090 {
1091 out << "#define GL_USES_POINT_COORD\n";
1092 }
1093
1094 if (mUsesFrontFacing)
1095 {
1096 out << "#define GL_USES_FRONT_FACING\n";
1097 }
1098
1099 if (mUsesPointSize)
1100 {
1101 out << "#define GL_USES_POINT_SIZE\n";
1102 }
1103
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001104 if (mUsesDepthRange)
1105 {
1106 out << "#define GL_USES_DEPTH_RANGE\n";
1107 }
1108
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001109 if (mUsesXor)
1110 {
1111 out << "bool xor(bool p, bool q)\n"
1112 "{\n"
1113 " return (p || q) && !(p && q);\n"
1114 "}\n"
1115 "\n";
1116 }
1117
1118 if (mUsesMod1)
1119 {
1120 out << "float mod(float x, float y)\n"
1121 "{\n"
1122 " return x - y * floor(x / y);\n"
1123 "}\n"
1124 "\n";
1125 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001126
1127 if (mUsesMod2v)
1128 {
1129 out << "float2 mod(float2 x, float2 y)\n"
1130 "{\n"
1131 " return x - y * floor(x / y);\n"
1132 "}\n"
1133 "\n";
1134 }
1135
1136 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001137 {
1138 out << "float2 mod(float2 x, float y)\n"
1139 "{\n"
1140 " return x - y * floor(x / y);\n"
1141 "}\n"
1142 "\n";
1143 }
1144
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001145 if (mUsesMod3v)
1146 {
1147 out << "float3 mod(float3 x, float3 y)\n"
1148 "{\n"
1149 " return x - y * floor(x / y);\n"
1150 "}\n"
1151 "\n";
1152 }
1153
1154 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001155 {
1156 out << "float3 mod(float3 x, float y)\n"
1157 "{\n"
1158 " return x - y * floor(x / y);\n"
1159 "}\n"
1160 "\n";
1161 }
1162
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001163 if (mUsesMod4v)
1164 {
1165 out << "float4 mod(float4 x, float4 y)\n"
1166 "{\n"
1167 " return x - y * floor(x / y);\n"
1168 "}\n"
1169 "\n";
1170 }
1171
1172 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001173 {
1174 out << "float4 mod(float4 x, float y)\n"
1175 "{\n"
1176 " return x - y * floor(x / y);\n"
1177 "}\n"
1178 "\n";
1179 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001180
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001181 if (mUsesFaceforward1)
1182 {
1183 out << "float faceforward(float N, float I, float Nref)\n"
1184 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001185 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001186 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001187 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001188 " }\n"
1189 " else\n"
1190 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001191 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001192 " }\n"
1193 "}\n"
1194 "\n";
1195 }
1196
1197 if (mUsesFaceforward2)
1198 {
1199 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1200 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001201 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001202 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001203 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001204 " }\n"
1205 " else\n"
1206 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001207 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001208 " }\n"
1209 "}\n"
1210 "\n";
1211 }
1212
1213 if (mUsesFaceforward3)
1214 {
1215 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1216 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001217 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001218 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001219 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001220 " }\n"
1221 " else\n"
1222 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001223 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001224 " }\n"
1225 "}\n"
1226 "\n";
1227 }
1228
1229 if (mUsesFaceforward4)
1230 {
1231 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1232 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001233 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001234 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001235 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001236 " }\n"
1237 " else\n"
1238 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001239 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001240 " }\n"
1241 "}\n"
1242 "\n";
1243 }
1244
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001245 for (unsigned int cols = 2; cols <= 4; cols++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001246 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001247 for (unsigned int rows = 2; rows <= 4; rows++)
1248 {
1249 if (mUsesEqualMat[cols][rows])
1250 {
1251 TString matrixType = "float" + str(cols) + "x" + str(rows);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001252
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001253 out << "bool equal(" + matrixType + " m, " + matrixType + " n)\n"
1254 "{\n";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001255
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001256 for (unsigned int row = 0; row < rows; row++)
1257 {
1258 if (row == 0)
1259 {
1260 out << " return ";
1261 }
1262 else
1263 {
1264 out << " ";
1265 }
1266
1267 for (unsigned int col = 0; col < cols; col++)
1268 {
1269 TString index = "[" + str(col) + "][" + str(row) + "]";
1270 out << "m" + index + " == n" + index;
1271
1272 if (col == cols-1 && row == rows-1)
1273 {
1274 out << ";\n";
1275 }
1276 else if (col == cols-1)
1277 {
1278 out << " &&\n";
1279 }
1280 else
1281 {
1282 out << " && ";
1283 }
1284 }
1285 }
1286
1287 out << "}\n";
1288 }
1289 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001290 }
1291
1292 if (mUsesEqualVec2)
1293 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001294 out << "bool equal(float2 v, float2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001295 "{\n"
1296 " return v.x == u.x && v.y == u.y;\n"
1297 "}\n";
1298 }
1299
1300 if (mUsesEqualVec3)
1301 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001302 out << "bool equal(float3 v, float3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001303 "{\n"
1304 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1305 "}\n";
1306 }
1307
1308 if (mUsesEqualVec4)
1309 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001310 out << "bool equal(float4 v, float4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001311 "{\n"
1312 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1313 "}\n";
1314 }
1315
1316 if (mUsesEqualIVec2)
1317 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001318 out << "bool equal(int2 v, int2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001319 "{\n"
1320 " return v.x == u.x && v.y == u.y;\n"
1321 "}\n";
1322 }
1323
1324 if (mUsesEqualIVec3)
1325 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001326 out << "bool equal(int3 v, int3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001327 "{\n"
1328 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1329 "}\n";
1330 }
1331
1332 if (mUsesEqualIVec4)
1333 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001334 out << "bool equal(int4 v, int4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001335 "{\n"
1336 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1337 "}\n";
1338 }
1339
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001340 if (mUsesEqualUVec2)
1341 {
1342 out << "bool equal(uint2 v, uint2 u)\n"
1343 "{\n"
1344 " return v.x == u.x && v.y == u.y;\n"
1345 "}\n";
1346 }
1347
1348 if (mUsesEqualUVec3)
1349 {
1350 out << "bool equal(uint3 v, uint3 u)\n"
1351 "{\n"
1352 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1353 "}\n";
1354 }
1355
1356 if (mUsesEqualUVec4)
1357 {
1358 out << "bool equal(uint4 v, uint4 u)\n"
1359 "{\n"
1360 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1361 "}\n";
1362 }
1363
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001364 if (mUsesEqualBVec2)
1365 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001366 out << "bool equal(bool2 v, bool2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001367 "{\n"
1368 " return v.x == u.x && v.y == u.y;\n"
1369 "}\n";
1370 }
1371
1372 if (mUsesEqualBVec3)
1373 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001374 out << "bool equal(bool3 v, bool3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001375 "{\n"
1376 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1377 "}\n";
1378 }
1379
1380 if (mUsesEqualBVec4)
1381 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001382 out << "bool equal(bool4 v, bool4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001383 "{\n"
1384 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1385 "}\n";
1386 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001387
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001388 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001389 {
1390 out << "float atanyx(float y, float x)\n"
1391 "{\n"
1392 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1393 " return atan2(y, x);\n"
1394 "}\n";
1395 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001396
1397 if (mUsesAtan2_2)
1398 {
1399 out << "float2 atanyx(float2 y, float2 x)\n"
1400 "{\n"
1401 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1402 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1403 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1404 "}\n";
1405 }
1406
1407 if (mUsesAtan2_3)
1408 {
1409 out << "float3 atanyx(float3 y, float3 x)\n"
1410 "{\n"
1411 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1412 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1413 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1414 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1415 "}\n";
1416 }
1417
1418 if (mUsesAtan2_4)
1419 {
1420 out << "float4 atanyx(float4 y, float4 x)\n"
1421 "{\n"
1422 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1423 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1424 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1425 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1426 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1427 "}\n";
1428 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001429}
1430
1431void OutputHLSL::visitSymbol(TIntermSymbol *node)
1432{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001433 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001434
Jamie Madill570e04d2013-06-21 09:15:33 -04001435 // Handle accessing std140 structs by value
1436 if (mFlaggedStructMappedNames.count(node) > 0)
1437 {
1438 out << mFlaggedStructMappedNames[node];
1439 return;
1440 }
1441
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001442 TString name = node->getSymbol();
1443
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001444 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001445 {
1446 mUsesDepthRange = true;
1447 out << name;
1448 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001449 else
1450 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001451 TQualifier qualifier = node->getQualifier();
1452
1453 if (qualifier == EvqUniform)
1454 {
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001455 if (node->getType().isInterfaceBlockMember())
1456 {
1457 const TString& interfaceBlockTypeName = node->getType().getInterfaceBlockType()->getTypeName();
1458 mReferencedInterfaceBlocks[interfaceBlockTypeName] = node;
1459 out << decorateUniform(name, node->getType());
1460 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001461 else if (node->getBasicType() == EbtInterfaceBlock)
1462 {
1463 const TString& interfaceBlockTypeName = node->getType().getTypeName();
1464 mReferencedInterfaceBlocks[interfaceBlockTypeName] = node;
1465 out << decorateUniform(name, node->getType());
1466 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001467 else
1468 {
1469 mReferencedUniforms[name] = node;
1470 out << decorateUniform(name, node->getType());
1471 }
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001472 }
Jamie Madillb120eac2013-06-12 14:08:13 -04001473 else if (qualifier == EvqAttribute || qualifier == EvqVertexInput)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001474 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001475 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001476 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001477 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001478 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001479 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001480 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001481 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001482 }
Jamie Madill46131a32013-06-20 11:55:50 -04001483 else if (qualifier == EvqFragmentOutput)
1484 {
1485 mReferencedOutputVariables[name] = node;
1486 out << "out_" << name;
1487 }
1488 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001489 {
1490 out << "gl_Color[0]";
1491 mUsesFragColor = true;
1492 }
1493 else if (qualifier == EvqFragData)
1494 {
1495 out << "gl_Color";
1496 mUsesFragData = true;
1497 }
1498 else if (qualifier == EvqFragCoord)
1499 {
1500 mUsesFragCoord = true;
1501 out << name;
1502 }
1503 else if (qualifier == EvqPointCoord)
1504 {
1505 mUsesPointCoord = true;
1506 out << name;
1507 }
1508 else if (qualifier == EvqFrontFacing)
1509 {
1510 mUsesFrontFacing = true;
1511 out << name;
1512 }
1513 else if (qualifier == EvqPointSize)
1514 {
1515 mUsesPointSize = true;
1516 out << name;
1517 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001518 else
1519 {
1520 out << decorate(name);
1521 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001522 }
1523}
1524
1525bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1526{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001527 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001528
Jamie Madill570e04d2013-06-21 09:15:33 -04001529 // Handle accessing std140 structs by value
1530 if (mFlaggedStructMappedNames.count(node) > 0)
1531 {
1532 out << mFlaggedStructMappedNames[node];
1533 return false;
1534 }
1535
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001536 switch (node->getOp())
1537 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001538 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001539 case EOpInitialize:
1540 if (visit == PreVisit)
1541 {
1542 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1543 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1544 // new variable is created before the assignment is evaluated), so we need to convert
1545 // this to "float t = x, x = t;".
1546
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001547 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1548 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001549
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001550 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1551 expression->traverse(&searchSymbol);
1552 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001553
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001554 if (sameSymbol)
1555 {
1556 // Type already printed
1557 out << "t" + str(mUniqueIndex) + " = ";
1558 expression->traverse(this);
1559 out << ", ";
1560 symbolNode->traverse(this);
1561 out << " = t" + str(mUniqueIndex);
1562
1563 mUniqueIndex++;
1564 return false;
1565 }
1566 }
1567 else if (visit == InVisit)
1568 {
1569 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001570 }
1571 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001572 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1573 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1574 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1575 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1576 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1577 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001578 if (visit == PreVisit)
1579 {
1580 out << "(";
1581 }
1582 else if (visit == InVisit)
1583 {
1584 out << " = mul(";
1585 node->getLeft()->traverse(this);
1586 out << ", transpose(";
1587 }
1588 else
1589 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001590 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001591 }
1592 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001593 case EOpMatrixTimesMatrixAssign:
1594 if (visit == PreVisit)
1595 {
1596 out << "(";
1597 }
1598 else if (visit == InVisit)
1599 {
1600 out << " = mul(";
1601 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001602 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001603 }
1604 else
1605 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001606 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001607 }
1608 break;
1609 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001610 case EOpIndexDirect:
1611 if (node->getLeft()->getBasicType() == EbtInterfaceBlock)
1612 {
1613 if (visit == PreVisit)
1614 {
1615 const TType &interfaceBlockType = node->getLeft()->getType();
1616 mReferencedInterfaceBlocks[interfaceBlockType.getInstanceName()] = node->getLeft()->getAsSymbolNode();
1617 out << interfaceBlockInstanceString(interfaceBlockType, node->getRight()->getAsConstantUnion()->getIConst(0));
1618 return false;
1619 }
1620 }
1621 else
1622 {
1623 outputTriplet(visit, "", "[", "]");
1624 }
1625 break;
1626 case EOpIndexIndirect:
1627 // We do not currently support indirect references to interface blocks
1628 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1629 outputTriplet(visit, "", "[", "]");
1630 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001631 case EOpIndexDirectStruct:
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001632 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001633 if (visit == InVisit)
1634 {
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00001635 out << "." + decorateField(node->getType().getFieldName(), node->getLeft()->getType());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001636
1637 return false;
1638 }
1639 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001640 case EOpVectorSwizzle:
1641 if (visit == InVisit)
1642 {
1643 out << ".";
1644
1645 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1646
1647 if (swizzle)
1648 {
1649 TIntermSequence &sequence = swizzle->getSequence();
1650
1651 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1652 {
1653 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1654
1655 if (element)
1656 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001657 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001658
1659 switch (i)
1660 {
1661 case 0: out << "x"; break;
1662 case 1: out << "y"; break;
1663 case 2: out << "z"; break;
1664 case 3: out << "w"; break;
1665 default: UNREACHABLE();
1666 }
1667 }
1668 else UNREACHABLE();
1669 }
1670 }
1671 else UNREACHABLE();
1672
1673 return false; // Fully processed
1674 }
1675 break;
1676 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1677 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1678 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1679 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001680 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001681 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001682 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001683 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001684 if (node->getOp() == EOpEqual)
1685 {
1686 outputTriplet(visit, "(", " == ", ")");
1687 }
1688 else
1689 {
1690 outputTriplet(visit, "(", " != ", ")");
1691 }
1692 }
1693 else if (node->getLeft()->getBasicType() == EbtStruct)
1694 {
1695 if (node->getOp() == EOpEqual)
1696 {
1697 out << "(";
1698 }
1699 else
1700 {
1701 out << "!(";
1702 }
1703
1704 const TTypeList *fields = node->getLeft()->getType().getStruct();
1705
1706 for (size_t i = 0; i < fields->size(); i++)
1707 {
1708 const TType *fieldType = (*fields)[i].type;
1709
1710 node->getLeft()->traverse(this);
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00001711 out << "." + decorateField(fieldType->getFieldName(), node->getLeft()->getType()) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001712 node->getRight()->traverse(this);
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00001713 out << "." + decorateField(fieldType->getFieldName(), node->getLeft()->getType());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001714
1715 if (i < fields->size() - 1)
1716 {
1717 out << " && ";
1718 }
1719 }
1720
1721 out << ")";
1722
1723 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001724 }
1725 else
1726 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001727 if (node->getLeft()->isMatrix())
1728 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001729 mUsesEqualMat[node->getLeft()->getCols()][node->getLeft()->getRows()] = true;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001730 }
1731 else if (node->getLeft()->isVector())
1732 {
1733 switch (node->getLeft()->getBasicType())
1734 {
1735 case EbtFloat:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001736 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001737 {
1738 case 2: mUsesEqualVec2 = true; break;
1739 case 3: mUsesEqualVec3 = true; break;
1740 case 4: mUsesEqualVec4 = true; break;
1741 default: UNREACHABLE();
1742 }
1743 break;
1744 case EbtInt:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001745 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001746 {
1747 case 2: mUsesEqualIVec2 = true; break;
1748 case 3: mUsesEqualIVec3 = true; break;
1749 case 4: mUsesEqualIVec4 = true; break;
1750 default: UNREACHABLE();
1751 }
1752 break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001753 case EbtUInt:
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001754 switch (node->getLeft()->getNominalSize())
1755 {
1756 case 2: mUsesEqualUVec2 = true; break;
1757 case 3: mUsesEqualUVec3 = true; break;
1758 case 4: mUsesEqualUVec4 = true; break;
1759 default: UNREACHABLE();
1760 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001761 break;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001762 case EbtBool:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001763 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001764 {
1765 case 2: mUsesEqualBVec2 = true; break;
1766 case 3: mUsesEqualBVec3 = true; break;
1767 case 4: mUsesEqualBVec4 = true; break;
1768 default: UNREACHABLE();
1769 }
1770 break;
1771 default: UNREACHABLE();
1772 }
1773 }
1774 else UNREACHABLE();
1775
1776 if (node->getOp() == EOpEqual)
1777 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001778 outputTriplet(visit, "equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001779 }
1780 else
1781 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001782 outputTriplet(visit, "!equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001783 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001784 }
1785 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001786 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1787 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1788 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1789 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1790 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001791 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001792 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1793 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001794 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001795 case EOpLogicalOr:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001796 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001797 return false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001798 case EOpLogicalXor:
1799 mUsesXor = true;
1800 outputTriplet(visit, "xor(", ", ", ")");
1801 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001802 case EOpLogicalAnd:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001803 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001804 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001805 default: UNREACHABLE();
1806 }
1807
1808 return true;
1809}
1810
1811bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1812{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001813 switch (node->getOp())
1814 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001815 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1816 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1817 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1818 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1819 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1820 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1821 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001822 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04001823 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001824 case EOpConvFloatToBool:
1825 switch (node->getOperand()->getType().getNominalSize())
1826 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001827 case 1: outputTriplet(visit, "bool(", "", ")"); break;
1828 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
1829 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
1830 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001831 default: UNREACHABLE();
1832 }
1833 break;
1834 case EOpConvBoolToFloat:
1835 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04001836 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001837 switch (node->getOperand()->getType().getNominalSize())
1838 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001839 case 1: outputTriplet(visit, "float(", "", ")"); break;
1840 case 2: outputTriplet(visit, "float2(", "", ")"); break;
1841 case 3: outputTriplet(visit, "float3(", "", ")"); break;
1842 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001843 default: UNREACHABLE();
1844 }
1845 break;
1846 case EOpConvFloatToInt:
1847 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04001848 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001849 switch (node->getOperand()->getType().getNominalSize())
1850 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001851 case 1: outputTriplet(visit, "int(", "", ")"); break;
1852 case 2: outputTriplet(visit, "int2(", "", ")"); break;
1853 case 3: outputTriplet(visit, "int3(", "", ")"); break;
1854 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001855 default: UNREACHABLE();
1856 }
1857 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04001858 case EOpConvFloatToUInt:
1859 case EOpConvBoolToUInt:
1860 case EOpConvIntToUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001861 switch (node->getOperand()->getType().getCols())
1862 {
1863 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001864 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
1865 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
1866 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001867 default: UNREACHABLE();
1868 }
1869 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001870 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1871 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1872 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1873 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1874 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1875 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1876 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1877 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1878 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1879 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1880 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1881 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1882 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1883 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1884 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1885 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1886 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1887 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1888 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1889 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1890 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001891 case EOpDFdx:
1892 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1893 {
1894 outputTriplet(visit, "(", "", ", 0.0)");
1895 }
1896 else
1897 {
1898 outputTriplet(visit, "ddx(", "", ")");
1899 }
1900 break;
1901 case EOpDFdy:
1902 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1903 {
1904 outputTriplet(visit, "(", "", ", 0.0)");
1905 }
1906 else
1907 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001908 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001909 }
1910 break;
1911 case EOpFwidth:
1912 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1913 {
1914 outputTriplet(visit, "(", "", ", 0.0)");
1915 }
1916 else
1917 {
1918 outputTriplet(visit, "fwidth(", "", ")");
1919 }
1920 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001921 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1922 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001923 default: UNREACHABLE();
1924 }
1925
1926 return true;
1927}
1928
1929bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1930{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001931 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001932
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001933 switch (node->getOp())
1934 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001935 case EOpSequence:
1936 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001937 if (mInsideFunction)
1938 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001939 outputLineDirective(node->getLine());
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001940 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001941
1942 mScopeDepth++;
1943
1944 if (mScopeBracket.size() < mScopeDepth)
1945 {
1946 mScopeBracket.push_back(0); // New scope level
1947 }
1948 else
1949 {
1950 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
1951 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001952 }
1953
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001954 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
1955 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001956 outputLineDirective((*sit)->getLine());
1957
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001958 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001959
1960 out << ";\n";
1961 }
1962
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001963 if (mInsideFunction)
1964 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001965 outputLineDirective(node->getEndLine());
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001966 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001967
1968 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001969 }
1970
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001971 return false;
1972 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001973 case EOpDeclaration:
1974 if (visit == PreVisit)
1975 {
1976 TIntermSequence &sequence = node->getSequence();
1977 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001978
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001979 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001980 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001981 if (variable->getType().getStruct())
1982 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001983 addConstructor(variable->getType(), scopedStruct(variable->getType().getTypeName()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001984 }
1985
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001986 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001987 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00001988 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001989 {
1990 out << "static ";
1991 }
1992
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001993 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001994
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001995 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001996 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001997 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001998
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001999 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002000 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002001 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002002 out << arrayString(symbol->getType());
daniel@transgaming.com7127f202010-04-15 20:45:22 +00002003 out << " = " + initializer(variable->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002004 }
2005 else
2006 {
2007 (*sit)->traverse(this);
2008 }
2009
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002010 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002011 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002012 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002013 }
2014 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002015 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002016 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2017 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002018 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002019 }
2020 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002021 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002022 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002023 {
2024 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2025 {
2026 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2027
2028 if (symbol)
2029 {
2030 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2031 mReferencedVaryings[symbol->getSymbol()] = symbol;
2032 }
2033 else
2034 {
2035 (*sit)->traverse(this);
2036 }
2037 }
2038 }
2039
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002040 return false;
2041 }
2042 else if (visit == InVisit)
2043 {
2044 out << ", ";
2045 }
2046 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002047 case EOpPrototype:
2048 if (visit == PreVisit)
2049 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002050 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002051
2052 TIntermSequence &arguments = node->getSequence();
2053
2054 for (unsigned int i = 0; i < arguments.size(); i++)
2055 {
2056 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2057
2058 if (symbol)
2059 {
2060 out << argumentString(symbol);
2061
2062 if (i < arguments.size() - 1)
2063 {
2064 out << ", ";
2065 }
2066 }
2067 else UNREACHABLE();
2068 }
2069
2070 out << ");\n";
2071
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002072 // Also prototype the Lod0 variant if needed
2073 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2074 {
2075 mOutputLod0Function = true;
2076 node->traverse(this);
2077 mOutputLod0Function = false;
2078 }
2079
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002080 return false;
2081 }
2082 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002083 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002084 case EOpFunction:
2085 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002086 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002087
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002088 out << typeString(node->getType()) << " ";
2089
2090 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002091 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002092 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002093 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002094 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002095 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002096 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002097 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002098
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002099 TIntermSequence &sequence = node->getSequence();
2100 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2101
2102 for (unsigned int i = 0; i < arguments.size(); i++)
2103 {
2104 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2105
2106 if (symbol)
2107 {
2108 if (symbol->getType().getStruct())
2109 {
2110 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getTypeName()), NULL);
2111 }
2112
2113 out << argumentString(symbol);
2114
2115 if (i < arguments.size() - 1)
2116 {
2117 out << ", ";
2118 }
2119 }
2120 else UNREACHABLE();
2121 }
2122
2123 out << ")\n"
2124 "{\n";
2125
2126 if (sequence.size() > 1)
2127 {
2128 mInsideFunction = true;
2129 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002130 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002131 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002132
2133 out << "}\n";
2134
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002135 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2136 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002137 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002138 {
2139 mOutputLod0Function = true;
2140 node->traverse(this);
2141 mOutputLod0Function = false;
2142 }
2143 }
2144
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002145 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002146 }
2147 break;
2148 case EOpFunctionCall:
2149 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002150 TString name = TFunction::unmangleName(node->getName());
2151 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002152 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002153
2154 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002155 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002156 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002157 }
2158 else
2159 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002160 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2161
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002162 TextureFunction textureFunction;
2163 textureFunction.sampler = samplerType;
2164 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
2165 textureFunction.mipmap = TextureFunction::IMPLICIT;
2166
2167 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002168 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002169 textureFunction.mipmap = TextureFunction::IMPLICIT;
2170 textureFunction.proj = false;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002171 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002172 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002173 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002174 textureFunction.mipmap = TextureFunction::IMPLICIT;
2175 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002176 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002177 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002178 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002179 textureFunction.mipmap = TextureFunction::LOD;
2180 textureFunction.proj = false;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002181 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002182 else if (name == "texture2DProjLod" || name == "textureProjLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002183 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002184 textureFunction.mipmap = TextureFunction::LOD;
2185 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002186 }
2187 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002188
2189 if (textureFunction.mipmap != TextureFunction::LOD)
2190 {
2191 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2192 {
2193 textureFunction.mipmap = TextureFunction::LOD0;
2194 }
2195 else if (arguments.size() == 3)
2196 {
2197 textureFunction.mipmap = TextureFunction::BIAS;
2198 }
2199 }
2200
2201 mUsesTexture.insert(textureFunction);
2202
2203 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002204 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002205
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002206 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2207 {
2208 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2209 {
2210 out << "texture_";
2211 (*arg)->traverse(this);
2212 out << ", sampler_";
2213 }
2214
2215 (*arg)->traverse(this);
2216
2217 if (arg < arguments.end() - 1)
2218 {
2219 out << ", ";
2220 }
2221 }
2222
2223 out << ")";
2224
2225 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002226 }
2227 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002228 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002229 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002230 addConstructor(node->getType(), "vec1", &node->getSequence());
2231 outputTriplet(visit, "vec1(", "", ")");
2232 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002233 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002234 addConstructor(node->getType(), "vec2", &node->getSequence());
2235 outputTriplet(visit, "vec2(", ", ", ")");
2236 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002237 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002238 addConstructor(node->getType(), "vec3", &node->getSequence());
2239 outputTriplet(visit, "vec3(", ", ", ")");
2240 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002241 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002242 addConstructor(node->getType(), "vec4", &node->getSequence());
2243 outputTriplet(visit, "vec4(", ", ", ")");
2244 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002245 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002246 addConstructor(node->getType(), "bvec1", &node->getSequence());
2247 outputTriplet(visit, "bvec1(", "", ")");
2248 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002249 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002250 addConstructor(node->getType(), "bvec2", &node->getSequence());
2251 outputTriplet(visit, "bvec2(", ", ", ")");
2252 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002253 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002254 addConstructor(node->getType(), "bvec3", &node->getSequence());
2255 outputTriplet(visit, "bvec3(", ", ", ")");
2256 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002257 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002258 addConstructor(node->getType(), "bvec4", &node->getSequence());
2259 outputTriplet(visit, "bvec4(", ", ", ")");
2260 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002261 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002262 addConstructor(node->getType(), "ivec1", &node->getSequence());
2263 outputTriplet(visit, "ivec1(", "", ")");
2264 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002265 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002266 addConstructor(node->getType(), "ivec2", &node->getSequence());
2267 outputTriplet(visit, "ivec2(", ", ", ")");
2268 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002269 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002270 addConstructor(node->getType(), "ivec3", &node->getSequence());
2271 outputTriplet(visit, "ivec3(", ", ", ")");
2272 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002273 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002274 addConstructor(node->getType(), "ivec4", &node->getSequence());
2275 outputTriplet(visit, "ivec4(", ", ", ")");
2276 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002277 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002278 addConstructor(node->getType(), "uvec1", &node->getSequence());
2279 outputTriplet(visit, "uvec1(", "", ")");
2280 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002281 case EOpConstructUVec2:
2282 addConstructor(node->getType(), "uvec2", &node->getSequence());
2283 outputTriplet(visit, "uvec2(", ", ", ")");
2284 break;
2285 case EOpConstructUVec3:
2286 addConstructor(node->getType(), "uvec3", &node->getSequence());
2287 outputTriplet(visit, "uvec3(", ", ", ")");
2288 break;
2289 case EOpConstructUVec4:
2290 addConstructor(node->getType(), "uvec4", &node->getSequence());
2291 outputTriplet(visit, "uvec4(", ", ", ")");
2292 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002293 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002294 addConstructor(node->getType(), "mat2", &node->getSequence());
2295 outputTriplet(visit, "mat2(", ", ", ")");
2296 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002297 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002298 addConstructor(node->getType(), "mat3", &node->getSequence());
2299 outputTriplet(visit, "mat3(", ", ", ")");
2300 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002301 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002302 addConstructor(node->getType(), "mat4", &node->getSequence());
2303 outputTriplet(visit, "mat4(", ", ", ")");
2304 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002305 case EOpConstructStruct:
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002306 addConstructor(node->getType(), scopedStruct(node->getType().getTypeName()), &node->getSequence());
2307 outputTriplet(visit, structLookup(node->getType().getTypeName()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002308 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002309 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2310 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2311 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2312 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2313 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2314 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002315 case EOpMod:
2316 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002317 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002318 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2319 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2320 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002321 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002322 case 11: mUsesMod1 = true; break;
2323 case 22: mUsesMod2v = true; break;
2324 case 21: mUsesMod2f = true; break;
2325 case 33: mUsesMod3v = true; break;
2326 case 31: mUsesMod3f = true; break;
2327 case 44: mUsesMod4v = true; break;
2328 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002329 default: UNREACHABLE();
2330 }
2331
2332 outputTriplet(visit, "mod(", ", ", ")");
2333 }
2334 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002335 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002336 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002337 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002338 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2339 {
2340 case 1: mUsesAtan2_1 = true; break;
2341 case 2: mUsesAtan2_2 = true; break;
2342 case 3: mUsesAtan2_3 = true; break;
2343 case 4: mUsesAtan2_4 = true; break;
2344 default: UNREACHABLE();
2345 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002346 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002347 break;
2348 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2349 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2350 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2351 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2352 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2353 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2354 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2355 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2356 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002357 case EOpFaceForward:
2358 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002359 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002360 {
2361 case 1: mUsesFaceforward1 = true; break;
2362 case 2: mUsesFaceforward2 = true; break;
2363 case 3: mUsesFaceforward3 = true; break;
2364 case 4: mUsesFaceforward4 = true; break;
2365 default: UNREACHABLE();
2366 }
2367
2368 outputTriplet(visit, "faceforward(", ", ", ")");
2369 }
2370 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002371 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2372 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2373 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002374 default: UNREACHABLE();
2375 }
2376
2377 return true;
2378}
2379
2380bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2381{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002382 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002383
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002384 if (node->usesTernaryOperator())
2385 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002386 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002387 }
2388 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002389 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002390 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002391
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002392 out << "if(";
2393
2394 node->getCondition()->traverse(this);
2395
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002396 out << ")\n";
2397
2398 outputLineDirective(node->getLine());
2399 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002400
daniel@transgaming.combb885322010-04-15 20:45:24 +00002401 if (node->getTrueBlock())
2402 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002403 traverseStatements(node->getTrueBlock());
daniel@transgaming.combb885322010-04-15 20:45:24 +00002404 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002405
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002406 outputLineDirective(node->getLine());
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002407 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002408
2409 if (node->getFalseBlock())
2410 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002411 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002412
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002413 outputLineDirective(node->getFalseBlock()->getLine());
2414 out << "{\n";
2415
2416 outputLineDirective(node->getFalseBlock()->getLine());
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002417 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002418
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002419 outputLineDirective(node->getFalseBlock()->getLine());
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002420 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002421 }
2422 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002423
2424 return false;
2425}
2426
2427void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2428{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002429 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002430}
2431
2432bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2433{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002434 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2435
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002436 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002437 {
2438 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2439 }
2440
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002441 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002442 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002443 if (handleExcessiveLoop(node))
2444 {
2445 return false;
2446 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002447 }
2448
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002449 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002450
alokp@chromium.org52813552010-11-16 18:36:09 +00002451 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002452 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002453 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002454
2455 outputLineDirective(node->getLine());
2456 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002457 }
2458 else
2459 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002460 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002461
2462 if (node->getInit())
2463 {
2464 node->getInit()->traverse(this);
2465 }
2466
2467 out << "; ";
2468
alokp@chromium.org52813552010-11-16 18:36:09 +00002469 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002470 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002471 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002472 }
2473
2474 out << "; ";
2475
alokp@chromium.org52813552010-11-16 18:36:09 +00002476 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002477 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002478 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002479 }
2480
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002481 out << ")\n";
2482
2483 outputLineDirective(node->getLine());
2484 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002485 }
2486
2487 if (node->getBody())
2488 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002489 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002490 }
2491
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002492 outputLineDirective(node->getLine());
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002493 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002494
alokp@chromium.org52813552010-11-16 18:36:09 +00002495 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002496 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002497 outputLineDirective(node->getCondition()->getLine());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002498 out << "while(\n";
2499
alokp@chromium.org52813552010-11-16 18:36:09 +00002500 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002501
daniel@transgaming.com73536982012-03-21 20:45:49 +00002502 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002503 }
2504
daniel@transgaming.com73536982012-03-21 20:45:49 +00002505 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002506
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002507 mInsideDiscontinuousLoop = wasDiscontinuous;
2508
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002509 return false;
2510}
2511
2512bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2513{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002514 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002515
2516 switch (node->getFlowOp())
2517 {
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002518 case EOpKill: outputTriplet(visit, "discard;\n", "", ""); break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002519 case EOpBreak:
2520 if (visit == PreVisit)
2521 {
2522 if (mExcessiveLoopIndex)
2523 {
2524 out << "{Break";
2525 mExcessiveLoopIndex->traverse(this);
2526 out << " = true; break;}\n";
2527 }
2528 else
2529 {
2530 out << "break;\n";
2531 }
2532 }
2533 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002534 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002535 case EOpReturn:
2536 if (visit == PreVisit)
2537 {
2538 if (node->getExpression())
2539 {
2540 out << "return ";
2541 }
2542 else
2543 {
2544 out << "return;\n";
2545 }
2546 }
2547 else if (visit == PostVisit)
2548 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002549 if (node->getExpression())
2550 {
2551 out << ";\n";
2552 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002553 }
2554 break;
2555 default: UNREACHABLE();
2556 }
2557
2558 return true;
2559}
2560
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002561void OutputHLSL::traverseStatements(TIntermNode *node)
2562{
2563 if (isSingleStatement(node))
2564 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002565 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002566 }
2567
2568 node->traverse(this);
2569}
2570
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002571bool OutputHLSL::isSingleStatement(TIntermNode *node)
2572{
2573 TIntermAggregate *aggregate = node->getAsAggregate();
2574
2575 if (aggregate)
2576 {
2577 if (aggregate->getOp() == EOpSequence)
2578 {
2579 return false;
2580 }
2581 else
2582 {
2583 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2584 {
2585 if (!isSingleStatement(*sit))
2586 {
2587 return false;
2588 }
2589 }
2590
2591 return true;
2592 }
2593 }
2594
2595 return true;
2596}
2597
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002598// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2599// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002600bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2601{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002602 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002603 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002604
2605 // Parse loops of the form:
2606 // for(int index = initial; index [comparator] limit; index += increment)
2607 TIntermSymbol *index = NULL;
2608 TOperator comparator = EOpNull;
2609 int initial = 0;
2610 int limit = 0;
2611 int increment = 0;
2612
2613 // Parse index name and intial value
2614 if (node->getInit())
2615 {
2616 TIntermAggregate *init = node->getInit()->getAsAggregate();
2617
2618 if (init)
2619 {
2620 TIntermSequence &sequence = init->getSequence();
2621 TIntermTyped *variable = sequence[0]->getAsTyped();
2622
2623 if (variable && variable->getQualifier() == EvqTemporary)
2624 {
2625 TIntermBinary *assign = variable->getAsBinaryNode();
2626
2627 if (assign->getOp() == EOpInitialize)
2628 {
2629 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2630 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2631
2632 if (symbol && constant)
2633 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002634 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002635 {
2636 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002637 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002638 }
2639 }
2640 }
2641 }
2642 }
2643 }
2644
2645 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002646 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002647 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002648 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002649
2650 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2651 {
2652 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2653
2654 if (constant)
2655 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002656 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002657 {
2658 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002659 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002660 }
2661 }
2662 }
2663 }
2664
2665 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002666 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002667 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002668 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2669 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002670
2671 if (binaryTerminal)
2672 {
2673 TOperator op = binaryTerminal->getOp();
2674 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2675
2676 if (constant)
2677 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002678 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002679 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002680 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002681
2682 switch (op)
2683 {
2684 case EOpAddAssign: increment = value; break;
2685 case EOpSubAssign: increment = -value; break;
2686 default: UNIMPLEMENTED();
2687 }
2688 }
2689 }
2690 }
2691 else if (unaryTerminal)
2692 {
2693 TOperator op = unaryTerminal->getOp();
2694
2695 switch (op)
2696 {
2697 case EOpPostIncrement: increment = 1; break;
2698 case EOpPostDecrement: increment = -1; break;
2699 case EOpPreIncrement: increment = 1; break;
2700 case EOpPreDecrement: increment = -1; break;
2701 default: UNIMPLEMENTED();
2702 }
2703 }
2704 }
2705
2706 if (index != NULL && comparator != EOpNull && increment != 0)
2707 {
2708 if (comparator == EOpLessThanEqual)
2709 {
2710 comparator = EOpLessThan;
2711 limit += 1;
2712 }
2713
2714 if (comparator == EOpLessThan)
2715 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002716 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002717
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002718 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002719 {
2720 return false; // Not an excessive loop
2721 }
2722
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002723 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2724 mExcessiveLoopIndex = index;
2725
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002726 out << "{int ";
2727 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002728 out << ";\n"
2729 "bool Break";
2730 index->traverse(this);
2731 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002732
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002733 bool firstLoopFragment = true;
2734
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002735 while (iterations > 0)
2736 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002737 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002738
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002739 if (!firstLoopFragment)
2740 {
2741 out << "if(!Break";
2742 index->traverse(this);
2743 out << ") {\n";
2744 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002745
2746 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2747 {
2748 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2749 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002750
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002751 // for(int index = initial; index < clampedLimit; index += increment)
2752
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002753 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002754 index->traverse(this);
2755 out << " = ";
2756 out << initial;
2757
2758 out << "; ";
2759 index->traverse(this);
2760 out << " < ";
2761 out << clampedLimit;
2762
2763 out << "; ";
2764 index->traverse(this);
2765 out << " += ";
2766 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002767 out << ")\n";
2768
2769 outputLineDirective(node->getLine());
2770 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002771
2772 if (node->getBody())
2773 {
2774 node->getBody()->traverse(this);
2775 }
2776
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002777 outputLineDirective(node->getLine());
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002778 out << ";}\n";
2779
2780 if (!firstLoopFragment)
2781 {
2782 out << "}\n";
2783 }
2784
2785 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002786
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002787 initial += MAX_LOOP_ITERATIONS * increment;
2788 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002789 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002790
2791 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002792
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002793 mExcessiveLoopIndex = restoreIndex;
2794
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002795 return true;
2796 }
2797 else UNIMPLEMENTED();
2798 }
2799
2800 return false; // Not handled as an excessive loop
2801}
2802
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002803void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002804{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002805 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002806
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002807 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002808 {
2809 out << preString;
2810 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002811 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002812 {
2813 out << inString;
2814 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002815 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002816 {
2817 out << postString;
2818 }
2819}
2820
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002821void OutputHLSL::outputLineDirective(int line)
2822{
2823 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2824 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002825 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002826 mBody << "#line " << line;
2827
2828 if (mContext.sourcePath)
2829 {
2830 mBody << " \"" << mContext.sourcePath << "\"";
2831 }
2832
2833 mBody << "\n";
2834 }
2835}
2836
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002837TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2838{
2839 TQualifier qualifier = symbol->getQualifier();
2840 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002841 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002842
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002843 if (name.empty()) // HLSL demands named arguments, also for prototypes
2844 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002845 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002846 }
2847 else
2848 {
2849 name = decorate(name);
2850 }
2851
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002852 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2853 {
2854 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
2855 qualifierString(qualifier) + " SamplerState sampler_" + name + arrayString(type);
2856 }
2857
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002858 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002859}
2860
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00002861TString OutputHLSL::interpolationString(TQualifier qualifier)
2862{
2863 switch(qualifier)
2864 {
2865 case EvqVaryingIn: return "";
2866 case EvqInvariantVaryingIn: return "";
2867 case EvqSmoothIn: return "linear";
2868 case EvqFlatIn: return "nointerpolation";
2869 case EvqCentroidIn: return "centroid";
2870 case EvqVaryingOut: return "";
2871 case EvqInvariantVaryingOut: return "";
2872 case EvqSmoothOut: return "linear";
2873 case EvqFlatOut: return "nointerpolation";
2874 case EvqCentroidOut: return "centroid";
2875 default: UNREACHABLE();
2876 }
2877
2878 return "";
2879}
2880
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002881TString OutputHLSL::qualifierString(TQualifier qualifier)
2882{
2883 switch(qualifier)
2884 {
2885 case EvqIn: return "in";
2886 case EvqOut: return "out";
2887 case EvqInOut: return "inout";
2888 case EvqConstReadOnly: return "const";
2889 default: UNREACHABLE();
2890 }
2891
2892 return "";
2893}
2894
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002895TString OutputHLSL::typeString(const TType &type)
2896{
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002897 if (type.getBasicType() == EbtStruct)
2898 {
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002899 if (type.getTypeName() != "")
2900 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002901 return structLookup(type.getTypeName());
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002902 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00002903 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002904 {
Jamie Madillc835df62013-06-21 09:15:32 -04002905 return structureString(type, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002906 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002907 }
2908 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002909 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00002910 int cols = type.getCols();
2911 int rows = type.getRows();
2912 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002913 }
2914 else
2915 {
2916 switch (type.getBasicType())
2917 {
2918 case EbtFloat:
2919 switch (type.getNominalSize())
2920 {
2921 case 1: return "float";
2922 case 2: return "float2";
2923 case 3: return "float3";
2924 case 4: return "float4";
2925 }
2926 case EbtInt:
2927 switch (type.getNominalSize())
2928 {
2929 case 1: return "int";
2930 case 2: return "int2";
2931 case 3: return "int3";
2932 case 4: return "int4";
2933 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002934 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04002935 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002936 {
2937 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002938 case 2: return "uint2";
2939 case 3: return "uint3";
2940 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002941 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002942 case EbtBool:
2943 switch (type.getNominalSize())
2944 {
2945 case 1: return "bool";
2946 case 2: return "bool2";
2947 case 3: return "bool3";
2948 case 4: return "bool4";
2949 }
2950 case EbtVoid:
2951 return "void";
2952 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04002953 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04002954 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04002955 case EbtSampler2DArray:
2956 case EbtISampler2DArray:
2957 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002958 return "sampler2D";
2959 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04002960 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04002961 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002962 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00002963 case EbtSamplerExternalOES:
2964 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002965 default:
2966 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002967 }
2968 }
2969
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00002970 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002971 return "<unknown type>";
2972}
2973
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00002974TString OutputHLSL::textureString(const TType &type)
2975{
2976 switch (type.getBasicType())
2977 {
Nicolas Capensfb50dff2013-06-24 16:16:23 -04002978 case EbtSampler2D: return "Texture2D";
2979 case EbtSamplerCube: return "TextureCube";
2980 case EbtSamplerExternalOES: return "Texture2D";
2981 case EbtSampler2DArray: return "Texture2DArray";
2982 case EbtISampler2D: return "Texture2D<int4>";
2983 case EbtISamplerCube: return "TextureCube<int4>";
2984 case EbtISampler2DArray: return "Texture2DArray<int4>";
2985 case EbtUSampler2D: return "Texture2D<uint4>";
2986 case EbtUSamplerCube: return "TextureCube<uint4>";
2987 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00002988 default:
2989 break;
2990 }
2991
2992 UNREACHABLE();
2993 return "<unknown texture type>";
2994}
2995
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002996TString OutputHLSL::arrayString(const TType &type)
2997{
2998 if (!type.isArray())
2999 {
3000 return "";
3001 }
3002
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003003 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003004}
3005
3006TString OutputHLSL::initializer(const TType &type)
3007{
3008 TString string;
3009
daniel@transgaming.comead23042010-04-29 03:35:36 +00003010 for (int component = 0; component < type.getObjectSize(); component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003011 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003012 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003013
daniel@transgaming.comead23042010-04-29 03:35:36 +00003014 if (component < type.getObjectSize() - 1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003015 {
3016 string += ", ";
3017 }
3018 }
3019
daniel@transgaming.comead23042010-04-29 03:35:36 +00003020 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003021}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003022
Jamie Madillc835df62013-06-21 09:15:32 -04003023TString OutputHLSL::structureString(const TType &structType, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003024{
3025 ASSERT(structType.getStruct());
3026
3027 const TTypeList &fields = *structType.getStruct();
3028 const bool isNameless = (structType.getTypeName() == "");
Jamie Madillc835df62013-06-21 09:15:32 -04003029 const TString &structName = structureTypeName(structType, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003030
3031 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3032
3033 TString structure;
3034 structure += declareString + "\n"
3035 "{\n";
3036
Jamie Madillc835df62013-06-21 09:15:32 -04003037 int elementIndex = 0;
3038
Jamie Madill9cf6c072013-06-20 11:55:53 -04003039 for (unsigned int i = 0; i < fields.size(); i++)
3040 {
3041 const TType &field = *fields[i].type;
3042
Jamie Madillc835df62013-06-21 09:15:32 -04003043 if (useStd140Packing)
3044 {
3045 structure += std140PrePaddingString(field, &elementIndex);
3046 }
3047
3048 structure += " " + structureTypeName(field, useHLSLRowMajorPacking, useStd140Packing) + " " +
Jamie Madill9cf6c072013-06-20 11:55:53 -04003049 decorateField(field.getFieldName(), structType) + arrayString(field) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003050
3051 if (useStd140Packing)
3052 {
Jamie Madille4075c92013-06-21 09:15:32 -04003053 structure += std140PostPaddingString(field, useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003054 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003055 }
3056
3057 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
3058 structure += (isNameless ? "} " : "};\n");
3059
Jamie Madille4075c92013-06-21 09:15:32 -04003060 // Add remaining element index to the global map, for use with nested structs in standard layouts
3061 if (useStd140Packing)
3062 {
3063 mStd140StructElementIndexes[structName] = elementIndex;
3064 }
3065
Jamie Madill9cf6c072013-06-20 11:55:53 -04003066 return structure;
3067}
3068
Jamie Madillc835df62013-06-21 09:15:32 -04003069TString OutputHLSL::structureTypeName(const TType &structType, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003070{
3071 if (structType.getBasicType() != EbtStruct)
3072 {
3073 return typeString(structType);
3074 }
3075
3076 if (structType.getTypeName() == "")
3077 {
3078 return "";
3079 }
3080
3081 TString prefix = "";
3082
3083 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3084 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003085
3086 if (useStd140Packing)
3087 {
3088 prefix += "std";
3089 }
3090
Jamie Madill9cf6c072013-06-20 11:55:53 -04003091 if (useHLSLRowMajorPacking)
3092 {
Jamie Madillc835df62013-06-21 09:15:32 -04003093 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003094 prefix += "rm";
3095 }
3096
3097 return prefix + typeString(structType);
3098}
3099
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003100void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003101{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003102 if (name == "")
3103 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003104 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003105 }
3106
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003107 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
3108 {
3109 return; // Already added
3110 }
3111
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003112 TType ctorType = type;
3113 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003114 ctorType.setPrecision(EbpHigh);
3115 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003116
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003117 TString ctorName = type.getStruct() ? decorate(name) : name;
3118
3119 typedef std::vector<TType> ParameterArray;
3120 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003121
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003122 if (type.getStruct())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003123 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003124 mStructNames.insert(decorate(name));
3125
Jamie Madillc835df62013-06-21 09:15:32 -04003126 const TString &structure = structureString(type, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003127
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003128 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structure) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003129 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003130 // Add row-major packed struct for interface blocks
3131 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madillc835df62013-06-21 09:15:32 -04003132 structureString(type, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003133 "#pragma pack_matrix(column_major)\n";
3134
Jamie Madillc835df62013-06-21 09:15:32 -04003135 const TString &std140Prefix = "std";
3136 TString std140String = structureString(type, false, true);
3137
3138 const TString &std140RowMajorPrefix = "std_rm";
3139 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
3140 structureString(type, true, true) +
3141 "#pragma pack_matrix(column_major)\n";
3142
Jamie Madill9cf6c072013-06-20 11:55:53 -04003143 mStructDeclarations.push_back(structure);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003144 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003145 mStructDeclarations.push_back(std140String);
3146 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003147 }
3148
Jamie Madill9cf6c072013-06-20 11:55:53 -04003149 const TTypeList &fields = *type.getStruct();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003150 for (unsigned int i = 0; i < fields.size(); i++)
3151 {
3152 ctorParameters.push_back(*fields[i].type);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003153 }
3154 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003155 else if (parameters)
3156 {
3157 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3158 {
3159 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3160 }
3161 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003162 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003163
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003164 TString constructor;
3165
3166 if (ctorType.getStruct())
3167 {
3168 constructor += ctorName + " " + ctorName + "_ctor(";
3169 }
3170 else // Built-in type
3171 {
3172 constructor += typeString(ctorType) + " " + ctorName + "(";
3173 }
3174
3175 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3176 {
3177 const TType &type = ctorParameters[parameter];
3178
3179 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3180
3181 if (parameter < ctorParameters.size() - 1)
3182 {
3183 constructor += ", ";
3184 }
3185 }
3186
3187 constructor += ")\n"
3188 "{\n";
3189
3190 if (ctorType.getStruct())
3191 {
3192 constructor += " " + ctorName + " structure = {";
3193 }
3194 else
3195 {
3196 constructor += " return " + typeString(ctorType) + "(";
3197 }
3198
3199 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3200 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003201 int rows = ctorType.getRows();
3202 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003203 const TType &parameter = ctorParameters[0];
3204
3205 if (parameter.isScalar())
3206 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003207 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003208 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003209 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003210 {
3211 constructor += TString((row == col) ? "x0" : "0.0");
3212
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003213 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003214 {
3215 constructor += ", ";
3216 }
3217 }
3218 }
3219 }
3220 else if (parameter.isMatrix())
3221 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003222 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003223 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003224 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003225 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003226 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003227 {
3228 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3229 }
3230 else
3231 {
3232 constructor += TString((row == col) ? "1.0" : "0.0");
3233 }
3234
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003235 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003236 {
3237 constructor += ", ";
3238 }
3239 }
3240 }
3241 }
3242 else UNREACHABLE();
3243 }
3244 else
3245 {
3246 int remainingComponents = ctorType.getObjectSize();
3247 int parameterIndex = 0;
3248
3249 while (remainingComponents > 0)
3250 {
3251 const TType &parameter = ctorParameters[parameterIndex];
3252 bool moreParameters = parameterIndex < (int)ctorParameters.size() - 1;
3253
3254 constructor += "x" + str(parameterIndex);
3255
3256 if (parameter.isScalar())
3257 {
3258 remainingComponents -= parameter.getObjectSize();
3259 }
3260 else if (parameter.isVector())
3261 {
3262 if (remainingComponents == parameter.getObjectSize() || moreParameters)
3263 {
3264 remainingComponents -= parameter.getObjectSize();
3265 }
Jamie Madilla9f52472013-06-06 11:56:43 -04003266 else if (remainingComponents < parameter.getNominalSize())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003267 {
3268 switch (remainingComponents)
3269 {
3270 case 1: constructor += ".x"; break;
3271 case 2: constructor += ".xy"; break;
3272 case 3: constructor += ".xyz"; break;
3273 case 4: constructor += ".xyzw"; break;
3274 default: UNREACHABLE();
3275 }
3276
3277 remainingComponents = 0;
3278 }
3279 else UNREACHABLE();
3280 }
3281 else if (parameter.isMatrix() || parameter.getStruct())
3282 {
3283 ASSERT(remainingComponents == parameter.getObjectSize() || moreParameters);
3284
3285 remainingComponents -= parameter.getObjectSize();
3286 }
3287 else UNREACHABLE();
3288
3289 if (moreParameters)
3290 {
3291 parameterIndex++;
3292 }
3293
3294 if (remainingComponents)
3295 {
3296 constructor += ", ";
3297 }
3298 }
3299 }
3300
3301 if (ctorType.getStruct())
3302 {
3303 constructor += "};\n"
3304 " return structure;\n"
3305 "}\n";
3306 }
3307 else
3308 {
3309 constructor += ");\n"
3310 "}\n";
3311 }
3312
daniel@transgaming.com63691862010-04-29 03:32:42 +00003313 mConstructors.insert(constructor);
3314}
3315
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003316const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3317{
3318 TInfoSinkBase &out = mBody;
3319
3320 if (type.getBasicType() == EbtStruct)
3321 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003322 out << structLookup(type.getTypeName()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003323
3324 const TTypeList *structure = type.getStruct();
3325
3326 for (size_t i = 0; i < structure->size(); i++)
3327 {
3328 const TType *fieldType = (*structure)[i].type;
3329
3330 constUnion = writeConstantUnion(*fieldType, constUnion);
3331
3332 if (i != structure->size() - 1)
3333 {
3334 out << ", ";
3335 }
3336 }
3337
3338 out << ")";
3339 }
3340 else
3341 {
3342 int size = type.getObjectSize();
3343 bool writeType = size > 1;
3344
3345 if (writeType)
3346 {
3347 out << typeString(type) << "(";
3348 }
3349
3350 for (int i = 0; i < size; i++, constUnion++)
3351 {
3352 switch (constUnion->getType())
3353 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003354 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003355 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003356 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003357 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003358 default: UNREACHABLE();
3359 }
3360
3361 if (i != size - 1)
3362 {
3363 out << ", ";
3364 }
3365 }
3366
3367 if (writeType)
3368 {
3369 out << ")";
3370 }
3371 }
3372
3373 return constUnion;
3374}
3375
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003376TString OutputHLSL::scopeString(unsigned int depthLimit)
3377{
3378 TString string;
3379
3380 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3381 {
3382 string += "_" + str(i);
3383 }
3384
3385 return string;
3386}
3387
3388TString OutputHLSL::scopedStruct(const TString &typeName)
3389{
3390 if (typeName == "")
3391 {
3392 return typeName;
3393 }
3394
3395 return typeName + scopeString(mScopeDepth);
3396}
3397
3398TString OutputHLSL::structLookup(const TString &typeName)
3399{
3400 for (int depth = mScopeDepth; depth >= 0; depth--)
3401 {
3402 TString scopedName = decorate(typeName + scopeString(depth));
3403
3404 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3405 {
3406 if (*structName == scopedName)
3407 {
3408 return scopedName;
3409 }
3410 }
3411 }
3412
3413 UNREACHABLE(); // Should have found a matching constructor
3414
3415 return typeName;
3416}
3417
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003418TString OutputHLSL::decorate(const TString &string)
3419{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003420 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003421 {
3422 return "_" + string;
3423 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003424
3425 return string;
3426}
3427
apatrick@chromium.org65756022012-01-17 21:45:38 +00003428TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003429{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003430 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003431 {
3432 return "ex_" + string;
3433 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003434
3435 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003436}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003437
3438TString OutputHLSL::decorateField(const TString &string, const TType &structure)
3439{
3440 if (structure.getTypeName().compare(0, 3, "gl_") != 0)
3441 {
3442 return decorate(string);
3443 }
3444
3445 return string;
3446}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003447
3448TString OutputHLSL::registerString(TIntermSymbol *operand)
3449{
3450 ASSERT(operand->getQualifier() == EvqUniform);
3451
3452 if (IsSampler(operand->getBasicType()))
3453 {
3454 return "s" + str(samplerRegister(operand));
3455 }
3456
3457 return "c" + str(uniformRegister(operand));
3458}
3459
3460int OutputHLSL::samplerRegister(TIntermSymbol *sampler)
3461{
3462 const TType &type = sampler->getType();
3463 ASSERT(IsSampler(type.getBasicType()));
3464
3465 int index = mSamplerRegister;
3466 mSamplerRegister += sampler->totalRegisterCount();
3467
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003468 declareUniform(type, sampler->getSymbol(), index);
3469
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003470 return index;
3471}
3472
3473int OutputHLSL::uniformRegister(TIntermSymbol *uniform)
3474{
3475 const TType &type = uniform->getType();
3476 ASSERT(!IsSampler(type.getBasicType()));
3477
3478 int index = mUniformRegister;
3479 mUniformRegister += uniform->totalRegisterCount();
3480
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003481 declareUniform(type, uniform->getSymbol(), index);
3482
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003483 return index;
3484}
3485
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003486void OutputHLSL::declareUniformToList(const TType &type, const TString &name, int index, ActiveUniforms& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003487{
3488 const TTypeList *structure = type.getStruct();
3489
3490 if (!structure)
3491 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003492 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
3493 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 +00003494 }
3495 else
3496 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003497 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 +00003498
3499 int fieldIndex = index;
3500
3501 for (size_t i = 0; i < structure->size(); i++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003502 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003503 TType fieldType = *(*structure)[i].type;
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003504 const TString &fieldName = fieldType.getFieldName();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003505
Jamie Madill010fffa2013-06-20 11:55:53 -04003506 // make sure to copy matrix packing information
3507 fieldType.setLayoutQualifier(type.getLayoutQualifier());
3508
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003509 declareUniformToList(fieldType, fieldName, fieldIndex, structUniform.fields);
3510 fieldIndex += fieldType.totalRegisterCount();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003511 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003512
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003513 output.push_back(structUniform);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003514 }
3515}
3516
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003517void OutputHLSL::declareUniform(const TType &type, const TString &name, int index)
3518{
3519 declareUniformToList(type, name, index, mActiveUniforms);
3520}
3521
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003522GLenum OutputHLSL::glVariableType(const TType &type)
3523{
3524 if (type.getBasicType() == EbtFloat)
3525 {
3526 if (type.isScalar())
3527 {
3528 return GL_FLOAT;
3529 }
3530 else if (type.isVector())
3531 {
3532 switch(type.getNominalSize())
3533 {
3534 case 2: return GL_FLOAT_VEC2;
3535 case 3: return GL_FLOAT_VEC3;
3536 case 4: return GL_FLOAT_VEC4;
3537 default: UNREACHABLE();
3538 }
3539 }
3540 else if (type.isMatrix())
3541 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003542 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003543 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003544 case 2:
3545 switch(type.getRows())
3546 {
3547 case 2: return GL_FLOAT_MAT2;
3548 case 3: return GL_FLOAT_MAT2x3;
3549 case 4: return GL_FLOAT_MAT2x4;
3550 default: UNREACHABLE();
3551 }
3552
3553 case 3:
3554 switch(type.getRows())
3555 {
3556 case 2: return GL_FLOAT_MAT3x2;
3557 case 3: return GL_FLOAT_MAT3;
3558 case 4: return GL_FLOAT_MAT3x4;
3559 default: UNREACHABLE();
3560 }
3561
3562 case 4:
3563 switch(type.getRows())
3564 {
3565 case 2: return GL_FLOAT_MAT4x2;
3566 case 3: return GL_FLOAT_MAT4x3;
3567 case 4: return GL_FLOAT_MAT4;
3568 default: UNREACHABLE();
3569 }
3570
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003571 default: UNREACHABLE();
3572 }
3573 }
3574 else UNREACHABLE();
3575 }
3576 else if (type.getBasicType() == EbtInt)
3577 {
3578 if (type.isScalar())
3579 {
3580 return GL_INT;
3581 }
3582 else if (type.isVector())
3583 {
3584 switch(type.getNominalSize())
3585 {
3586 case 2: return GL_INT_VEC2;
3587 case 3: return GL_INT_VEC3;
3588 case 4: return GL_INT_VEC4;
3589 default: UNREACHABLE();
3590 }
3591 }
3592 else UNREACHABLE();
3593 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003594 else if (type.getBasicType() == EbtUInt)
3595 {
3596 if (type.isScalar())
3597 {
3598 return GL_UNSIGNED_INT;
3599 }
3600 else if (type.isVector())
3601 {
Jamie Madill22d63da2013-06-07 12:45:12 -04003602 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003603 {
3604 case 2: return GL_UNSIGNED_INT_VEC2;
3605 case 3: return GL_UNSIGNED_INT_VEC3;
3606 case 4: return GL_UNSIGNED_INT_VEC4;
3607 default: UNREACHABLE();
3608 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003609 }
3610 else UNREACHABLE();
3611 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003612 else if (type.getBasicType() == EbtBool)
3613 {
3614 if (type.isScalar())
3615 {
3616 return GL_BOOL;
3617 }
3618 else if (type.isVector())
3619 {
3620 switch(type.getNominalSize())
3621 {
3622 case 2: return GL_BOOL_VEC2;
3623 case 3: return GL_BOOL_VEC3;
3624 case 4: return GL_BOOL_VEC4;
3625 default: UNREACHABLE();
3626 }
3627 }
3628 else UNREACHABLE();
3629 }
3630 else if (type.getBasicType() == EbtSampler2D)
3631 {
3632 return GL_SAMPLER_2D;
3633 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04003634 else if (type.getBasicType() == EbtSampler3D)
3635 {
3636 return GL_SAMPLER_3D;
3637 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003638 else if (type.getBasicType() == EbtSamplerCube)
3639 {
3640 return GL_SAMPLER_CUBE;
3641 }
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003642 else if (type.getBasicType() == EbtSampler2DArray)
3643 {
3644 return GL_SAMPLER_2D_ARRAY;
3645 }
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003646 else if (type.getBasicType() == EbtISampler2D)
3647 {
3648 return GL_INT_SAMPLER_2D;
3649 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04003650 else if (type.getBasicType() == EbtISampler3D)
3651 {
3652 return GL_INT_SAMPLER_3D;
3653 }
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003654 else if (type.getBasicType() == EbtISamplerCube)
3655 {
3656 return GL_INT_SAMPLER_CUBE;
3657 }
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003658 else if (type.getBasicType() == EbtISampler2DArray)
3659 {
3660 return GL_INT_SAMPLER_2D_ARRAY;
3661 }
Nicolas Capens075368e2013-06-24 15:58:30 -04003662 else if (type.getBasicType() == EbtUSampler2D)
3663 {
3664 return GL_UNSIGNED_INT_SAMPLER_2D;
3665 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04003666 else if (type.getBasicType() == EbtUSampler3D)
3667 {
3668 return GL_UNSIGNED_INT_SAMPLER_3D;
3669 }
Nicolas Capens075368e2013-06-24 15:58:30 -04003670 else if (type.getBasicType() == EbtUSamplerCube)
3671 {
3672 return GL_UNSIGNED_INT_SAMPLER_CUBE;
3673 }
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003674 else if (type.getBasicType() == EbtUSampler2DArray)
3675 {
3676 return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3677 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003678 else UNREACHABLE();
3679
3680 return GL_NONE;
3681}
3682
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003683GLenum OutputHLSL::glVariablePrecision(const TType &type)
3684{
3685 if (type.getBasicType() == EbtFloat)
3686 {
3687 switch (type.getPrecision())
3688 {
3689 case EbpHigh: return GL_HIGH_FLOAT;
3690 case EbpMedium: return GL_MEDIUM_FLOAT;
3691 case EbpLow: return GL_LOW_FLOAT;
3692 case EbpUndefined:
3693 // Should be defined as the default precision by the parser
3694 default: UNREACHABLE();
3695 }
3696 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003697 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003698 {
3699 switch (type.getPrecision())
3700 {
3701 case EbpHigh: return GL_HIGH_INT;
3702 case EbpMedium: return GL_MEDIUM_INT;
3703 case EbpLow: return GL_LOW_INT;
3704 case EbpUndefined:
3705 // Should be defined as the default precision by the parser
3706 default: UNREACHABLE();
3707 }
3708 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003709
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00003710 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003711 return GL_NONE;
3712}
3713
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003714bool OutputHLSL::isVaryingOut(TQualifier qualifier)
3715{
3716 switch(qualifier)
3717 {
3718 case EvqVaryingOut:
3719 case EvqInvariantVaryingOut:
3720 case EvqSmoothOut:
3721 case EvqFlatOut:
3722 case EvqCentroidOut:
3723 return true;
3724 }
3725
3726 return false;
3727}
3728
3729bool OutputHLSL::isVaryingIn(TQualifier qualifier)
3730{
3731 switch(qualifier)
3732 {
3733 case EvqVaryingIn:
3734 case EvqInvariantVaryingIn:
3735 case EvqSmoothIn:
3736 case EvqFlatIn:
3737 case EvqCentroidIn:
3738 return true;
3739 }
3740
3741 return false;
3742}
3743
3744bool OutputHLSL::isVarying(TQualifier qualifier)
3745{
3746 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
3747}
3748
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003749}