blob: 172e1bd4c29c9119114675c1f734d8a3a3d4155e [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;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400100 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000101 mUsesXor = false;
102 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000103 mUsesMod2v = false;
104 mUsesMod2f = false;
105 mUsesMod3v = false;
106 mUsesMod3f = false;
107 mUsesMod4v = false;
108 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000109 mUsesFaceforward1 = false;
110 mUsesFaceforward2 = false;
111 mUsesFaceforward3 = false;
112 mUsesFaceforward4 = false;
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000113
114 for (unsigned int col = 0; col <= 4; col++)
115 {
116 for (unsigned int row = 0; row <= 4; row++)
117 {
118 mUsesEqualMat[col][row] = false;
119 }
120 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000121 mUsesEqualVec2 = false;
122 mUsesEqualVec3 = false;
123 mUsesEqualVec4 = false;
124 mUsesEqualIVec2 = false;
125 mUsesEqualIVec3 = false;
126 mUsesEqualIVec4 = false;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +0000127 mUsesEqualUVec2 = false;
128 mUsesEqualUVec3 = false;
129 mUsesEqualUVec4 = false;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000130 mUsesEqualBVec2 = false;
131 mUsesEqualBVec3 = false;
132 mUsesEqualBVec4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000133 mUsesAtan2_1 = false;
134 mUsesAtan2_2 = false;
135 mUsesAtan2_3 = false;
136 mUsesAtan2_4 = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000137
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000138 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
139
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000140 mScopeDepth = 0;
141
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000142 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000143
144 mContainsLoopDiscontinuity = false;
145 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000146 mInsideDiscontinuousLoop = false;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000147
148 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000149
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000150 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000151 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000152 if (mContext.shaderType == SH_FRAGMENT_SHADER)
153 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000154 mUniformRegister = 3; // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000155 }
156 else
157 {
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000158 mUniformRegister = 2; // Reserve registers for dx_DepthRange and dx_ViewAdjust
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000159 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000160 }
161 else
162 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000163 mUniformRegister = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000164 }
165
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000166 mSamplerRegister = 0;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000167 mInterfaceBlockRegister = 2; // Reserve registers for the default uniform block and driver constants
Jamie Madill574d9dd2013-06-20 11:55:56 -0400168 mPaddingCounter = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000169}
170
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000171OutputHLSL::~OutputHLSL()
172{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000173 delete mUnfoldShortCircuit;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000174}
175
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000176void OutputHLSL::output()
177{
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +0000178 mContainsLoopDiscontinuity = mContext.shaderType == SH_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400179 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
180 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000181
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000182 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 +0000183 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000184
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000185 mContext.infoSink().obj << mHeader.c_str();
186 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000187}
188
Jamie Madill570e04d2013-06-21 09:15:33 -0400189void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
190{
191 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
192 {
193 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
194
195 // This will mark the necessary block elements as referenced
196 flaggedNode->traverse(this);
197 TString structName(mBody.c_str());
198 mBody.erase();
199
200 mFlaggedStructOriginalNames[flaggedNode] = structName;
201
202 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
203 {
204 structName.erase(pos, 1);
205 }
206
207 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
208 }
209}
210
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000211TInfoSinkBase &OutputHLSL::getBodyStream()
212{
213 return mBody;
214}
215
daniel@transgaming.com043da132012-12-20 21:12:22 +0000216const ActiveUniforms &OutputHLSL::getUniforms()
217{
218 return mActiveUniforms;
219}
220
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000221const ActiveInterfaceBlocks &OutputHLSL::getInterfaceBlocks() const
222{
223 return mActiveInterfaceBlocks;
224}
225
Jamie Madill46131a32013-06-20 11:55:50 -0400226const ActiveShaderVariables &OutputHLSL::getOutputVariables() const
227{
228 return mActiveOutputVariables;
229}
230
Jamie Madilldefb6742013-06-20 11:55:51 -0400231const ActiveShaderVariables &OutputHLSL::getAttributes() const
232{
233 return mActiveAttributes;
234}
235
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000236int OutputHLSL::vectorSize(const TType &type) const
237{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000238 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000239 int arraySize = type.isArray() ? type.getArraySize() : 1;
240
241 return elementSize * arraySize;
242}
243
Jamie Madill98493dd2013-07-08 14:39:03 -0400244TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, const TField &field)
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000245{
Jamie Madill98493dd2013-07-08 14:39:03 -0400246 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000247 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400248 return interfaceBlock.name() + "." + field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000249 }
250 else
251 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400252 return field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000253 }
254}
255
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000256TString OutputHLSL::decoratePrivate(const TString &privateText)
257{
258 return "dx_" + privateText;
259}
260
Jamie Madill98493dd2013-07-08 14:39:03 -0400261TString OutputHLSL::interfaceBlockStructNameString(const TInterfaceBlock &interfaceBlock)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000262{
Jamie Madill98493dd2013-07-08 14:39:03 -0400263 return decoratePrivate(interfaceBlock.name()) + "_type";
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000264}
265
Jamie Madill98493dd2013-07-08 14:39:03 -0400266TString OutputHLSL::interfaceBlockInstanceString(const TInterfaceBlock& interfaceBlock, unsigned int arrayIndex)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000267{
Jamie Madill98493dd2013-07-08 14:39:03 -0400268 if (!interfaceBlock.hasInstanceName())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000269 {
270 return "";
271 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400272 else if (interfaceBlock.isArray())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000273 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400274 return decoratePrivate(interfaceBlock.instanceName()) + "_" + str(arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000275 }
276 else
277 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400278 return decorate(interfaceBlock.instanceName());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000279 }
280}
281
Jamie Madill98493dd2013-07-08 14:39:03 -0400282TString OutputHLSL::interfaceBlockFieldTypeString(const TField &field, TLayoutBlockStorage blockStorage)
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000283{
Jamie Madill98493dd2013-07-08 14:39:03 -0400284 const TType &fieldType = *field.type();
285 const TLayoutMatrixPacking matrixPacking = fieldType.getLayoutQualifier().matrixPacking;
Jamie Madill529077d2013-06-20 11:55:54 -0400286 ASSERT(matrixPacking != EmpUnspecified);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000287
Jamie Madill98493dd2013-07-08 14:39:03 -0400288 if (fieldType.isMatrix())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000289 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400290 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill529077d2013-06-20 11:55:54 -0400291 const TString &matrixPackString = (matrixPacking == EmpRowMajor ? "column_major" : "row_major");
Jamie Madill98493dd2013-07-08 14:39:03 -0400292 return matrixPackString + " " + typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000293 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400294 else if (fieldType.getStruct())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000295 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400296 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill98493dd2013-07-08 14:39:03 -0400297 return structureTypeName(*fieldType.getStruct(), matrixPacking == EmpColumnMajor, blockStorage == EbsStd140);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000298 }
299 else
300 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400301 return typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000302 }
303}
304
Jamie Madill98493dd2013-07-08 14:39:03 -0400305TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, TLayoutBlockStorage blockStorage)
306{
307 TString hlsl;
308
309 int elementIndex = 0;
310
311 for (unsigned int typeIndex = 0; typeIndex < interfaceBlock.fields().size(); typeIndex++)
312 {
313 const TField &field = *interfaceBlock.fields()[typeIndex];
314 const TType &fieldType = *field.type();
315
316 if (blockStorage == EbsStd140)
317 {
318 // 2 and 3 component vector types in some cases need pre-padding
319 hlsl += std140PrePaddingString(fieldType, &elementIndex);
320 }
321
322 hlsl += " " + interfaceBlockFieldTypeString(field, blockStorage) +
323 " " + decorate(field.name()) + arrayString(fieldType) + ";\n";
324
325 // must pad out after matrices and arrays, where HLSL usually allows itself room to pack stuff
326 if (blockStorage == EbsStd140)
327 {
328 const bool useHLSLRowMajorPacking = (fieldType.getLayoutQualifier().matrixPacking == EmpColumnMajor);
329 hlsl += std140PostPaddingString(fieldType, useHLSLRowMajorPacking);
330 }
331 }
332
333 return hlsl;
334}
335
336TString OutputHLSL::interfaceBlockStructString(const TInterfaceBlock &interfaceBlock)
337{
338 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
339
340 return "struct " + interfaceBlockStructNameString(interfaceBlock) + "\n"
341 "{\n" +
342 interfaceBlockFieldString(interfaceBlock, blockStorage) +
343 "};\n\n";
344}
345
346TString OutputHLSL::interfaceBlockString(const TInterfaceBlock &interfaceBlock, unsigned int registerIndex, unsigned int arrayIndex)
347{
348 const TString &arrayIndexString = (arrayIndex != GL_INVALID_INDEX ? decorate(str(arrayIndex)) : "");
349 const TString &blockName = interfaceBlock.name() + arrayIndexString;
350 TString hlsl;
351
352 hlsl += "cbuffer " + blockName + " : register(b" + str(registerIndex) + ")\n"
353 "{\n";
354
355 if (interfaceBlock.hasInstanceName())
356 {
357 hlsl += " " + interfaceBlockStructNameString(interfaceBlock) + " " + interfaceBlockInstanceString(interfaceBlock, arrayIndex) + ";\n";
358 }
359 else
360 {
361 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
362 hlsl += interfaceBlockFieldString(interfaceBlock, blockStorage);
363 }
364
365 hlsl += "};\n\n";
366
367 return hlsl;
368}
369
Jamie Madill574d9dd2013-06-20 11:55:56 -0400370TString OutputHLSL::std140PrePaddingString(const TType &type, int *elementIndex)
371{
372 if (type.getBasicType() == EbtStruct || type.isMatrix() || type.isArray())
373 {
374 // no padding needed, HLSL will align the field to a new register
375 *elementIndex = 0;
376 return "";
377 }
378
379 const GLenum glType = glVariableType(type);
380 const int numComponents = gl::UniformComponentCount(glType);
381
382 if (numComponents >= 4)
383 {
384 // no padding needed, HLSL will align the field to a new register
385 *elementIndex = 0;
386 return "";
387 }
388
389 if (*elementIndex + numComponents > 4)
390 {
391 // no padding needed, HLSL will align the field to a new register
392 *elementIndex = numComponents;
393 return "";
394 }
395
396 TString padding;
397
398 const int alignment = numComponents == 3 ? 4 : numComponents;
399 const int paddingOffset = (*elementIndex % alignment);
400
401 if (paddingOffset != 0)
402 {
403 // padding is neccessary
404 for (int paddingIndex = paddingOffset; paddingIndex < alignment; paddingIndex++)
405 {
406 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
407 }
408
409 *elementIndex += (alignment - paddingOffset);
410 }
411
412 *elementIndex += numComponents;
413 *elementIndex %= 4;
414
415 return padding;
416}
417
Jamie Madille4075c92013-06-21 09:15:32 -0400418TString OutputHLSL::std140PostPaddingString(const TType &type, bool useHLSLRowMajorPacking)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400419{
Jamie Madillc835df62013-06-21 09:15:32 -0400420 if (!type.isMatrix() && !type.isArray() && type.getBasicType() != EbtStruct)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400421 {
422 return "";
423 }
424
Jamie Madill574d9dd2013-06-20 11:55:56 -0400425 int numComponents = 0;
426
427 if (type.isMatrix())
428 {
Jamie Madille4075c92013-06-21 09:15:32 -0400429 // This method can also be called from structureString, which does not use layout qualifiers.
430 // Thus, use the method parameter for determining the matrix packing.
431 //
432 // Note HLSL row major packing corresponds to GL API column-major, and vice-versa, since we
433 // wish to always transpose GL matrices to play well with HLSL's matrix array indexing.
434 //
435 const bool isRowMajorMatrix = !useHLSLRowMajorPacking;
Jamie Madillc835df62013-06-21 09:15:32 -0400436 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400437 numComponents = gl::MatrixComponentCount(glType, isRowMajorMatrix);
438 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400439 else if (type.getStruct())
Jamie Madillc835df62013-06-21 09:15:32 -0400440 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400441 const TString &structName = structureTypeName(*type.getStruct(), useHLSLRowMajorPacking, true);
Jamie Madille4075c92013-06-21 09:15:32 -0400442 numComponents = mStd140StructElementIndexes[structName];
443
444 if (numComponents == 0)
445 {
446 return "";
447 }
Jamie Madillc835df62013-06-21 09:15:32 -0400448 }
Jamie Madill574d9dd2013-06-20 11:55:56 -0400449 else
450 {
Jamie Madillc835df62013-06-21 09:15:32 -0400451 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400452 numComponents = gl::UniformComponentCount(glType);
453 }
454
455 TString padding;
456 for (int paddingOffset = numComponents; paddingOffset < 4; paddingOffset++)
457 {
458 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
459 }
460 return padding;
461}
462
Jamie Madill440dc742013-06-20 11:55:55 -0400463// Use the same layout for packed and shared
464void setBlockLayout(InterfaceBlock *interfaceBlock, BlockLayoutType newLayout)
465{
466 interfaceBlock->layout = newLayout;
467 interfaceBlock->blockInfo.clear();
468
469 switch (newLayout)
470 {
471 case BLOCKLAYOUT_SHARED:
472 case BLOCKLAYOUT_PACKED:
473 {
474 HLSLBlockEncoder hlslEncoder(&interfaceBlock->blockInfo);
475 hlslEncoder.encodeFields(interfaceBlock->activeUniforms);
476 interfaceBlock->dataSize = hlslEncoder.getBlockSize();
477 }
478 break;
479
480 case BLOCKLAYOUT_STANDARD:
481 {
482 Std140BlockEncoder stdEncoder(&interfaceBlock->blockInfo);
483 stdEncoder.encodeFields(interfaceBlock->activeUniforms);
484 interfaceBlock->dataSize = stdEncoder.getBlockSize();
485 }
486 break;
487
488 default:
489 UNREACHABLE();
490 break;
491 }
492}
493
Jamie Madill574d9dd2013-06-20 11:55:56 -0400494BlockLayoutType convertBlockLayoutType(TLayoutBlockStorage blockStorage)
495{
496 switch (blockStorage)
497 {
498 case EbsPacked: return BLOCKLAYOUT_PACKED;
499 case EbsShared: return BLOCKLAYOUT_SHARED;
500 case EbsStd140: return BLOCKLAYOUT_STANDARD;
501 default: UNREACHABLE(); return BLOCKLAYOUT_SHARED;
502 }
503}
504
Jamie Madill98493dd2013-07-08 14:39:03 -0400505TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400506{
507 TString init;
508
509 TString preIndentString;
510 TString fullIndentString;
511
512 for (int spaces = 0; spaces < (indent * 4); spaces++)
513 {
514 preIndentString += ' ';
515 }
516
517 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
518 {
519 fullIndentString += ' ';
520 }
521
522 init += preIndentString + "{\n";
523
Jamie Madill98493dd2013-07-08 14:39:03 -0400524 const TFieldList &fields = structure.fields();
525 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400526 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400527 const TField &field = *fields[fieldIndex];
528 const TString &fieldName = rhsStructName + "." + decorate(field.name());
529 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400530
Jamie Madill98493dd2013-07-08 14:39:03 -0400531 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400532 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400533 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400534 }
535 else
536 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400537 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400538 }
539 }
540
541 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
542
543 return init;
544}
545
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000546void OutputHLSL::header()
547{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000548 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000549
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000550 TString uniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000551 TString interfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000552 TString varyings;
553 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400554 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000555
556 for (ReferencedSymbols::const_iterator uniform = mReferencedUniforms.begin(); uniform != mReferencedUniforms.end(); uniform++)
557 {
558 const TType &type = uniform->second->getType();
559 const TString &name = uniform->second->getSymbol();
560
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000561 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture
562 {
563 int index = samplerRegister(mReferencedUniforms[name]);
564
565 uniforms += "uniform SamplerState sampler_" + decorateUniform(name, type) + arrayString(type) +
566 " : register(s" + str(index) + ");\n";
567
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000568 uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) +
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000569 " : register(t" + str(index) + ");\n";
570 }
571 else
572 {
573 uniforms += "uniform " + typeString(type) + " " + decorateUniform(name, type) + arrayString(type) +
574 " : register(" + registerString(mReferencedUniforms[name]) + ");\n";
575 }
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000576 }
577
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000578 for (ReferencedSymbols::const_iterator interfaceBlockIt = mReferencedInterfaceBlocks.begin(); interfaceBlockIt != mReferencedInterfaceBlocks.end(); interfaceBlockIt++)
579 {
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000580 const TType &nodeType = interfaceBlockIt->second->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -0400581 const TInterfaceBlock &interfaceBlock = *nodeType.getInterfaceBlock();
582 const TFieldList &fieldList = interfaceBlock.fields();
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000583
Jamie Madill98493dd2013-07-08 14:39:03 -0400584 unsigned int arraySize = static_cast<unsigned int>(interfaceBlock.arraySize());
585 sh::InterfaceBlock activeBlock(interfaceBlock.name().c_str(), arraySize, mInterfaceBlockRegister);
586 for (unsigned int typeIndex = 0; typeIndex < fieldList.size(); typeIndex++)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000587 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400588 const TField &field = *fieldList[typeIndex];
589 const TString &fullUniformName = interfaceBlockFieldString(interfaceBlock, field);
590 declareUniformToList(*field.type(), fullUniformName, typeIndex, activeBlock.activeUniforms);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000591 }
592
Jamie Madill98493dd2013-07-08 14:39:03 -0400593 mInterfaceBlockRegister += std::max(1u, arraySize);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000594
Jamie Madill98493dd2013-07-08 14:39:03 -0400595 BlockLayoutType blockLayoutType = convertBlockLayoutType(interfaceBlock.blockStorage());
596 setBlockLayout(&activeBlock, blockLayoutType);
597 mActiveInterfaceBlocks.push_back(activeBlock);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000598
Jamie Madill98493dd2013-07-08 14:39:03 -0400599 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000600 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400601 interfaceBlocks += interfaceBlockStructString(interfaceBlock);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000602 }
603
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000604 if (arraySize > 0)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000605 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000606 for (unsigned int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
607 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400608 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex + arrayIndex, arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000609 }
610 }
611 else
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000612 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400613 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000614 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000615 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000616
Jamie Madill570e04d2013-06-21 09:15:33 -0400617 for (auto flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
618 {
619 TIntermTyped *structNode = flaggedStructIt->first;
620 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400621 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400622 const TString &originalName = mFlaggedStructOriginalNames[structNode];
623
Jamie Madill98493dd2013-07-08 14:39:03 -0400624 flaggedStructs += "static " + decorate(structure.name()) + " " + mappedName + " =\n";
625 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400626 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
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400705 if (mUsesFragDepth)
706 {
707 out << "static float gl_Depth = 0.0;\n";
708 }
709
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000710 if (mUsesFragCoord)
711 {
712 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
713 }
714
715 if (mUsesPointCoord)
716 {
717 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
718 }
719
720 if (mUsesFrontFacing)
721 {
722 out << "static bool gl_FrontFacing = false;\n";
723 }
724
725 out << "\n";
726
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000727 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000728 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000729 out << "struct gl_DepthRangeParameters\n"
730 "{\n"
731 " float near;\n"
732 " float far;\n"
733 " float diff;\n"
734 "};\n"
735 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000736 }
737
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000738 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000739 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000740 out << "cbuffer DriverConstants : register(b1)\n"
741 "{\n";
742
743 if (mUsesDepthRange)
744 {
745 out << " float3 dx_DepthRange : packoffset(c0);\n";
746 }
747
748 if (mUsesFragCoord)
749 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000750 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000751 }
752
753 if (mUsesFragCoord || mUsesFrontFacing)
754 {
755 out << " float3 dx_DepthFront : packoffset(c2);\n";
756 }
757
758 out << "};\n";
759 }
760 else
761 {
762 if (mUsesDepthRange)
763 {
764 out << "uniform float3 dx_DepthRange : register(c0);";
765 }
766
767 if (mUsesFragCoord)
768 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000769 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000770 }
771
772 if (mUsesFragCoord || mUsesFrontFacing)
773 {
774 out << "uniform float3 dx_DepthFront : register(c2);\n";
775 }
776 }
777
778 out << "\n";
779
780 if (mUsesDepthRange)
781 {
782 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
783 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000784 }
785
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000786 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000787 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000788
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000789 if (!interfaceBlocks.empty())
790 {
791 out << interfaceBlocks;
792 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400793
794 if (!flaggedStructs.empty())
795 {
796 out << "// Std140 Structures accessed by value\n";
797 out << "\n";
798 out << flaggedStructs;
799 out << "\n";
800 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000801 }
802
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000803 if (usingMRTExtension && mNumRenderTargets > 1)
804 {
805 out << "#define GL_USES_MRT\n";
806 }
807
808 if (mUsesFragColor)
809 {
810 out << "#define GL_USES_FRAG_COLOR\n";
811 }
812
813 if (mUsesFragData)
814 {
815 out << "#define GL_USES_FRAG_DATA\n";
816 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000817 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000818 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000819 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000820 out << "// Attributes\n";
821 out << attributes;
822 out << "\n"
823 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
824
825 if (mUsesPointSize)
826 {
827 out << "static float gl_PointSize = float(1);\n";
828 }
829
830 out << "\n"
831 "// Varyings\n";
832 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000833 out << "\n";
834
835 if (mUsesDepthRange)
836 {
837 out << "struct gl_DepthRangeParameters\n"
838 "{\n"
839 " float near;\n"
840 " float far;\n"
841 " float diff;\n"
842 "};\n"
843 "\n";
844 }
845
846 if (mOutputType == SH_HLSL11_OUTPUT)
847 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000848 if (mUsesDepthRange)
849 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000850 out << "cbuffer DriverConstants : register(b1)\n"
851 "{\n"
852 " float3 dx_DepthRange : packoffset(c0);\n"
853 "};\n"
854 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000855 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000856 }
857 else
858 {
859 if (mUsesDepthRange)
860 {
861 out << "uniform float3 dx_DepthRange : register(c0);\n";
862 }
863
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000864 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000865 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000866 }
867
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000868 if (mUsesDepthRange)
869 {
870 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
871 "\n";
872 }
873
874 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000876
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000877 if (!interfaceBlocks.empty())
878 {
879 out << interfaceBlocks;
880 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400881
882 if (!flaggedStructs.empty())
883 {
884 out << "// Std140 Structures accessed by value\n";
885 out << "\n";
886 out << flaggedStructs;
887 out << "\n";
888 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000889 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400890 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000891
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400892 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
893 {
894 // Return type
895 switch(textureFunction->sampler)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000896 {
Nicolas Capensfb50dff2013-06-24 16:16:23 -0400897 case EbtSampler2D: out << "float4 "; break;
898 case EbtSampler3D: out << "float4 "; break;
899 case EbtSamplerCube: out << "float4 "; break;
900 case EbtSampler2DArray: out << "float4 "; break;
901 case EbtISampler2D: out << "int4 "; break;
902 case EbtISampler3D: out << "int4 "; break;
903 case EbtISamplerCube: out << "int4 "; break;
904 case EbtISampler2DArray: out << "int4 "; break;
905 case EbtUSampler2D: out << "uint4 "; break;
906 case EbtUSampler3D: out << "uint4 "; break;
907 case EbtUSamplerCube: out << "uint4 "; break;
908 case EbtUSampler2DArray: out << "uint4 "; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400909 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000910 }
911
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400912 // Function name
913 out << textureFunction->name();
914
915 // Argument list
916 int hlslCoords = 4;
917
918 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000919 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400920 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000921 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400922 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
923 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
924 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000925 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400926
927 switch(textureFunction->mipmap)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000928 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400929 case TextureFunction::IMPLICIT: break;
930 case TextureFunction::BIAS: hlslCoords = 4; break;
931 case TextureFunction::LOD: hlslCoords = 4; break;
932 case TextureFunction::LOD0: hlslCoords = 4; break;
933 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000934 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400935 }
936 else if (mOutputType == SH_HLSL11_OUTPUT)
937 {
938 switch(textureFunction->sampler)
939 {
Nicolas Capensfb50dff2013-06-24 16:16:23 -0400940 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
941 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
942 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
943 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 2; break;
944 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
945 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
946 case EbtISamplerCube: out << "TextureCube<int4> x, SamplerState s"; hlslCoords = 3; break;
947 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 2; break;
948 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
949 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
950 case EbtUSamplerCube: out << "TextureCube<uint4> x, SamplerState s"; hlslCoords = 3; break;
951 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 2; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400952 default: UNREACHABLE();
953 }
954 }
955 else UNREACHABLE();
956
957 switch(textureFunction->coords)
958 {
959 case 2: out << ", float2 t"; break;
960 case 3: out << ", float3 t"; break;
961 case 4: out << ", float4 t"; break;
962 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000963 }
964
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400965 switch(textureFunction->mipmap)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000966 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400967 case TextureFunction::IMPLICIT: break;
968 case TextureFunction::BIAS: out << ", float bias"; break;
969 case TextureFunction::LOD: out << ", float lod"; break;
970 case TextureFunction::LOD0: break;
971 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000972 }
973
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400974 out << ")\n"
975 "{\n"
976 " return ";
977
978 // HLSL intrinsic
979 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000980 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400981 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000982 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400983 case EbtSampler2D: out << "tex2D"; break;
984 case EbtSamplerCube: out << "texCUBE"; break;
985 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000986 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400987 }
988 else if (mOutputType == SH_HLSL11_OUTPUT)
989 {
990 out << "x.Sample";
991 }
992 else UNREACHABLE();
993
994 if (mOutputType == SH_HLSL9_OUTPUT)
995 {
996 switch(textureFunction->mipmap)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000997 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400998 case TextureFunction::IMPLICIT: out << "(s, "; break;
999 case TextureFunction::BIAS: out << "bias(s, "; break;
1000 case TextureFunction::LOD: out << "lod(s, "; break;
1001 case TextureFunction::LOD0: out << "lod(s, "; break;
1002 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001003 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001004 }
1005 else if (mOutputType == SH_HLSL11_OUTPUT)
1006 {
1007 switch(textureFunction->mipmap)
1008 {
1009 case TextureFunction::IMPLICIT: out << "(s, "; break;
1010 case TextureFunction::BIAS: out << "Bias(s, "; break;
1011 case TextureFunction::LOD: out << "Level(s, "; break;
1012 case TextureFunction::LOD0: out << "Level(s, "; break;
1013 default: UNREACHABLE();
1014 }
1015 }
1016 else UNREACHABLE();
1017
1018 switch(hlslCoords)
1019 {
1020 case 2: out << "float2("; break;
1021 case 3: out << "float3("; break;
1022 case 4: out << "float4("; break;
1023 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +00001024 }
1025
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001026 TString proj = "";
1027
1028 if (textureFunction->proj)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001029 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001030 switch(textureFunction->coords)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001031 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001032 case 3: proj = " / t.z"; break;
1033 case 4: proj = " / t.w"; break;
1034 default: UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001035 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001036 }
1037
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001038 out << "t.x" + proj + ", t.y" + proj;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001039
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001040 if (mOutputType == SH_HLSL9_OUTPUT)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001041 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001042 if (hlslCoords >= 3)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001043 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001044 if (textureFunction->coords < 3)
1045 {
1046 out << ", 0";
1047 }
1048 else
1049 {
1050 out << ", t.z" + proj;
1051 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001052 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001053
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001054 if (hlslCoords == 4)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001055 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001056 switch(textureFunction->mipmap)
1057 {
1058 case TextureFunction::BIAS: out << ", bias"; break;
1059 case TextureFunction::LOD: out << ", lod"; break;
1060 case TextureFunction::LOD0: out << ", 0"; break;
1061 default: UNREACHABLE();
1062 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001063 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001064
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001065 out << "));\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +00001066 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001067 else if (mOutputType == SH_HLSL11_OUTPUT)
1068 {
1069 if (hlslCoords >= 3)
1070 {
1071 out << ", t.z" + proj;
1072 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001073
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001074 switch(textureFunction->mipmap)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001075 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001076 case TextureFunction::IMPLICIT: out << "));"; break;
1077 case TextureFunction::BIAS: out << "), bias);"; break;
1078 case TextureFunction::LOD: out << "), lod);"; break;
1079 case TextureFunction::LOD0: out << "), 0);"; break;
1080 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001081 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001082 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001083 else UNREACHABLE();
1084
1085 out << "}\n"
1086 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001087 }
1088
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001089 if (mUsesFragCoord)
1090 {
1091 out << "#define GL_USES_FRAG_COORD\n";
1092 }
1093
1094 if (mUsesPointCoord)
1095 {
1096 out << "#define GL_USES_POINT_COORD\n";
1097 }
1098
1099 if (mUsesFrontFacing)
1100 {
1101 out << "#define GL_USES_FRONT_FACING\n";
1102 }
1103
1104 if (mUsesPointSize)
1105 {
1106 out << "#define GL_USES_POINT_SIZE\n";
1107 }
1108
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001109 if (mUsesFragDepth)
1110 {
1111 out << "#define GL_USES_FRAG_DEPTH\n";
1112 }
1113
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001114 if (mUsesDepthRange)
1115 {
1116 out << "#define GL_USES_DEPTH_RANGE\n";
1117 }
1118
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001119 if (mUsesXor)
1120 {
1121 out << "bool xor(bool p, bool q)\n"
1122 "{\n"
1123 " return (p || q) && !(p && q);\n"
1124 "}\n"
1125 "\n";
1126 }
1127
1128 if (mUsesMod1)
1129 {
1130 out << "float mod(float x, float y)\n"
1131 "{\n"
1132 " return x - y * floor(x / y);\n"
1133 "}\n"
1134 "\n";
1135 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001136
1137 if (mUsesMod2v)
1138 {
1139 out << "float2 mod(float2 x, float2 y)\n"
1140 "{\n"
1141 " return x - y * floor(x / y);\n"
1142 "}\n"
1143 "\n";
1144 }
1145
1146 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001147 {
1148 out << "float2 mod(float2 x, float y)\n"
1149 "{\n"
1150 " return x - y * floor(x / y);\n"
1151 "}\n"
1152 "\n";
1153 }
1154
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001155 if (mUsesMod3v)
1156 {
1157 out << "float3 mod(float3 x, float3 y)\n"
1158 "{\n"
1159 " return x - y * floor(x / y);\n"
1160 "}\n"
1161 "\n";
1162 }
1163
1164 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001165 {
1166 out << "float3 mod(float3 x, float y)\n"
1167 "{\n"
1168 " return x - y * floor(x / y);\n"
1169 "}\n"
1170 "\n";
1171 }
1172
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001173 if (mUsesMod4v)
1174 {
1175 out << "float4 mod(float4 x, float4 y)\n"
1176 "{\n"
1177 " return x - y * floor(x / y);\n"
1178 "}\n"
1179 "\n";
1180 }
1181
1182 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001183 {
1184 out << "float4 mod(float4 x, float y)\n"
1185 "{\n"
1186 " return x - y * floor(x / y);\n"
1187 "}\n"
1188 "\n";
1189 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001190
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001191 if (mUsesFaceforward1)
1192 {
1193 out << "float faceforward(float N, float I, float Nref)\n"
1194 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001195 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001196 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001197 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001198 " }\n"
1199 " else\n"
1200 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001201 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001202 " }\n"
1203 "}\n"
1204 "\n";
1205 }
1206
1207 if (mUsesFaceforward2)
1208 {
1209 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1210 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001211 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001212 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001213 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001214 " }\n"
1215 " else\n"
1216 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001217 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001218 " }\n"
1219 "}\n"
1220 "\n";
1221 }
1222
1223 if (mUsesFaceforward3)
1224 {
1225 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1226 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001227 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001228 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001229 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001230 " }\n"
1231 " else\n"
1232 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001233 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001234 " }\n"
1235 "}\n"
1236 "\n";
1237 }
1238
1239 if (mUsesFaceforward4)
1240 {
1241 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1242 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001243 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001244 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001245 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001246 " }\n"
1247 " else\n"
1248 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001249 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001250 " }\n"
1251 "}\n"
1252 "\n";
1253 }
1254
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001255 for (unsigned int cols = 2; cols <= 4; cols++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001256 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001257 for (unsigned int rows = 2; rows <= 4; rows++)
1258 {
1259 if (mUsesEqualMat[cols][rows])
1260 {
1261 TString matrixType = "float" + str(cols) + "x" + str(rows);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001262
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001263 out << "bool equal(" + matrixType + " m, " + matrixType + " n)\n"
1264 "{\n";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001265
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001266 for (unsigned int row = 0; row < rows; row++)
1267 {
1268 if (row == 0)
1269 {
1270 out << " return ";
1271 }
1272 else
1273 {
1274 out << " ";
1275 }
1276
1277 for (unsigned int col = 0; col < cols; col++)
1278 {
1279 TString index = "[" + str(col) + "][" + str(row) + "]";
1280 out << "m" + index + " == n" + index;
1281
1282 if (col == cols-1 && row == rows-1)
1283 {
1284 out << ";\n";
1285 }
1286 else if (col == cols-1)
1287 {
1288 out << " &&\n";
1289 }
1290 else
1291 {
1292 out << " && ";
1293 }
1294 }
1295 }
1296
1297 out << "}\n";
1298 }
1299 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001300 }
1301
1302 if (mUsesEqualVec2)
1303 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001304 out << "bool equal(float2 v, float2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001305 "{\n"
1306 " return v.x == u.x && v.y == u.y;\n"
1307 "}\n";
1308 }
1309
1310 if (mUsesEqualVec3)
1311 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001312 out << "bool equal(float3 v, float3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001313 "{\n"
1314 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1315 "}\n";
1316 }
1317
1318 if (mUsesEqualVec4)
1319 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001320 out << "bool equal(float4 v, float4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001321 "{\n"
1322 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1323 "}\n";
1324 }
1325
1326 if (mUsesEqualIVec2)
1327 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001328 out << "bool equal(int2 v, int2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001329 "{\n"
1330 " return v.x == u.x && v.y == u.y;\n"
1331 "}\n";
1332 }
1333
1334 if (mUsesEqualIVec3)
1335 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001336 out << "bool equal(int3 v, int3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001337 "{\n"
1338 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1339 "}\n";
1340 }
1341
1342 if (mUsesEqualIVec4)
1343 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001344 out << "bool equal(int4 v, int4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001345 "{\n"
1346 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1347 "}\n";
1348 }
1349
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001350 if (mUsesEqualUVec2)
1351 {
1352 out << "bool equal(uint2 v, uint2 u)\n"
1353 "{\n"
1354 " return v.x == u.x && v.y == u.y;\n"
1355 "}\n";
1356 }
1357
1358 if (mUsesEqualUVec3)
1359 {
1360 out << "bool equal(uint3 v, uint3 u)\n"
1361 "{\n"
1362 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1363 "}\n";
1364 }
1365
1366 if (mUsesEqualUVec4)
1367 {
1368 out << "bool equal(uint4 v, uint4 u)\n"
1369 "{\n"
1370 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1371 "}\n";
1372 }
1373
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001374 if (mUsesEqualBVec2)
1375 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001376 out << "bool equal(bool2 v, bool2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001377 "{\n"
1378 " return v.x == u.x && v.y == u.y;\n"
1379 "}\n";
1380 }
1381
1382 if (mUsesEqualBVec3)
1383 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001384 out << "bool equal(bool3 v, bool3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001385 "{\n"
1386 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1387 "}\n";
1388 }
1389
1390 if (mUsesEqualBVec4)
1391 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001392 out << "bool equal(bool4 v, bool4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001393 "{\n"
1394 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1395 "}\n";
1396 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001397
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001398 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001399 {
1400 out << "float atanyx(float y, float x)\n"
1401 "{\n"
1402 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1403 " return atan2(y, x);\n"
1404 "}\n";
1405 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001406
1407 if (mUsesAtan2_2)
1408 {
1409 out << "float2 atanyx(float2 y, float2 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 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1414 "}\n";
1415 }
1416
1417 if (mUsesAtan2_3)
1418 {
1419 out << "float3 atanyx(float3 y, float3 x)\n"
1420 "{\n"
1421 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1422 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1423 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1424 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1425 "}\n";
1426 }
1427
1428 if (mUsesAtan2_4)
1429 {
1430 out << "float4 atanyx(float4 y, float4 x)\n"
1431 "{\n"
1432 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1433 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1434 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1435 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1436 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1437 "}\n";
1438 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001439}
1440
1441void OutputHLSL::visitSymbol(TIntermSymbol *node)
1442{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001443 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001444
Jamie Madill570e04d2013-06-21 09:15:33 -04001445 // Handle accessing std140 structs by value
1446 if (mFlaggedStructMappedNames.count(node) > 0)
1447 {
1448 out << mFlaggedStructMappedNames[node];
1449 return;
1450 }
1451
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001452 TString name = node->getSymbol();
1453
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001454 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001455 {
1456 mUsesDepthRange = true;
1457 out << name;
1458 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001459 else
1460 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001461 TQualifier qualifier = node->getQualifier();
1462
1463 if (qualifier == EvqUniform)
1464 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001465 const TType& nodeType = node->getType();
1466 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1467
1468 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001469 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001470 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001471 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001472 else
1473 {
1474 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001475 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001476
1477 out << decorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001478 }
Jamie Madillb120eac2013-06-12 14:08:13 -04001479 else if (qualifier == EvqAttribute || qualifier == EvqVertexInput)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001480 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001481 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001482 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001483 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001484 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001485 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001486 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001487 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001488 }
Jamie Madill46131a32013-06-20 11:55:50 -04001489 else if (qualifier == EvqFragmentOutput)
1490 {
1491 mReferencedOutputVariables[name] = node;
1492 out << "out_" << name;
1493 }
1494 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001495 {
1496 out << "gl_Color[0]";
1497 mUsesFragColor = true;
1498 }
1499 else if (qualifier == EvqFragData)
1500 {
1501 out << "gl_Color";
1502 mUsesFragData = true;
1503 }
1504 else if (qualifier == EvqFragCoord)
1505 {
1506 mUsesFragCoord = true;
1507 out << name;
1508 }
1509 else if (qualifier == EvqPointCoord)
1510 {
1511 mUsesPointCoord = true;
1512 out << name;
1513 }
1514 else if (qualifier == EvqFrontFacing)
1515 {
1516 mUsesFrontFacing = true;
1517 out << name;
1518 }
1519 else if (qualifier == EvqPointSize)
1520 {
1521 mUsesPointSize = true;
1522 out << name;
1523 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001524 else if (name == "gl_FragDepthEXT")
1525 {
1526 mUsesFragDepth = true;
1527 out << "gl_Depth";
1528 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001529 else
1530 {
1531 out << decorate(name);
1532 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001533 }
1534}
1535
1536bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1537{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001538 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001539
Jamie Madill570e04d2013-06-21 09:15:33 -04001540 // Handle accessing std140 structs by value
1541 if (mFlaggedStructMappedNames.count(node) > 0)
1542 {
1543 out << mFlaggedStructMappedNames[node];
1544 return false;
1545 }
1546
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001547 switch (node->getOp())
1548 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001549 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001550 case EOpInitialize:
1551 if (visit == PreVisit)
1552 {
1553 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1554 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1555 // new variable is created before the assignment is evaluated), so we need to convert
1556 // this to "float t = x, x = t;".
1557
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001558 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1559 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001560
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001561 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1562 expression->traverse(&searchSymbol);
1563 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001564
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001565 if (sameSymbol)
1566 {
1567 // Type already printed
1568 out << "t" + str(mUniqueIndex) + " = ";
1569 expression->traverse(this);
1570 out << ", ";
1571 symbolNode->traverse(this);
1572 out << " = t" + str(mUniqueIndex);
1573
1574 mUniqueIndex++;
1575 return false;
1576 }
1577 }
1578 else if (visit == InVisit)
1579 {
1580 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001581 }
1582 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001583 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1584 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1585 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1586 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1587 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1588 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001589 if (visit == PreVisit)
1590 {
1591 out << "(";
1592 }
1593 else if (visit == InVisit)
1594 {
1595 out << " = mul(";
1596 node->getLeft()->traverse(this);
1597 out << ", transpose(";
1598 }
1599 else
1600 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001601 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001602 }
1603 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001604 case EOpMatrixTimesMatrixAssign:
1605 if (visit == PreVisit)
1606 {
1607 out << "(";
1608 }
1609 else if (visit == InVisit)
1610 {
1611 out << " = mul(";
1612 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001613 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001614 }
1615 else
1616 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001617 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001618 }
1619 break;
1620 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001621 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001622 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001623 const TType& leftType = node->getLeft()->getType();
1624 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001625 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001626 if (visit == PreVisit)
1627 {
1628 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1629 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1630
1631 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1632 out << interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1633
1634 return false;
1635 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001636 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001637 else
1638 {
1639 outputTriplet(visit, "", "[", "]");
1640 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001641 }
1642 break;
1643 case EOpIndexIndirect:
1644 // We do not currently support indirect references to interface blocks
1645 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1646 outputTriplet(visit, "", "[", "]");
1647 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001648 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001649 if (visit == InVisit)
1650 {
1651 const TStructure* structure = node->getLeft()->getType().getStruct();
1652 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1653 const TField* field = structure->fields()[index->getIConst(0)];
1654 out << "." + decorateField(field->name(), *structure);
1655
1656 return false;
1657 }
1658 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001659 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001660 if (visit == InVisit)
1661 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001662 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1663 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1664 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
1665 out << "." + decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001666
1667 return false;
1668 }
1669 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001670 case EOpVectorSwizzle:
1671 if (visit == InVisit)
1672 {
1673 out << ".";
1674
1675 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1676
1677 if (swizzle)
1678 {
1679 TIntermSequence &sequence = swizzle->getSequence();
1680
1681 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1682 {
1683 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1684
1685 if (element)
1686 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001687 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001688
1689 switch (i)
1690 {
1691 case 0: out << "x"; break;
1692 case 1: out << "y"; break;
1693 case 2: out << "z"; break;
1694 case 3: out << "w"; break;
1695 default: UNREACHABLE();
1696 }
1697 }
1698 else UNREACHABLE();
1699 }
1700 }
1701 else UNREACHABLE();
1702
1703 return false; // Fully processed
1704 }
1705 break;
1706 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1707 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1708 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1709 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001710 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001711 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001712 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001713 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001714 if (node->getOp() == EOpEqual)
1715 {
1716 outputTriplet(visit, "(", " == ", ")");
1717 }
1718 else
1719 {
1720 outputTriplet(visit, "(", " != ", ")");
1721 }
1722 }
1723 else if (node->getLeft()->getBasicType() == EbtStruct)
1724 {
1725 if (node->getOp() == EOpEqual)
1726 {
1727 out << "(";
1728 }
1729 else
1730 {
1731 out << "!(";
1732 }
1733
Jamie Madill98493dd2013-07-08 14:39:03 -04001734 const TStructure &structure = *node->getLeft()->getType().getStruct();
1735 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001736
Jamie Madill98493dd2013-07-08 14:39:03 -04001737 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001738 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001739 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001740
1741 node->getLeft()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001742 out << "." + decorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001743 node->getRight()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001744 out << "." + decorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001745
Jamie Madill98493dd2013-07-08 14:39:03 -04001746 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001747 {
1748 out << " && ";
1749 }
1750 }
1751
1752 out << ")";
1753
1754 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001755 }
1756 else
1757 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001758 if (node->getLeft()->isMatrix())
1759 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001760 mUsesEqualMat[node->getLeft()->getCols()][node->getLeft()->getRows()] = true;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001761 }
1762 else if (node->getLeft()->isVector())
1763 {
1764 switch (node->getLeft()->getBasicType())
1765 {
1766 case EbtFloat:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001767 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001768 {
1769 case 2: mUsesEqualVec2 = true; break;
1770 case 3: mUsesEqualVec3 = true; break;
1771 case 4: mUsesEqualVec4 = true; break;
1772 default: UNREACHABLE();
1773 }
1774 break;
1775 case EbtInt:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001776 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001777 {
1778 case 2: mUsesEqualIVec2 = true; break;
1779 case 3: mUsesEqualIVec3 = true; break;
1780 case 4: mUsesEqualIVec4 = true; break;
1781 default: UNREACHABLE();
1782 }
1783 break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001784 case EbtUInt:
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001785 switch (node->getLeft()->getNominalSize())
1786 {
1787 case 2: mUsesEqualUVec2 = true; break;
1788 case 3: mUsesEqualUVec3 = true; break;
1789 case 4: mUsesEqualUVec4 = true; break;
1790 default: UNREACHABLE();
1791 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001792 break;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001793 case EbtBool:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001794 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001795 {
1796 case 2: mUsesEqualBVec2 = true; break;
1797 case 3: mUsesEqualBVec3 = true; break;
1798 case 4: mUsesEqualBVec4 = true; break;
1799 default: UNREACHABLE();
1800 }
1801 break;
1802 default: UNREACHABLE();
1803 }
1804 }
1805 else UNREACHABLE();
1806
1807 if (node->getOp() == EOpEqual)
1808 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001809 outputTriplet(visit, "equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001810 }
1811 else
1812 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001813 outputTriplet(visit, "!equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001814 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001815 }
1816 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001817 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1818 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1819 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1820 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1821 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001822 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001823 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1824 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001825 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001826 case EOpLogicalOr:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001827 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001828 return false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001829 case EOpLogicalXor:
1830 mUsesXor = true;
1831 outputTriplet(visit, "xor(", ", ", ")");
1832 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001833 case EOpLogicalAnd:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001834 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001835 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001836 default: UNREACHABLE();
1837 }
1838
1839 return true;
1840}
1841
1842bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1843{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001844 switch (node->getOp())
1845 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001846 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1847 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1848 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1849 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1850 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1851 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1852 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001853 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04001854 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001855 case EOpConvFloatToBool:
1856 switch (node->getOperand()->getType().getNominalSize())
1857 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001858 case 1: outputTriplet(visit, "bool(", "", ")"); break;
1859 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
1860 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
1861 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001862 default: UNREACHABLE();
1863 }
1864 break;
1865 case EOpConvBoolToFloat:
1866 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04001867 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001868 switch (node->getOperand()->getType().getNominalSize())
1869 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001870 case 1: outputTriplet(visit, "float(", "", ")"); break;
1871 case 2: outputTriplet(visit, "float2(", "", ")"); break;
1872 case 3: outputTriplet(visit, "float3(", "", ")"); break;
1873 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001874 default: UNREACHABLE();
1875 }
1876 break;
1877 case EOpConvFloatToInt:
1878 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04001879 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001880 switch (node->getOperand()->getType().getNominalSize())
1881 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001882 case 1: outputTriplet(visit, "int(", "", ")"); break;
1883 case 2: outputTriplet(visit, "int2(", "", ")"); break;
1884 case 3: outputTriplet(visit, "int3(", "", ")"); break;
1885 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001886 default: UNREACHABLE();
1887 }
1888 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04001889 case EOpConvFloatToUInt:
1890 case EOpConvBoolToUInt:
1891 case EOpConvIntToUInt:
Jamie Madilla16f1672013-07-03 15:15:17 -04001892 switch (node->getOperand()->getType().getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001893 {
1894 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001895 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
1896 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
1897 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001898 default: UNREACHABLE();
1899 }
1900 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001901 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1902 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1903 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1904 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1905 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1906 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1907 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1908 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1909 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1910 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1911 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1912 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1913 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1914 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1915 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1916 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1917 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1918 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1919 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1920 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1921 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001922 case EOpDFdx:
1923 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1924 {
1925 outputTriplet(visit, "(", "", ", 0.0)");
1926 }
1927 else
1928 {
1929 outputTriplet(visit, "ddx(", "", ")");
1930 }
1931 break;
1932 case EOpDFdy:
1933 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1934 {
1935 outputTriplet(visit, "(", "", ", 0.0)");
1936 }
1937 else
1938 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001939 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001940 }
1941 break;
1942 case EOpFwidth:
1943 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1944 {
1945 outputTriplet(visit, "(", "", ", 0.0)");
1946 }
1947 else
1948 {
1949 outputTriplet(visit, "fwidth(", "", ")");
1950 }
1951 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001952 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1953 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001954 default: UNREACHABLE();
1955 }
1956
1957 return true;
1958}
1959
1960bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1961{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001962 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001963
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001964 switch (node->getOp())
1965 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001966 case EOpSequence:
1967 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001968 if (mInsideFunction)
1969 {
Jamie Madill075edd82013-07-08 13:30:19 -04001970 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001971 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001972
1973 mScopeDepth++;
1974
1975 if (mScopeBracket.size() < mScopeDepth)
1976 {
1977 mScopeBracket.push_back(0); // New scope level
1978 }
1979 else
1980 {
1981 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
1982 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001983 }
1984
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001985 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
1986 {
Jamie Madill075edd82013-07-08 13:30:19 -04001987 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001988
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001989 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001990
1991 out << ";\n";
1992 }
1993
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001994 if (mInsideFunction)
1995 {
Jamie Madill075edd82013-07-08 13:30:19 -04001996 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001997 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001998
1999 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002000 }
2001
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002002 return false;
2003 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002004 case EOpDeclaration:
2005 if (visit == PreVisit)
2006 {
2007 TIntermSequence &sequence = node->getSequence();
2008 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002009
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00002010 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002011 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002012 if (variable->getType().getStruct())
2013 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002014 addConstructor(variable->getType(), scopedStruct(variable->getType().getStruct()->name()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00002015 }
2016
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002017 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002018 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00002019 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002020 {
2021 out << "static ";
2022 }
2023
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002024 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002025
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002026 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002027 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002028 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002029
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002030 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002031 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002032 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002033 out << arrayString(symbol->getType());
daniel@transgaming.com7127f202010-04-15 20:45:22 +00002034 out << " = " + initializer(variable->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002035 }
2036 else
2037 {
2038 (*sit)->traverse(this);
2039 }
2040
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002041 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002042 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002043 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002044 }
2045 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002046 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002047 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2048 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002049 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002050 }
2051 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002052 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002053 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002054 {
2055 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2056 {
2057 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2058
2059 if (symbol)
2060 {
2061 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2062 mReferencedVaryings[symbol->getSymbol()] = symbol;
2063 }
2064 else
2065 {
2066 (*sit)->traverse(this);
2067 }
2068 }
2069 }
2070
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002071 return false;
2072 }
2073 else if (visit == InVisit)
2074 {
2075 out << ", ";
2076 }
2077 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002078 case EOpPrototype:
2079 if (visit == PreVisit)
2080 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002081 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002082
2083 TIntermSequence &arguments = node->getSequence();
2084
2085 for (unsigned int i = 0; i < arguments.size(); i++)
2086 {
2087 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2088
2089 if (symbol)
2090 {
2091 out << argumentString(symbol);
2092
2093 if (i < arguments.size() - 1)
2094 {
2095 out << ", ";
2096 }
2097 }
2098 else UNREACHABLE();
2099 }
2100
2101 out << ");\n";
2102
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002103 // Also prototype the Lod0 variant if needed
2104 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2105 {
2106 mOutputLod0Function = true;
2107 node->traverse(this);
2108 mOutputLod0Function = false;
2109 }
2110
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002111 return false;
2112 }
2113 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002114 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002115 case EOpFunction:
2116 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002117 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002118
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002119 out << typeString(node->getType()) << " ";
2120
2121 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002122 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002123 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002124 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002125 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002126 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002127 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002128 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002129
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002130 TIntermSequence &sequence = node->getSequence();
2131 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2132
2133 for (unsigned int i = 0; i < arguments.size(); i++)
2134 {
2135 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2136
2137 if (symbol)
2138 {
2139 if (symbol->getType().getStruct())
2140 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002141 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getStruct()->name()), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002142 }
2143
2144 out << argumentString(symbol);
2145
2146 if (i < arguments.size() - 1)
2147 {
2148 out << ", ";
2149 }
2150 }
2151 else UNREACHABLE();
2152 }
2153
2154 out << ")\n"
2155 "{\n";
2156
2157 if (sequence.size() > 1)
2158 {
2159 mInsideFunction = true;
2160 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002161 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002162 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002163
2164 out << "}\n";
2165
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002166 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2167 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002168 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002169 {
2170 mOutputLod0Function = true;
2171 node->traverse(this);
2172 mOutputLod0Function = false;
2173 }
2174 }
2175
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002176 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002177 }
2178 break;
2179 case EOpFunctionCall:
2180 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002181 TString name = TFunction::unmangleName(node->getName());
2182 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002183 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002184
2185 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002186 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002187 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002188 }
2189 else
2190 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002191 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2192
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002193 TextureFunction textureFunction;
2194 textureFunction.sampler = samplerType;
2195 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
2196 textureFunction.mipmap = TextureFunction::IMPLICIT;
2197
2198 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002199 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002200 textureFunction.mipmap = TextureFunction::IMPLICIT;
2201 textureFunction.proj = false;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002202 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002203 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002204 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002205 textureFunction.mipmap = TextureFunction::IMPLICIT;
2206 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002207 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002208 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002209 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002210 textureFunction.mipmap = TextureFunction::LOD;
2211 textureFunction.proj = false;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002212 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002213 else if (name == "texture2DProjLod" || name == "textureProjLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002214 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002215 textureFunction.mipmap = TextureFunction::LOD;
2216 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002217 }
2218 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002219
2220 if (textureFunction.mipmap != TextureFunction::LOD)
2221 {
2222 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2223 {
2224 textureFunction.mipmap = TextureFunction::LOD0;
2225 }
2226 else if (arguments.size() == 3)
2227 {
2228 textureFunction.mipmap = TextureFunction::BIAS;
2229 }
2230 }
2231
2232 mUsesTexture.insert(textureFunction);
2233
2234 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002235 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002236
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002237 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2238 {
2239 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2240 {
2241 out << "texture_";
2242 (*arg)->traverse(this);
2243 out << ", sampler_";
2244 }
2245
2246 (*arg)->traverse(this);
2247
2248 if (arg < arguments.end() - 1)
2249 {
2250 out << ", ";
2251 }
2252 }
2253
2254 out << ")";
2255
2256 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002257 }
2258 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002259 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002260 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002261 addConstructor(node->getType(), "vec1", &node->getSequence());
2262 outputTriplet(visit, "vec1(", "", ")");
2263 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002264 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002265 addConstructor(node->getType(), "vec2", &node->getSequence());
2266 outputTriplet(visit, "vec2(", ", ", ")");
2267 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002268 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002269 addConstructor(node->getType(), "vec3", &node->getSequence());
2270 outputTriplet(visit, "vec3(", ", ", ")");
2271 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002272 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002273 addConstructor(node->getType(), "vec4", &node->getSequence());
2274 outputTriplet(visit, "vec4(", ", ", ")");
2275 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002276 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002277 addConstructor(node->getType(), "bvec1", &node->getSequence());
2278 outputTriplet(visit, "bvec1(", "", ")");
2279 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002280 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002281 addConstructor(node->getType(), "bvec2", &node->getSequence());
2282 outputTriplet(visit, "bvec2(", ", ", ")");
2283 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002284 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002285 addConstructor(node->getType(), "bvec3", &node->getSequence());
2286 outputTriplet(visit, "bvec3(", ", ", ")");
2287 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002288 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002289 addConstructor(node->getType(), "bvec4", &node->getSequence());
2290 outputTriplet(visit, "bvec4(", ", ", ")");
2291 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002292 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002293 addConstructor(node->getType(), "ivec1", &node->getSequence());
2294 outputTriplet(visit, "ivec1(", "", ")");
2295 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002296 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002297 addConstructor(node->getType(), "ivec2", &node->getSequence());
2298 outputTriplet(visit, "ivec2(", ", ", ")");
2299 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002300 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002301 addConstructor(node->getType(), "ivec3", &node->getSequence());
2302 outputTriplet(visit, "ivec3(", ", ", ")");
2303 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002304 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002305 addConstructor(node->getType(), "ivec4", &node->getSequence());
2306 outputTriplet(visit, "ivec4(", ", ", ")");
2307 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002308 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002309 addConstructor(node->getType(), "uvec1", &node->getSequence());
2310 outputTriplet(visit, "uvec1(", "", ")");
2311 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002312 case EOpConstructUVec2:
2313 addConstructor(node->getType(), "uvec2", &node->getSequence());
2314 outputTriplet(visit, "uvec2(", ", ", ")");
2315 break;
2316 case EOpConstructUVec3:
2317 addConstructor(node->getType(), "uvec3", &node->getSequence());
2318 outputTriplet(visit, "uvec3(", ", ", ")");
2319 break;
2320 case EOpConstructUVec4:
2321 addConstructor(node->getType(), "uvec4", &node->getSequence());
2322 outputTriplet(visit, "uvec4(", ", ", ")");
2323 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002324 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002325 addConstructor(node->getType(), "mat2", &node->getSequence());
2326 outputTriplet(visit, "mat2(", ", ", ")");
2327 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002328 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002329 addConstructor(node->getType(), "mat3", &node->getSequence());
2330 outputTriplet(visit, "mat3(", ", ", ")");
2331 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002332 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002333 addConstructor(node->getType(), "mat4", &node->getSequence());
2334 outputTriplet(visit, "mat4(", ", ", ")");
2335 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002336 case EOpConstructStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04002337 addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence());
2338 outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002339 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002340 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2341 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2342 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2343 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2344 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2345 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002346 case EOpMod:
2347 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002348 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002349 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2350 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2351 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002352 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002353 case 11: mUsesMod1 = true; break;
2354 case 22: mUsesMod2v = true; break;
2355 case 21: mUsesMod2f = true; break;
2356 case 33: mUsesMod3v = true; break;
2357 case 31: mUsesMod3f = true; break;
2358 case 44: mUsesMod4v = true; break;
2359 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002360 default: UNREACHABLE();
2361 }
2362
2363 outputTriplet(visit, "mod(", ", ", ")");
2364 }
2365 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002366 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002367 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002368 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002369 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2370 {
2371 case 1: mUsesAtan2_1 = true; break;
2372 case 2: mUsesAtan2_2 = true; break;
2373 case 3: mUsesAtan2_3 = true; break;
2374 case 4: mUsesAtan2_4 = true; break;
2375 default: UNREACHABLE();
2376 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002377 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002378 break;
2379 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2380 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2381 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2382 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2383 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2384 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2385 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2386 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2387 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002388 case EOpFaceForward:
2389 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002390 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002391 {
2392 case 1: mUsesFaceforward1 = true; break;
2393 case 2: mUsesFaceforward2 = true; break;
2394 case 3: mUsesFaceforward3 = true; break;
2395 case 4: mUsesFaceforward4 = true; break;
2396 default: UNREACHABLE();
2397 }
2398
2399 outputTriplet(visit, "faceforward(", ", ", ")");
2400 }
2401 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002402 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2403 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2404 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002405 default: UNREACHABLE();
2406 }
2407
2408 return true;
2409}
2410
2411bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2412{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002413 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002414
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002415 if (node->usesTernaryOperator())
2416 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002417 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002418 }
2419 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002420 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002421 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002422
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002423 out << "if(";
2424
2425 node->getCondition()->traverse(this);
2426
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002427 out << ")\n";
2428
Jamie Madill075edd82013-07-08 13:30:19 -04002429 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002430 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002431
daniel@transgaming.combb885322010-04-15 20:45:24 +00002432 if (node->getTrueBlock())
2433 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002434 traverseStatements(node->getTrueBlock());
daniel@transgaming.combb885322010-04-15 20:45:24 +00002435 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002436
Jamie Madill075edd82013-07-08 13:30:19 -04002437 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002438 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002439
2440 if (node->getFalseBlock())
2441 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002442 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002443
Jamie Madill075edd82013-07-08 13:30:19 -04002444 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002445 out << "{\n";
2446
Jamie Madill075edd82013-07-08 13:30:19 -04002447 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002448 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002449
Jamie Madill075edd82013-07-08 13:30:19 -04002450 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002451 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002452 }
2453 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002454
2455 return false;
2456}
2457
2458void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2459{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002460 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002461}
2462
2463bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2464{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002465 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2466
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002467 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002468 {
2469 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2470 }
2471
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002472 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002473 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002474 if (handleExcessiveLoop(node))
2475 {
2476 return false;
2477 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002478 }
2479
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002480 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002481
alokp@chromium.org52813552010-11-16 18:36:09 +00002482 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002483 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002484 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002485
Jamie Madill075edd82013-07-08 13:30:19 -04002486 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002487 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002488 }
2489 else
2490 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002491 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002492
2493 if (node->getInit())
2494 {
2495 node->getInit()->traverse(this);
2496 }
2497
2498 out << "; ";
2499
alokp@chromium.org52813552010-11-16 18:36:09 +00002500 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002501 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002502 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002503 }
2504
2505 out << "; ";
2506
alokp@chromium.org52813552010-11-16 18:36:09 +00002507 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002508 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002509 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002510 }
2511
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002512 out << ")\n";
2513
Jamie Madill075edd82013-07-08 13:30:19 -04002514 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002515 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002516 }
2517
2518 if (node->getBody())
2519 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002520 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002521 }
2522
Jamie Madill075edd82013-07-08 13:30:19 -04002523 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002524 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002525
alokp@chromium.org52813552010-11-16 18:36:09 +00002526 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002527 {
Jamie Madill075edd82013-07-08 13:30:19 -04002528 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002529 out << "while(\n";
2530
alokp@chromium.org52813552010-11-16 18:36:09 +00002531 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002532
daniel@transgaming.com73536982012-03-21 20:45:49 +00002533 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002534 }
2535
daniel@transgaming.com73536982012-03-21 20:45:49 +00002536 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002537
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002538 mInsideDiscontinuousLoop = wasDiscontinuous;
2539
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002540 return false;
2541}
2542
2543bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2544{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002545 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002546
2547 switch (node->getFlowOp())
2548 {
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002549 case EOpKill: outputTriplet(visit, "discard;\n", "", ""); break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002550 case EOpBreak:
2551 if (visit == PreVisit)
2552 {
2553 if (mExcessiveLoopIndex)
2554 {
2555 out << "{Break";
2556 mExcessiveLoopIndex->traverse(this);
2557 out << " = true; break;}\n";
2558 }
2559 else
2560 {
2561 out << "break;\n";
2562 }
2563 }
2564 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002565 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002566 case EOpReturn:
2567 if (visit == PreVisit)
2568 {
2569 if (node->getExpression())
2570 {
2571 out << "return ";
2572 }
2573 else
2574 {
2575 out << "return;\n";
2576 }
2577 }
2578 else if (visit == PostVisit)
2579 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002580 if (node->getExpression())
2581 {
2582 out << ";\n";
2583 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002584 }
2585 break;
2586 default: UNREACHABLE();
2587 }
2588
2589 return true;
2590}
2591
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002592void OutputHLSL::traverseStatements(TIntermNode *node)
2593{
2594 if (isSingleStatement(node))
2595 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002596 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002597 }
2598
2599 node->traverse(this);
2600}
2601
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002602bool OutputHLSL::isSingleStatement(TIntermNode *node)
2603{
2604 TIntermAggregate *aggregate = node->getAsAggregate();
2605
2606 if (aggregate)
2607 {
2608 if (aggregate->getOp() == EOpSequence)
2609 {
2610 return false;
2611 }
2612 else
2613 {
2614 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2615 {
2616 if (!isSingleStatement(*sit))
2617 {
2618 return false;
2619 }
2620 }
2621
2622 return true;
2623 }
2624 }
2625
2626 return true;
2627}
2628
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002629// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2630// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002631bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2632{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002633 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002634 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002635
2636 // Parse loops of the form:
2637 // for(int index = initial; index [comparator] limit; index += increment)
2638 TIntermSymbol *index = NULL;
2639 TOperator comparator = EOpNull;
2640 int initial = 0;
2641 int limit = 0;
2642 int increment = 0;
2643
2644 // Parse index name and intial value
2645 if (node->getInit())
2646 {
2647 TIntermAggregate *init = node->getInit()->getAsAggregate();
2648
2649 if (init)
2650 {
2651 TIntermSequence &sequence = init->getSequence();
2652 TIntermTyped *variable = sequence[0]->getAsTyped();
2653
2654 if (variable && variable->getQualifier() == EvqTemporary)
2655 {
2656 TIntermBinary *assign = variable->getAsBinaryNode();
2657
2658 if (assign->getOp() == EOpInitialize)
2659 {
2660 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2661 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2662
2663 if (symbol && constant)
2664 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002665 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002666 {
2667 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002668 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002669 }
2670 }
2671 }
2672 }
2673 }
2674 }
2675
2676 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002677 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002678 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002679 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002680
2681 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2682 {
2683 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2684
2685 if (constant)
2686 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002687 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002688 {
2689 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002690 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002691 }
2692 }
2693 }
2694 }
2695
2696 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002697 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002698 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002699 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2700 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002701
2702 if (binaryTerminal)
2703 {
2704 TOperator op = binaryTerminal->getOp();
2705 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2706
2707 if (constant)
2708 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002709 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002710 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002711 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002712
2713 switch (op)
2714 {
2715 case EOpAddAssign: increment = value; break;
2716 case EOpSubAssign: increment = -value; break;
2717 default: UNIMPLEMENTED();
2718 }
2719 }
2720 }
2721 }
2722 else if (unaryTerminal)
2723 {
2724 TOperator op = unaryTerminal->getOp();
2725
2726 switch (op)
2727 {
2728 case EOpPostIncrement: increment = 1; break;
2729 case EOpPostDecrement: increment = -1; break;
2730 case EOpPreIncrement: increment = 1; break;
2731 case EOpPreDecrement: increment = -1; break;
2732 default: UNIMPLEMENTED();
2733 }
2734 }
2735 }
2736
2737 if (index != NULL && comparator != EOpNull && increment != 0)
2738 {
2739 if (comparator == EOpLessThanEqual)
2740 {
2741 comparator = EOpLessThan;
2742 limit += 1;
2743 }
2744
2745 if (comparator == EOpLessThan)
2746 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002747 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002748
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002749 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002750 {
2751 return false; // Not an excessive loop
2752 }
2753
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002754 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2755 mExcessiveLoopIndex = index;
2756
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002757 out << "{int ";
2758 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002759 out << ";\n"
2760 "bool Break";
2761 index->traverse(this);
2762 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002763
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002764 bool firstLoopFragment = true;
2765
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002766 while (iterations > 0)
2767 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002768 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002769
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002770 if (!firstLoopFragment)
2771 {
2772 out << "if(!Break";
2773 index->traverse(this);
2774 out << ") {\n";
2775 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002776
2777 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2778 {
2779 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2780 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002781
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002782 // for(int index = initial; index < clampedLimit; index += increment)
2783
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002784 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002785 index->traverse(this);
2786 out << " = ";
2787 out << initial;
2788
2789 out << "; ";
2790 index->traverse(this);
2791 out << " < ";
2792 out << clampedLimit;
2793
2794 out << "; ";
2795 index->traverse(this);
2796 out << " += ";
2797 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002798 out << ")\n";
2799
Jamie Madill075edd82013-07-08 13:30:19 -04002800 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002801 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002802
2803 if (node->getBody())
2804 {
2805 node->getBody()->traverse(this);
2806 }
2807
Jamie Madill075edd82013-07-08 13:30:19 -04002808 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002809 out << ";}\n";
2810
2811 if (!firstLoopFragment)
2812 {
2813 out << "}\n";
2814 }
2815
2816 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002817
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002818 initial += MAX_LOOP_ITERATIONS * increment;
2819 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002820 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002821
2822 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002823
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002824 mExcessiveLoopIndex = restoreIndex;
2825
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002826 return true;
2827 }
2828 else UNIMPLEMENTED();
2829 }
2830
2831 return false; // Not handled as an excessive loop
2832}
2833
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002834void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002835{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002836 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002837
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002838 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002839 {
2840 out << preString;
2841 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002842 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002843 {
2844 out << inString;
2845 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002846 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002847 {
2848 out << postString;
2849 }
2850}
2851
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002852void OutputHLSL::outputLineDirective(int line)
2853{
2854 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2855 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002856 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002857 mBody << "#line " << line;
2858
2859 if (mContext.sourcePath)
2860 {
2861 mBody << " \"" << mContext.sourcePath << "\"";
2862 }
2863
2864 mBody << "\n";
2865 }
2866}
2867
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002868TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2869{
2870 TQualifier qualifier = symbol->getQualifier();
2871 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002872 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002873
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002874 if (name.empty()) // HLSL demands named arguments, also for prototypes
2875 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002876 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002877 }
2878 else
2879 {
2880 name = decorate(name);
2881 }
2882
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002883 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2884 {
2885 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
2886 qualifierString(qualifier) + " SamplerState sampler_" + name + arrayString(type);
2887 }
2888
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002889 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002890}
2891
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00002892TString OutputHLSL::interpolationString(TQualifier qualifier)
2893{
2894 switch(qualifier)
2895 {
2896 case EvqVaryingIn: return "";
2897 case EvqInvariantVaryingIn: return "";
2898 case EvqSmoothIn: return "linear";
2899 case EvqFlatIn: return "nointerpolation";
2900 case EvqCentroidIn: return "centroid";
2901 case EvqVaryingOut: return "";
2902 case EvqInvariantVaryingOut: return "";
2903 case EvqSmoothOut: return "linear";
2904 case EvqFlatOut: return "nointerpolation";
2905 case EvqCentroidOut: return "centroid";
2906 default: UNREACHABLE();
2907 }
2908
2909 return "";
2910}
2911
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002912TString OutputHLSL::qualifierString(TQualifier qualifier)
2913{
2914 switch(qualifier)
2915 {
2916 case EvqIn: return "in";
2917 case EvqOut: return "out";
2918 case EvqInOut: return "inout";
2919 case EvqConstReadOnly: return "const";
2920 default: UNREACHABLE();
2921 }
2922
2923 return "";
2924}
2925
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002926TString OutputHLSL::typeString(const TType &type)
2927{
Jamie Madill98493dd2013-07-08 14:39:03 -04002928 const TStructure* structure = type.getStruct();
2929 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002930 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002931 const TString& typeName = structure->name();
2932 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002933 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002934 return structLookup(typeName);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002935 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00002936 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002937 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002938 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002939 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002940 }
2941 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002942 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00002943 int cols = type.getCols();
2944 int rows = type.getRows();
2945 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002946 }
2947 else
2948 {
2949 switch (type.getBasicType())
2950 {
2951 case EbtFloat:
2952 switch (type.getNominalSize())
2953 {
2954 case 1: return "float";
2955 case 2: return "float2";
2956 case 3: return "float3";
2957 case 4: return "float4";
2958 }
2959 case EbtInt:
2960 switch (type.getNominalSize())
2961 {
2962 case 1: return "int";
2963 case 2: return "int2";
2964 case 3: return "int3";
2965 case 4: return "int4";
2966 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002967 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04002968 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002969 {
2970 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002971 case 2: return "uint2";
2972 case 3: return "uint3";
2973 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002974 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002975 case EbtBool:
2976 switch (type.getNominalSize())
2977 {
2978 case 1: return "bool";
2979 case 2: return "bool2";
2980 case 3: return "bool3";
2981 case 4: return "bool4";
2982 }
2983 case EbtVoid:
2984 return "void";
2985 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04002986 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04002987 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04002988 case EbtSampler2DArray:
2989 case EbtISampler2DArray:
2990 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002991 return "sampler2D";
2992 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04002993 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04002994 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002995 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00002996 case EbtSamplerExternalOES:
2997 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002998 default:
2999 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003000 }
3001 }
3002
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003003 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003004 return "<unknown type>";
3005}
3006
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003007TString OutputHLSL::textureString(const TType &type)
3008{
3009 switch (type.getBasicType())
3010 {
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003011 case EbtSampler2D: return "Texture2D";
3012 case EbtSamplerCube: return "TextureCube";
3013 case EbtSamplerExternalOES: return "Texture2D";
3014 case EbtSampler2DArray: return "Texture2DArray";
Jamie Madill94c3f422013-07-03 15:16:13 -04003015 case EbtSampler3D: return "Texture3D";
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003016 case EbtISampler2D: return "Texture2D<int4>";
3017 case EbtISamplerCube: return "TextureCube<int4>";
3018 case EbtISampler2DArray: return "Texture2DArray<int4>";
3019 case EbtUSampler2D: return "Texture2D<uint4>";
3020 case EbtUSamplerCube: return "TextureCube<uint4>";
3021 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003022 default:
3023 break;
3024 }
3025
3026 UNREACHABLE();
3027 return "<unknown texture type>";
3028}
3029
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003030TString OutputHLSL::arrayString(const TType &type)
3031{
3032 if (!type.isArray())
3033 {
3034 return "";
3035 }
3036
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003037 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003038}
3039
3040TString OutputHLSL::initializer(const TType &type)
3041{
3042 TString string;
3043
Jamie Madill94bf7f22013-07-08 13:31:15 -04003044 size_t size = type.getObjectSize();
3045 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003046 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003047 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003048
Jamie Madill94bf7f22013-07-08 13:31:15 -04003049 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003050 {
3051 string += ", ";
3052 }
3053 }
3054
daniel@transgaming.comead23042010-04-29 03:35:36 +00003055 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003056}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003057
Jamie Madill98493dd2013-07-08 14:39:03 -04003058TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003059{
Jamie Madill98493dd2013-07-08 14:39:03 -04003060 const TFieldList &fields = structure.fields();
3061 const bool isNameless = (structure.name() == "");
3062 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003063 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3064
Jamie Madill98493dd2013-07-08 14:39:03 -04003065 TString string;
3066 string += declareString + "\n"
3067 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003068
Jamie Madillc835df62013-06-21 09:15:32 -04003069 int elementIndex = 0;
3070
Jamie Madill9cf6c072013-06-20 11:55:53 -04003071 for (unsigned int i = 0; i < fields.size(); i++)
3072 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003073 const TField &field = *fields[i];
3074 const TType &fieldType = *field.type();
3075 const TStructure *fieldStruct = fieldType.getStruct();
3076 const TString &fieldTypeString = fieldStruct ? structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) : typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003077
Jamie Madillc835df62013-06-21 09:15:32 -04003078 if (useStd140Packing)
3079 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003080 string += std140PrePaddingString(*field.type(), &elementIndex);
Jamie Madillc835df62013-06-21 09:15:32 -04003081 }
3082
Jamie Madill98493dd2013-07-08 14:39:03 -04003083 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003084
3085 if (useStd140Packing)
3086 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003087 string += std140PostPaddingString(*field.type(), useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003088 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003089 }
3090
3091 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003092 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003093
Jamie Madille4075c92013-06-21 09:15:32 -04003094 // Add remaining element index to the global map, for use with nested structs in standard layouts
3095 if (useStd140Packing)
3096 {
3097 mStd140StructElementIndexes[structName] = elementIndex;
3098 }
3099
Jamie Madill98493dd2013-07-08 14:39:03 -04003100 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003101}
3102
Jamie Madill98493dd2013-07-08 14:39:03 -04003103TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003104{
Jamie Madill98493dd2013-07-08 14:39:03 -04003105 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003106 {
3107 return "";
3108 }
3109
3110 TString prefix = "";
3111
3112 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3113 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003114
3115 if (useStd140Packing)
3116 {
3117 prefix += "std";
3118 }
3119
Jamie Madill9cf6c072013-06-20 11:55:53 -04003120 if (useHLSLRowMajorPacking)
3121 {
Jamie Madillc835df62013-06-21 09:15:32 -04003122 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003123 prefix += "rm";
3124 }
3125
Jamie Madill98493dd2013-07-08 14:39:03 -04003126 return prefix + structLookup(structure.name());
Jamie Madill9cf6c072013-06-20 11:55:53 -04003127}
3128
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003129void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003130{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003131 if (name == "")
3132 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003133 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003134 }
3135
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003136 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
3137 {
3138 return; // Already added
3139 }
3140
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003141 TType ctorType = type;
3142 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003143 ctorType.setPrecision(EbpHigh);
3144 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003145
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003146 TString ctorName = type.getStruct() ? decorate(name) : name;
3147
3148 typedef std::vector<TType> ParameterArray;
3149 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003150
Jamie Madill98493dd2013-07-08 14:39:03 -04003151 const TStructure* structure = type.getStruct();
3152 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003153 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003154 mStructNames.insert(decorate(name));
3155
Jamie Madill98493dd2013-07-08 14:39:03 -04003156 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003157
Jamie Madill98493dd2013-07-08 14:39:03 -04003158 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003159 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003160 // Add row-major packed struct for interface blocks
3161 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003162 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003163 "#pragma pack_matrix(column_major)\n";
3164
Jamie Madillc835df62013-06-21 09:15:32 -04003165 const TString &std140Prefix = "std";
Jamie Madill98493dd2013-07-08 14:39:03 -04003166 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003167
3168 const TString &std140RowMajorPrefix = "std_rm";
3169 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003170 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003171 "#pragma pack_matrix(column_major)\n";
3172
Jamie Madill98493dd2013-07-08 14:39:03 -04003173 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003174 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003175 mStructDeclarations.push_back(std140String);
3176 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003177 }
3178
Jamie Madill98493dd2013-07-08 14:39:03 -04003179 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003180 for (unsigned int i = 0; i < fields.size(); i++)
3181 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003182 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003183 }
3184 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003185 else if (parameters)
3186 {
3187 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3188 {
3189 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3190 }
3191 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003192 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003193
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003194 TString constructor;
3195
3196 if (ctorType.getStruct())
3197 {
3198 constructor += ctorName + " " + ctorName + "_ctor(";
3199 }
3200 else // Built-in type
3201 {
3202 constructor += typeString(ctorType) + " " + ctorName + "(";
3203 }
3204
3205 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3206 {
3207 const TType &type = ctorParameters[parameter];
3208
3209 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3210
3211 if (parameter < ctorParameters.size() - 1)
3212 {
3213 constructor += ", ";
3214 }
3215 }
3216
3217 constructor += ")\n"
3218 "{\n";
3219
3220 if (ctorType.getStruct())
3221 {
3222 constructor += " " + ctorName + " structure = {";
3223 }
3224 else
3225 {
3226 constructor += " return " + typeString(ctorType) + "(";
3227 }
3228
3229 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3230 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003231 int rows = ctorType.getRows();
3232 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003233 const TType &parameter = ctorParameters[0];
3234
3235 if (parameter.isScalar())
3236 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003237 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003238 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003239 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003240 {
3241 constructor += TString((row == col) ? "x0" : "0.0");
3242
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003243 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003244 {
3245 constructor += ", ";
3246 }
3247 }
3248 }
3249 }
3250 else if (parameter.isMatrix())
3251 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003252 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003253 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003254 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003255 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003256 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003257 {
3258 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3259 }
3260 else
3261 {
3262 constructor += TString((row == col) ? "1.0" : "0.0");
3263 }
3264
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003265 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003266 {
3267 constructor += ", ";
3268 }
3269 }
3270 }
3271 }
3272 else UNREACHABLE();
3273 }
3274 else
3275 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003276 size_t remainingComponents = ctorType.getObjectSize();
3277 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003278
3279 while (remainingComponents > 0)
3280 {
3281 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003282 const size_t parameterSize = parameter.getObjectSize();
3283 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003284
3285 constructor += "x" + str(parameterIndex);
3286
3287 if (parameter.isScalar())
3288 {
3289 remainingComponents -= parameter.getObjectSize();
3290 }
3291 else if (parameter.isVector())
3292 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003293 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003294 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003295 ASSERT(parameterSize <= remainingComponents);
3296 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003297 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003298 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003299 {
3300 switch (remainingComponents)
3301 {
3302 case 1: constructor += ".x"; break;
3303 case 2: constructor += ".xy"; break;
3304 case 3: constructor += ".xyz"; break;
3305 case 4: constructor += ".xyzw"; break;
3306 default: UNREACHABLE();
3307 }
3308
3309 remainingComponents = 0;
3310 }
3311 else UNREACHABLE();
3312 }
3313 else if (parameter.isMatrix() || parameter.getStruct())
3314 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003315 ASSERT(remainingComponents == parameterSize || moreParameters);
3316 ASSERT(parameterSize <= remainingComponents);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003317
Jamie Madill94bf7f22013-07-08 13:31:15 -04003318 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003319 }
3320 else UNREACHABLE();
3321
3322 if (moreParameters)
3323 {
3324 parameterIndex++;
3325 }
3326
3327 if (remainingComponents)
3328 {
3329 constructor += ", ";
3330 }
3331 }
3332 }
3333
3334 if (ctorType.getStruct())
3335 {
3336 constructor += "};\n"
3337 " return structure;\n"
3338 "}\n";
3339 }
3340 else
3341 {
3342 constructor += ");\n"
3343 "}\n";
3344 }
3345
daniel@transgaming.com63691862010-04-29 03:32:42 +00003346 mConstructors.insert(constructor);
3347}
3348
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003349const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3350{
3351 TInfoSinkBase &out = mBody;
3352
Jamie Madill98493dd2013-07-08 14:39:03 -04003353 const TStructure* structure = type.getStruct();
3354 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003355 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003356 out << structLookup(structure->name()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003357
Jamie Madill98493dd2013-07-08 14:39:03 -04003358 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003359
Jamie Madill98493dd2013-07-08 14:39:03 -04003360 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003361 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003362 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003363
3364 constUnion = writeConstantUnion(*fieldType, constUnion);
3365
Jamie Madill98493dd2013-07-08 14:39:03 -04003366 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003367 {
3368 out << ", ";
3369 }
3370 }
3371
3372 out << ")";
3373 }
3374 else
3375 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003376 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003377 bool writeType = size > 1;
3378
3379 if (writeType)
3380 {
3381 out << typeString(type) << "(";
3382 }
3383
Jamie Madill94bf7f22013-07-08 13:31:15 -04003384 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003385 {
3386 switch (constUnion->getType())
3387 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003388 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003389 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003390 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003391 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003392 default: UNREACHABLE();
3393 }
3394
3395 if (i != size - 1)
3396 {
3397 out << ", ";
3398 }
3399 }
3400
3401 if (writeType)
3402 {
3403 out << ")";
3404 }
3405 }
3406
3407 return constUnion;
3408}
3409
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003410TString OutputHLSL::scopeString(unsigned int depthLimit)
3411{
3412 TString string;
3413
3414 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3415 {
3416 string += "_" + str(i);
3417 }
3418
3419 return string;
3420}
3421
3422TString OutputHLSL::scopedStruct(const TString &typeName)
3423{
3424 if (typeName == "")
3425 {
3426 return typeName;
3427 }
3428
3429 return typeName + scopeString(mScopeDepth);
3430}
3431
3432TString OutputHLSL::structLookup(const TString &typeName)
3433{
3434 for (int depth = mScopeDepth; depth >= 0; depth--)
3435 {
3436 TString scopedName = decorate(typeName + scopeString(depth));
3437
3438 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3439 {
3440 if (*structName == scopedName)
3441 {
3442 return scopedName;
3443 }
3444 }
3445 }
3446
3447 UNREACHABLE(); // Should have found a matching constructor
3448
3449 return typeName;
3450}
3451
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003452TString OutputHLSL::decorate(const TString &string)
3453{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003454 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003455 {
3456 return "_" + string;
3457 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003458
3459 return string;
3460}
3461
apatrick@chromium.org65756022012-01-17 21:45:38 +00003462TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003463{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003464 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003465 {
3466 return "ex_" + string;
3467 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003468
3469 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003470}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003471
Jamie Madill98493dd2013-07-08 14:39:03 -04003472TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003473{
Jamie Madill98493dd2013-07-08 14:39:03 -04003474 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003475 {
3476 return decorate(string);
3477 }
3478
3479 return string;
3480}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003481
3482TString OutputHLSL::registerString(TIntermSymbol *operand)
3483{
3484 ASSERT(operand->getQualifier() == EvqUniform);
3485
3486 if (IsSampler(operand->getBasicType()))
3487 {
3488 return "s" + str(samplerRegister(operand));
3489 }
3490
3491 return "c" + str(uniformRegister(operand));
3492}
3493
3494int OutputHLSL::samplerRegister(TIntermSymbol *sampler)
3495{
3496 const TType &type = sampler->getType();
3497 ASSERT(IsSampler(type.getBasicType()));
3498
3499 int index = mSamplerRegister;
3500 mSamplerRegister += sampler->totalRegisterCount();
3501
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003502 declareUniform(type, sampler->getSymbol(), index);
3503
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003504 return index;
3505}
3506
3507int OutputHLSL::uniformRegister(TIntermSymbol *uniform)
3508{
3509 const TType &type = uniform->getType();
3510 ASSERT(!IsSampler(type.getBasicType()));
3511
3512 int index = mUniformRegister;
3513 mUniformRegister += uniform->totalRegisterCount();
3514
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003515 declareUniform(type, uniform->getSymbol(), index);
3516
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003517 return index;
3518}
3519
Jamie Madill98493dd2013-07-08 14:39:03 -04003520void OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, ActiveUniforms& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003521{
Jamie Madill98493dd2013-07-08 14:39:03 -04003522 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003523
3524 if (!structure)
3525 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003526 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill98493dd2013-07-08 14:39:03 -04003527 output.push_back(Uniform(glVariableType(type), glVariablePrecision(type), name.c_str(), (unsigned int)type.getArraySize(), (unsigned int)registerIndex, isRowMajorMatrix));
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003528 }
3529 else
3530 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003531 Uniform structUniform(GL_NONE, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), (unsigned int)registerIndex, false);
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003532
Jamie Madill98493dd2013-07-08 14:39:03 -04003533 int fieldRegister = registerIndex;
3534 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003535
Jamie Madill98493dd2013-07-08 14:39:03 -04003536 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003537 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003538 TField *field = fields[fieldIndex];
3539 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003540
Jamie Madill010fffa2013-06-20 11:55:53 -04003541 // make sure to copy matrix packing information
Jamie Madill98493dd2013-07-08 14:39:03 -04003542 fieldType->setLayoutQualifier(type.getLayoutQualifier());
Jamie Madill010fffa2013-06-20 11:55:53 -04003543
Jamie Madill98493dd2013-07-08 14:39:03 -04003544 declareUniformToList(*fieldType, field->name(), fieldRegister, structUniform.fields);
3545 fieldRegister += fieldType->totalRegisterCount();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003546 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003547
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003548 output.push_back(structUniform);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003549 }
3550}
3551
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003552void OutputHLSL::declareUniform(const TType &type, const TString &name, int index)
3553{
3554 declareUniformToList(type, name, index, mActiveUniforms);
3555}
3556
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003557GLenum OutputHLSL::glVariableType(const TType &type)
3558{
3559 if (type.getBasicType() == EbtFloat)
3560 {
3561 if (type.isScalar())
3562 {
3563 return GL_FLOAT;
3564 }
3565 else if (type.isVector())
3566 {
3567 switch(type.getNominalSize())
3568 {
3569 case 2: return GL_FLOAT_VEC2;
3570 case 3: return GL_FLOAT_VEC3;
3571 case 4: return GL_FLOAT_VEC4;
3572 default: UNREACHABLE();
3573 }
3574 }
3575 else if (type.isMatrix())
3576 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003577 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003578 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003579 case 2:
3580 switch(type.getRows())
3581 {
3582 case 2: return GL_FLOAT_MAT2;
3583 case 3: return GL_FLOAT_MAT2x3;
3584 case 4: return GL_FLOAT_MAT2x4;
3585 default: UNREACHABLE();
3586 }
3587
3588 case 3:
3589 switch(type.getRows())
3590 {
3591 case 2: return GL_FLOAT_MAT3x2;
3592 case 3: return GL_FLOAT_MAT3;
3593 case 4: return GL_FLOAT_MAT3x4;
3594 default: UNREACHABLE();
3595 }
3596
3597 case 4:
3598 switch(type.getRows())
3599 {
3600 case 2: return GL_FLOAT_MAT4x2;
3601 case 3: return GL_FLOAT_MAT4x3;
3602 case 4: return GL_FLOAT_MAT4;
3603 default: UNREACHABLE();
3604 }
3605
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003606 default: UNREACHABLE();
3607 }
3608 }
3609 else UNREACHABLE();
3610 }
3611 else if (type.getBasicType() == EbtInt)
3612 {
3613 if (type.isScalar())
3614 {
3615 return GL_INT;
3616 }
3617 else if (type.isVector())
3618 {
3619 switch(type.getNominalSize())
3620 {
3621 case 2: return GL_INT_VEC2;
3622 case 3: return GL_INT_VEC3;
3623 case 4: return GL_INT_VEC4;
3624 default: UNREACHABLE();
3625 }
3626 }
3627 else UNREACHABLE();
3628 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003629 else if (type.getBasicType() == EbtUInt)
3630 {
3631 if (type.isScalar())
3632 {
3633 return GL_UNSIGNED_INT;
3634 }
3635 else if (type.isVector())
3636 {
Jamie Madill22d63da2013-06-07 12:45:12 -04003637 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003638 {
3639 case 2: return GL_UNSIGNED_INT_VEC2;
3640 case 3: return GL_UNSIGNED_INT_VEC3;
3641 case 4: return GL_UNSIGNED_INT_VEC4;
3642 default: UNREACHABLE();
3643 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003644 }
3645 else UNREACHABLE();
3646 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003647 else if (type.getBasicType() == EbtBool)
3648 {
3649 if (type.isScalar())
3650 {
3651 return GL_BOOL;
3652 }
3653 else if (type.isVector())
3654 {
3655 switch(type.getNominalSize())
3656 {
3657 case 2: return GL_BOOL_VEC2;
3658 case 3: return GL_BOOL_VEC3;
3659 case 4: return GL_BOOL_VEC4;
3660 default: UNREACHABLE();
3661 }
3662 }
3663 else UNREACHABLE();
3664 }
3665 else if (type.getBasicType() == EbtSampler2D)
3666 {
3667 return GL_SAMPLER_2D;
3668 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04003669 else if (type.getBasicType() == EbtSampler3D)
3670 {
3671 return GL_SAMPLER_3D;
3672 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003673 else if (type.getBasicType() == EbtSamplerCube)
3674 {
3675 return GL_SAMPLER_CUBE;
3676 }
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003677 else if (type.getBasicType() == EbtSampler2DArray)
3678 {
3679 return GL_SAMPLER_2D_ARRAY;
3680 }
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003681 else if (type.getBasicType() == EbtISampler2D)
3682 {
3683 return GL_INT_SAMPLER_2D;
3684 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04003685 else if (type.getBasicType() == EbtISampler3D)
3686 {
3687 return GL_INT_SAMPLER_3D;
3688 }
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003689 else if (type.getBasicType() == EbtISamplerCube)
3690 {
3691 return GL_INT_SAMPLER_CUBE;
3692 }
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003693 else if (type.getBasicType() == EbtISampler2DArray)
3694 {
3695 return GL_INT_SAMPLER_2D_ARRAY;
3696 }
Nicolas Capens075368e2013-06-24 15:58:30 -04003697 else if (type.getBasicType() == EbtUSampler2D)
3698 {
3699 return GL_UNSIGNED_INT_SAMPLER_2D;
3700 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04003701 else if (type.getBasicType() == EbtUSampler3D)
3702 {
3703 return GL_UNSIGNED_INT_SAMPLER_3D;
3704 }
Nicolas Capens075368e2013-06-24 15:58:30 -04003705 else if (type.getBasicType() == EbtUSamplerCube)
3706 {
3707 return GL_UNSIGNED_INT_SAMPLER_CUBE;
3708 }
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003709 else if (type.getBasicType() == EbtUSampler2DArray)
3710 {
3711 return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3712 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003713 else UNREACHABLE();
3714
3715 return GL_NONE;
3716}
3717
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003718GLenum OutputHLSL::glVariablePrecision(const TType &type)
3719{
3720 if (type.getBasicType() == EbtFloat)
3721 {
3722 switch (type.getPrecision())
3723 {
3724 case EbpHigh: return GL_HIGH_FLOAT;
3725 case EbpMedium: return GL_MEDIUM_FLOAT;
3726 case EbpLow: return GL_LOW_FLOAT;
3727 case EbpUndefined:
3728 // Should be defined as the default precision by the parser
3729 default: UNREACHABLE();
3730 }
3731 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003732 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003733 {
3734 switch (type.getPrecision())
3735 {
3736 case EbpHigh: return GL_HIGH_INT;
3737 case EbpMedium: return GL_MEDIUM_INT;
3738 case EbpLow: return GL_LOW_INT;
3739 case EbpUndefined:
3740 // Should be defined as the default precision by the parser
3741 default: UNREACHABLE();
3742 }
3743 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003744
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00003745 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003746 return GL_NONE;
3747}
3748
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003749bool OutputHLSL::isVaryingOut(TQualifier qualifier)
3750{
3751 switch(qualifier)
3752 {
3753 case EvqVaryingOut:
3754 case EvqInvariantVaryingOut:
3755 case EvqSmoothOut:
3756 case EvqFlatOut:
3757 case EvqCentroidOut:
3758 return true;
3759 }
3760
3761 return false;
3762}
3763
3764bool OutputHLSL::isVaryingIn(TQualifier qualifier)
3765{
3766 switch(qualifier)
3767 {
3768 case EvqVaryingIn:
3769 case EvqInvariantVaryingIn:
3770 case EvqSmoothIn:
3771 case EvqFlatIn:
3772 case EvqCentroidIn:
3773 return true;
3774 }
3775
3776 return false;
3777}
3778
3779bool OutputHLSL::isVarying(TQualifier qualifier)
3780{
3781 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
3782}
3783
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003784}