blob: 4f5a32e1f429332d255760661cd8b45dd926770e [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
Nicolas Capens6d232bb2013-07-08 15:56:38 -040037 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040038 {
39 name += "2D";
40 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040041 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040042 {
43 name += "3D";
44 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040045 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040046 {
47 name += "Cube";
48 }
49 else UNREACHABLE();
50
51 if (proj)
52 {
53 name += "Proj";
54 }
55
56 switch(mipmap)
57 {
58 case IMPLICIT: break;
59 case BIAS: break;
60 case LOD: name += "Lod"; break;
61 case LOD0: name += "Lod0"; break;
62 default: UNREACHABLE();
63 }
64
65 return name + "(";
66}
67
68bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
69{
70 if (sampler < rhs.sampler) return true;
71 if (coords < rhs.coords) return true;
72 if (!proj && rhs.proj) return true;
73 if (mipmap < rhs.mipmap) return true;
74
75 return false;
76}
77
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +000078OutputHLSL::OutputHLSL(TParseContext &context, const ShBuiltInResources& resources, ShShaderOutput outputType)
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +000079 : TIntermTraverser(true, true, true), mContext(context), mOutputType(outputType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000080{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000081 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +000082 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000083
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +000084 mUsesFragColor = false;
85 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +000086 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +000087 mUsesFragCoord = false;
88 mUsesPointCoord = false;
89 mUsesFrontFacing = false;
90 mUsesPointSize = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -040091 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +000092 mUsesXor = false;
93 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +000094 mUsesMod2v = false;
95 mUsesMod2f = false;
96 mUsesMod3v = false;
97 mUsesMod3f = false;
98 mUsesMod4v = false;
99 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000100 mUsesFaceforward1 = false;
101 mUsesFaceforward2 = false;
102 mUsesFaceforward3 = false;
103 mUsesFaceforward4 = false;
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000104
105 for (unsigned int col = 0; col <= 4; col++)
106 {
107 for (unsigned int row = 0; row <= 4; row++)
108 {
109 mUsesEqualMat[col][row] = false;
110 }
111 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000112 mUsesEqualVec2 = false;
113 mUsesEqualVec3 = false;
114 mUsesEqualVec4 = false;
115 mUsesEqualIVec2 = false;
116 mUsesEqualIVec3 = false;
117 mUsesEqualIVec4 = false;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +0000118 mUsesEqualUVec2 = false;
119 mUsesEqualUVec3 = false;
120 mUsesEqualUVec4 = false;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000121 mUsesEqualBVec2 = false;
122 mUsesEqualBVec3 = false;
123 mUsesEqualBVec4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000124 mUsesAtan2_1 = false;
125 mUsesAtan2_2 = false;
126 mUsesAtan2_3 = false;
127 mUsesAtan2_4 = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000128
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000129 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
130
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000131 mScopeDepth = 0;
132
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000133 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000134
135 mContainsLoopDiscontinuity = false;
136 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000137 mInsideDiscontinuousLoop = false;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000138
139 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000140
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000141 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000142 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000143 if (mContext.shaderType == SH_FRAGMENT_SHADER)
144 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000145 mUniformRegister = 3; // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000146 }
147 else
148 {
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000149 mUniformRegister = 2; // Reserve registers for dx_DepthRange and dx_ViewAdjust
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000150 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000151 }
152 else
153 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000154 mUniformRegister = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000155 }
156
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000157 mSamplerRegister = 0;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000158 mInterfaceBlockRegister = 2; // Reserve registers for the default uniform block and driver constants
Jamie Madill574d9dd2013-06-20 11:55:56 -0400159 mPaddingCounter = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000160}
161
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000162OutputHLSL::~OutputHLSL()
163{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000164 delete mUnfoldShortCircuit;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000165}
166
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000167void OutputHLSL::output()
168{
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +0000169 mContainsLoopDiscontinuity = mContext.shaderType == SH_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400170 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
171 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000172
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000173 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 +0000174 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000175
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000176 mContext.infoSink().obj << mHeader.c_str();
177 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000178}
179
Jamie Madill570e04d2013-06-21 09:15:33 -0400180void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
181{
182 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
183 {
184 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
185
186 // This will mark the necessary block elements as referenced
187 flaggedNode->traverse(this);
188 TString structName(mBody.c_str());
189 mBody.erase();
190
191 mFlaggedStructOriginalNames[flaggedNode] = structName;
192
193 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
194 {
195 structName.erase(pos, 1);
196 }
197
198 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
199 }
200}
201
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000202TInfoSinkBase &OutputHLSL::getBodyStream()
203{
204 return mBody;
205}
206
daniel@transgaming.com043da132012-12-20 21:12:22 +0000207const ActiveUniforms &OutputHLSL::getUniforms()
208{
209 return mActiveUniforms;
210}
211
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000212const ActiveInterfaceBlocks &OutputHLSL::getInterfaceBlocks() const
213{
214 return mActiveInterfaceBlocks;
215}
216
Jamie Madill46131a32013-06-20 11:55:50 -0400217const ActiveShaderVariables &OutputHLSL::getOutputVariables() const
218{
219 return mActiveOutputVariables;
220}
221
Jamie Madilldefb6742013-06-20 11:55:51 -0400222const ActiveShaderVariables &OutputHLSL::getAttributes() const
223{
224 return mActiveAttributes;
225}
226
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000227int OutputHLSL::vectorSize(const TType &type) const
228{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000229 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000230 int arraySize = type.isArray() ? type.getArraySize() : 1;
231
232 return elementSize * arraySize;
233}
234
Jamie Madill98493dd2013-07-08 14:39:03 -0400235TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, const TField &field)
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000236{
Jamie Madill98493dd2013-07-08 14:39:03 -0400237 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000238 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400239 return interfaceBlock.name() + "." + field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000240 }
241 else
242 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400243 return field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000244 }
245}
246
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000247TString OutputHLSL::decoratePrivate(const TString &privateText)
248{
249 return "dx_" + privateText;
250}
251
Jamie Madill98493dd2013-07-08 14:39:03 -0400252TString OutputHLSL::interfaceBlockStructNameString(const TInterfaceBlock &interfaceBlock)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000253{
Jamie Madill98493dd2013-07-08 14:39:03 -0400254 return decoratePrivate(interfaceBlock.name()) + "_type";
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000255}
256
Jamie Madill98493dd2013-07-08 14:39:03 -0400257TString OutputHLSL::interfaceBlockInstanceString(const TInterfaceBlock& interfaceBlock, unsigned int arrayIndex)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000258{
Jamie Madill98493dd2013-07-08 14:39:03 -0400259 if (!interfaceBlock.hasInstanceName())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000260 {
261 return "";
262 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400263 else if (interfaceBlock.isArray())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000264 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400265 return decoratePrivate(interfaceBlock.instanceName()) + "_" + str(arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000266 }
267 else
268 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400269 return decorate(interfaceBlock.instanceName());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000270 }
271}
272
Jamie Madill98493dd2013-07-08 14:39:03 -0400273TString OutputHLSL::interfaceBlockFieldTypeString(const TField &field, TLayoutBlockStorage blockStorage)
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000274{
Jamie Madill98493dd2013-07-08 14:39:03 -0400275 const TType &fieldType = *field.type();
276 const TLayoutMatrixPacking matrixPacking = fieldType.getLayoutQualifier().matrixPacking;
Jamie Madill529077d2013-06-20 11:55:54 -0400277 ASSERT(matrixPacking != EmpUnspecified);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000278
Jamie Madill98493dd2013-07-08 14:39:03 -0400279 if (fieldType.isMatrix())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000280 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400281 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill529077d2013-06-20 11:55:54 -0400282 const TString &matrixPackString = (matrixPacking == EmpRowMajor ? "column_major" : "row_major");
Jamie Madill98493dd2013-07-08 14:39:03 -0400283 return matrixPackString + " " + typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000284 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400285 else if (fieldType.getStruct())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000286 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400287 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill98493dd2013-07-08 14:39:03 -0400288 return structureTypeName(*fieldType.getStruct(), matrixPacking == EmpColumnMajor, blockStorage == EbsStd140);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000289 }
290 else
291 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400292 return typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000293 }
294}
295
Jamie Madill98493dd2013-07-08 14:39:03 -0400296TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, TLayoutBlockStorage blockStorage)
297{
298 TString hlsl;
299
300 int elementIndex = 0;
301
302 for (unsigned int typeIndex = 0; typeIndex < interfaceBlock.fields().size(); typeIndex++)
303 {
304 const TField &field = *interfaceBlock.fields()[typeIndex];
305 const TType &fieldType = *field.type();
306
307 if (blockStorage == EbsStd140)
308 {
309 // 2 and 3 component vector types in some cases need pre-padding
310 hlsl += std140PrePaddingString(fieldType, &elementIndex);
311 }
312
313 hlsl += " " + interfaceBlockFieldTypeString(field, blockStorage) +
314 " " + decorate(field.name()) + arrayString(fieldType) + ";\n";
315
316 // must pad out after matrices and arrays, where HLSL usually allows itself room to pack stuff
317 if (blockStorage == EbsStd140)
318 {
319 const bool useHLSLRowMajorPacking = (fieldType.getLayoutQualifier().matrixPacking == EmpColumnMajor);
320 hlsl += std140PostPaddingString(fieldType, useHLSLRowMajorPacking);
321 }
322 }
323
324 return hlsl;
325}
326
327TString OutputHLSL::interfaceBlockStructString(const TInterfaceBlock &interfaceBlock)
328{
329 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
330
331 return "struct " + interfaceBlockStructNameString(interfaceBlock) + "\n"
332 "{\n" +
333 interfaceBlockFieldString(interfaceBlock, blockStorage) +
334 "};\n\n";
335}
336
337TString OutputHLSL::interfaceBlockString(const TInterfaceBlock &interfaceBlock, unsigned int registerIndex, unsigned int arrayIndex)
338{
339 const TString &arrayIndexString = (arrayIndex != GL_INVALID_INDEX ? decorate(str(arrayIndex)) : "");
340 const TString &blockName = interfaceBlock.name() + arrayIndexString;
341 TString hlsl;
342
343 hlsl += "cbuffer " + blockName + " : register(b" + str(registerIndex) + ")\n"
344 "{\n";
345
346 if (interfaceBlock.hasInstanceName())
347 {
348 hlsl += " " + interfaceBlockStructNameString(interfaceBlock) + " " + interfaceBlockInstanceString(interfaceBlock, arrayIndex) + ";\n";
349 }
350 else
351 {
352 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
353 hlsl += interfaceBlockFieldString(interfaceBlock, blockStorage);
354 }
355
356 hlsl += "};\n\n";
357
358 return hlsl;
359}
360
Jamie Madill574d9dd2013-06-20 11:55:56 -0400361TString OutputHLSL::std140PrePaddingString(const TType &type, int *elementIndex)
362{
363 if (type.getBasicType() == EbtStruct || type.isMatrix() || type.isArray())
364 {
365 // no padding needed, HLSL will align the field to a new register
366 *elementIndex = 0;
367 return "";
368 }
369
370 const GLenum glType = glVariableType(type);
371 const int numComponents = gl::UniformComponentCount(glType);
372
373 if (numComponents >= 4)
374 {
375 // no padding needed, HLSL will align the field to a new register
376 *elementIndex = 0;
377 return "";
378 }
379
380 if (*elementIndex + numComponents > 4)
381 {
382 // no padding needed, HLSL will align the field to a new register
383 *elementIndex = numComponents;
384 return "";
385 }
386
387 TString padding;
388
389 const int alignment = numComponents == 3 ? 4 : numComponents;
390 const int paddingOffset = (*elementIndex % alignment);
391
392 if (paddingOffset != 0)
393 {
394 // padding is neccessary
395 for (int paddingIndex = paddingOffset; paddingIndex < alignment; paddingIndex++)
396 {
397 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
398 }
399
400 *elementIndex += (alignment - paddingOffset);
401 }
402
403 *elementIndex += numComponents;
404 *elementIndex %= 4;
405
406 return padding;
407}
408
Jamie Madille4075c92013-06-21 09:15:32 -0400409TString OutputHLSL::std140PostPaddingString(const TType &type, bool useHLSLRowMajorPacking)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400410{
Jamie Madillc835df62013-06-21 09:15:32 -0400411 if (!type.isMatrix() && !type.isArray() && type.getBasicType() != EbtStruct)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400412 {
413 return "";
414 }
415
Jamie Madill574d9dd2013-06-20 11:55:56 -0400416 int numComponents = 0;
417
418 if (type.isMatrix())
419 {
Jamie Madille4075c92013-06-21 09:15:32 -0400420 // This method can also be called from structureString, which does not use layout qualifiers.
421 // Thus, use the method parameter for determining the matrix packing.
422 //
423 // Note HLSL row major packing corresponds to GL API column-major, and vice-versa, since we
424 // wish to always transpose GL matrices to play well with HLSL's matrix array indexing.
425 //
426 const bool isRowMajorMatrix = !useHLSLRowMajorPacking;
Jamie Madillc835df62013-06-21 09:15:32 -0400427 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400428 numComponents = gl::MatrixComponentCount(glType, isRowMajorMatrix);
429 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400430 else if (type.getStruct())
Jamie Madillc835df62013-06-21 09:15:32 -0400431 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400432 const TString &structName = structureTypeName(*type.getStruct(), useHLSLRowMajorPacking, true);
Jamie Madille4075c92013-06-21 09:15:32 -0400433 numComponents = mStd140StructElementIndexes[structName];
434
435 if (numComponents == 0)
436 {
437 return "";
438 }
Jamie Madillc835df62013-06-21 09:15:32 -0400439 }
Jamie Madill574d9dd2013-06-20 11:55:56 -0400440 else
441 {
Jamie Madillc835df62013-06-21 09:15:32 -0400442 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400443 numComponents = gl::UniformComponentCount(glType);
444 }
445
446 TString padding;
447 for (int paddingOffset = numComponents; paddingOffset < 4; paddingOffset++)
448 {
449 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
450 }
451 return padding;
452}
453
Jamie Madill440dc742013-06-20 11:55:55 -0400454// Use the same layout for packed and shared
455void setBlockLayout(InterfaceBlock *interfaceBlock, BlockLayoutType newLayout)
456{
457 interfaceBlock->layout = newLayout;
458 interfaceBlock->blockInfo.clear();
459
460 switch (newLayout)
461 {
462 case BLOCKLAYOUT_SHARED:
463 case BLOCKLAYOUT_PACKED:
464 {
465 HLSLBlockEncoder hlslEncoder(&interfaceBlock->blockInfo);
466 hlslEncoder.encodeFields(interfaceBlock->activeUniforms);
467 interfaceBlock->dataSize = hlslEncoder.getBlockSize();
468 }
469 break;
470
471 case BLOCKLAYOUT_STANDARD:
472 {
473 Std140BlockEncoder stdEncoder(&interfaceBlock->blockInfo);
474 stdEncoder.encodeFields(interfaceBlock->activeUniforms);
475 interfaceBlock->dataSize = stdEncoder.getBlockSize();
476 }
477 break;
478
479 default:
480 UNREACHABLE();
481 break;
482 }
483}
484
Jamie Madill574d9dd2013-06-20 11:55:56 -0400485BlockLayoutType convertBlockLayoutType(TLayoutBlockStorage blockStorage)
486{
487 switch (blockStorage)
488 {
489 case EbsPacked: return BLOCKLAYOUT_PACKED;
490 case EbsShared: return BLOCKLAYOUT_SHARED;
491 case EbsStd140: return BLOCKLAYOUT_STANDARD;
492 default: UNREACHABLE(); return BLOCKLAYOUT_SHARED;
493 }
494}
495
Jamie Madill98493dd2013-07-08 14:39:03 -0400496TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400497{
498 TString init;
499
500 TString preIndentString;
501 TString fullIndentString;
502
503 for (int spaces = 0; spaces < (indent * 4); spaces++)
504 {
505 preIndentString += ' ';
506 }
507
508 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
509 {
510 fullIndentString += ' ';
511 }
512
513 init += preIndentString + "{\n";
514
Jamie Madill98493dd2013-07-08 14:39:03 -0400515 const TFieldList &fields = structure.fields();
516 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400517 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400518 const TField &field = *fields[fieldIndex];
519 const TString &fieldName = rhsStructName + "." + decorate(field.name());
520 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400521
Jamie Madill98493dd2013-07-08 14:39:03 -0400522 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400523 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400524 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400525 }
526 else
527 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400528 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400529 }
530 }
531
532 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
533
534 return init;
535}
536
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000537void OutputHLSL::header()
538{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000539 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000540
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000541 TString uniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000542 TString interfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000543 TString varyings;
544 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400545 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000546
547 for (ReferencedSymbols::const_iterator uniform = mReferencedUniforms.begin(); uniform != mReferencedUniforms.end(); uniform++)
548 {
549 const TType &type = uniform->second->getType();
550 const TString &name = uniform->second->getSymbol();
551
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000552 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture
553 {
554 int index = samplerRegister(mReferencedUniforms[name]);
555
556 uniforms += "uniform SamplerState sampler_" + decorateUniform(name, type) + arrayString(type) +
557 " : register(s" + str(index) + ");\n";
558
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000559 uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) +
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000560 " : register(t" + str(index) + ");\n";
561 }
562 else
563 {
564 uniforms += "uniform " + typeString(type) + " " + decorateUniform(name, type) + arrayString(type) +
565 " : register(" + registerString(mReferencedUniforms[name]) + ");\n";
566 }
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000567 }
568
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000569 for (ReferencedSymbols::const_iterator interfaceBlockIt = mReferencedInterfaceBlocks.begin(); interfaceBlockIt != mReferencedInterfaceBlocks.end(); interfaceBlockIt++)
570 {
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000571 const TType &nodeType = interfaceBlockIt->second->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -0400572 const TInterfaceBlock &interfaceBlock = *nodeType.getInterfaceBlock();
573 const TFieldList &fieldList = interfaceBlock.fields();
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000574
Jamie Madill98493dd2013-07-08 14:39:03 -0400575 unsigned int arraySize = static_cast<unsigned int>(interfaceBlock.arraySize());
576 sh::InterfaceBlock activeBlock(interfaceBlock.name().c_str(), arraySize, mInterfaceBlockRegister);
577 for (unsigned int typeIndex = 0; typeIndex < fieldList.size(); typeIndex++)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000578 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400579 const TField &field = *fieldList[typeIndex];
580 const TString &fullUniformName = interfaceBlockFieldString(interfaceBlock, field);
581 declareUniformToList(*field.type(), fullUniformName, typeIndex, activeBlock.activeUniforms);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000582 }
583
Jamie Madill98493dd2013-07-08 14:39:03 -0400584 mInterfaceBlockRegister += std::max(1u, arraySize);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000585
Jamie Madill98493dd2013-07-08 14:39:03 -0400586 BlockLayoutType blockLayoutType = convertBlockLayoutType(interfaceBlock.blockStorage());
587 setBlockLayout(&activeBlock, blockLayoutType);
588 mActiveInterfaceBlocks.push_back(activeBlock);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000589
Jamie Madill98493dd2013-07-08 14:39:03 -0400590 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000591 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400592 interfaceBlocks += interfaceBlockStructString(interfaceBlock);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000593 }
594
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000595 if (arraySize > 0)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000596 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000597 for (unsigned int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
598 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400599 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex + arrayIndex, arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000600 }
601 }
602 else
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000603 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400604 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000605 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000606 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000607
Jamie Madill570e04d2013-06-21 09:15:33 -0400608 for (auto flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
609 {
610 TIntermTyped *structNode = flaggedStructIt->first;
611 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400612 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400613 const TString &originalName = mFlaggedStructOriginalNames[structNode];
614
Jamie Madill98493dd2013-07-08 14:39:03 -0400615 flaggedStructs += "static " + decorate(structure.name()) + " " + mappedName + " =\n";
616 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400617 flaggedStructs += "\n";
618 }
619
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000620 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
621 {
622 const TType &type = varying->second->getType();
623 const TString &name = varying->second->getSymbol();
624
625 // Program linking depends on this exact format
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +0000626 varyings += "static " + interpolationString(type.getQualifier()) + " " + typeString(type) + " " +
627 decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000628 }
629
630 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
631 {
632 const TType &type = attribute->second->getType();
633 const TString &name = attribute->second->getSymbol();
634
635 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400636
637 ShaderVariable shaderVar(glVariableType(type), glVariablePrecision(type), name.c_str(),
638 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
639 mActiveAttributes.push_back(shaderVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000640 }
641
Jamie Madill529077d2013-06-20 11:55:54 -0400642 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
643 {
644 out << *structDeclaration;
645 }
646
647 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
648 {
649 out << *constructor;
650 }
651
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400652 if (mContext.shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000653 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000654 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000655 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000656
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000657 out << "// Varyings\n";
658 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400659 out << "\n";
660
661 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000662 {
Jamie Madill46131a32013-06-20 11:55:50 -0400663 for (auto outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000664 {
Jamie Madill46131a32013-06-20 11:55:50 -0400665 const TString &variableName = outputVariableIt->first;
666 const TType &variableType = outputVariableIt->second->getType();
667 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
668
669 out << "static " + typeString(variableType) + " out_" + variableName + arrayString(variableType) +
670 " = " + initializer(variableType) + ";\n";
671
672 ShaderVariable outputVar(glVariableType(variableType), glVariablePrecision(variableType), variableName.c_str(),
673 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
674 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000675 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000676 }
Jamie Madill46131a32013-06-20 11:55:50 -0400677 else
678 {
679 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
680
681 out << "static float4 gl_Color[" << numColorValues << "] =\n"
682 "{\n";
683 for (unsigned int i = 0; i < numColorValues; i++)
684 {
685 out << " float4(0, 0, 0, 0)";
686 if (i + 1 != numColorValues)
687 {
688 out << ",";
689 }
690 out << "\n";
691 }
692
693 out << "};\n";
694 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000695
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400696 if (mUsesFragDepth)
697 {
698 out << "static float gl_Depth = 0.0;\n";
699 }
700
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000701 if (mUsesFragCoord)
702 {
703 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
704 }
705
706 if (mUsesPointCoord)
707 {
708 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
709 }
710
711 if (mUsesFrontFacing)
712 {
713 out << "static bool gl_FrontFacing = false;\n";
714 }
715
716 out << "\n";
717
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000718 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000719 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000720 out << "struct gl_DepthRangeParameters\n"
721 "{\n"
722 " float near;\n"
723 " float far;\n"
724 " float diff;\n"
725 "};\n"
726 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000727 }
728
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000729 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000730 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000731 out << "cbuffer DriverConstants : register(b1)\n"
732 "{\n";
733
734 if (mUsesDepthRange)
735 {
736 out << " float3 dx_DepthRange : packoffset(c0);\n";
737 }
738
739 if (mUsesFragCoord)
740 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000741 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000742 }
743
744 if (mUsesFragCoord || mUsesFrontFacing)
745 {
746 out << " float3 dx_DepthFront : packoffset(c2);\n";
747 }
748
749 out << "};\n";
750 }
751 else
752 {
753 if (mUsesDepthRange)
754 {
755 out << "uniform float3 dx_DepthRange : register(c0);";
756 }
757
758 if (mUsesFragCoord)
759 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000760 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000761 }
762
763 if (mUsesFragCoord || mUsesFrontFacing)
764 {
765 out << "uniform float3 dx_DepthFront : register(c2);\n";
766 }
767 }
768
769 out << "\n";
770
771 if (mUsesDepthRange)
772 {
773 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
774 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000775 }
776
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000777 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000778 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000779
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000780 if (!interfaceBlocks.empty())
781 {
782 out << interfaceBlocks;
783 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400784
785 if (!flaggedStructs.empty())
786 {
787 out << "// Std140 Structures accessed by value\n";
788 out << "\n";
789 out << flaggedStructs;
790 out << "\n";
791 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000792 }
793
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000794 if (usingMRTExtension && mNumRenderTargets > 1)
795 {
796 out << "#define GL_USES_MRT\n";
797 }
798
799 if (mUsesFragColor)
800 {
801 out << "#define GL_USES_FRAG_COLOR\n";
802 }
803
804 if (mUsesFragData)
805 {
806 out << "#define GL_USES_FRAG_DATA\n";
807 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000808 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000809 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000811 out << "// Attributes\n";
812 out << attributes;
813 out << "\n"
814 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
815
816 if (mUsesPointSize)
817 {
818 out << "static float gl_PointSize = float(1);\n";
819 }
820
821 out << "\n"
822 "// Varyings\n";
823 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000824 out << "\n";
825
826 if (mUsesDepthRange)
827 {
828 out << "struct gl_DepthRangeParameters\n"
829 "{\n"
830 " float near;\n"
831 " float far;\n"
832 " float diff;\n"
833 "};\n"
834 "\n";
835 }
836
837 if (mOutputType == SH_HLSL11_OUTPUT)
838 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000839 if (mUsesDepthRange)
840 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000841 out << "cbuffer DriverConstants : register(b1)\n"
842 "{\n"
843 " float3 dx_DepthRange : packoffset(c0);\n"
844 "};\n"
845 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000846 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000847 }
848 else
849 {
850 if (mUsesDepthRange)
851 {
852 out << "uniform float3 dx_DepthRange : register(c0);\n";
853 }
854
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000855 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000856 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000857 }
858
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000859 if (mUsesDepthRange)
860 {
861 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
862 "\n";
863 }
864
865 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000866 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000867
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000868 if (!interfaceBlocks.empty())
869 {
870 out << interfaceBlocks;
871 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400872
873 if (!flaggedStructs.empty())
874 {
875 out << "// Std140 Structures accessed by value\n";
876 out << "\n";
877 out << flaggedStructs;
878 out << "\n";
879 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000880 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400881 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000882
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400883 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
884 {
885 // Return type
886 switch(textureFunction->sampler)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000887 {
Nicolas Capensfb50dff2013-06-24 16:16:23 -0400888 case EbtSampler2D: out << "float4 "; break;
889 case EbtSampler3D: out << "float4 "; break;
890 case EbtSamplerCube: out << "float4 "; break;
891 case EbtSampler2DArray: out << "float4 "; break;
892 case EbtISampler2D: out << "int4 "; break;
893 case EbtISampler3D: out << "int4 "; break;
894 case EbtISamplerCube: out << "int4 "; break;
895 case EbtISampler2DArray: out << "int4 "; break;
896 case EbtUSampler2D: out << "uint4 "; break;
897 case EbtUSampler3D: out << "uint4 "; break;
898 case EbtUSamplerCube: out << "uint4 "; break;
899 case EbtUSampler2DArray: out << "uint4 "; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400900 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000901 }
902
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400903 // Function name
904 out << textureFunction->name();
905
906 // Argument list
907 int hlslCoords = 4;
908
909 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000910 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400911 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000912 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400913 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
914 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
915 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000916 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400917
918 switch(textureFunction->mipmap)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000919 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400920 case TextureFunction::IMPLICIT: break;
921 case TextureFunction::BIAS: hlslCoords = 4; break;
922 case TextureFunction::LOD: hlslCoords = 4; break;
923 case TextureFunction::LOD0: hlslCoords = 4; break;
924 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000925 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400926 }
927 else if (mOutputType == SH_HLSL11_OUTPUT)
928 {
929 switch(textureFunction->sampler)
930 {
Nicolas Capensfb50dff2013-06-24 16:16:23 -0400931 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
932 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
933 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
934 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 2; break;
935 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
936 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
937 case EbtISamplerCube: out << "TextureCube<int4> x, SamplerState s"; hlslCoords = 3; break;
938 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 2; break;
939 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
940 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
941 case EbtUSamplerCube: out << "TextureCube<uint4> x, SamplerState s"; hlslCoords = 3; break;
942 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 2; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400943 default: UNREACHABLE();
944 }
945 }
946 else UNREACHABLE();
947
948 switch(textureFunction->coords)
949 {
950 case 2: out << ", float2 t"; break;
951 case 3: out << ", float3 t"; break;
952 case 4: out << ", float4 t"; break;
953 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000954 }
955
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400956 switch(textureFunction->mipmap)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000957 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400958 case TextureFunction::IMPLICIT: break;
959 case TextureFunction::BIAS: out << ", float bias"; break;
960 case TextureFunction::LOD: out << ", float lod"; break;
961 case TextureFunction::LOD0: break;
962 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000963 }
964
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400965 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400966 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400967
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400968 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
daniel@transgaming.com15795192011-05-11 15:36:20 +0000969 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400970 // Currently unsupported because TextureCube does not support Load
971 // This will require emulation using a Texture2DArray with 6 faces
972 if (textureFunction->sampler == EbtISamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000973 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400974 out << " return int4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000975 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400976 else if (textureFunction->sampler == EbtUSamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000977 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400978 out << " return uint4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000979 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400980 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400981 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400982 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400983 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400984 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400985 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400986 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400987 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400988 out << " uint width; uint height; uint numberOfLevels;\n"
989 " x.GetDimensions(0, width, height, numberOfLevels);\n";
990 }
991 else if (IsSampler3D(textureFunction->sampler))
992 {
993 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
994 " x.GetDimensions(0, width, height, depth, numberOfLevels);\n";
995 }
996 else UNREACHABLE();
997 }
998
999 out << " return ";
1000
1001 // HLSL intrinsic
1002 if (mOutputType == SH_HLSL9_OUTPUT)
1003 {
1004 switch(textureFunction->sampler)
1005 {
1006 case EbtSampler2D: out << "tex2D"; break;
1007 case EbtSamplerCube: out << "texCUBE"; break;
1008 default: UNREACHABLE();
1009 }
1010
1011 switch(textureFunction->mipmap)
1012 {
1013 case TextureFunction::IMPLICIT: out << "(s, "; break;
1014 case TextureFunction::BIAS: out << "bias(s, "; break;
1015 case TextureFunction::LOD: out << "lod(s, "; break;
1016 case TextureFunction::LOD0: out << "lod(s, "; break;
1017 default: UNREACHABLE();
1018 }
1019 }
1020 else if (mOutputType == SH_HLSL11_OUTPUT)
1021 {
1022 if (IsIntegerSampler(textureFunction->sampler))
1023 {
1024 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001025 }
1026 else
1027 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001028 switch(textureFunction->mipmap)
1029 {
1030 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1031 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1032 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1033 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
1034 default: UNREACHABLE();
1035 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001036 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001037 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001038 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001039
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001040 // Integer sampling requires integer addresses
1041 TString addressx = "";
1042 TString addressy = "";
1043 TString addressz = "";
1044 TString close = "";
1045
1046 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001047 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001048 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001049 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001050 case 2: out << "int3("; break;
1051 case 3: out << "int4("; break;
1052 default: UNREACHABLE();
1053 }
1054
1055 addressx = "int(floor(float(width) * frac(";
1056 addressy = "int(floor(float(height) * frac(";
1057 addressz = "int(floor(float(depth) * frac(";
1058 close = ")))";
1059 }
1060 else
1061 {
1062 switch(hlslCoords)
1063 {
1064 case 2: out << "float2("; break;
1065 case 3: out << "float3("; break;
1066 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001067 default: UNREACHABLE();
1068 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001069 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001070
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001071 TString proj = ""; // Only used for projected textures
1072
1073 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001074 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001075 switch(textureFunction->coords)
1076 {
1077 case 3: proj = " / t.z"; break;
1078 case 4: proj = " / t.w"; break;
1079 default: UNREACHABLE();
1080 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001081 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001082
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001083 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001084
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001085 if (mOutputType == SH_HLSL9_OUTPUT)
1086 {
1087 if (hlslCoords >= 3)
1088 {
1089 if (textureFunction->coords < 3)
1090 {
1091 out << ", 0";
1092 }
1093 else
1094 {
1095 out << ", t.z" + proj;
1096 }
1097 }
1098
1099 if (hlslCoords == 4)
1100 {
1101 switch(textureFunction->mipmap)
1102 {
1103 case TextureFunction::BIAS: out << ", bias"; break;
1104 case TextureFunction::LOD: out << ", lod"; break;
1105 case TextureFunction::LOD0: out << ", 0"; break;
1106 default: UNREACHABLE();
1107 }
1108 }
1109
1110 out << "));\n";
1111 }
1112 else if (mOutputType == SH_HLSL11_OUTPUT)
1113 {
1114 if (hlslCoords >= 3)
1115 {
1116 out << ", " + addressz + ("t.z" + proj) + close;
1117 }
1118
1119 if (!IsIntegerSampler(textureFunction->sampler))
1120 {
1121 switch(textureFunction->mipmap)
1122 {
1123 case TextureFunction::IMPLICIT: out << "));"; break;
1124 case TextureFunction::BIAS: out << "), bias);"; break;
1125 case TextureFunction::LOD: out << "), lod);"; break;
1126 case TextureFunction::LOD0: out << "), 0);"; break;
1127 default: UNREACHABLE();
1128 }
1129 }
1130 else
1131 {
1132 out << ", 0));"; // Sample from the top level
1133 }
1134 }
1135 else UNREACHABLE();
1136 }
1137
1138 out << "\n"
1139 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001140 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001141 }
1142
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001143 if (mUsesFragCoord)
1144 {
1145 out << "#define GL_USES_FRAG_COORD\n";
1146 }
1147
1148 if (mUsesPointCoord)
1149 {
1150 out << "#define GL_USES_POINT_COORD\n";
1151 }
1152
1153 if (mUsesFrontFacing)
1154 {
1155 out << "#define GL_USES_FRONT_FACING\n";
1156 }
1157
1158 if (mUsesPointSize)
1159 {
1160 out << "#define GL_USES_POINT_SIZE\n";
1161 }
1162
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001163 if (mUsesFragDepth)
1164 {
1165 out << "#define GL_USES_FRAG_DEPTH\n";
1166 }
1167
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001168 if (mUsesDepthRange)
1169 {
1170 out << "#define GL_USES_DEPTH_RANGE\n";
1171 }
1172
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001173 if (mUsesXor)
1174 {
1175 out << "bool xor(bool p, bool q)\n"
1176 "{\n"
1177 " return (p || q) && !(p && q);\n"
1178 "}\n"
1179 "\n";
1180 }
1181
1182 if (mUsesMod1)
1183 {
1184 out << "float mod(float x, float y)\n"
1185 "{\n"
1186 " return x - y * floor(x / y);\n"
1187 "}\n"
1188 "\n";
1189 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001190
1191 if (mUsesMod2v)
1192 {
1193 out << "float2 mod(float2 x, float2 y)\n"
1194 "{\n"
1195 " return x - y * floor(x / y);\n"
1196 "}\n"
1197 "\n";
1198 }
1199
1200 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001201 {
1202 out << "float2 mod(float2 x, float y)\n"
1203 "{\n"
1204 " return x - y * floor(x / y);\n"
1205 "}\n"
1206 "\n";
1207 }
1208
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001209 if (mUsesMod3v)
1210 {
1211 out << "float3 mod(float3 x, float3 y)\n"
1212 "{\n"
1213 " return x - y * floor(x / y);\n"
1214 "}\n"
1215 "\n";
1216 }
1217
1218 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001219 {
1220 out << "float3 mod(float3 x, float y)\n"
1221 "{\n"
1222 " return x - y * floor(x / y);\n"
1223 "}\n"
1224 "\n";
1225 }
1226
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001227 if (mUsesMod4v)
1228 {
1229 out << "float4 mod(float4 x, float4 y)\n"
1230 "{\n"
1231 " return x - y * floor(x / y);\n"
1232 "}\n"
1233 "\n";
1234 }
1235
1236 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001237 {
1238 out << "float4 mod(float4 x, float y)\n"
1239 "{\n"
1240 " return x - y * floor(x / y);\n"
1241 "}\n"
1242 "\n";
1243 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001244
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001245 if (mUsesFaceforward1)
1246 {
1247 out << "float faceforward(float N, float I, float Nref)\n"
1248 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001249 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001250 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001251 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001252 " }\n"
1253 " else\n"
1254 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001255 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001256 " }\n"
1257 "}\n"
1258 "\n";
1259 }
1260
1261 if (mUsesFaceforward2)
1262 {
1263 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1264 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001265 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001266 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001267 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001268 " }\n"
1269 " else\n"
1270 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001271 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001272 " }\n"
1273 "}\n"
1274 "\n";
1275 }
1276
1277 if (mUsesFaceforward3)
1278 {
1279 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1280 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001281 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001282 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001283 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001284 " }\n"
1285 " else\n"
1286 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001287 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001288 " }\n"
1289 "}\n"
1290 "\n";
1291 }
1292
1293 if (mUsesFaceforward4)
1294 {
1295 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1296 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001297 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001298 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001299 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001300 " }\n"
1301 " else\n"
1302 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001303 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001304 " }\n"
1305 "}\n"
1306 "\n";
1307 }
1308
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001309 for (unsigned int cols = 2; cols <= 4; cols++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001310 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001311 for (unsigned int rows = 2; rows <= 4; rows++)
1312 {
1313 if (mUsesEqualMat[cols][rows])
1314 {
1315 TString matrixType = "float" + str(cols) + "x" + str(rows);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001316
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001317 out << "bool equal(" + matrixType + " m, " + matrixType + " n)\n"
1318 "{\n";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001319
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001320 for (unsigned int row = 0; row < rows; row++)
1321 {
1322 if (row == 0)
1323 {
1324 out << " return ";
1325 }
1326 else
1327 {
1328 out << " ";
1329 }
1330
1331 for (unsigned int col = 0; col < cols; col++)
1332 {
1333 TString index = "[" + str(col) + "][" + str(row) + "]";
1334 out << "m" + index + " == n" + index;
1335
1336 if (col == cols-1 && row == rows-1)
1337 {
1338 out << ";\n";
1339 }
1340 else if (col == cols-1)
1341 {
1342 out << " &&\n";
1343 }
1344 else
1345 {
1346 out << " && ";
1347 }
1348 }
1349 }
1350
1351 out << "}\n";
1352 }
1353 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001354 }
1355
1356 if (mUsesEqualVec2)
1357 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001358 out << "bool equal(float2 v, float2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001359 "{\n"
1360 " return v.x == u.x && v.y == u.y;\n"
1361 "}\n";
1362 }
1363
1364 if (mUsesEqualVec3)
1365 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001366 out << "bool equal(float3 v, float3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001367 "{\n"
1368 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1369 "}\n";
1370 }
1371
1372 if (mUsesEqualVec4)
1373 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001374 out << "bool equal(float4 v, float4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001375 "{\n"
1376 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1377 "}\n";
1378 }
1379
1380 if (mUsesEqualIVec2)
1381 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001382 out << "bool equal(int2 v, int2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001383 "{\n"
1384 " return v.x == u.x && v.y == u.y;\n"
1385 "}\n";
1386 }
1387
1388 if (mUsesEqualIVec3)
1389 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001390 out << "bool equal(int3 v, int3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001391 "{\n"
1392 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1393 "}\n";
1394 }
1395
1396 if (mUsesEqualIVec4)
1397 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001398 out << "bool equal(int4 v, int4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001399 "{\n"
1400 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1401 "}\n";
1402 }
1403
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001404 if (mUsesEqualUVec2)
1405 {
1406 out << "bool equal(uint2 v, uint2 u)\n"
1407 "{\n"
1408 " return v.x == u.x && v.y == u.y;\n"
1409 "}\n";
1410 }
1411
1412 if (mUsesEqualUVec3)
1413 {
1414 out << "bool equal(uint3 v, uint3 u)\n"
1415 "{\n"
1416 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1417 "}\n";
1418 }
1419
1420 if (mUsesEqualUVec4)
1421 {
1422 out << "bool equal(uint4 v, uint4 u)\n"
1423 "{\n"
1424 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1425 "}\n";
1426 }
1427
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001428 if (mUsesEqualBVec2)
1429 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001430 out << "bool equal(bool2 v, bool2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001431 "{\n"
1432 " return v.x == u.x && v.y == u.y;\n"
1433 "}\n";
1434 }
1435
1436 if (mUsesEqualBVec3)
1437 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001438 out << "bool equal(bool3 v, bool3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001439 "{\n"
1440 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1441 "}\n";
1442 }
1443
1444 if (mUsesEqualBVec4)
1445 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001446 out << "bool equal(bool4 v, bool4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001447 "{\n"
1448 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1449 "}\n";
1450 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001451
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001452 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001453 {
1454 out << "float atanyx(float y, float x)\n"
1455 "{\n"
1456 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1457 " return atan2(y, x);\n"
1458 "}\n";
1459 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001460
1461 if (mUsesAtan2_2)
1462 {
1463 out << "float2 atanyx(float2 y, float2 x)\n"
1464 "{\n"
1465 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1466 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1467 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1468 "}\n";
1469 }
1470
1471 if (mUsesAtan2_3)
1472 {
1473 out << "float3 atanyx(float3 y, float3 x)\n"
1474 "{\n"
1475 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1476 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1477 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1478 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1479 "}\n";
1480 }
1481
1482 if (mUsesAtan2_4)
1483 {
1484 out << "float4 atanyx(float4 y, float4 x)\n"
1485 "{\n"
1486 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1487 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1488 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1489 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1490 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1491 "}\n";
1492 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001493}
1494
1495void OutputHLSL::visitSymbol(TIntermSymbol *node)
1496{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001497 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001498
Jamie Madill570e04d2013-06-21 09:15:33 -04001499 // Handle accessing std140 structs by value
1500 if (mFlaggedStructMappedNames.count(node) > 0)
1501 {
1502 out << mFlaggedStructMappedNames[node];
1503 return;
1504 }
1505
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001506 TString name = node->getSymbol();
1507
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001508 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001509 {
1510 mUsesDepthRange = true;
1511 out << name;
1512 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001513 else
1514 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001515 TQualifier qualifier = node->getQualifier();
1516
1517 if (qualifier == EvqUniform)
1518 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001519 const TType& nodeType = node->getType();
1520 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1521
1522 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001523 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001524 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001525 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001526 else
1527 {
1528 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001529 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001530
1531 out << decorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001532 }
Jamie Madillb120eac2013-06-12 14:08:13 -04001533 else if (qualifier == EvqAttribute || qualifier == EvqVertexInput)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001534 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001535 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001536 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001537 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001538 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001539 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001540 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001541 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001542 }
Jamie Madill46131a32013-06-20 11:55:50 -04001543 else if (qualifier == EvqFragmentOutput)
1544 {
1545 mReferencedOutputVariables[name] = node;
1546 out << "out_" << name;
1547 }
1548 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001549 {
1550 out << "gl_Color[0]";
1551 mUsesFragColor = true;
1552 }
1553 else if (qualifier == EvqFragData)
1554 {
1555 out << "gl_Color";
1556 mUsesFragData = true;
1557 }
1558 else if (qualifier == EvqFragCoord)
1559 {
1560 mUsesFragCoord = true;
1561 out << name;
1562 }
1563 else if (qualifier == EvqPointCoord)
1564 {
1565 mUsesPointCoord = true;
1566 out << name;
1567 }
1568 else if (qualifier == EvqFrontFacing)
1569 {
1570 mUsesFrontFacing = true;
1571 out << name;
1572 }
1573 else if (qualifier == EvqPointSize)
1574 {
1575 mUsesPointSize = true;
1576 out << name;
1577 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001578 else if (name == "gl_FragDepthEXT")
1579 {
1580 mUsesFragDepth = true;
1581 out << "gl_Depth";
1582 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001583 else
1584 {
1585 out << decorate(name);
1586 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001587 }
1588}
1589
1590bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1591{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001592 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001593
Jamie Madill570e04d2013-06-21 09:15:33 -04001594 // Handle accessing std140 structs by value
1595 if (mFlaggedStructMappedNames.count(node) > 0)
1596 {
1597 out << mFlaggedStructMappedNames[node];
1598 return false;
1599 }
1600
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001601 switch (node->getOp())
1602 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001603 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001604 case EOpInitialize:
1605 if (visit == PreVisit)
1606 {
1607 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1608 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1609 // new variable is created before the assignment is evaluated), so we need to convert
1610 // this to "float t = x, x = t;".
1611
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001612 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1613 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001614
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001615 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1616 expression->traverse(&searchSymbol);
1617 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001618
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001619 if (sameSymbol)
1620 {
1621 // Type already printed
1622 out << "t" + str(mUniqueIndex) + " = ";
1623 expression->traverse(this);
1624 out << ", ";
1625 symbolNode->traverse(this);
1626 out << " = t" + str(mUniqueIndex);
1627
1628 mUniqueIndex++;
1629 return false;
1630 }
1631 }
1632 else if (visit == InVisit)
1633 {
1634 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001635 }
1636 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001637 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1638 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1639 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1640 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1641 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1642 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001643 if (visit == PreVisit)
1644 {
1645 out << "(";
1646 }
1647 else if (visit == InVisit)
1648 {
1649 out << " = mul(";
1650 node->getLeft()->traverse(this);
1651 out << ", transpose(";
1652 }
1653 else
1654 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001655 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001656 }
1657 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001658 case EOpMatrixTimesMatrixAssign:
1659 if (visit == PreVisit)
1660 {
1661 out << "(";
1662 }
1663 else if (visit == InVisit)
1664 {
1665 out << " = mul(";
1666 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001667 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001668 }
1669 else
1670 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001671 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001672 }
1673 break;
1674 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001675 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001676 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001677 const TType& leftType = node->getLeft()->getType();
1678 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001679 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001680 if (visit == PreVisit)
1681 {
1682 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1683 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1684
1685 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1686 out << interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1687
1688 return false;
1689 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001690 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001691 else
1692 {
1693 outputTriplet(visit, "", "[", "]");
1694 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001695 }
1696 break;
1697 case EOpIndexIndirect:
1698 // We do not currently support indirect references to interface blocks
1699 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1700 outputTriplet(visit, "", "[", "]");
1701 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001702 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001703 if (visit == InVisit)
1704 {
1705 const TStructure* structure = node->getLeft()->getType().getStruct();
1706 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1707 const TField* field = structure->fields()[index->getIConst(0)];
1708 out << "." + decorateField(field->name(), *structure);
1709
1710 return false;
1711 }
1712 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001713 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001714 if (visit == InVisit)
1715 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001716 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1717 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1718 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
1719 out << "." + decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001720
1721 return false;
1722 }
1723 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001724 case EOpVectorSwizzle:
1725 if (visit == InVisit)
1726 {
1727 out << ".";
1728
1729 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1730
1731 if (swizzle)
1732 {
1733 TIntermSequence &sequence = swizzle->getSequence();
1734
1735 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1736 {
1737 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1738
1739 if (element)
1740 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001741 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001742
1743 switch (i)
1744 {
1745 case 0: out << "x"; break;
1746 case 1: out << "y"; break;
1747 case 2: out << "z"; break;
1748 case 3: out << "w"; break;
1749 default: UNREACHABLE();
1750 }
1751 }
1752 else UNREACHABLE();
1753 }
1754 }
1755 else UNREACHABLE();
1756
1757 return false; // Fully processed
1758 }
1759 break;
1760 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1761 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1762 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1763 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001764 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001765 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001766 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001767 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001768 if (node->getOp() == EOpEqual)
1769 {
1770 outputTriplet(visit, "(", " == ", ")");
1771 }
1772 else
1773 {
1774 outputTriplet(visit, "(", " != ", ")");
1775 }
1776 }
1777 else if (node->getLeft()->getBasicType() == EbtStruct)
1778 {
1779 if (node->getOp() == EOpEqual)
1780 {
1781 out << "(";
1782 }
1783 else
1784 {
1785 out << "!(";
1786 }
1787
Jamie Madill98493dd2013-07-08 14:39:03 -04001788 const TStructure &structure = *node->getLeft()->getType().getStruct();
1789 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001790
Jamie Madill98493dd2013-07-08 14:39:03 -04001791 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001792 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001793 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001794
1795 node->getLeft()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001796 out << "." + decorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001797 node->getRight()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001798 out << "." + decorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001799
Jamie Madill98493dd2013-07-08 14:39:03 -04001800 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001801 {
1802 out << " && ";
1803 }
1804 }
1805
1806 out << ")";
1807
1808 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001809 }
1810 else
1811 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001812 if (node->getLeft()->isMatrix())
1813 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001814 mUsesEqualMat[node->getLeft()->getCols()][node->getLeft()->getRows()] = true;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001815 }
1816 else if (node->getLeft()->isVector())
1817 {
1818 switch (node->getLeft()->getBasicType())
1819 {
1820 case EbtFloat:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001821 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001822 {
1823 case 2: mUsesEqualVec2 = true; break;
1824 case 3: mUsesEqualVec3 = true; break;
1825 case 4: mUsesEqualVec4 = true; break;
1826 default: UNREACHABLE();
1827 }
1828 break;
1829 case EbtInt:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001830 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001831 {
1832 case 2: mUsesEqualIVec2 = true; break;
1833 case 3: mUsesEqualIVec3 = true; break;
1834 case 4: mUsesEqualIVec4 = true; break;
1835 default: UNREACHABLE();
1836 }
1837 break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001838 case EbtUInt:
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001839 switch (node->getLeft()->getNominalSize())
1840 {
1841 case 2: mUsesEqualUVec2 = true; break;
1842 case 3: mUsesEqualUVec3 = true; break;
1843 case 4: mUsesEqualUVec4 = true; break;
1844 default: UNREACHABLE();
1845 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001846 break;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001847 case EbtBool:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001848 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001849 {
1850 case 2: mUsesEqualBVec2 = true; break;
1851 case 3: mUsesEqualBVec3 = true; break;
1852 case 4: mUsesEqualBVec4 = true; break;
1853 default: UNREACHABLE();
1854 }
1855 break;
1856 default: UNREACHABLE();
1857 }
1858 }
1859 else UNREACHABLE();
1860
1861 if (node->getOp() == EOpEqual)
1862 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001863 outputTriplet(visit, "equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001864 }
1865 else
1866 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001867 outputTriplet(visit, "!equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001868 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001869 }
1870 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001871 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1872 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1873 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1874 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1875 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001876 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001877 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1878 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001879 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001880 case EOpLogicalOr:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001881 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001882 return false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001883 case EOpLogicalXor:
1884 mUsesXor = true;
1885 outputTriplet(visit, "xor(", ", ", ")");
1886 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001887 case EOpLogicalAnd:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001888 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001889 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001890 default: UNREACHABLE();
1891 }
1892
1893 return true;
1894}
1895
1896bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1897{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001898 switch (node->getOp())
1899 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001900 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1901 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1902 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1903 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1904 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1905 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1906 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001907 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04001908 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001909 case EOpConvFloatToBool:
1910 switch (node->getOperand()->getType().getNominalSize())
1911 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001912 case 1: outputTriplet(visit, "bool(", "", ")"); break;
1913 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
1914 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
1915 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001916 default: UNREACHABLE();
1917 }
1918 break;
1919 case EOpConvBoolToFloat:
1920 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04001921 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001922 switch (node->getOperand()->getType().getNominalSize())
1923 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001924 case 1: outputTriplet(visit, "float(", "", ")"); break;
1925 case 2: outputTriplet(visit, "float2(", "", ")"); break;
1926 case 3: outputTriplet(visit, "float3(", "", ")"); break;
1927 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001928 default: UNREACHABLE();
1929 }
1930 break;
1931 case EOpConvFloatToInt:
1932 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04001933 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001934 switch (node->getOperand()->getType().getNominalSize())
1935 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001936 case 1: outputTriplet(visit, "int(", "", ")"); break;
1937 case 2: outputTriplet(visit, "int2(", "", ")"); break;
1938 case 3: outputTriplet(visit, "int3(", "", ")"); break;
1939 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001940 default: UNREACHABLE();
1941 }
1942 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04001943 case EOpConvFloatToUInt:
1944 case EOpConvBoolToUInt:
1945 case EOpConvIntToUInt:
Jamie Madilla16f1672013-07-03 15:15:17 -04001946 switch (node->getOperand()->getType().getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001947 {
1948 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001949 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
1950 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
1951 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001952 default: UNREACHABLE();
1953 }
1954 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001955 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1956 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1957 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1958 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1959 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1960 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1961 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1962 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1963 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1964 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1965 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1966 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1967 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1968 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1969 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1970 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1971 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1972 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1973 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1974 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1975 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001976 case EOpDFdx:
1977 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1978 {
1979 outputTriplet(visit, "(", "", ", 0.0)");
1980 }
1981 else
1982 {
1983 outputTriplet(visit, "ddx(", "", ")");
1984 }
1985 break;
1986 case EOpDFdy:
1987 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1988 {
1989 outputTriplet(visit, "(", "", ", 0.0)");
1990 }
1991 else
1992 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001993 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001994 }
1995 break;
1996 case EOpFwidth:
1997 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1998 {
1999 outputTriplet(visit, "(", "", ", 0.0)");
2000 }
2001 else
2002 {
2003 outputTriplet(visit, "fwidth(", "", ")");
2004 }
2005 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002006 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
2007 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002008 default: UNREACHABLE();
2009 }
2010
2011 return true;
2012}
2013
2014bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2015{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002016 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002017
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002018 switch (node->getOp())
2019 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002020 case EOpSequence:
2021 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002022 if (mInsideFunction)
2023 {
Jamie Madill075edd82013-07-08 13:30:19 -04002024 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002025 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002026
2027 mScopeDepth++;
2028
2029 if (mScopeBracket.size() < mScopeDepth)
2030 {
2031 mScopeBracket.push_back(0); // New scope level
2032 }
2033 else
2034 {
2035 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
2036 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002037 }
2038
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002039 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
2040 {
Jamie Madill075edd82013-07-08 13:30:19 -04002041 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002042
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002043 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002044
2045 out << ";\n";
2046 }
2047
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002048 if (mInsideFunction)
2049 {
Jamie Madill075edd82013-07-08 13:30:19 -04002050 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002051 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002052
2053 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002054 }
2055
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002056 return false;
2057 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002058 case EOpDeclaration:
2059 if (visit == PreVisit)
2060 {
2061 TIntermSequence &sequence = node->getSequence();
2062 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002063
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00002064 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002065 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002066 if (variable->getType().getStruct())
2067 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002068 addConstructor(variable->getType(), scopedStruct(variable->getType().getStruct()->name()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00002069 }
2070
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002071 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002072 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00002073 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002074 {
2075 out << "static ";
2076 }
2077
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002078 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002079
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002080 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002081 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002082 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002083
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002084 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002085 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002086 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002087 out << arrayString(symbol->getType());
daniel@transgaming.com7127f202010-04-15 20:45:22 +00002088 out << " = " + initializer(variable->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002089 }
2090 else
2091 {
2092 (*sit)->traverse(this);
2093 }
2094
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002095 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002096 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002097 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002098 }
2099 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002100 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002101 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2102 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002103 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002104 }
2105 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002106 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002107 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002108 {
2109 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2110 {
2111 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2112
2113 if (symbol)
2114 {
2115 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2116 mReferencedVaryings[symbol->getSymbol()] = symbol;
2117 }
2118 else
2119 {
2120 (*sit)->traverse(this);
2121 }
2122 }
2123 }
2124
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002125 return false;
2126 }
2127 else if (visit == InVisit)
2128 {
2129 out << ", ";
2130 }
2131 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002132 case EOpPrototype:
2133 if (visit == PreVisit)
2134 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002135 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002136
2137 TIntermSequence &arguments = node->getSequence();
2138
2139 for (unsigned int i = 0; i < arguments.size(); i++)
2140 {
2141 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2142
2143 if (symbol)
2144 {
2145 out << argumentString(symbol);
2146
2147 if (i < arguments.size() - 1)
2148 {
2149 out << ", ";
2150 }
2151 }
2152 else UNREACHABLE();
2153 }
2154
2155 out << ");\n";
2156
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002157 // Also prototype the Lod0 variant if needed
2158 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2159 {
2160 mOutputLod0Function = true;
2161 node->traverse(this);
2162 mOutputLod0Function = false;
2163 }
2164
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002165 return false;
2166 }
2167 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002168 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002169 case EOpFunction:
2170 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002171 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002172
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002173 out << typeString(node->getType()) << " ";
2174
2175 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002176 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002177 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002178 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002179 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002180 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002181 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002182 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002183
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002184 TIntermSequence &sequence = node->getSequence();
2185 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2186
2187 for (unsigned int i = 0; i < arguments.size(); i++)
2188 {
2189 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2190
2191 if (symbol)
2192 {
2193 if (symbol->getType().getStruct())
2194 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002195 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getStruct()->name()), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002196 }
2197
2198 out << argumentString(symbol);
2199
2200 if (i < arguments.size() - 1)
2201 {
2202 out << ", ";
2203 }
2204 }
2205 else UNREACHABLE();
2206 }
2207
2208 out << ")\n"
2209 "{\n";
2210
2211 if (sequence.size() > 1)
2212 {
2213 mInsideFunction = true;
2214 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002215 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002216 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002217
2218 out << "}\n";
2219
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002220 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2221 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002222 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002223 {
2224 mOutputLod0Function = true;
2225 node->traverse(this);
2226 mOutputLod0Function = false;
2227 }
2228 }
2229
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002230 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002231 }
2232 break;
2233 case EOpFunctionCall:
2234 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002235 TString name = TFunction::unmangleName(node->getName());
2236 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002237 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002238
2239 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002240 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002241 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002242 }
2243 else
2244 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002245 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2246
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002247 TextureFunction textureFunction;
2248 textureFunction.sampler = samplerType;
2249 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
2250 textureFunction.mipmap = TextureFunction::IMPLICIT;
2251
2252 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002253 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002254 textureFunction.mipmap = TextureFunction::IMPLICIT;
2255 textureFunction.proj = false;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002256 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002257 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002258 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002259 textureFunction.mipmap = TextureFunction::IMPLICIT;
2260 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002261 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002262 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002263 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002264 textureFunction.mipmap = TextureFunction::LOD;
2265 textureFunction.proj = false;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002266 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002267 else if (name == "texture2DProjLod" || name == "textureProjLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002268 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002269 textureFunction.mipmap = TextureFunction::LOD;
2270 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002271 }
2272 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002273
2274 if (textureFunction.mipmap != TextureFunction::LOD)
2275 {
2276 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2277 {
2278 textureFunction.mipmap = TextureFunction::LOD0;
2279 }
2280 else if (arguments.size() == 3)
2281 {
2282 textureFunction.mipmap = TextureFunction::BIAS;
2283 }
2284 }
2285
2286 mUsesTexture.insert(textureFunction);
2287
2288 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002289 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002290
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002291 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2292 {
2293 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2294 {
2295 out << "texture_";
2296 (*arg)->traverse(this);
2297 out << ", sampler_";
2298 }
2299
2300 (*arg)->traverse(this);
2301
2302 if (arg < arguments.end() - 1)
2303 {
2304 out << ", ";
2305 }
2306 }
2307
2308 out << ")";
2309
2310 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002311 }
2312 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002313 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002314 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002315 addConstructor(node->getType(), "vec1", &node->getSequence());
2316 outputTriplet(visit, "vec1(", "", ")");
2317 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002318 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002319 addConstructor(node->getType(), "vec2", &node->getSequence());
2320 outputTriplet(visit, "vec2(", ", ", ")");
2321 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002322 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002323 addConstructor(node->getType(), "vec3", &node->getSequence());
2324 outputTriplet(visit, "vec3(", ", ", ")");
2325 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002326 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002327 addConstructor(node->getType(), "vec4", &node->getSequence());
2328 outputTriplet(visit, "vec4(", ", ", ")");
2329 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002330 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002331 addConstructor(node->getType(), "bvec1", &node->getSequence());
2332 outputTriplet(visit, "bvec1(", "", ")");
2333 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002334 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002335 addConstructor(node->getType(), "bvec2", &node->getSequence());
2336 outputTriplet(visit, "bvec2(", ", ", ")");
2337 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002338 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002339 addConstructor(node->getType(), "bvec3", &node->getSequence());
2340 outputTriplet(visit, "bvec3(", ", ", ")");
2341 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002342 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002343 addConstructor(node->getType(), "bvec4", &node->getSequence());
2344 outputTriplet(visit, "bvec4(", ", ", ")");
2345 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002346 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002347 addConstructor(node->getType(), "ivec1", &node->getSequence());
2348 outputTriplet(visit, "ivec1(", "", ")");
2349 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002350 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002351 addConstructor(node->getType(), "ivec2", &node->getSequence());
2352 outputTriplet(visit, "ivec2(", ", ", ")");
2353 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002354 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002355 addConstructor(node->getType(), "ivec3", &node->getSequence());
2356 outputTriplet(visit, "ivec3(", ", ", ")");
2357 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002358 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002359 addConstructor(node->getType(), "ivec4", &node->getSequence());
2360 outputTriplet(visit, "ivec4(", ", ", ")");
2361 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002362 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002363 addConstructor(node->getType(), "uvec1", &node->getSequence());
2364 outputTriplet(visit, "uvec1(", "", ")");
2365 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002366 case EOpConstructUVec2:
2367 addConstructor(node->getType(), "uvec2", &node->getSequence());
2368 outputTriplet(visit, "uvec2(", ", ", ")");
2369 break;
2370 case EOpConstructUVec3:
2371 addConstructor(node->getType(), "uvec3", &node->getSequence());
2372 outputTriplet(visit, "uvec3(", ", ", ")");
2373 break;
2374 case EOpConstructUVec4:
2375 addConstructor(node->getType(), "uvec4", &node->getSequence());
2376 outputTriplet(visit, "uvec4(", ", ", ")");
2377 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002378 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002379 addConstructor(node->getType(), "mat2", &node->getSequence());
2380 outputTriplet(visit, "mat2(", ", ", ")");
2381 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002382 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002383 addConstructor(node->getType(), "mat3", &node->getSequence());
2384 outputTriplet(visit, "mat3(", ", ", ")");
2385 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002386 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002387 addConstructor(node->getType(), "mat4", &node->getSequence());
2388 outputTriplet(visit, "mat4(", ", ", ")");
2389 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002390 case EOpConstructStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04002391 addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence());
2392 outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002393 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002394 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2395 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2396 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2397 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2398 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2399 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002400 case EOpMod:
2401 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002402 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002403 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2404 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2405 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002406 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002407 case 11: mUsesMod1 = true; break;
2408 case 22: mUsesMod2v = true; break;
2409 case 21: mUsesMod2f = true; break;
2410 case 33: mUsesMod3v = true; break;
2411 case 31: mUsesMod3f = true; break;
2412 case 44: mUsesMod4v = true; break;
2413 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002414 default: UNREACHABLE();
2415 }
2416
2417 outputTriplet(visit, "mod(", ", ", ")");
2418 }
2419 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002420 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002421 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002422 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002423 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2424 {
2425 case 1: mUsesAtan2_1 = true; break;
2426 case 2: mUsesAtan2_2 = true; break;
2427 case 3: mUsesAtan2_3 = true; break;
2428 case 4: mUsesAtan2_4 = true; break;
2429 default: UNREACHABLE();
2430 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002431 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002432 break;
2433 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2434 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2435 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2436 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2437 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2438 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2439 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2440 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2441 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002442 case EOpFaceForward:
2443 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002444 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002445 {
2446 case 1: mUsesFaceforward1 = true; break;
2447 case 2: mUsesFaceforward2 = true; break;
2448 case 3: mUsesFaceforward3 = true; break;
2449 case 4: mUsesFaceforward4 = true; break;
2450 default: UNREACHABLE();
2451 }
2452
2453 outputTriplet(visit, "faceforward(", ", ", ")");
2454 }
2455 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002456 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2457 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2458 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002459 default: UNREACHABLE();
2460 }
2461
2462 return true;
2463}
2464
2465bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2466{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002467 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002468
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002469 if (node->usesTernaryOperator())
2470 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002471 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002472 }
2473 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002474 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002475 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002476
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002477 out << "if(";
2478
2479 node->getCondition()->traverse(this);
2480
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002481 out << ")\n";
2482
Jamie Madill075edd82013-07-08 13:30:19 -04002483 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002484 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002485
daniel@transgaming.combb885322010-04-15 20:45:24 +00002486 if (node->getTrueBlock())
2487 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002488 traverseStatements(node->getTrueBlock());
daniel@transgaming.combb885322010-04-15 20:45:24 +00002489 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002490
Jamie Madill075edd82013-07-08 13:30:19 -04002491 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002492 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002493
2494 if (node->getFalseBlock())
2495 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002496 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002497
Jamie Madill075edd82013-07-08 13:30:19 -04002498 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002499 out << "{\n";
2500
Jamie Madill075edd82013-07-08 13:30:19 -04002501 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002502 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002503
Jamie Madill075edd82013-07-08 13:30:19 -04002504 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002505 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002506 }
2507 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002508
2509 return false;
2510}
2511
2512void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2513{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002514 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002515}
2516
2517bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2518{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002519 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2520
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002521 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002522 {
2523 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2524 }
2525
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002526 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002527 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002528 if (handleExcessiveLoop(node))
2529 {
2530 return false;
2531 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002532 }
2533
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002534 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002535
alokp@chromium.org52813552010-11-16 18:36:09 +00002536 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002537 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002538 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002539
Jamie Madill075edd82013-07-08 13:30:19 -04002540 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002541 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002542 }
2543 else
2544 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002545 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002546
2547 if (node->getInit())
2548 {
2549 node->getInit()->traverse(this);
2550 }
2551
2552 out << "; ";
2553
alokp@chromium.org52813552010-11-16 18:36:09 +00002554 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002555 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002556 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002557 }
2558
2559 out << "; ";
2560
alokp@chromium.org52813552010-11-16 18:36:09 +00002561 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002562 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002563 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002564 }
2565
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002566 out << ")\n";
2567
Jamie Madill075edd82013-07-08 13:30:19 -04002568 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002569 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002570 }
2571
2572 if (node->getBody())
2573 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002574 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002575 }
2576
Jamie Madill075edd82013-07-08 13:30:19 -04002577 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002578 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002579
alokp@chromium.org52813552010-11-16 18:36:09 +00002580 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002581 {
Jamie Madill075edd82013-07-08 13:30:19 -04002582 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002583 out << "while(\n";
2584
alokp@chromium.org52813552010-11-16 18:36:09 +00002585 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002586
daniel@transgaming.com73536982012-03-21 20:45:49 +00002587 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002588 }
2589
daniel@transgaming.com73536982012-03-21 20:45:49 +00002590 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002591
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002592 mInsideDiscontinuousLoop = wasDiscontinuous;
2593
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002594 return false;
2595}
2596
2597bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2598{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002599 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002600
2601 switch (node->getFlowOp())
2602 {
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002603 case EOpKill: outputTriplet(visit, "discard;\n", "", ""); break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002604 case EOpBreak:
2605 if (visit == PreVisit)
2606 {
2607 if (mExcessiveLoopIndex)
2608 {
2609 out << "{Break";
2610 mExcessiveLoopIndex->traverse(this);
2611 out << " = true; break;}\n";
2612 }
2613 else
2614 {
2615 out << "break;\n";
2616 }
2617 }
2618 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002619 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002620 case EOpReturn:
2621 if (visit == PreVisit)
2622 {
2623 if (node->getExpression())
2624 {
2625 out << "return ";
2626 }
2627 else
2628 {
2629 out << "return;\n";
2630 }
2631 }
2632 else if (visit == PostVisit)
2633 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002634 if (node->getExpression())
2635 {
2636 out << ";\n";
2637 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002638 }
2639 break;
2640 default: UNREACHABLE();
2641 }
2642
2643 return true;
2644}
2645
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002646void OutputHLSL::traverseStatements(TIntermNode *node)
2647{
2648 if (isSingleStatement(node))
2649 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002650 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002651 }
2652
2653 node->traverse(this);
2654}
2655
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002656bool OutputHLSL::isSingleStatement(TIntermNode *node)
2657{
2658 TIntermAggregate *aggregate = node->getAsAggregate();
2659
2660 if (aggregate)
2661 {
2662 if (aggregate->getOp() == EOpSequence)
2663 {
2664 return false;
2665 }
2666 else
2667 {
2668 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2669 {
2670 if (!isSingleStatement(*sit))
2671 {
2672 return false;
2673 }
2674 }
2675
2676 return true;
2677 }
2678 }
2679
2680 return true;
2681}
2682
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002683// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2684// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002685bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2686{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002687 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002688 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002689
2690 // Parse loops of the form:
2691 // for(int index = initial; index [comparator] limit; index += increment)
2692 TIntermSymbol *index = NULL;
2693 TOperator comparator = EOpNull;
2694 int initial = 0;
2695 int limit = 0;
2696 int increment = 0;
2697
2698 // Parse index name and intial value
2699 if (node->getInit())
2700 {
2701 TIntermAggregate *init = node->getInit()->getAsAggregate();
2702
2703 if (init)
2704 {
2705 TIntermSequence &sequence = init->getSequence();
2706 TIntermTyped *variable = sequence[0]->getAsTyped();
2707
2708 if (variable && variable->getQualifier() == EvqTemporary)
2709 {
2710 TIntermBinary *assign = variable->getAsBinaryNode();
2711
2712 if (assign->getOp() == EOpInitialize)
2713 {
2714 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2715 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2716
2717 if (symbol && constant)
2718 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002719 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002720 {
2721 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002722 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002723 }
2724 }
2725 }
2726 }
2727 }
2728 }
2729
2730 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002731 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002732 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002733 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002734
2735 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2736 {
2737 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2738
2739 if (constant)
2740 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002741 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002742 {
2743 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002744 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002745 }
2746 }
2747 }
2748 }
2749
2750 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002751 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002752 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002753 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2754 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002755
2756 if (binaryTerminal)
2757 {
2758 TOperator op = binaryTerminal->getOp();
2759 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2760
2761 if (constant)
2762 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002763 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002764 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002765 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002766
2767 switch (op)
2768 {
2769 case EOpAddAssign: increment = value; break;
2770 case EOpSubAssign: increment = -value; break;
2771 default: UNIMPLEMENTED();
2772 }
2773 }
2774 }
2775 }
2776 else if (unaryTerminal)
2777 {
2778 TOperator op = unaryTerminal->getOp();
2779
2780 switch (op)
2781 {
2782 case EOpPostIncrement: increment = 1; break;
2783 case EOpPostDecrement: increment = -1; break;
2784 case EOpPreIncrement: increment = 1; break;
2785 case EOpPreDecrement: increment = -1; break;
2786 default: UNIMPLEMENTED();
2787 }
2788 }
2789 }
2790
2791 if (index != NULL && comparator != EOpNull && increment != 0)
2792 {
2793 if (comparator == EOpLessThanEqual)
2794 {
2795 comparator = EOpLessThan;
2796 limit += 1;
2797 }
2798
2799 if (comparator == EOpLessThan)
2800 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002801 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002802
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002803 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002804 {
2805 return false; // Not an excessive loop
2806 }
2807
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002808 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2809 mExcessiveLoopIndex = index;
2810
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002811 out << "{int ";
2812 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002813 out << ";\n"
2814 "bool Break";
2815 index->traverse(this);
2816 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002817
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002818 bool firstLoopFragment = true;
2819
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002820 while (iterations > 0)
2821 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002822 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002823
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002824 if (!firstLoopFragment)
2825 {
2826 out << "if(!Break";
2827 index->traverse(this);
2828 out << ") {\n";
2829 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002830
2831 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2832 {
2833 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2834 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002835
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002836 // for(int index = initial; index < clampedLimit; index += increment)
2837
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002838 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002839 index->traverse(this);
2840 out << " = ";
2841 out << initial;
2842
2843 out << "; ";
2844 index->traverse(this);
2845 out << " < ";
2846 out << clampedLimit;
2847
2848 out << "; ";
2849 index->traverse(this);
2850 out << " += ";
2851 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002852 out << ")\n";
2853
Jamie Madill075edd82013-07-08 13:30:19 -04002854 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002855 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002856
2857 if (node->getBody())
2858 {
2859 node->getBody()->traverse(this);
2860 }
2861
Jamie Madill075edd82013-07-08 13:30:19 -04002862 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002863 out << ";}\n";
2864
2865 if (!firstLoopFragment)
2866 {
2867 out << "}\n";
2868 }
2869
2870 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002871
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002872 initial += MAX_LOOP_ITERATIONS * increment;
2873 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002874 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002875
2876 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002877
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002878 mExcessiveLoopIndex = restoreIndex;
2879
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002880 return true;
2881 }
2882 else UNIMPLEMENTED();
2883 }
2884
2885 return false; // Not handled as an excessive loop
2886}
2887
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002888void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002889{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002890 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002891
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002892 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002893 {
2894 out << preString;
2895 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002896 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002897 {
2898 out << inString;
2899 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002900 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002901 {
2902 out << postString;
2903 }
2904}
2905
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002906void OutputHLSL::outputLineDirective(int line)
2907{
2908 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2909 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002910 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002911 mBody << "#line " << line;
2912
2913 if (mContext.sourcePath)
2914 {
2915 mBody << " \"" << mContext.sourcePath << "\"";
2916 }
2917
2918 mBody << "\n";
2919 }
2920}
2921
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002922TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2923{
2924 TQualifier qualifier = symbol->getQualifier();
2925 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002926 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002927
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002928 if (name.empty()) // HLSL demands named arguments, also for prototypes
2929 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002930 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002931 }
2932 else
2933 {
2934 name = decorate(name);
2935 }
2936
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002937 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2938 {
2939 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
2940 qualifierString(qualifier) + " SamplerState sampler_" + name + arrayString(type);
2941 }
2942
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002943 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002944}
2945
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00002946TString OutputHLSL::interpolationString(TQualifier qualifier)
2947{
2948 switch(qualifier)
2949 {
2950 case EvqVaryingIn: return "";
2951 case EvqInvariantVaryingIn: return "";
2952 case EvqSmoothIn: return "linear";
2953 case EvqFlatIn: return "nointerpolation";
2954 case EvqCentroidIn: return "centroid";
2955 case EvqVaryingOut: return "";
2956 case EvqInvariantVaryingOut: return "";
2957 case EvqSmoothOut: return "linear";
2958 case EvqFlatOut: return "nointerpolation";
2959 case EvqCentroidOut: return "centroid";
2960 default: UNREACHABLE();
2961 }
2962
2963 return "";
2964}
2965
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002966TString OutputHLSL::qualifierString(TQualifier qualifier)
2967{
2968 switch(qualifier)
2969 {
2970 case EvqIn: return "in";
2971 case EvqOut: return "out";
2972 case EvqInOut: return "inout";
2973 case EvqConstReadOnly: return "const";
2974 default: UNREACHABLE();
2975 }
2976
2977 return "";
2978}
2979
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002980TString OutputHLSL::typeString(const TType &type)
2981{
Jamie Madill98493dd2013-07-08 14:39:03 -04002982 const TStructure* structure = type.getStruct();
2983 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002984 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002985 const TString& typeName = structure->name();
2986 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002987 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002988 return structLookup(typeName);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002989 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00002990 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002991 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002992 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002993 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002994 }
2995 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002996 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00002997 int cols = type.getCols();
2998 int rows = type.getRows();
2999 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003000 }
3001 else
3002 {
3003 switch (type.getBasicType())
3004 {
3005 case EbtFloat:
3006 switch (type.getNominalSize())
3007 {
3008 case 1: return "float";
3009 case 2: return "float2";
3010 case 3: return "float3";
3011 case 4: return "float4";
3012 }
3013 case EbtInt:
3014 switch (type.getNominalSize())
3015 {
3016 case 1: return "int";
3017 case 2: return "int2";
3018 case 3: return "int3";
3019 case 4: return "int4";
3020 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003021 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04003022 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003023 {
3024 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003025 case 2: return "uint2";
3026 case 3: return "uint3";
3027 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003028 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003029 case EbtBool:
3030 switch (type.getNominalSize())
3031 {
3032 case 1: return "bool";
3033 case 2: return "bool2";
3034 case 3: return "bool3";
3035 case 4: return "bool4";
3036 }
3037 case EbtVoid:
3038 return "void";
3039 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003040 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04003041 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003042 case EbtSampler2DArray:
3043 case EbtISampler2DArray:
3044 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003045 return "sampler2D";
3046 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003047 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04003048 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003049 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00003050 case EbtSamplerExternalOES:
3051 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00003052 default:
3053 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003054 }
3055 }
3056
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003057 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003058 return "<unknown type>";
3059}
3060
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003061TString OutputHLSL::textureString(const TType &type)
3062{
3063 switch (type.getBasicType())
3064 {
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003065 case EbtSampler2D: return "Texture2D";
3066 case EbtSamplerCube: return "TextureCube";
3067 case EbtSamplerExternalOES: return "Texture2D";
3068 case EbtSampler2DArray: return "Texture2DArray";
Jamie Madill94c3f422013-07-03 15:16:13 -04003069 case EbtSampler3D: return "Texture3D";
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003070 case EbtISampler2D: return "Texture2D<int4>";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04003071 case EbtISampler3D: return "Texture3D<int4>";
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003072 case EbtISamplerCube: return "TextureCube<int4>";
3073 case EbtISampler2DArray: return "Texture2DArray<int4>";
3074 case EbtUSampler2D: return "Texture2D<uint4>";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04003075 case EbtUSampler3D: return "Texture3D<uint4>";
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003076 case EbtUSamplerCube: return "TextureCube<uint4>";
3077 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003078 default:
3079 break;
3080 }
3081
3082 UNREACHABLE();
3083 return "<unknown texture type>";
3084}
3085
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003086TString OutputHLSL::arrayString(const TType &type)
3087{
3088 if (!type.isArray())
3089 {
3090 return "";
3091 }
3092
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003093 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003094}
3095
3096TString OutputHLSL::initializer(const TType &type)
3097{
3098 TString string;
3099
Jamie Madill94bf7f22013-07-08 13:31:15 -04003100 size_t size = type.getObjectSize();
3101 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003102 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003103 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003104
Jamie Madill94bf7f22013-07-08 13:31:15 -04003105 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003106 {
3107 string += ", ";
3108 }
3109 }
3110
daniel@transgaming.comead23042010-04-29 03:35:36 +00003111 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003112}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003113
Jamie Madill98493dd2013-07-08 14:39:03 -04003114TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003115{
Jamie Madill98493dd2013-07-08 14:39:03 -04003116 const TFieldList &fields = structure.fields();
3117 const bool isNameless = (structure.name() == "");
3118 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003119 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3120
Jamie Madill98493dd2013-07-08 14:39:03 -04003121 TString string;
3122 string += declareString + "\n"
3123 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003124
Jamie Madillc835df62013-06-21 09:15:32 -04003125 int elementIndex = 0;
3126
Jamie Madill9cf6c072013-06-20 11:55:53 -04003127 for (unsigned int i = 0; i < fields.size(); i++)
3128 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003129 const TField &field = *fields[i];
3130 const TType &fieldType = *field.type();
3131 const TStructure *fieldStruct = fieldType.getStruct();
3132 const TString &fieldTypeString = fieldStruct ? structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) : typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003133
Jamie Madillc835df62013-06-21 09:15:32 -04003134 if (useStd140Packing)
3135 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003136 string += std140PrePaddingString(*field.type(), &elementIndex);
Jamie Madillc835df62013-06-21 09:15:32 -04003137 }
3138
Jamie Madill98493dd2013-07-08 14:39:03 -04003139 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003140
3141 if (useStd140Packing)
3142 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003143 string += std140PostPaddingString(*field.type(), useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003144 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003145 }
3146
3147 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003148 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003149
Jamie Madille4075c92013-06-21 09:15:32 -04003150 // Add remaining element index to the global map, for use with nested structs in standard layouts
3151 if (useStd140Packing)
3152 {
3153 mStd140StructElementIndexes[structName] = elementIndex;
3154 }
3155
Jamie Madill98493dd2013-07-08 14:39:03 -04003156 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003157}
3158
Jamie Madill98493dd2013-07-08 14:39:03 -04003159TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003160{
Jamie Madill98493dd2013-07-08 14:39:03 -04003161 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003162 {
3163 return "";
3164 }
3165
3166 TString prefix = "";
3167
3168 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3169 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003170
3171 if (useStd140Packing)
3172 {
3173 prefix += "std";
3174 }
3175
Jamie Madill9cf6c072013-06-20 11:55:53 -04003176 if (useHLSLRowMajorPacking)
3177 {
Jamie Madillc835df62013-06-21 09:15:32 -04003178 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003179 prefix += "rm";
3180 }
3181
Jamie Madill98493dd2013-07-08 14:39:03 -04003182 return prefix + structLookup(structure.name());
Jamie Madill9cf6c072013-06-20 11:55:53 -04003183}
3184
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003185void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003186{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003187 if (name == "")
3188 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003189 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003190 }
3191
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003192 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
3193 {
3194 return; // Already added
3195 }
3196
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003197 TType ctorType = type;
3198 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003199 ctorType.setPrecision(EbpHigh);
3200 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003201
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003202 TString ctorName = type.getStruct() ? decorate(name) : name;
3203
3204 typedef std::vector<TType> ParameterArray;
3205 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003206
Jamie Madill98493dd2013-07-08 14:39:03 -04003207 const TStructure* structure = type.getStruct();
3208 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003209 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003210 mStructNames.insert(decorate(name));
3211
Jamie Madill98493dd2013-07-08 14:39:03 -04003212 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003213
Jamie Madill98493dd2013-07-08 14:39:03 -04003214 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003215 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003216 // Add row-major packed struct for interface blocks
3217 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003218 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003219 "#pragma pack_matrix(column_major)\n";
3220
Jamie Madillc835df62013-06-21 09:15:32 -04003221 const TString &std140Prefix = "std";
Jamie Madill98493dd2013-07-08 14:39:03 -04003222 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003223
3224 const TString &std140RowMajorPrefix = "std_rm";
3225 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003226 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003227 "#pragma pack_matrix(column_major)\n";
3228
Jamie Madill98493dd2013-07-08 14:39:03 -04003229 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003230 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003231 mStructDeclarations.push_back(std140String);
3232 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003233 }
3234
Jamie Madill98493dd2013-07-08 14:39:03 -04003235 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003236 for (unsigned int i = 0; i < fields.size(); i++)
3237 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003238 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003239 }
3240 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003241 else if (parameters)
3242 {
3243 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3244 {
3245 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3246 }
3247 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003248 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003249
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003250 TString constructor;
3251
3252 if (ctorType.getStruct())
3253 {
3254 constructor += ctorName + " " + ctorName + "_ctor(";
3255 }
3256 else // Built-in type
3257 {
3258 constructor += typeString(ctorType) + " " + ctorName + "(";
3259 }
3260
3261 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3262 {
3263 const TType &type = ctorParameters[parameter];
3264
3265 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3266
3267 if (parameter < ctorParameters.size() - 1)
3268 {
3269 constructor += ", ";
3270 }
3271 }
3272
3273 constructor += ")\n"
3274 "{\n";
3275
3276 if (ctorType.getStruct())
3277 {
3278 constructor += " " + ctorName + " structure = {";
3279 }
3280 else
3281 {
3282 constructor += " return " + typeString(ctorType) + "(";
3283 }
3284
3285 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3286 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003287 int rows = ctorType.getRows();
3288 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003289 const TType &parameter = ctorParameters[0];
3290
3291 if (parameter.isScalar())
3292 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003293 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003294 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003295 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003296 {
3297 constructor += TString((row == col) ? "x0" : "0.0");
3298
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003299 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003300 {
3301 constructor += ", ";
3302 }
3303 }
3304 }
3305 }
3306 else if (parameter.isMatrix())
3307 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003308 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003309 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003310 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003311 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003312 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003313 {
3314 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3315 }
3316 else
3317 {
3318 constructor += TString((row == col) ? "1.0" : "0.0");
3319 }
3320
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003321 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003322 {
3323 constructor += ", ";
3324 }
3325 }
3326 }
3327 }
3328 else UNREACHABLE();
3329 }
3330 else
3331 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003332 size_t remainingComponents = ctorType.getObjectSize();
3333 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003334
3335 while (remainingComponents > 0)
3336 {
3337 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003338 const size_t parameterSize = parameter.getObjectSize();
3339 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003340
3341 constructor += "x" + str(parameterIndex);
3342
3343 if (parameter.isScalar())
3344 {
3345 remainingComponents -= parameter.getObjectSize();
3346 }
3347 else if (parameter.isVector())
3348 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003349 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003350 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003351 ASSERT(parameterSize <= remainingComponents);
3352 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003353 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003354 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003355 {
3356 switch (remainingComponents)
3357 {
3358 case 1: constructor += ".x"; break;
3359 case 2: constructor += ".xy"; break;
3360 case 3: constructor += ".xyz"; break;
3361 case 4: constructor += ".xyzw"; break;
3362 default: UNREACHABLE();
3363 }
3364
3365 remainingComponents = 0;
3366 }
3367 else UNREACHABLE();
3368 }
3369 else if (parameter.isMatrix() || parameter.getStruct())
3370 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003371 ASSERT(remainingComponents == parameterSize || moreParameters);
3372 ASSERT(parameterSize <= remainingComponents);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003373
Jamie Madill94bf7f22013-07-08 13:31:15 -04003374 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003375 }
3376 else UNREACHABLE();
3377
3378 if (moreParameters)
3379 {
3380 parameterIndex++;
3381 }
3382
3383 if (remainingComponents)
3384 {
3385 constructor += ", ";
3386 }
3387 }
3388 }
3389
3390 if (ctorType.getStruct())
3391 {
3392 constructor += "};\n"
3393 " return structure;\n"
3394 "}\n";
3395 }
3396 else
3397 {
3398 constructor += ");\n"
3399 "}\n";
3400 }
3401
daniel@transgaming.com63691862010-04-29 03:32:42 +00003402 mConstructors.insert(constructor);
3403}
3404
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003405const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3406{
3407 TInfoSinkBase &out = mBody;
3408
Jamie Madill98493dd2013-07-08 14:39:03 -04003409 const TStructure* structure = type.getStruct();
3410 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003411 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003412 out << structLookup(structure->name()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003413
Jamie Madill98493dd2013-07-08 14:39:03 -04003414 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003415
Jamie Madill98493dd2013-07-08 14:39:03 -04003416 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003417 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003418 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003419
3420 constUnion = writeConstantUnion(*fieldType, constUnion);
3421
Jamie Madill98493dd2013-07-08 14:39:03 -04003422 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003423 {
3424 out << ", ";
3425 }
3426 }
3427
3428 out << ")";
3429 }
3430 else
3431 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003432 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003433 bool writeType = size > 1;
3434
3435 if (writeType)
3436 {
3437 out << typeString(type) << "(";
3438 }
3439
Jamie Madill94bf7f22013-07-08 13:31:15 -04003440 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003441 {
3442 switch (constUnion->getType())
3443 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003444 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003445 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003446 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003447 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003448 default: UNREACHABLE();
3449 }
3450
3451 if (i != size - 1)
3452 {
3453 out << ", ";
3454 }
3455 }
3456
3457 if (writeType)
3458 {
3459 out << ")";
3460 }
3461 }
3462
3463 return constUnion;
3464}
3465
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003466TString OutputHLSL::scopeString(unsigned int depthLimit)
3467{
3468 TString string;
3469
3470 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3471 {
3472 string += "_" + str(i);
3473 }
3474
3475 return string;
3476}
3477
3478TString OutputHLSL::scopedStruct(const TString &typeName)
3479{
3480 if (typeName == "")
3481 {
3482 return typeName;
3483 }
3484
3485 return typeName + scopeString(mScopeDepth);
3486}
3487
3488TString OutputHLSL::structLookup(const TString &typeName)
3489{
3490 for (int depth = mScopeDepth; depth >= 0; depth--)
3491 {
3492 TString scopedName = decorate(typeName + scopeString(depth));
3493
3494 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3495 {
3496 if (*structName == scopedName)
3497 {
3498 return scopedName;
3499 }
3500 }
3501 }
3502
3503 UNREACHABLE(); // Should have found a matching constructor
3504
3505 return typeName;
3506}
3507
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003508TString OutputHLSL::decorate(const TString &string)
3509{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003510 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003511 {
3512 return "_" + string;
3513 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003514
3515 return string;
3516}
3517
apatrick@chromium.org65756022012-01-17 21:45:38 +00003518TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003519{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003520 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003521 {
3522 return "ex_" + string;
3523 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003524
3525 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003526}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003527
Jamie Madill98493dd2013-07-08 14:39:03 -04003528TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003529{
Jamie Madill98493dd2013-07-08 14:39:03 -04003530 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003531 {
3532 return decorate(string);
3533 }
3534
3535 return string;
3536}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003537
3538TString OutputHLSL::registerString(TIntermSymbol *operand)
3539{
3540 ASSERT(operand->getQualifier() == EvqUniform);
3541
3542 if (IsSampler(operand->getBasicType()))
3543 {
3544 return "s" + str(samplerRegister(operand));
3545 }
3546
3547 return "c" + str(uniformRegister(operand));
3548}
3549
3550int OutputHLSL::samplerRegister(TIntermSymbol *sampler)
3551{
3552 const TType &type = sampler->getType();
3553 ASSERT(IsSampler(type.getBasicType()));
3554
3555 int index = mSamplerRegister;
3556 mSamplerRegister += sampler->totalRegisterCount();
3557
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003558 declareUniform(type, sampler->getSymbol(), index);
3559
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003560 return index;
3561}
3562
3563int OutputHLSL::uniformRegister(TIntermSymbol *uniform)
3564{
3565 const TType &type = uniform->getType();
3566 ASSERT(!IsSampler(type.getBasicType()));
3567
3568 int index = mUniformRegister;
3569 mUniformRegister += uniform->totalRegisterCount();
3570
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003571 declareUniform(type, uniform->getSymbol(), index);
3572
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003573 return index;
3574}
3575
Jamie Madill98493dd2013-07-08 14:39:03 -04003576void OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, ActiveUniforms& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003577{
Jamie Madill98493dd2013-07-08 14:39:03 -04003578 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003579
3580 if (!structure)
3581 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003582 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill98493dd2013-07-08 14:39:03 -04003583 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 +00003584 }
3585 else
3586 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003587 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 +00003588
Jamie Madill98493dd2013-07-08 14:39:03 -04003589 int fieldRegister = registerIndex;
3590 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003591
Jamie Madill98493dd2013-07-08 14:39:03 -04003592 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003593 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003594 TField *field = fields[fieldIndex];
3595 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003596
Jamie Madill010fffa2013-06-20 11:55:53 -04003597 // make sure to copy matrix packing information
Jamie Madill98493dd2013-07-08 14:39:03 -04003598 fieldType->setLayoutQualifier(type.getLayoutQualifier());
Jamie Madill010fffa2013-06-20 11:55:53 -04003599
Jamie Madill98493dd2013-07-08 14:39:03 -04003600 declareUniformToList(*fieldType, field->name(), fieldRegister, structUniform.fields);
3601 fieldRegister += fieldType->totalRegisterCount();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003602 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003603
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003604 output.push_back(structUniform);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003605 }
3606}
3607
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003608void OutputHLSL::declareUniform(const TType &type, const TString &name, int index)
3609{
3610 declareUniformToList(type, name, index, mActiveUniforms);
3611}
3612
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003613GLenum OutputHLSL::glVariableType(const TType &type)
3614{
3615 if (type.getBasicType() == EbtFloat)
3616 {
3617 if (type.isScalar())
3618 {
3619 return GL_FLOAT;
3620 }
3621 else if (type.isVector())
3622 {
3623 switch(type.getNominalSize())
3624 {
3625 case 2: return GL_FLOAT_VEC2;
3626 case 3: return GL_FLOAT_VEC3;
3627 case 4: return GL_FLOAT_VEC4;
3628 default: UNREACHABLE();
3629 }
3630 }
3631 else if (type.isMatrix())
3632 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003633 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003634 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003635 case 2:
3636 switch(type.getRows())
3637 {
3638 case 2: return GL_FLOAT_MAT2;
3639 case 3: return GL_FLOAT_MAT2x3;
3640 case 4: return GL_FLOAT_MAT2x4;
3641 default: UNREACHABLE();
3642 }
3643
3644 case 3:
3645 switch(type.getRows())
3646 {
3647 case 2: return GL_FLOAT_MAT3x2;
3648 case 3: return GL_FLOAT_MAT3;
3649 case 4: return GL_FLOAT_MAT3x4;
3650 default: UNREACHABLE();
3651 }
3652
3653 case 4:
3654 switch(type.getRows())
3655 {
3656 case 2: return GL_FLOAT_MAT4x2;
3657 case 3: return GL_FLOAT_MAT4x3;
3658 case 4: return GL_FLOAT_MAT4;
3659 default: UNREACHABLE();
3660 }
3661
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003662 default: UNREACHABLE();
3663 }
3664 }
3665 else UNREACHABLE();
3666 }
3667 else if (type.getBasicType() == EbtInt)
3668 {
3669 if (type.isScalar())
3670 {
3671 return GL_INT;
3672 }
3673 else if (type.isVector())
3674 {
3675 switch(type.getNominalSize())
3676 {
3677 case 2: return GL_INT_VEC2;
3678 case 3: return GL_INT_VEC3;
3679 case 4: return GL_INT_VEC4;
3680 default: UNREACHABLE();
3681 }
3682 }
3683 else UNREACHABLE();
3684 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003685 else if (type.getBasicType() == EbtUInt)
3686 {
3687 if (type.isScalar())
3688 {
3689 return GL_UNSIGNED_INT;
3690 }
3691 else if (type.isVector())
3692 {
Jamie Madill22d63da2013-06-07 12:45:12 -04003693 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003694 {
3695 case 2: return GL_UNSIGNED_INT_VEC2;
3696 case 3: return GL_UNSIGNED_INT_VEC3;
3697 case 4: return GL_UNSIGNED_INT_VEC4;
3698 default: UNREACHABLE();
3699 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003700 }
3701 else UNREACHABLE();
3702 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003703 else if (type.getBasicType() == EbtBool)
3704 {
3705 if (type.isScalar())
3706 {
3707 return GL_BOOL;
3708 }
3709 else if (type.isVector())
3710 {
3711 switch(type.getNominalSize())
3712 {
3713 case 2: return GL_BOOL_VEC2;
3714 case 3: return GL_BOOL_VEC3;
3715 case 4: return GL_BOOL_VEC4;
3716 default: UNREACHABLE();
3717 }
3718 }
3719 else UNREACHABLE();
3720 }
3721 else if (type.getBasicType() == EbtSampler2D)
3722 {
3723 return GL_SAMPLER_2D;
3724 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04003725 else if (type.getBasicType() == EbtSampler3D)
3726 {
3727 return GL_SAMPLER_3D;
3728 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003729 else if (type.getBasicType() == EbtSamplerCube)
3730 {
3731 return GL_SAMPLER_CUBE;
3732 }
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003733 else if (type.getBasicType() == EbtSampler2DArray)
3734 {
3735 return GL_SAMPLER_2D_ARRAY;
3736 }
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003737 else if (type.getBasicType() == EbtISampler2D)
3738 {
3739 return GL_INT_SAMPLER_2D;
3740 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04003741 else if (type.getBasicType() == EbtISampler3D)
3742 {
3743 return GL_INT_SAMPLER_3D;
3744 }
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003745 else if (type.getBasicType() == EbtISamplerCube)
3746 {
3747 return GL_INT_SAMPLER_CUBE;
3748 }
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003749 else if (type.getBasicType() == EbtISampler2DArray)
3750 {
3751 return GL_INT_SAMPLER_2D_ARRAY;
3752 }
Nicolas Capens075368e2013-06-24 15:58:30 -04003753 else if (type.getBasicType() == EbtUSampler2D)
3754 {
3755 return GL_UNSIGNED_INT_SAMPLER_2D;
3756 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04003757 else if (type.getBasicType() == EbtUSampler3D)
3758 {
3759 return GL_UNSIGNED_INT_SAMPLER_3D;
3760 }
Nicolas Capens075368e2013-06-24 15:58:30 -04003761 else if (type.getBasicType() == EbtUSamplerCube)
3762 {
3763 return GL_UNSIGNED_INT_SAMPLER_CUBE;
3764 }
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003765 else if (type.getBasicType() == EbtUSampler2DArray)
3766 {
3767 return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3768 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003769 else UNREACHABLE();
3770
3771 return GL_NONE;
3772}
3773
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003774GLenum OutputHLSL::glVariablePrecision(const TType &type)
3775{
3776 if (type.getBasicType() == EbtFloat)
3777 {
3778 switch (type.getPrecision())
3779 {
3780 case EbpHigh: return GL_HIGH_FLOAT;
3781 case EbpMedium: return GL_MEDIUM_FLOAT;
3782 case EbpLow: return GL_LOW_FLOAT;
3783 case EbpUndefined:
3784 // Should be defined as the default precision by the parser
3785 default: UNREACHABLE();
3786 }
3787 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003788 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003789 {
3790 switch (type.getPrecision())
3791 {
3792 case EbpHigh: return GL_HIGH_INT;
3793 case EbpMedium: return GL_MEDIUM_INT;
3794 case EbpLow: return GL_LOW_INT;
3795 case EbpUndefined:
3796 // Should be defined as the default precision by the parser
3797 default: UNREACHABLE();
3798 }
3799 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003800
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00003801 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003802 return GL_NONE;
3803}
3804
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003805bool OutputHLSL::isVaryingOut(TQualifier qualifier)
3806{
3807 switch(qualifier)
3808 {
3809 case EvqVaryingOut:
3810 case EvqInvariantVaryingOut:
3811 case EvqSmoothOut:
3812 case EvqFlatOut:
3813 case EvqCentroidOut:
3814 return true;
3815 }
3816
3817 return false;
3818}
3819
3820bool OutputHLSL::isVaryingIn(TQualifier qualifier)
3821{
3822 switch(qualifier)
3823 {
3824 case EvqVaryingIn:
3825 case EvqInvariantVaryingIn:
3826 case EvqSmoothIn:
3827 case EvqFlatIn:
3828 case EvqCentroidIn:
3829 return true;
3830 }
3831
3832 return false;
3833}
3834
3835bool OutputHLSL::isVarying(TQualifier qualifier)
3836{
3837 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
3838}
3839
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003840}