blob: 9e24dcea9c51e957e748944c796a248d8af94c99 [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;
Nicolas Capens93e50de2013-07-09 13:46:28 -0400934 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capensfb50dff2013-06-24 16:16:23 -0400935 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;
Nicolas Capens93e50de2013-07-09 13:46:28 -0400938 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capensfb50dff2013-06-24 16:16:23 -0400939 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;
Nicolas Capens93e50de2013-07-09 13:46:28 -0400942 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; 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 Capens93e50de2013-07-09 13:46:28 -0400988 if (IsSamplerArray(textureFunction->sampler))
989 {
990 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
991 " x.GetDimensions(0, width, height, layers, numberOfLevels);\n";
992 }
993 else
994 {
995 out << " uint width; uint height; uint numberOfLevels;\n"
996 " x.GetDimensions(0, width, height, numberOfLevels);\n";
997 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400998 }
999 else if (IsSampler3D(textureFunction->sampler))
1000 {
1001 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
1002 " x.GetDimensions(0, width, height, depth, numberOfLevels);\n";
1003 }
1004 else UNREACHABLE();
1005 }
1006
1007 out << " return ";
1008
1009 // HLSL intrinsic
1010 if (mOutputType == SH_HLSL9_OUTPUT)
1011 {
1012 switch(textureFunction->sampler)
1013 {
1014 case EbtSampler2D: out << "tex2D"; break;
1015 case EbtSamplerCube: out << "texCUBE"; break;
1016 default: UNREACHABLE();
1017 }
1018
1019 switch(textureFunction->mipmap)
1020 {
1021 case TextureFunction::IMPLICIT: out << "(s, "; break;
1022 case TextureFunction::BIAS: out << "bias(s, "; break;
1023 case TextureFunction::LOD: out << "lod(s, "; break;
1024 case TextureFunction::LOD0: out << "lod(s, "; break;
1025 default: UNREACHABLE();
1026 }
1027 }
1028 else if (mOutputType == SH_HLSL11_OUTPUT)
1029 {
1030 if (IsIntegerSampler(textureFunction->sampler))
1031 {
1032 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001033 }
1034 else
1035 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001036 switch(textureFunction->mipmap)
1037 {
1038 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1039 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1040 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1041 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
1042 default: UNREACHABLE();
1043 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001044 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001045 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001046 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001047
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001048 // Integer sampling requires integer addresses
1049 TString addressx = "";
1050 TString addressy = "";
1051 TString addressz = "";
1052 TString close = "";
1053
1054 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001055 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001056 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001057 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001058 case 2: out << "int3("; break;
1059 case 3: out << "int4("; break;
1060 default: UNREACHABLE();
1061 }
1062
Nicolas Capens93e50de2013-07-09 13:46:28 -04001063 addressx = "int(floor(float(width) * frac((";
1064 addressy = "int(floor(float(height) * frac((";
1065
1066 if (IsSamplerArray(textureFunction->sampler))
1067 {
1068 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1069 }
1070 else
1071 {
1072 addressz = "int(floor(float(depth) * frac((";
1073 }
1074
1075 close = "))))";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001076 }
1077 else
1078 {
1079 switch(hlslCoords)
1080 {
1081 case 2: out << "float2("; break;
1082 case 3: out << "float3("; break;
1083 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001084 default: UNREACHABLE();
1085 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001086 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001087
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001088 TString proj = ""; // Only used for projected textures
1089
1090 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001091 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001092 switch(textureFunction->coords)
1093 {
1094 case 3: proj = " / t.z"; break;
1095 case 4: proj = " / t.w"; break;
1096 default: UNREACHABLE();
1097 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001098 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001099
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001100 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001101
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001102 if (mOutputType == SH_HLSL9_OUTPUT)
1103 {
1104 if (hlslCoords >= 3)
1105 {
1106 if (textureFunction->coords < 3)
1107 {
1108 out << ", 0";
1109 }
1110 else
1111 {
1112 out << ", t.z" + proj;
1113 }
1114 }
1115
1116 if (hlslCoords == 4)
1117 {
1118 switch(textureFunction->mipmap)
1119 {
1120 case TextureFunction::BIAS: out << ", bias"; break;
1121 case TextureFunction::LOD: out << ", lod"; break;
1122 case TextureFunction::LOD0: out << ", 0"; break;
1123 default: UNREACHABLE();
1124 }
1125 }
1126
1127 out << "));\n";
1128 }
1129 else if (mOutputType == SH_HLSL11_OUTPUT)
1130 {
1131 if (hlslCoords >= 3)
1132 {
1133 out << ", " + addressz + ("t.z" + proj) + close;
1134 }
1135
1136 if (!IsIntegerSampler(textureFunction->sampler))
1137 {
1138 switch(textureFunction->mipmap)
1139 {
1140 case TextureFunction::IMPLICIT: out << "));"; break;
1141 case TextureFunction::BIAS: out << "), bias);"; break;
1142 case TextureFunction::LOD: out << "), lod);"; break;
1143 case TextureFunction::LOD0: out << "), 0);"; break;
1144 default: UNREACHABLE();
1145 }
1146 }
1147 else
1148 {
1149 out << ", 0));"; // Sample from the top level
1150 }
1151 }
1152 else UNREACHABLE();
1153 }
1154
1155 out << "\n"
1156 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001157 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001158 }
1159
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001160 if (mUsesFragCoord)
1161 {
1162 out << "#define GL_USES_FRAG_COORD\n";
1163 }
1164
1165 if (mUsesPointCoord)
1166 {
1167 out << "#define GL_USES_POINT_COORD\n";
1168 }
1169
1170 if (mUsesFrontFacing)
1171 {
1172 out << "#define GL_USES_FRONT_FACING\n";
1173 }
1174
1175 if (mUsesPointSize)
1176 {
1177 out << "#define GL_USES_POINT_SIZE\n";
1178 }
1179
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001180 if (mUsesFragDepth)
1181 {
1182 out << "#define GL_USES_FRAG_DEPTH\n";
1183 }
1184
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001185 if (mUsesDepthRange)
1186 {
1187 out << "#define GL_USES_DEPTH_RANGE\n";
1188 }
1189
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001190 if (mUsesXor)
1191 {
1192 out << "bool xor(bool p, bool q)\n"
1193 "{\n"
1194 " return (p || q) && !(p && q);\n"
1195 "}\n"
1196 "\n";
1197 }
1198
1199 if (mUsesMod1)
1200 {
1201 out << "float mod(float x, float y)\n"
1202 "{\n"
1203 " return x - y * floor(x / y);\n"
1204 "}\n"
1205 "\n";
1206 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001207
1208 if (mUsesMod2v)
1209 {
1210 out << "float2 mod(float2 x, float2 y)\n"
1211 "{\n"
1212 " return x - y * floor(x / y);\n"
1213 "}\n"
1214 "\n";
1215 }
1216
1217 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001218 {
1219 out << "float2 mod(float2 x, float y)\n"
1220 "{\n"
1221 " return x - y * floor(x / y);\n"
1222 "}\n"
1223 "\n";
1224 }
1225
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001226 if (mUsesMod3v)
1227 {
1228 out << "float3 mod(float3 x, float3 y)\n"
1229 "{\n"
1230 " return x - y * floor(x / y);\n"
1231 "}\n"
1232 "\n";
1233 }
1234
1235 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001236 {
1237 out << "float3 mod(float3 x, float y)\n"
1238 "{\n"
1239 " return x - y * floor(x / y);\n"
1240 "}\n"
1241 "\n";
1242 }
1243
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001244 if (mUsesMod4v)
1245 {
1246 out << "float4 mod(float4 x, float4 y)\n"
1247 "{\n"
1248 " return x - y * floor(x / y);\n"
1249 "}\n"
1250 "\n";
1251 }
1252
1253 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001254 {
1255 out << "float4 mod(float4 x, float y)\n"
1256 "{\n"
1257 " return x - y * floor(x / y);\n"
1258 "}\n"
1259 "\n";
1260 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001261
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001262 if (mUsesFaceforward1)
1263 {
1264 out << "float faceforward(float N, float I, float Nref)\n"
1265 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001266 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001267 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001268 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001269 " }\n"
1270 " else\n"
1271 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001272 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001273 " }\n"
1274 "}\n"
1275 "\n";
1276 }
1277
1278 if (mUsesFaceforward2)
1279 {
1280 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1281 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001282 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001283 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001284 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001285 " }\n"
1286 " else\n"
1287 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001288 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001289 " }\n"
1290 "}\n"
1291 "\n";
1292 }
1293
1294 if (mUsesFaceforward3)
1295 {
1296 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1297 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001298 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001299 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001300 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001301 " }\n"
1302 " else\n"
1303 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001304 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001305 " }\n"
1306 "}\n"
1307 "\n";
1308 }
1309
1310 if (mUsesFaceforward4)
1311 {
1312 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1313 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001314 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001315 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001316 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001317 " }\n"
1318 " else\n"
1319 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001320 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001321 " }\n"
1322 "}\n"
1323 "\n";
1324 }
1325
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001326 for (unsigned int cols = 2; cols <= 4; cols++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001327 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001328 for (unsigned int rows = 2; rows <= 4; rows++)
1329 {
1330 if (mUsesEqualMat[cols][rows])
1331 {
1332 TString matrixType = "float" + str(cols) + "x" + str(rows);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001333
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001334 out << "bool equal(" + matrixType + " m, " + matrixType + " n)\n"
1335 "{\n";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001336
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001337 for (unsigned int row = 0; row < rows; row++)
1338 {
1339 if (row == 0)
1340 {
1341 out << " return ";
1342 }
1343 else
1344 {
1345 out << " ";
1346 }
1347
1348 for (unsigned int col = 0; col < cols; col++)
1349 {
1350 TString index = "[" + str(col) + "][" + str(row) + "]";
1351 out << "m" + index + " == n" + index;
1352
1353 if (col == cols-1 && row == rows-1)
1354 {
1355 out << ";\n";
1356 }
1357 else if (col == cols-1)
1358 {
1359 out << " &&\n";
1360 }
1361 else
1362 {
1363 out << " && ";
1364 }
1365 }
1366 }
1367
1368 out << "}\n";
1369 }
1370 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001371 }
1372
1373 if (mUsesEqualVec2)
1374 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001375 out << "bool equal(float2 v, float2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001376 "{\n"
1377 " return v.x == u.x && v.y == u.y;\n"
1378 "}\n";
1379 }
1380
1381 if (mUsesEqualVec3)
1382 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001383 out << "bool equal(float3 v, float3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001384 "{\n"
1385 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1386 "}\n";
1387 }
1388
1389 if (mUsesEqualVec4)
1390 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001391 out << "bool equal(float4 v, float4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001392 "{\n"
1393 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1394 "}\n";
1395 }
1396
1397 if (mUsesEqualIVec2)
1398 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001399 out << "bool equal(int2 v, int2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001400 "{\n"
1401 " return v.x == u.x && v.y == u.y;\n"
1402 "}\n";
1403 }
1404
1405 if (mUsesEqualIVec3)
1406 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001407 out << "bool equal(int3 v, int3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001408 "{\n"
1409 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1410 "}\n";
1411 }
1412
1413 if (mUsesEqualIVec4)
1414 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001415 out << "bool equal(int4 v, int4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001416 "{\n"
1417 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1418 "}\n";
1419 }
1420
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001421 if (mUsesEqualUVec2)
1422 {
1423 out << "bool equal(uint2 v, uint2 u)\n"
1424 "{\n"
1425 " return v.x == u.x && v.y == u.y;\n"
1426 "}\n";
1427 }
1428
1429 if (mUsesEqualUVec3)
1430 {
1431 out << "bool equal(uint3 v, uint3 u)\n"
1432 "{\n"
1433 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1434 "}\n";
1435 }
1436
1437 if (mUsesEqualUVec4)
1438 {
1439 out << "bool equal(uint4 v, uint4 u)\n"
1440 "{\n"
1441 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1442 "}\n";
1443 }
1444
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001445 if (mUsesEqualBVec2)
1446 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001447 out << "bool equal(bool2 v, bool2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001448 "{\n"
1449 " return v.x == u.x && v.y == u.y;\n"
1450 "}\n";
1451 }
1452
1453 if (mUsesEqualBVec3)
1454 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001455 out << "bool equal(bool3 v, bool3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001456 "{\n"
1457 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1458 "}\n";
1459 }
1460
1461 if (mUsesEqualBVec4)
1462 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001463 out << "bool equal(bool4 v, bool4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001464 "{\n"
1465 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1466 "}\n";
1467 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001468
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001469 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001470 {
1471 out << "float atanyx(float y, float x)\n"
1472 "{\n"
1473 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1474 " return atan2(y, x);\n"
1475 "}\n";
1476 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001477
1478 if (mUsesAtan2_2)
1479 {
1480 out << "float2 atanyx(float2 y, float2 x)\n"
1481 "{\n"
1482 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1483 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1484 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1485 "}\n";
1486 }
1487
1488 if (mUsesAtan2_3)
1489 {
1490 out << "float3 atanyx(float3 y, float3 x)\n"
1491 "{\n"
1492 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1493 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1494 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1495 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1496 "}\n";
1497 }
1498
1499 if (mUsesAtan2_4)
1500 {
1501 out << "float4 atanyx(float4 y, float4 x)\n"
1502 "{\n"
1503 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1504 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1505 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1506 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1507 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1508 "}\n";
1509 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001510}
1511
1512void OutputHLSL::visitSymbol(TIntermSymbol *node)
1513{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001514 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001515
Jamie Madill570e04d2013-06-21 09:15:33 -04001516 // Handle accessing std140 structs by value
1517 if (mFlaggedStructMappedNames.count(node) > 0)
1518 {
1519 out << mFlaggedStructMappedNames[node];
1520 return;
1521 }
1522
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001523 TString name = node->getSymbol();
1524
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001525 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001526 {
1527 mUsesDepthRange = true;
1528 out << name;
1529 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001530 else
1531 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001532 TQualifier qualifier = node->getQualifier();
1533
1534 if (qualifier == EvqUniform)
1535 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001536 const TType& nodeType = node->getType();
1537 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1538
1539 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001540 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001541 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001542 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001543 else
1544 {
1545 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001546 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001547
1548 out << decorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001549 }
Jamie Madillb120eac2013-06-12 14:08:13 -04001550 else if (qualifier == EvqAttribute || qualifier == EvqVertexInput)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001551 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001552 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001553 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001554 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001555 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001556 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001557 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001558 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001559 }
Jamie Madill46131a32013-06-20 11:55:50 -04001560 else if (qualifier == EvqFragmentOutput)
1561 {
1562 mReferencedOutputVariables[name] = node;
1563 out << "out_" << name;
1564 }
1565 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001566 {
1567 out << "gl_Color[0]";
1568 mUsesFragColor = true;
1569 }
1570 else if (qualifier == EvqFragData)
1571 {
1572 out << "gl_Color";
1573 mUsesFragData = true;
1574 }
1575 else if (qualifier == EvqFragCoord)
1576 {
1577 mUsesFragCoord = true;
1578 out << name;
1579 }
1580 else if (qualifier == EvqPointCoord)
1581 {
1582 mUsesPointCoord = true;
1583 out << name;
1584 }
1585 else if (qualifier == EvqFrontFacing)
1586 {
1587 mUsesFrontFacing = true;
1588 out << name;
1589 }
1590 else if (qualifier == EvqPointSize)
1591 {
1592 mUsesPointSize = true;
1593 out << name;
1594 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001595 else if (name == "gl_FragDepthEXT")
1596 {
1597 mUsesFragDepth = true;
1598 out << "gl_Depth";
1599 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001600 else
1601 {
1602 out << decorate(name);
1603 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001604 }
1605}
1606
1607bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1608{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001609 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001610
Jamie Madill570e04d2013-06-21 09:15:33 -04001611 // Handle accessing std140 structs by value
1612 if (mFlaggedStructMappedNames.count(node) > 0)
1613 {
1614 out << mFlaggedStructMappedNames[node];
1615 return false;
1616 }
1617
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001618 switch (node->getOp())
1619 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001620 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001621 case EOpInitialize:
1622 if (visit == PreVisit)
1623 {
1624 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1625 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1626 // new variable is created before the assignment is evaluated), so we need to convert
1627 // this to "float t = x, x = t;".
1628
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001629 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1630 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001631
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001632 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1633 expression->traverse(&searchSymbol);
1634 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001635
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001636 if (sameSymbol)
1637 {
1638 // Type already printed
1639 out << "t" + str(mUniqueIndex) + " = ";
1640 expression->traverse(this);
1641 out << ", ";
1642 symbolNode->traverse(this);
1643 out << " = t" + str(mUniqueIndex);
1644
1645 mUniqueIndex++;
1646 return false;
1647 }
1648 }
1649 else if (visit == InVisit)
1650 {
1651 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001652 }
1653 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001654 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1655 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1656 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1657 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1658 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1659 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001660 if (visit == PreVisit)
1661 {
1662 out << "(";
1663 }
1664 else if (visit == InVisit)
1665 {
1666 out << " = mul(";
1667 node->getLeft()->traverse(this);
1668 out << ", transpose(";
1669 }
1670 else
1671 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001672 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001673 }
1674 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001675 case EOpMatrixTimesMatrixAssign:
1676 if (visit == PreVisit)
1677 {
1678 out << "(";
1679 }
1680 else if (visit == InVisit)
1681 {
1682 out << " = mul(";
1683 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001684 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001685 }
1686 else
1687 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001688 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001689 }
1690 break;
1691 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001692 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001693 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001694 const TType& leftType = node->getLeft()->getType();
1695 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001696 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001697 if (visit == PreVisit)
1698 {
1699 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1700 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1701
1702 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1703 out << interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1704
1705 return false;
1706 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001707 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001708 else
1709 {
1710 outputTriplet(visit, "", "[", "]");
1711 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001712 }
1713 break;
1714 case EOpIndexIndirect:
1715 // We do not currently support indirect references to interface blocks
1716 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1717 outputTriplet(visit, "", "[", "]");
1718 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001719 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001720 if (visit == InVisit)
1721 {
1722 const TStructure* structure = node->getLeft()->getType().getStruct();
1723 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1724 const TField* field = structure->fields()[index->getIConst(0)];
1725 out << "." + decorateField(field->name(), *structure);
1726
1727 return false;
1728 }
1729 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001730 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001731 if (visit == InVisit)
1732 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001733 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1734 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1735 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
1736 out << "." + decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001737
1738 return false;
1739 }
1740 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001741 case EOpVectorSwizzle:
1742 if (visit == InVisit)
1743 {
1744 out << ".";
1745
1746 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1747
1748 if (swizzle)
1749 {
1750 TIntermSequence &sequence = swizzle->getSequence();
1751
1752 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1753 {
1754 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1755
1756 if (element)
1757 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001758 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001759
1760 switch (i)
1761 {
1762 case 0: out << "x"; break;
1763 case 1: out << "y"; break;
1764 case 2: out << "z"; break;
1765 case 3: out << "w"; break;
1766 default: UNREACHABLE();
1767 }
1768 }
1769 else UNREACHABLE();
1770 }
1771 }
1772 else UNREACHABLE();
1773
1774 return false; // Fully processed
1775 }
1776 break;
1777 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1778 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1779 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1780 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001781 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001782 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001783 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001784 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001785 if (node->getOp() == EOpEqual)
1786 {
1787 outputTriplet(visit, "(", " == ", ")");
1788 }
1789 else
1790 {
1791 outputTriplet(visit, "(", " != ", ")");
1792 }
1793 }
1794 else if (node->getLeft()->getBasicType() == EbtStruct)
1795 {
1796 if (node->getOp() == EOpEqual)
1797 {
1798 out << "(";
1799 }
1800 else
1801 {
1802 out << "!(";
1803 }
1804
Jamie Madill98493dd2013-07-08 14:39:03 -04001805 const TStructure &structure = *node->getLeft()->getType().getStruct();
1806 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001807
Jamie Madill98493dd2013-07-08 14:39:03 -04001808 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001809 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001810 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001811
1812 node->getLeft()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001813 out << "." + decorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001814 node->getRight()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001815 out << "." + decorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001816
Jamie Madill98493dd2013-07-08 14:39:03 -04001817 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001818 {
1819 out << " && ";
1820 }
1821 }
1822
1823 out << ")";
1824
1825 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001826 }
1827 else
1828 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001829 if (node->getLeft()->isMatrix())
1830 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001831 mUsesEqualMat[node->getLeft()->getCols()][node->getLeft()->getRows()] = true;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001832 }
1833 else if (node->getLeft()->isVector())
1834 {
1835 switch (node->getLeft()->getBasicType())
1836 {
1837 case EbtFloat:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001838 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001839 {
1840 case 2: mUsesEqualVec2 = true; break;
1841 case 3: mUsesEqualVec3 = true; break;
1842 case 4: mUsesEqualVec4 = true; break;
1843 default: UNREACHABLE();
1844 }
1845 break;
1846 case EbtInt:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001847 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001848 {
1849 case 2: mUsesEqualIVec2 = true; break;
1850 case 3: mUsesEqualIVec3 = true; break;
1851 case 4: mUsesEqualIVec4 = true; break;
1852 default: UNREACHABLE();
1853 }
1854 break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001855 case EbtUInt:
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001856 switch (node->getLeft()->getNominalSize())
1857 {
1858 case 2: mUsesEqualUVec2 = true; break;
1859 case 3: mUsesEqualUVec3 = true; break;
1860 case 4: mUsesEqualUVec4 = true; break;
1861 default: UNREACHABLE();
1862 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001863 break;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001864 case EbtBool:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001865 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001866 {
1867 case 2: mUsesEqualBVec2 = true; break;
1868 case 3: mUsesEqualBVec3 = true; break;
1869 case 4: mUsesEqualBVec4 = true; break;
1870 default: UNREACHABLE();
1871 }
1872 break;
1873 default: UNREACHABLE();
1874 }
1875 }
1876 else UNREACHABLE();
1877
1878 if (node->getOp() == EOpEqual)
1879 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001880 outputTriplet(visit, "equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001881 }
1882 else
1883 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001884 outputTriplet(visit, "!equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001885 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001886 }
1887 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001888 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1889 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1890 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1891 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1892 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001893 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001894 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1895 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001896 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001897 case EOpLogicalOr:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001898 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001899 return false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001900 case EOpLogicalXor:
1901 mUsesXor = true;
1902 outputTriplet(visit, "xor(", ", ", ")");
1903 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001904 case EOpLogicalAnd:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001905 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001906 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001907 default: UNREACHABLE();
1908 }
1909
1910 return true;
1911}
1912
1913bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1914{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001915 switch (node->getOp())
1916 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001917 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1918 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1919 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1920 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1921 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1922 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1923 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001924 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04001925 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001926 case EOpConvFloatToBool:
1927 switch (node->getOperand()->getType().getNominalSize())
1928 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001929 case 1: outputTriplet(visit, "bool(", "", ")"); break;
1930 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
1931 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
1932 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001933 default: UNREACHABLE();
1934 }
1935 break;
1936 case EOpConvBoolToFloat:
1937 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04001938 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001939 switch (node->getOperand()->getType().getNominalSize())
1940 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001941 case 1: outputTriplet(visit, "float(", "", ")"); break;
1942 case 2: outputTriplet(visit, "float2(", "", ")"); break;
1943 case 3: outputTriplet(visit, "float3(", "", ")"); break;
1944 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001945 default: UNREACHABLE();
1946 }
1947 break;
1948 case EOpConvFloatToInt:
1949 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04001950 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001951 switch (node->getOperand()->getType().getNominalSize())
1952 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001953 case 1: outputTriplet(visit, "int(", "", ")"); break;
1954 case 2: outputTriplet(visit, "int2(", "", ")"); break;
1955 case 3: outputTriplet(visit, "int3(", "", ")"); break;
1956 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001957 default: UNREACHABLE();
1958 }
1959 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04001960 case EOpConvFloatToUInt:
1961 case EOpConvBoolToUInt:
1962 case EOpConvIntToUInt:
Jamie Madilla16f1672013-07-03 15:15:17 -04001963 switch (node->getOperand()->getType().getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001964 {
1965 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001966 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
1967 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
1968 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001969 default: UNREACHABLE();
1970 }
1971 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001972 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1973 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1974 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1975 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1976 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1977 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1978 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1979 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1980 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1981 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1982 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1983 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1984 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1985 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1986 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1987 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1988 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1989 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1990 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1991 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1992 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001993 case EOpDFdx:
1994 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1995 {
1996 outputTriplet(visit, "(", "", ", 0.0)");
1997 }
1998 else
1999 {
2000 outputTriplet(visit, "ddx(", "", ")");
2001 }
2002 break;
2003 case EOpDFdy:
2004 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2005 {
2006 outputTriplet(visit, "(", "", ", 0.0)");
2007 }
2008 else
2009 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00002010 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002011 }
2012 break;
2013 case EOpFwidth:
2014 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2015 {
2016 outputTriplet(visit, "(", "", ", 0.0)");
2017 }
2018 else
2019 {
2020 outputTriplet(visit, "fwidth(", "", ")");
2021 }
2022 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002023 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
2024 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002025 default: UNREACHABLE();
2026 }
2027
2028 return true;
2029}
2030
2031bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2032{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002033 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002034
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002035 switch (node->getOp())
2036 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002037 case EOpSequence:
2038 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002039 if (mInsideFunction)
2040 {
Jamie Madill075edd82013-07-08 13:30:19 -04002041 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002042 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002043
2044 mScopeDepth++;
2045
2046 if (mScopeBracket.size() < mScopeDepth)
2047 {
2048 mScopeBracket.push_back(0); // New scope level
2049 }
2050 else
2051 {
2052 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
2053 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002054 }
2055
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002056 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
2057 {
Jamie Madill075edd82013-07-08 13:30:19 -04002058 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002059
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002060 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002061
2062 out << ";\n";
2063 }
2064
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002065 if (mInsideFunction)
2066 {
Jamie Madill075edd82013-07-08 13:30:19 -04002067 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002068 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002069
2070 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002071 }
2072
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002073 return false;
2074 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002075 case EOpDeclaration:
2076 if (visit == PreVisit)
2077 {
2078 TIntermSequence &sequence = node->getSequence();
2079 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002080
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00002081 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002082 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002083 if (variable->getType().getStruct())
2084 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002085 addConstructor(variable->getType(), scopedStruct(variable->getType().getStruct()->name()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00002086 }
2087
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002088 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002089 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00002090 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002091 {
2092 out << "static ";
2093 }
2094
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002095 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002096
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002097 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002098 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002099 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002100
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002101 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002102 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002103 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002104 out << arrayString(symbol->getType());
daniel@transgaming.com7127f202010-04-15 20:45:22 +00002105 out << " = " + initializer(variable->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002106 }
2107 else
2108 {
2109 (*sit)->traverse(this);
2110 }
2111
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002112 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002113 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002114 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002115 }
2116 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002117 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002118 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2119 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002120 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002121 }
2122 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002123 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002124 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002125 {
2126 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2127 {
2128 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2129
2130 if (symbol)
2131 {
2132 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2133 mReferencedVaryings[symbol->getSymbol()] = symbol;
2134 }
2135 else
2136 {
2137 (*sit)->traverse(this);
2138 }
2139 }
2140 }
2141
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002142 return false;
2143 }
2144 else if (visit == InVisit)
2145 {
2146 out << ", ";
2147 }
2148 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002149 case EOpPrototype:
2150 if (visit == PreVisit)
2151 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002152 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002153
2154 TIntermSequence &arguments = node->getSequence();
2155
2156 for (unsigned int i = 0; i < arguments.size(); i++)
2157 {
2158 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2159
2160 if (symbol)
2161 {
2162 out << argumentString(symbol);
2163
2164 if (i < arguments.size() - 1)
2165 {
2166 out << ", ";
2167 }
2168 }
2169 else UNREACHABLE();
2170 }
2171
2172 out << ");\n";
2173
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002174 // Also prototype the Lod0 variant if needed
2175 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2176 {
2177 mOutputLod0Function = true;
2178 node->traverse(this);
2179 mOutputLod0Function = false;
2180 }
2181
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002182 return false;
2183 }
2184 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002185 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002186 case EOpFunction:
2187 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002188 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002189
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002190 out << typeString(node->getType()) << " ";
2191
2192 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002193 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002194 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002195 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002196 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002197 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002198 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002199 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002200
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002201 TIntermSequence &sequence = node->getSequence();
2202 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2203
2204 for (unsigned int i = 0; i < arguments.size(); i++)
2205 {
2206 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2207
2208 if (symbol)
2209 {
2210 if (symbol->getType().getStruct())
2211 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002212 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getStruct()->name()), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002213 }
2214
2215 out << argumentString(symbol);
2216
2217 if (i < arguments.size() - 1)
2218 {
2219 out << ", ";
2220 }
2221 }
2222 else UNREACHABLE();
2223 }
2224
2225 out << ")\n"
2226 "{\n";
2227
2228 if (sequence.size() > 1)
2229 {
2230 mInsideFunction = true;
2231 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002232 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002233 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002234
2235 out << "}\n";
2236
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002237 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2238 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002239 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002240 {
2241 mOutputLod0Function = true;
2242 node->traverse(this);
2243 mOutputLod0Function = false;
2244 }
2245 }
2246
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002247 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002248 }
2249 break;
2250 case EOpFunctionCall:
2251 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002252 TString name = TFunction::unmangleName(node->getName());
2253 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002254 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002255
2256 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002257 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002258 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002259 }
2260 else
2261 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002262 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2263
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002264 TextureFunction textureFunction;
2265 textureFunction.sampler = samplerType;
2266 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
2267 textureFunction.mipmap = TextureFunction::IMPLICIT;
2268
2269 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002270 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002271 textureFunction.mipmap = TextureFunction::IMPLICIT;
2272 textureFunction.proj = false;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002273 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002274 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002275 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002276 textureFunction.mipmap = TextureFunction::IMPLICIT;
2277 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002278 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002279 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002280 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002281 textureFunction.mipmap = TextureFunction::LOD;
2282 textureFunction.proj = false;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002283 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002284 else if (name == "texture2DProjLod" || name == "textureProjLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002285 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002286 textureFunction.mipmap = TextureFunction::LOD;
2287 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002288 }
2289 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002290
2291 if (textureFunction.mipmap != TextureFunction::LOD)
2292 {
2293 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2294 {
2295 textureFunction.mipmap = TextureFunction::LOD0;
2296 }
2297 else if (arguments.size() == 3)
2298 {
2299 textureFunction.mipmap = TextureFunction::BIAS;
2300 }
2301 }
2302
2303 mUsesTexture.insert(textureFunction);
2304
2305 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002306 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002307
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002308 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2309 {
2310 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2311 {
2312 out << "texture_";
2313 (*arg)->traverse(this);
2314 out << ", sampler_";
2315 }
2316
2317 (*arg)->traverse(this);
2318
2319 if (arg < arguments.end() - 1)
2320 {
2321 out << ", ";
2322 }
2323 }
2324
2325 out << ")";
2326
2327 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002328 }
2329 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002330 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002331 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002332 addConstructor(node->getType(), "vec1", &node->getSequence());
2333 outputTriplet(visit, "vec1(", "", ")");
2334 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002335 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002336 addConstructor(node->getType(), "vec2", &node->getSequence());
2337 outputTriplet(visit, "vec2(", ", ", ")");
2338 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002339 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002340 addConstructor(node->getType(), "vec3", &node->getSequence());
2341 outputTriplet(visit, "vec3(", ", ", ")");
2342 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002343 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002344 addConstructor(node->getType(), "vec4", &node->getSequence());
2345 outputTriplet(visit, "vec4(", ", ", ")");
2346 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002347 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002348 addConstructor(node->getType(), "bvec1", &node->getSequence());
2349 outputTriplet(visit, "bvec1(", "", ")");
2350 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002351 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002352 addConstructor(node->getType(), "bvec2", &node->getSequence());
2353 outputTriplet(visit, "bvec2(", ", ", ")");
2354 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002355 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002356 addConstructor(node->getType(), "bvec3", &node->getSequence());
2357 outputTriplet(visit, "bvec3(", ", ", ")");
2358 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002359 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002360 addConstructor(node->getType(), "bvec4", &node->getSequence());
2361 outputTriplet(visit, "bvec4(", ", ", ")");
2362 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002363 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002364 addConstructor(node->getType(), "ivec1", &node->getSequence());
2365 outputTriplet(visit, "ivec1(", "", ")");
2366 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002367 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002368 addConstructor(node->getType(), "ivec2", &node->getSequence());
2369 outputTriplet(visit, "ivec2(", ", ", ")");
2370 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002371 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002372 addConstructor(node->getType(), "ivec3", &node->getSequence());
2373 outputTriplet(visit, "ivec3(", ", ", ")");
2374 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002375 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002376 addConstructor(node->getType(), "ivec4", &node->getSequence());
2377 outputTriplet(visit, "ivec4(", ", ", ")");
2378 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002379 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002380 addConstructor(node->getType(), "uvec1", &node->getSequence());
2381 outputTriplet(visit, "uvec1(", "", ")");
2382 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002383 case EOpConstructUVec2:
2384 addConstructor(node->getType(), "uvec2", &node->getSequence());
2385 outputTriplet(visit, "uvec2(", ", ", ")");
2386 break;
2387 case EOpConstructUVec3:
2388 addConstructor(node->getType(), "uvec3", &node->getSequence());
2389 outputTriplet(visit, "uvec3(", ", ", ")");
2390 break;
2391 case EOpConstructUVec4:
2392 addConstructor(node->getType(), "uvec4", &node->getSequence());
2393 outputTriplet(visit, "uvec4(", ", ", ")");
2394 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002395 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002396 addConstructor(node->getType(), "mat2", &node->getSequence());
2397 outputTriplet(visit, "mat2(", ", ", ")");
2398 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002399 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002400 addConstructor(node->getType(), "mat3", &node->getSequence());
2401 outputTriplet(visit, "mat3(", ", ", ")");
2402 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002403 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002404 addConstructor(node->getType(), "mat4", &node->getSequence());
2405 outputTriplet(visit, "mat4(", ", ", ")");
2406 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002407 case EOpConstructStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04002408 addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence());
2409 outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002410 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002411 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2412 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2413 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2414 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2415 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2416 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002417 case EOpMod:
2418 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002419 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002420 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2421 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2422 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002423 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002424 case 11: mUsesMod1 = true; break;
2425 case 22: mUsesMod2v = true; break;
2426 case 21: mUsesMod2f = true; break;
2427 case 33: mUsesMod3v = true; break;
2428 case 31: mUsesMod3f = true; break;
2429 case 44: mUsesMod4v = true; break;
2430 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002431 default: UNREACHABLE();
2432 }
2433
2434 outputTriplet(visit, "mod(", ", ", ")");
2435 }
2436 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002437 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002438 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002439 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002440 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2441 {
2442 case 1: mUsesAtan2_1 = true; break;
2443 case 2: mUsesAtan2_2 = true; break;
2444 case 3: mUsesAtan2_3 = true; break;
2445 case 4: mUsesAtan2_4 = true; break;
2446 default: UNREACHABLE();
2447 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002448 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002449 break;
2450 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2451 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2452 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2453 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2454 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2455 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2456 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2457 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2458 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002459 case EOpFaceForward:
2460 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002461 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002462 {
2463 case 1: mUsesFaceforward1 = true; break;
2464 case 2: mUsesFaceforward2 = true; break;
2465 case 3: mUsesFaceforward3 = true; break;
2466 case 4: mUsesFaceforward4 = true; break;
2467 default: UNREACHABLE();
2468 }
2469
2470 outputTriplet(visit, "faceforward(", ", ", ")");
2471 }
2472 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002473 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2474 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2475 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002476 default: UNREACHABLE();
2477 }
2478
2479 return true;
2480}
2481
2482bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2483{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002484 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002485
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002486 if (node->usesTernaryOperator())
2487 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002488 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002489 }
2490 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002491 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002492 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002493
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002494 out << "if(";
2495
2496 node->getCondition()->traverse(this);
2497
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002498 out << ")\n";
2499
Jamie Madill075edd82013-07-08 13:30:19 -04002500 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002501 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002502
daniel@transgaming.combb885322010-04-15 20:45:24 +00002503 if (node->getTrueBlock())
2504 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002505 traverseStatements(node->getTrueBlock());
daniel@transgaming.combb885322010-04-15 20:45:24 +00002506 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002507
Jamie Madill075edd82013-07-08 13:30:19 -04002508 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002509 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002510
2511 if (node->getFalseBlock())
2512 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002513 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002514
Jamie Madill075edd82013-07-08 13:30:19 -04002515 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002516 out << "{\n";
2517
Jamie Madill075edd82013-07-08 13:30:19 -04002518 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002519 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002520
Jamie Madill075edd82013-07-08 13:30:19 -04002521 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002522 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002523 }
2524 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002525
2526 return false;
2527}
2528
2529void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2530{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002531 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002532}
2533
2534bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2535{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002536 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2537
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002538 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002539 {
2540 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2541 }
2542
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002543 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002544 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002545 if (handleExcessiveLoop(node))
2546 {
2547 return false;
2548 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002549 }
2550
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002551 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002552
alokp@chromium.org52813552010-11-16 18:36:09 +00002553 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002554 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002555 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002556
Jamie Madill075edd82013-07-08 13:30:19 -04002557 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002558 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002559 }
2560 else
2561 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002562 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002563
2564 if (node->getInit())
2565 {
2566 node->getInit()->traverse(this);
2567 }
2568
2569 out << "; ";
2570
alokp@chromium.org52813552010-11-16 18:36:09 +00002571 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002572 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002573 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002574 }
2575
2576 out << "; ";
2577
alokp@chromium.org52813552010-11-16 18:36:09 +00002578 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002579 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002580 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002581 }
2582
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002583 out << ")\n";
2584
Jamie Madill075edd82013-07-08 13:30:19 -04002585 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002586 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002587 }
2588
2589 if (node->getBody())
2590 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002591 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002592 }
2593
Jamie Madill075edd82013-07-08 13:30:19 -04002594 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002595 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002596
alokp@chromium.org52813552010-11-16 18:36:09 +00002597 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002598 {
Jamie Madill075edd82013-07-08 13:30:19 -04002599 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002600 out << "while(\n";
2601
alokp@chromium.org52813552010-11-16 18:36:09 +00002602 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002603
daniel@transgaming.com73536982012-03-21 20:45:49 +00002604 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002605 }
2606
daniel@transgaming.com73536982012-03-21 20:45:49 +00002607 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002608
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002609 mInsideDiscontinuousLoop = wasDiscontinuous;
2610
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002611 return false;
2612}
2613
2614bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2615{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002616 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002617
2618 switch (node->getFlowOp())
2619 {
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002620 case EOpKill: outputTriplet(visit, "discard;\n", "", ""); break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002621 case EOpBreak:
2622 if (visit == PreVisit)
2623 {
2624 if (mExcessiveLoopIndex)
2625 {
2626 out << "{Break";
2627 mExcessiveLoopIndex->traverse(this);
2628 out << " = true; break;}\n";
2629 }
2630 else
2631 {
2632 out << "break;\n";
2633 }
2634 }
2635 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002636 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002637 case EOpReturn:
2638 if (visit == PreVisit)
2639 {
2640 if (node->getExpression())
2641 {
2642 out << "return ";
2643 }
2644 else
2645 {
2646 out << "return;\n";
2647 }
2648 }
2649 else if (visit == PostVisit)
2650 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002651 if (node->getExpression())
2652 {
2653 out << ";\n";
2654 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002655 }
2656 break;
2657 default: UNREACHABLE();
2658 }
2659
2660 return true;
2661}
2662
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002663void OutputHLSL::traverseStatements(TIntermNode *node)
2664{
2665 if (isSingleStatement(node))
2666 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002667 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002668 }
2669
2670 node->traverse(this);
2671}
2672
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002673bool OutputHLSL::isSingleStatement(TIntermNode *node)
2674{
2675 TIntermAggregate *aggregate = node->getAsAggregate();
2676
2677 if (aggregate)
2678 {
2679 if (aggregate->getOp() == EOpSequence)
2680 {
2681 return false;
2682 }
2683 else
2684 {
2685 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2686 {
2687 if (!isSingleStatement(*sit))
2688 {
2689 return false;
2690 }
2691 }
2692
2693 return true;
2694 }
2695 }
2696
2697 return true;
2698}
2699
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002700// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2701// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002702bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2703{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002704 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002705 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002706
2707 // Parse loops of the form:
2708 // for(int index = initial; index [comparator] limit; index += increment)
2709 TIntermSymbol *index = NULL;
2710 TOperator comparator = EOpNull;
2711 int initial = 0;
2712 int limit = 0;
2713 int increment = 0;
2714
2715 // Parse index name and intial value
2716 if (node->getInit())
2717 {
2718 TIntermAggregate *init = node->getInit()->getAsAggregate();
2719
2720 if (init)
2721 {
2722 TIntermSequence &sequence = init->getSequence();
2723 TIntermTyped *variable = sequence[0]->getAsTyped();
2724
2725 if (variable && variable->getQualifier() == EvqTemporary)
2726 {
2727 TIntermBinary *assign = variable->getAsBinaryNode();
2728
2729 if (assign->getOp() == EOpInitialize)
2730 {
2731 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2732 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2733
2734 if (symbol && constant)
2735 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002736 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002737 {
2738 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002739 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002740 }
2741 }
2742 }
2743 }
2744 }
2745 }
2746
2747 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002748 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002749 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002750 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002751
2752 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2753 {
2754 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2755
2756 if (constant)
2757 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002758 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002759 {
2760 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002761 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002762 }
2763 }
2764 }
2765 }
2766
2767 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002768 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002769 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002770 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2771 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002772
2773 if (binaryTerminal)
2774 {
2775 TOperator op = binaryTerminal->getOp();
2776 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2777
2778 if (constant)
2779 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002780 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002781 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002782 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002783
2784 switch (op)
2785 {
2786 case EOpAddAssign: increment = value; break;
2787 case EOpSubAssign: increment = -value; break;
2788 default: UNIMPLEMENTED();
2789 }
2790 }
2791 }
2792 }
2793 else if (unaryTerminal)
2794 {
2795 TOperator op = unaryTerminal->getOp();
2796
2797 switch (op)
2798 {
2799 case EOpPostIncrement: increment = 1; break;
2800 case EOpPostDecrement: increment = -1; break;
2801 case EOpPreIncrement: increment = 1; break;
2802 case EOpPreDecrement: increment = -1; break;
2803 default: UNIMPLEMENTED();
2804 }
2805 }
2806 }
2807
2808 if (index != NULL && comparator != EOpNull && increment != 0)
2809 {
2810 if (comparator == EOpLessThanEqual)
2811 {
2812 comparator = EOpLessThan;
2813 limit += 1;
2814 }
2815
2816 if (comparator == EOpLessThan)
2817 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002818 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002819
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002820 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002821 {
2822 return false; // Not an excessive loop
2823 }
2824
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002825 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2826 mExcessiveLoopIndex = index;
2827
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002828 out << "{int ";
2829 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002830 out << ";\n"
2831 "bool Break";
2832 index->traverse(this);
2833 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002834
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002835 bool firstLoopFragment = true;
2836
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002837 while (iterations > 0)
2838 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002839 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002840
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002841 if (!firstLoopFragment)
2842 {
2843 out << "if(!Break";
2844 index->traverse(this);
2845 out << ") {\n";
2846 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002847
2848 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2849 {
2850 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2851 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002852
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002853 // for(int index = initial; index < clampedLimit; index += increment)
2854
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002855 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002856 index->traverse(this);
2857 out << " = ";
2858 out << initial;
2859
2860 out << "; ";
2861 index->traverse(this);
2862 out << " < ";
2863 out << clampedLimit;
2864
2865 out << "; ";
2866 index->traverse(this);
2867 out << " += ";
2868 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002869 out << ")\n";
2870
Jamie Madill075edd82013-07-08 13:30:19 -04002871 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002872 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002873
2874 if (node->getBody())
2875 {
2876 node->getBody()->traverse(this);
2877 }
2878
Jamie Madill075edd82013-07-08 13:30:19 -04002879 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002880 out << ";}\n";
2881
2882 if (!firstLoopFragment)
2883 {
2884 out << "}\n";
2885 }
2886
2887 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002888
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002889 initial += MAX_LOOP_ITERATIONS * increment;
2890 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002891 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002892
2893 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002894
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002895 mExcessiveLoopIndex = restoreIndex;
2896
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002897 return true;
2898 }
2899 else UNIMPLEMENTED();
2900 }
2901
2902 return false; // Not handled as an excessive loop
2903}
2904
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002905void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002906{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002907 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002908
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002909 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002910 {
2911 out << preString;
2912 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002913 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002914 {
2915 out << inString;
2916 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002917 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002918 {
2919 out << postString;
2920 }
2921}
2922
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002923void OutputHLSL::outputLineDirective(int line)
2924{
2925 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2926 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002927 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002928 mBody << "#line " << line;
2929
2930 if (mContext.sourcePath)
2931 {
2932 mBody << " \"" << mContext.sourcePath << "\"";
2933 }
2934
2935 mBody << "\n";
2936 }
2937}
2938
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002939TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2940{
2941 TQualifier qualifier = symbol->getQualifier();
2942 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002943 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002944
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002945 if (name.empty()) // HLSL demands named arguments, also for prototypes
2946 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002947 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002948 }
2949 else
2950 {
2951 name = decorate(name);
2952 }
2953
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002954 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2955 {
2956 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
2957 qualifierString(qualifier) + " SamplerState sampler_" + name + arrayString(type);
2958 }
2959
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002960 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002961}
2962
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00002963TString OutputHLSL::interpolationString(TQualifier qualifier)
2964{
2965 switch(qualifier)
2966 {
2967 case EvqVaryingIn: return "";
2968 case EvqInvariantVaryingIn: return "";
2969 case EvqSmoothIn: return "linear";
2970 case EvqFlatIn: return "nointerpolation";
2971 case EvqCentroidIn: return "centroid";
2972 case EvqVaryingOut: return "";
2973 case EvqInvariantVaryingOut: return "";
2974 case EvqSmoothOut: return "linear";
2975 case EvqFlatOut: return "nointerpolation";
2976 case EvqCentroidOut: return "centroid";
2977 default: UNREACHABLE();
2978 }
2979
2980 return "";
2981}
2982
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002983TString OutputHLSL::qualifierString(TQualifier qualifier)
2984{
2985 switch(qualifier)
2986 {
2987 case EvqIn: return "in";
2988 case EvqOut: return "out";
2989 case EvqInOut: return "inout";
2990 case EvqConstReadOnly: return "const";
2991 default: UNREACHABLE();
2992 }
2993
2994 return "";
2995}
2996
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002997TString OutputHLSL::typeString(const TType &type)
2998{
Jamie Madill98493dd2013-07-08 14:39:03 -04002999 const TStructure* structure = type.getStruct();
3000 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003001 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003002 const TString& typeName = structure->name();
3003 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003004 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003005 return structLookup(typeName);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003006 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003007 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003008 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003009 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003010 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003011 }
3012 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003013 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003014 int cols = type.getCols();
3015 int rows = type.getRows();
3016 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003017 }
3018 else
3019 {
3020 switch (type.getBasicType())
3021 {
3022 case EbtFloat:
3023 switch (type.getNominalSize())
3024 {
3025 case 1: return "float";
3026 case 2: return "float2";
3027 case 3: return "float3";
3028 case 4: return "float4";
3029 }
3030 case EbtInt:
3031 switch (type.getNominalSize())
3032 {
3033 case 1: return "int";
3034 case 2: return "int2";
3035 case 3: return "int3";
3036 case 4: return "int4";
3037 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003038 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04003039 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003040 {
3041 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003042 case 2: return "uint2";
3043 case 3: return "uint3";
3044 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003045 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003046 case EbtBool:
3047 switch (type.getNominalSize())
3048 {
3049 case 1: return "bool";
3050 case 2: return "bool2";
3051 case 3: return "bool3";
3052 case 4: return "bool4";
3053 }
3054 case EbtVoid:
3055 return "void";
3056 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003057 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04003058 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003059 case EbtSampler2DArray:
3060 case EbtISampler2DArray:
3061 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003062 return "sampler2D";
3063 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003064 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04003065 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003066 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00003067 case EbtSamplerExternalOES:
3068 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00003069 default:
3070 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003071 }
3072 }
3073
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003074 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003075 return "<unknown type>";
3076}
3077
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003078TString OutputHLSL::textureString(const TType &type)
3079{
3080 switch (type.getBasicType())
3081 {
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003082 case EbtSampler2D: return "Texture2D";
3083 case EbtSamplerCube: return "TextureCube";
3084 case EbtSamplerExternalOES: return "Texture2D";
3085 case EbtSampler2DArray: return "Texture2DArray";
Jamie Madill94c3f422013-07-03 15:16:13 -04003086 case EbtSampler3D: return "Texture3D";
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003087 case EbtISampler2D: return "Texture2D<int4>";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04003088 case EbtISampler3D: return "Texture3D<int4>";
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003089 case EbtISamplerCube: return "TextureCube<int4>";
3090 case EbtISampler2DArray: return "Texture2DArray<int4>";
3091 case EbtUSampler2D: return "Texture2D<uint4>";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04003092 case EbtUSampler3D: return "Texture3D<uint4>";
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003093 case EbtUSamplerCube: return "TextureCube<uint4>";
3094 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003095 default:
3096 break;
3097 }
3098
3099 UNREACHABLE();
3100 return "<unknown texture type>";
3101}
3102
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003103TString OutputHLSL::arrayString(const TType &type)
3104{
3105 if (!type.isArray())
3106 {
3107 return "";
3108 }
3109
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003110 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003111}
3112
3113TString OutputHLSL::initializer(const TType &type)
3114{
3115 TString string;
3116
Jamie Madill94bf7f22013-07-08 13:31:15 -04003117 size_t size = type.getObjectSize();
3118 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003119 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003120 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003121
Jamie Madill94bf7f22013-07-08 13:31:15 -04003122 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003123 {
3124 string += ", ";
3125 }
3126 }
3127
daniel@transgaming.comead23042010-04-29 03:35:36 +00003128 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003129}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003130
Jamie Madill98493dd2013-07-08 14:39:03 -04003131TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003132{
Jamie Madill98493dd2013-07-08 14:39:03 -04003133 const TFieldList &fields = structure.fields();
3134 const bool isNameless = (structure.name() == "");
3135 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003136 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3137
Jamie Madill98493dd2013-07-08 14:39:03 -04003138 TString string;
3139 string += declareString + "\n"
3140 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003141
Jamie Madillc835df62013-06-21 09:15:32 -04003142 int elementIndex = 0;
3143
Jamie Madill9cf6c072013-06-20 11:55:53 -04003144 for (unsigned int i = 0; i < fields.size(); i++)
3145 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003146 const TField &field = *fields[i];
3147 const TType &fieldType = *field.type();
3148 const TStructure *fieldStruct = fieldType.getStruct();
3149 const TString &fieldTypeString = fieldStruct ? structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) : typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003150
Jamie Madillc835df62013-06-21 09:15:32 -04003151 if (useStd140Packing)
3152 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003153 string += std140PrePaddingString(*field.type(), &elementIndex);
Jamie Madillc835df62013-06-21 09:15:32 -04003154 }
3155
Jamie Madill98493dd2013-07-08 14:39:03 -04003156 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003157
3158 if (useStd140Packing)
3159 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003160 string += std140PostPaddingString(*field.type(), useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003161 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003162 }
3163
3164 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003165 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003166
Jamie Madille4075c92013-06-21 09:15:32 -04003167 // Add remaining element index to the global map, for use with nested structs in standard layouts
3168 if (useStd140Packing)
3169 {
3170 mStd140StructElementIndexes[structName] = elementIndex;
3171 }
3172
Jamie Madill98493dd2013-07-08 14:39:03 -04003173 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003174}
3175
Jamie Madill98493dd2013-07-08 14:39:03 -04003176TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003177{
Jamie Madill98493dd2013-07-08 14:39:03 -04003178 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003179 {
3180 return "";
3181 }
3182
3183 TString prefix = "";
3184
3185 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3186 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003187
3188 if (useStd140Packing)
3189 {
3190 prefix += "std";
3191 }
3192
Jamie Madill9cf6c072013-06-20 11:55:53 -04003193 if (useHLSLRowMajorPacking)
3194 {
Jamie Madillc835df62013-06-21 09:15:32 -04003195 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003196 prefix += "rm";
3197 }
3198
Jamie Madill98493dd2013-07-08 14:39:03 -04003199 return prefix + structLookup(structure.name());
Jamie Madill9cf6c072013-06-20 11:55:53 -04003200}
3201
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003202void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003203{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003204 if (name == "")
3205 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003206 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003207 }
3208
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003209 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
3210 {
3211 return; // Already added
3212 }
3213
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003214 TType ctorType = type;
3215 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003216 ctorType.setPrecision(EbpHigh);
3217 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003218
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003219 TString ctorName = type.getStruct() ? decorate(name) : name;
3220
3221 typedef std::vector<TType> ParameterArray;
3222 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003223
Jamie Madill98493dd2013-07-08 14:39:03 -04003224 const TStructure* structure = type.getStruct();
3225 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003226 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003227 mStructNames.insert(decorate(name));
3228
Jamie Madill98493dd2013-07-08 14:39:03 -04003229 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003230
Jamie Madill98493dd2013-07-08 14:39:03 -04003231 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003232 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003233 // Add row-major packed struct for interface blocks
3234 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003235 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003236 "#pragma pack_matrix(column_major)\n";
3237
Jamie Madillc835df62013-06-21 09:15:32 -04003238 const TString &std140Prefix = "std";
Jamie Madill98493dd2013-07-08 14:39:03 -04003239 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003240
3241 const TString &std140RowMajorPrefix = "std_rm";
3242 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003243 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003244 "#pragma pack_matrix(column_major)\n";
3245
Jamie Madill98493dd2013-07-08 14:39:03 -04003246 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003247 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003248 mStructDeclarations.push_back(std140String);
3249 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003250 }
3251
Jamie Madill98493dd2013-07-08 14:39:03 -04003252 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003253 for (unsigned int i = 0; i < fields.size(); i++)
3254 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003255 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003256 }
3257 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003258 else if (parameters)
3259 {
3260 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3261 {
3262 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3263 }
3264 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003265 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003266
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003267 TString constructor;
3268
3269 if (ctorType.getStruct())
3270 {
3271 constructor += ctorName + " " + ctorName + "_ctor(";
3272 }
3273 else // Built-in type
3274 {
3275 constructor += typeString(ctorType) + " " + ctorName + "(";
3276 }
3277
3278 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3279 {
3280 const TType &type = ctorParameters[parameter];
3281
3282 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3283
3284 if (parameter < ctorParameters.size() - 1)
3285 {
3286 constructor += ", ";
3287 }
3288 }
3289
3290 constructor += ")\n"
3291 "{\n";
3292
3293 if (ctorType.getStruct())
3294 {
3295 constructor += " " + ctorName + " structure = {";
3296 }
3297 else
3298 {
3299 constructor += " return " + typeString(ctorType) + "(";
3300 }
3301
3302 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3303 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003304 int rows = ctorType.getRows();
3305 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003306 const TType &parameter = ctorParameters[0];
3307
3308 if (parameter.isScalar())
3309 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003310 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003311 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003312 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003313 {
3314 constructor += TString((row == col) ? "x0" : "0.0");
3315
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003316 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003317 {
3318 constructor += ", ";
3319 }
3320 }
3321 }
3322 }
3323 else if (parameter.isMatrix())
3324 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003325 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003326 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003327 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003328 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003329 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003330 {
3331 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3332 }
3333 else
3334 {
3335 constructor += TString((row == col) ? "1.0" : "0.0");
3336 }
3337
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003338 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003339 {
3340 constructor += ", ";
3341 }
3342 }
3343 }
3344 }
3345 else UNREACHABLE();
3346 }
3347 else
3348 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003349 size_t remainingComponents = ctorType.getObjectSize();
3350 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003351
3352 while (remainingComponents > 0)
3353 {
3354 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003355 const size_t parameterSize = parameter.getObjectSize();
3356 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003357
3358 constructor += "x" + str(parameterIndex);
3359
3360 if (parameter.isScalar())
3361 {
3362 remainingComponents -= parameter.getObjectSize();
3363 }
3364 else if (parameter.isVector())
3365 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003366 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003367 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003368 ASSERT(parameterSize <= remainingComponents);
3369 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003370 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003371 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003372 {
3373 switch (remainingComponents)
3374 {
3375 case 1: constructor += ".x"; break;
3376 case 2: constructor += ".xy"; break;
3377 case 3: constructor += ".xyz"; break;
3378 case 4: constructor += ".xyzw"; break;
3379 default: UNREACHABLE();
3380 }
3381
3382 remainingComponents = 0;
3383 }
3384 else UNREACHABLE();
3385 }
3386 else if (parameter.isMatrix() || parameter.getStruct())
3387 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003388 ASSERT(remainingComponents == parameterSize || moreParameters);
3389 ASSERT(parameterSize <= remainingComponents);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003390
Jamie Madill94bf7f22013-07-08 13:31:15 -04003391 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003392 }
3393 else UNREACHABLE();
3394
3395 if (moreParameters)
3396 {
3397 parameterIndex++;
3398 }
3399
3400 if (remainingComponents)
3401 {
3402 constructor += ", ";
3403 }
3404 }
3405 }
3406
3407 if (ctorType.getStruct())
3408 {
3409 constructor += "};\n"
3410 " return structure;\n"
3411 "}\n";
3412 }
3413 else
3414 {
3415 constructor += ");\n"
3416 "}\n";
3417 }
3418
daniel@transgaming.com63691862010-04-29 03:32:42 +00003419 mConstructors.insert(constructor);
3420}
3421
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003422const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3423{
3424 TInfoSinkBase &out = mBody;
3425
Jamie Madill98493dd2013-07-08 14:39:03 -04003426 const TStructure* structure = type.getStruct();
3427 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003428 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003429 out << structLookup(structure->name()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003430
Jamie Madill98493dd2013-07-08 14:39:03 -04003431 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003432
Jamie Madill98493dd2013-07-08 14:39:03 -04003433 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003434 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003435 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003436
3437 constUnion = writeConstantUnion(*fieldType, constUnion);
3438
Jamie Madill98493dd2013-07-08 14:39:03 -04003439 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003440 {
3441 out << ", ";
3442 }
3443 }
3444
3445 out << ")";
3446 }
3447 else
3448 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003449 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003450 bool writeType = size > 1;
3451
3452 if (writeType)
3453 {
3454 out << typeString(type) << "(";
3455 }
3456
Jamie Madill94bf7f22013-07-08 13:31:15 -04003457 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003458 {
3459 switch (constUnion->getType())
3460 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003461 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003462 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003463 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003464 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003465 default: UNREACHABLE();
3466 }
3467
3468 if (i != size - 1)
3469 {
3470 out << ", ";
3471 }
3472 }
3473
3474 if (writeType)
3475 {
3476 out << ")";
3477 }
3478 }
3479
3480 return constUnion;
3481}
3482
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003483TString OutputHLSL::scopeString(unsigned int depthLimit)
3484{
3485 TString string;
3486
3487 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3488 {
3489 string += "_" + str(i);
3490 }
3491
3492 return string;
3493}
3494
3495TString OutputHLSL::scopedStruct(const TString &typeName)
3496{
3497 if (typeName == "")
3498 {
3499 return typeName;
3500 }
3501
3502 return typeName + scopeString(mScopeDepth);
3503}
3504
3505TString OutputHLSL::structLookup(const TString &typeName)
3506{
3507 for (int depth = mScopeDepth; depth >= 0; depth--)
3508 {
3509 TString scopedName = decorate(typeName + scopeString(depth));
3510
3511 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3512 {
3513 if (*structName == scopedName)
3514 {
3515 return scopedName;
3516 }
3517 }
3518 }
3519
3520 UNREACHABLE(); // Should have found a matching constructor
3521
3522 return typeName;
3523}
3524
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003525TString OutputHLSL::decorate(const TString &string)
3526{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003527 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003528 {
3529 return "_" + string;
3530 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003531
3532 return string;
3533}
3534
apatrick@chromium.org65756022012-01-17 21:45:38 +00003535TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003536{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003537 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003538 {
3539 return "ex_" + string;
3540 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003541
3542 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003543}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003544
Jamie Madill98493dd2013-07-08 14:39:03 -04003545TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003546{
Jamie Madill98493dd2013-07-08 14:39:03 -04003547 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003548 {
3549 return decorate(string);
3550 }
3551
3552 return string;
3553}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003554
3555TString OutputHLSL::registerString(TIntermSymbol *operand)
3556{
3557 ASSERT(operand->getQualifier() == EvqUniform);
3558
3559 if (IsSampler(operand->getBasicType()))
3560 {
3561 return "s" + str(samplerRegister(operand));
3562 }
3563
3564 return "c" + str(uniformRegister(operand));
3565}
3566
3567int OutputHLSL::samplerRegister(TIntermSymbol *sampler)
3568{
3569 const TType &type = sampler->getType();
3570 ASSERT(IsSampler(type.getBasicType()));
3571
3572 int index = mSamplerRegister;
3573 mSamplerRegister += sampler->totalRegisterCount();
3574
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003575 declareUniform(type, sampler->getSymbol(), index);
3576
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003577 return index;
3578}
3579
3580int OutputHLSL::uniformRegister(TIntermSymbol *uniform)
3581{
3582 const TType &type = uniform->getType();
3583 ASSERT(!IsSampler(type.getBasicType()));
3584
3585 int index = mUniformRegister;
3586 mUniformRegister += uniform->totalRegisterCount();
3587
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003588 declareUniform(type, uniform->getSymbol(), index);
3589
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003590 return index;
3591}
3592
Jamie Madill98493dd2013-07-08 14:39:03 -04003593void OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, ActiveUniforms& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003594{
Jamie Madill98493dd2013-07-08 14:39:03 -04003595 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003596
3597 if (!structure)
3598 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003599 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill98493dd2013-07-08 14:39:03 -04003600 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 +00003601 }
3602 else
3603 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003604 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 +00003605
Jamie Madill98493dd2013-07-08 14:39:03 -04003606 int fieldRegister = registerIndex;
3607 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003608
Jamie Madill98493dd2013-07-08 14:39:03 -04003609 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003610 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003611 TField *field = fields[fieldIndex];
3612 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003613
Jamie Madill010fffa2013-06-20 11:55:53 -04003614 // make sure to copy matrix packing information
Jamie Madill98493dd2013-07-08 14:39:03 -04003615 fieldType->setLayoutQualifier(type.getLayoutQualifier());
Jamie Madill010fffa2013-06-20 11:55:53 -04003616
Jamie Madill98493dd2013-07-08 14:39:03 -04003617 declareUniformToList(*fieldType, field->name(), fieldRegister, structUniform.fields);
3618 fieldRegister += fieldType->totalRegisterCount();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003619 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003620
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003621 output.push_back(structUniform);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003622 }
3623}
3624
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003625void OutputHLSL::declareUniform(const TType &type, const TString &name, int index)
3626{
3627 declareUniformToList(type, name, index, mActiveUniforms);
3628}
3629
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003630GLenum OutputHLSL::glVariableType(const TType &type)
3631{
3632 if (type.getBasicType() == EbtFloat)
3633 {
3634 if (type.isScalar())
3635 {
3636 return GL_FLOAT;
3637 }
3638 else if (type.isVector())
3639 {
3640 switch(type.getNominalSize())
3641 {
3642 case 2: return GL_FLOAT_VEC2;
3643 case 3: return GL_FLOAT_VEC3;
3644 case 4: return GL_FLOAT_VEC4;
3645 default: UNREACHABLE();
3646 }
3647 }
3648 else if (type.isMatrix())
3649 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003650 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003651 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003652 case 2:
3653 switch(type.getRows())
3654 {
3655 case 2: return GL_FLOAT_MAT2;
3656 case 3: return GL_FLOAT_MAT2x3;
3657 case 4: return GL_FLOAT_MAT2x4;
3658 default: UNREACHABLE();
3659 }
3660
3661 case 3:
3662 switch(type.getRows())
3663 {
3664 case 2: return GL_FLOAT_MAT3x2;
3665 case 3: return GL_FLOAT_MAT3;
3666 case 4: return GL_FLOAT_MAT3x4;
3667 default: UNREACHABLE();
3668 }
3669
3670 case 4:
3671 switch(type.getRows())
3672 {
3673 case 2: return GL_FLOAT_MAT4x2;
3674 case 3: return GL_FLOAT_MAT4x3;
3675 case 4: return GL_FLOAT_MAT4;
3676 default: UNREACHABLE();
3677 }
3678
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003679 default: UNREACHABLE();
3680 }
3681 }
3682 else UNREACHABLE();
3683 }
3684 else if (type.getBasicType() == EbtInt)
3685 {
3686 if (type.isScalar())
3687 {
3688 return GL_INT;
3689 }
3690 else if (type.isVector())
3691 {
3692 switch(type.getNominalSize())
3693 {
3694 case 2: return GL_INT_VEC2;
3695 case 3: return GL_INT_VEC3;
3696 case 4: return GL_INT_VEC4;
3697 default: UNREACHABLE();
3698 }
3699 }
3700 else UNREACHABLE();
3701 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003702 else if (type.getBasicType() == EbtUInt)
3703 {
3704 if (type.isScalar())
3705 {
3706 return GL_UNSIGNED_INT;
3707 }
3708 else if (type.isVector())
3709 {
Jamie Madill22d63da2013-06-07 12:45:12 -04003710 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003711 {
3712 case 2: return GL_UNSIGNED_INT_VEC2;
3713 case 3: return GL_UNSIGNED_INT_VEC3;
3714 case 4: return GL_UNSIGNED_INT_VEC4;
3715 default: UNREACHABLE();
3716 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003717 }
3718 else UNREACHABLE();
3719 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003720 else if (type.getBasicType() == EbtBool)
3721 {
3722 if (type.isScalar())
3723 {
3724 return GL_BOOL;
3725 }
3726 else if (type.isVector())
3727 {
3728 switch(type.getNominalSize())
3729 {
3730 case 2: return GL_BOOL_VEC2;
3731 case 3: return GL_BOOL_VEC3;
3732 case 4: return GL_BOOL_VEC4;
3733 default: UNREACHABLE();
3734 }
3735 }
3736 else UNREACHABLE();
3737 }
3738 else if (type.getBasicType() == EbtSampler2D)
3739 {
3740 return GL_SAMPLER_2D;
3741 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04003742 else if (type.getBasicType() == EbtSampler3D)
3743 {
3744 return GL_SAMPLER_3D;
3745 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003746 else if (type.getBasicType() == EbtSamplerCube)
3747 {
3748 return GL_SAMPLER_CUBE;
3749 }
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003750 else if (type.getBasicType() == EbtSampler2DArray)
3751 {
3752 return GL_SAMPLER_2D_ARRAY;
3753 }
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003754 else if (type.getBasicType() == EbtISampler2D)
3755 {
3756 return GL_INT_SAMPLER_2D;
3757 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04003758 else if (type.getBasicType() == EbtISampler3D)
3759 {
3760 return GL_INT_SAMPLER_3D;
3761 }
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003762 else if (type.getBasicType() == EbtISamplerCube)
3763 {
3764 return GL_INT_SAMPLER_CUBE;
3765 }
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003766 else if (type.getBasicType() == EbtISampler2DArray)
3767 {
3768 return GL_INT_SAMPLER_2D_ARRAY;
3769 }
Nicolas Capens075368e2013-06-24 15:58:30 -04003770 else if (type.getBasicType() == EbtUSampler2D)
3771 {
3772 return GL_UNSIGNED_INT_SAMPLER_2D;
3773 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04003774 else if (type.getBasicType() == EbtUSampler3D)
3775 {
3776 return GL_UNSIGNED_INT_SAMPLER_3D;
3777 }
Nicolas Capens075368e2013-06-24 15:58:30 -04003778 else if (type.getBasicType() == EbtUSamplerCube)
3779 {
3780 return GL_UNSIGNED_INT_SAMPLER_CUBE;
3781 }
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003782 else if (type.getBasicType() == EbtUSampler2DArray)
3783 {
3784 return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3785 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003786 else UNREACHABLE();
3787
3788 return GL_NONE;
3789}
3790
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003791GLenum OutputHLSL::glVariablePrecision(const TType &type)
3792{
3793 if (type.getBasicType() == EbtFloat)
3794 {
3795 switch (type.getPrecision())
3796 {
3797 case EbpHigh: return GL_HIGH_FLOAT;
3798 case EbpMedium: return GL_MEDIUM_FLOAT;
3799 case EbpLow: return GL_LOW_FLOAT;
3800 case EbpUndefined:
3801 // Should be defined as the default precision by the parser
3802 default: UNREACHABLE();
3803 }
3804 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003805 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003806 {
3807 switch (type.getPrecision())
3808 {
3809 case EbpHigh: return GL_HIGH_INT;
3810 case EbpMedium: return GL_MEDIUM_INT;
3811 case EbpLow: return GL_LOW_INT;
3812 case EbpUndefined:
3813 // Should be defined as the default precision by the parser
3814 default: UNREACHABLE();
3815 }
3816 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003817
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00003818 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003819 return GL_NONE;
3820}
3821
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003822bool OutputHLSL::isVaryingOut(TQualifier qualifier)
3823{
3824 switch(qualifier)
3825 {
3826 case EvqVaryingOut:
3827 case EvqInvariantVaryingOut:
3828 case EvqSmoothOut:
3829 case EvqFlatOut:
3830 case EvqCentroidOut:
3831 return true;
3832 }
3833
3834 return false;
3835}
3836
3837bool OutputHLSL::isVaryingIn(TQualifier qualifier)
3838{
3839 switch(qualifier)
3840 {
3841 case EvqVaryingIn:
3842 case EvqInvariantVaryingIn:
3843 case EvqSmoothIn:
3844 case EvqFlatIn:
3845 case EvqCentroidIn:
3846 return true;
3847 }
3848
3849 return false;
3850}
3851
3852bool OutputHLSL::isVarying(TQualifier qualifier)
3853{
3854 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
3855}
3856
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003857}