blob: dfcc268f08098f6b0694c7a77ef74b8499d092f3 [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
Nicolas Capens75fb4752013-07-10 15:14:47 -040056 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040057 {
58 case IMPLICIT: break;
59 case BIAS: break;
60 case LOD: name += "Lod"; break;
61 case LOD0: name += "Lod0"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -040062 case SIZE: name += "Size"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040063 default: UNREACHABLE();
64 }
65
66 return name + "(";
67}
68
69bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
70{
71 if (sampler < rhs.sampler) return true;
72 if (coords < rhs.coords) return true;
73 if (!proj && rhs.proj) return true;
Nicolas Capens75fb4752013-07-10 15:14:47 -040074 if (method < rhs.method) return true;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040075
76 return false;
77}
78
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +000079OutputHLSL::OutputHLSL(TParseContext &context, const ShBuiltInResources& resources, ShShaderOutput outputType)
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +000080 : TIntermTraverser(true, true, true), mContext(context), mOutputType(outputType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000081{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000082 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +000083 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000084
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +000085 mUsesFragColor = false;
86 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +000087 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +000088 mUsesFragCoord = false;
89 mUsesPointCoord = false;
90 mUsesFrontFacing = false;
91 mUsesPointSize = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -040092 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +000093 mUsesXor = false;
94 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +000095 mUsesMod2v = false;
96 mUsesMod2f = false;
97 mUsesMod3v = false;
98 mUsesMod3f = false;
99 mUsesMod4v = false;
100 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000101 mUsesFaceforward1 = false;
102 mUsesFaceforward2 = false;
103 mUsesFaceforward3 = false;
104 mUsesFaceforward4 = false;
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000105
106 for (unsigned int col = 0; col <= 4; col++)
107 {
108 for (unsigned int row = 0; row <= 4; row++)
109 {
110 mUsesEqualMat[col][row] = false;
111 }
112 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000113 mUsesEqualVec2 = false;
114 mUsesEqualVec3 = false;
115 mUsesEqualVec4 = false;
116 mUsesEqualIVec2 = false;
117 mUsesEqualIVec3 = false;
118 mUsesEqualIVec4 = false;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +0000119 mUsesEqualUVec2 = false;
120 mUsesEqualUVec3 = false;
121 mUsesEqualUVec4 = false;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000122 mUsesEqualBVec2 = false;
123 mUsesEqualBVec3 = false;
124 mUsesEqualBVec4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000125 mUsesAtan2_1 = false;
126 mUsesAtan2_2 = false;
127 mUsesAtan2_3 = false;
128 mUsesAtan2_4 = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000129
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000130 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
131
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000132 mScopeDepth = 0;
133
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000134 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000135
136 mContainsLoopDiscontinuity = false;
137 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000138 mInsideDiscontinuousLoop = false;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000139
140 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000141
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000142 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000143 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000144 if (mContext.shaderType == SH_FRAGMENT_SHADER)
145 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000146 mUniformRegister = 3; // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000147 }
148 else
149 {
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000150 mUniformRegister = 2; // Reserve registers for dx_DepthRange and dx_ViewAdjust
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000151 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000152 }
153 else
154 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000155 mUniformRegister = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000156 }
157
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000158 mSamplerRegister = 0;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000159 mInterfaceBlockRegister = 2; // Reserve registers for the default uniform block and driver constants
Jamie Madill574d9dd2013-06-20 11:55:56 -0400160 mPaddingCounter = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000161}
162
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000163OutputHLSL::~OutputHLSL()
164{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000165 delete mUnfoldShortCircuit;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000166}
167
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000168void OutputHLSL::output()
169{
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +0000170 mContainsLoopDiscontinuity = mContext.shaderType == SH_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400171 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
172 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000173
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000174 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 +0000175 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000176
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000177 mContext.infoSink().obj << mHeader.c_str();
178 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000179}
180
Jamie Madill570e04d2013-06-21 09:15:33 -0400181void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
182{
183 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
184 {
185 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
186
187 // This will mark the necessary block elements as referenced
188 flaggedNode->traverse(this);
189 TString structName(mBody.c_str());
190 mBody.erase();
191
192 mFlaggedStructOriginalNames[flaggedNode] = structName;
193
194 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
195 {
196 structName.erase(pos, 1);
197 }
198
199 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
200 }
201}
202
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000203TInfoSinkBase &OutputHLSL::getBodyStream()
204{
205 return mBody;
206}
207
daniel@transgaming.com043da132012-12-20 21:12:22 +0000208const ActiveUniforms &OutputHLSL::getUniforms()
209{
210 return mActiveUniforms;
211}
212
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000213const ActiveInterfaceBlocks &OutputHLSL::getInterfaceBlocks() const
214{
215 return mActiveInterfaceBlocks;
216}
217
Jamie Madill46131a32013-06-20 11:55:50 -0400218const ActiveShaderVariables &OutputHLSL::getOutputVariables() const
219{
220 return mActiveOutputVariables;
221}
222
Jamie Madilldefb6742013-06-20 11:55:51 -0400223const ActiveShaderVariables &OutputHLSL::getAttributes() const
224{
225 return mActiveAttributes;
226}
227
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000228int OutputHLSL::vectorSize(const TType &type) const
229{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000230 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000231 int arraySize = type.isArray() ? type.getArraySize() : 1;
232
233 return elementSize * arraySize;
234}
235
Jamie Madill98493dd2013-07-08 14:39:03 -0400236TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, const TField &field)
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000237{
Jamie Madill98493dd2013-07-08 14:39:03 -0400238 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000239 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400240 return interfaceBlock.name() + "." + field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000241 }
242 else
243 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400244 return field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000245 }
246}
247
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000248TString OutputHLSL::decoratePrivate(const TString &privateText)
249{
250 return "dx_" + privateText;
251}
252
Jamie Madill98493dd2013-07-08 14:39:03 -0400253TString OutputHLSL::interfaceBlockStructNameString(const TInterfaceBlock &interfaceBlock)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000254{
Jamie Madill98493dd2013-07-08 14:39:03 -0400255 return decoratePrivate(interfaceBlock.name()) + "_type";
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000256}
257
Jamie Madill98493dd2013-07-08 14:39:03 -0400258TString OutputHLSL::interfaceBlockInstanceString(const TInterfaceBlock& interfaceBlock, unsigned int arrayIndex)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000259{
Jamie Madill98493dd2013-07-08 14:39:03 -0400260 if (!interfaceBlock.hasInstanceName())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000261 {
262 return "";
263 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400264 else if (interfaceBlock.isArray())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000265 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400266 return decoratePrivate(interfaceBlock.instanceName()) + "_" + str(arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000267 }
268 else
269 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400270 return decorate(interfaceBlock.instanceName());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000271 }
272}
273
Jamie Madill98493dd2013-07-08 14:39:03 -0400274TString OutputHLSL::interfaceBlockFieldTypeString(const TField &field, TLayoutBlockStorage blockStorage)
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000275{
Jamie Madill98493dd2013-07-08 14:39:03 -0400276 const TType &fieldType = *field.type();
277 const TLayoutMatrixPacking matrixPacking = fieldType.getLayoutQualifier().matrixPacking;
Jamie Madill529077d2013-06-20 11:55:54 -0400278 ASSERT(matrixPacking != EmpUnspecified);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000279
Jamie Madill98493dd2013-07-08 14:39:03 -0400280 if (fieldType.isMatrix())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000281 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400282 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill529077d2013-06-20 11:55:54 -0400283 const TString &matrixPackString = (matrixPacking == EmpRowMajor ? "column_major" : "row_major");
Jamie Madill98493dd2013-07-08 14:39:03 -0400284 return matrixPackString + " " + typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000285 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400286 else if (fieldType.getStruct())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000287 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400288 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill98493dd2013-07-08 14:39:03 -0400289 return structureTypeName(*fieldType.getStruct(), matrixPacking == EmpColumnMajor, blockStorage == EbsStd140);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000290 }
291 else
292 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400293 return typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000294 }
295}
296
Jamie Madill98493dd2013-07-08 14:39:03 -0400297TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, TLayoutBlockStorage blockStorage)
298{
299 TString hlsl;
300
301 int elementIndex = 0;
302
303 for (unsigned int typeIndex = 0; typeIndex < interfaceBlock.fields().size(); typeIndex++)
304 {
305 const TField &field = *interfaceBlock.fields()[typeIndex];
306 const TType &fieldType = *field.type();
307
308 if (blockStorage == EbsStd140)
309 {
310 // 2 and 3 component vector types in some cases need pre-padding
311 hlsl += std140PrePaddingString(fieldType, &elementIndex);
312 }
313
314 hlsl += " " + interfaceBlockFieldTypeString(field, blockStorage) +
315 " " + decorate(field.name()) + arrayString(fieldType) + ";\n";
316
317 // must pad out after matrices and arrays, where HLSL usually allows itself room to pack stuff
318 if (blockStorage == EbsStd140)
319 {
320 const bool useHLSLRowMajorPacking = (fieldType.getLayoutQualifier().matrixPacking == EmpColumnMajor);
321 hlsl += std140PostPaddingString(fieldType, useHLSLRowMajorPacking);
322 }
323 }
324
325 return hlsl;
326}
327
328TString OutputHLSL::interfaceBlockStructString(const TInterfaceBlock &interfaceBlock)
329{
330 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
331
332 return "struct " + interfaceBlockStructNameString(interfaceBlock) + "\n"
333 "{\n" +
334 interfaceBlockFieldString(interfaceBlock, blockStorage) +
335 "};\n\n";
336}
337
338TString OutputHLSL::interfaceBlockString(const TInterfaceBlock &interfaceBlock, unsigned int registerIndex, unsigned int arrayIndex)
339{
340 const TString &arrayIndexString = (arrayIndex != GL_INVALID_INDEX ? decorate(str(arrayIndex)) : "");
341 const TString &blockName = interfaceBlock.name() + arrayIndexString;
342 TString hlsl;
343
344 hlsl += "cbuffer " + blockName + " : register(b" + str(registerIndex) + ")\n"
345 "{\n";
346
347 if (interfaceBlock.hasInstanceName())
348 {
349 hlsl += " " + interfaceBlockStructNameString(interfaceBlock) + " " + interfaceBlockInstanceString(interfaceBlock, arrayIndex) + ";\n";
350 }
351 else
352 {
353 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
354 hlsl += interfaceBlockFieldString(interfaceBlock, blockStorage);
355 }
356
357 hlsl += "};\n\n";
358
359 return hlsl;
360}
361
Jamie Madill574d9dd2013-06-20 11:55:56 -0400362TString OutputHLSL::std140PrePaddingString(const TType &type, int *elementIndex)
363{
364 if (type.getBasicType() == EbtStruct || type.isMatrix() || type.isArray())
365 {
366 // no padding needed, HLSL will align the field to a new register
367 *elementIndex = 0;
368 return "";
369 }
370
371 const GLenum glType = glVariableType(type);
372 const int numComponents = gl::UniformComponentCount(glType);
373
374 if (numComponents >= 4)
375 {
376 // no padding needed, HLSL will align the field to a new register
377 *elementIndex = 0;
378 return "";
379 }
380
381 if (*elementIndex + numComponents > 4)
382 {
383 // no padding needed, HLSL will align the field to a new register
384 *elementIndex = numComponents;
385 return "";
386 }
387
388 TString padding;
389
390 const int alignment = numComponents == 3 ? 4 : numComponents;
391 const int paddingOffset = (*elementIndex % alignment);
392
393 if (paddingOffset != 0)
394 {
395 // padding is neccessary
396 for (int paddingIndex = paddingOffset; paddingIndex < alignment; paddingIndex++)
397 {
398 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
399 }
400
401 *elementIndex += (alignment - paddingOffset);
402 }
403
404 *elementIndex += numComponents;
405 *elementIndex %= 4;
406
407 return padding;
408}
409
Jamie Madille4075c92013-06-21 09:15:32 -0400410TString OutputHLSL::std140PostPaddingString(const TType &type, bool useHLSLRowMajorPacking)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400411{
Jamie Madillc835df62013-06-21 09:15:32 -0400412 if (!type.isMatrix() && !type.isArray() && type.getBasicType() != EbtStruct)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400413 {
414 return "";
415 }
416
Jamie Madill574d9dd2013-06-20 11:55:56 -0400417 int numComponents = 0;
418
419 if (type.isMatrix())
420 {
Jamie Madille4075c92013-06-21 09:15:32 -0400421 // This method can also be called from structureString, which does not use layout qualifiers.
422 // Thus, use the method parameter for determining the matrix packing.
423 //
424 // Note HLSL row major packing corresponds to GL API column-major, and vice-versa, since we
425 // wish to always transpose GL matrices to play well with HLSL's matrix array indexing.
426 //
427 const bool isRowMajorMatrix = !useHLSLRowMajorPacking;
Jamie Madillc835df62013-06-21 09:15:32 -0400428 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400429 numComponents = gl::MatrixComponentCount(glType, isRowMajorMatrix);
430 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400431 else if (type.getStruct())
Jamie Madillc835df62013-06-21 09:15:32 -0400432 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400433 const TString &structName = structureTypeName(*type.getStruct(), useHLSLRowMajorPacking, true);
Jamie Madille4075c92013-06-21 09:15:32 -0400434 numComponents = mStd140StructElementIndexes[structName];
435
436 if (numComponents == 0)
437 {
438 return "";
439 }
Jamie Madillc835df62013-06-21 09:15:32 -0400440 }
Jamie Madill574d9dd2013-06-20 11:55:56 -0400441 else
442 {
Jamie Madillc835df62013-06-21 09:15:32 -0400443 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400444 numComponents = gl::UniformComponentCount(glType);
445 }
446
447 TString padding;
448 for (int paddingOffset = numComponents; paddingOffset < 4; paddingOffset++)
449 {
450 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
451 }
452 return padding;
453}
454
Jamie Madill440dc742013-06-20 11:55:55 -0400455// Use the same layout for packed and shared
456void setBlockLayout(InterfaceBlock *interfaceBlock, BlockLayoutType newLayout)
457{
458 interfaceBlock->layout = newLayout;
459 interfaceBlock->blockInfo.clear();
460
461 switch (newLayout)
462 {
463 case BLOCKLAYOUT_SHARED:
464 case BLOCKLAYOUT_PACKED:
465 {
466 HLSLBlockEncoder hlslEncoder(&interfaceBlock->blockInfo);
467 hlslEncoder.encodeFields(interfaceBlock->activeUniforms);
468 interfaceBlock->dataSize = hlslEncoder.getBlockSize();
469 }
470 break;
471
472 case BLOCKLAYOUT_STANDARD:
473 {
474 Std140BlockEncoder stdEncoder(&interfaceBlock->blockInfo);
475 stdEncoder.encodeFields(interfaceBlock->activeUniforms);
476 interfaceBlock->dataSize = stdEncoder.getBlockSize();
477 }
478 break;
479
480 default:
481 UNREACHABLE();
482 break;
483 }
484}
485
Jamie Madill574d9dd2013-06-20 11:55:56 -0400486BlockLayoutType convertBlockLayoutType(TLayoutBlockStorage blockStorage)
487{
488 switch (blockStorage)
489 {
490 case EbsPacked: return BLOCKLAYOUT_PACKED;
491 case EbsShared: return BLOCKLAYOUT_SHARED;
492 case EbsStd140: return BLOCKLAYOUT_STANDARD;
493 default: UNREACHABLE(); return BLOCKLAYOUT_SHARED;
494 }
495}
496
Jamie Madill98493dd2013-07-08 14:39:03 -0400497TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400498{
499 TString init;
500
501 TString preIndentString;
502 TString fullIndentString;
503
504 for (int spaces = 0; spaces < (indent * 4); spaces++)
505 {
506 preIndentString += ' ';
507 }
508
509 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
510 {
511 fullIndentString += ' ';
512 }
513
514 init += preIndentString + "{\n";
515
Jamie Madill98493dd2013-07-08 14:39:03 -0400516 const TFieldList &fields = structure.fields();
517 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400518 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400519 const TField &field = *fields[fieldIndex];
520 const TString &fieldName = rhsStructName + "." + decorate(field.name());
521 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400522
Jamie Madill98493dd2013-07-08 14:39:03 -0400523 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400524 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400525 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400526 }
527 else
528 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400529 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400530 }
531 }
532
533 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
534
535 return init;
536}
537
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000538void OutputHLSL::header()
539{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000540 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000541
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000542 TString uniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000543 TString interfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000544 TString varyings;
545 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400546 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000547
548 for (ReferencedSymbols::const_iterator uniform = mReferencedUniforms.begin(); uniform != mReferencedUniforms.end(); uniform++)
549 {
550 const TType &type = uniform->second->getType();
551 const TString &name = uniform->second->getSymbol();
552
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000553 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture
554 {
555 int index = samplerRegister(mReferencedUniforms[name]);
556
557 uniforms += "uniform SamplerState sampler_" + decorateUniform(name, type) + arrayString(type) +
558 " : register(s" + str(index) + ");\n";
559
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000560 uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) +
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000561 " : register(t" + str(index) + ");\n";
562 }
563 else
564 {
565 uniforms += "uniform " + typeString(type) + " " + decorateUniform(name, type) + arrayString(type) +
566 " : register(" + registerString(mReferencedUniforms[name]) + ");\n";
567 }
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000568 }
569
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000570 for (ReferencedSymbols::const_iterator interfaceBlockIt = mReferencedInterfaceBlocks.begin(); interfaceBlockIt != mReferencedInterfaceBlocks.end(); interfaceBlockIt++)
571 {
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000572 const TType &nodeType = interfaceBlockIt->second->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -0400573 const TInterfaceBlock &interfaceBlock = *nodeType.getInterfaceBlock();
574 const TFieldList &fieldList = interfaceBlock.fields();
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000575
Jamie Madill98493dd2013-07-08 14:39:03 -0400576 unsigned int arraySize = static_cast<unsigned int>(interfaceBlock.arraySize());
577 sh::InterfaceBlock activeBlock(interfaceBlock.name().c_str(), arraySize, mInterfaceBlockRegister);
578 for (unsigned int typeIndex = 0; typeIndex < fieldList.size(); typeIndex++)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000579 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400580 const TField &field = *fieldList[typeIndex];
581 const TString &fullUniformName = interfaceBlockFieldString(interfaceBlock, field);
582 declareUniformToList(*field.type(), fullUniformName, typeIndex, activeBlock.activeUniforms);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000583 }
584
Jamie Madill98493dd2013-07-08 14:39:03 -0400585 mInterfaceBlockRegister += std::max(1u, arraySize);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000586
Jamie Madill98493dd2013-07-08 14:39:03 -0400587 BlockLayoutType blockLayoutType = convertBlockLayoutType(interfaceBlock.blockStorage());
588 setBlockLayout(&activeBlock, blockLayoutType);
589 mActiveInterfaceBlocks.push_back(activeBlock);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000590
Jamie Madill98493dd2013-07-08 14:39:03 -0400591 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000592 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400593 interfaceBlocks += interfaceBlockStructString(interfaceBlock);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000594 }
595
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000596 if (arraySize > 0)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000597 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000598 for (unsigned int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
599 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400600 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex + arrayIndex, arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000601 }
602 }
603 else
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000604 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400605 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000606 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000607 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000608
Jamie Madill570e04d2013-06-21 09:15:33 -0400609 for (auto flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
610 {
611 TIntermTyped *structNode = flaggedStructIt->first;
612 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400613 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400614 const TString &originalName = mFlaggedStructOriginalNames[structNode];
615
Jamie Madill98493dd2013-07-08 14:39:03 -0400616 flaggedStructs += "static " + decorate(structure.name()) + " " + mappedName + " =\n";
617 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400618 flaggedStructs += "\n";
619 }
620
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000621 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
622 {
623 const TType &type = varying->second->getType();
624 const TString &name = varying->second->getSymbol();
625
626 // Program linking depends on this exact format
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +0000627 varyings += "static " + interpolationString(type.getQualifier()) + " " + typeString(type) + " " +
628 decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000629 }
630
631 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
632 {
633 const TType &type = attribute->second->getType();
634 const TString &name = attribute->second->getSymbol();
635
636 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400637
638 ShaderVariable shaderVar(glVariableType(type), glVariablePrecision(type), name.c_str(),
639 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
640 mActiveAttributes.push_back(shaderVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000641 }
642
Jamie Madill529077d2013-06-20 11:55:54 -0400643 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
644 {
645 out << *structDeclaration;
646 }
647
648 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
649 {
650 out << *constructor;
651 }
652
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400653 if (mContext.shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000654 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000655 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000656 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000657
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000658 out << "// Varyings\n";
659 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400660 out << "\n";
661
662 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000663 {
Jamie Madill46131a32013-06-20 11:55:50 -0400664 for (auto outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000665 {
Jamie Madill46131a32013-06-20 11:55:50 -0400666 const TString &variableName = outputVariableIt->first;
667 const TType &variableType = outputVariableIt->second->getType();
668 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
669
670 out << "static " + typeString(variableType) + " out_" + variableName + arrayString(variableType) +
671 " = " + initializer(variableType) + ";\n";
672
673 ShaderVariable outputVar(glVariableType(variableType), glVariablePrecision(variableType), variableName.c_str(),
674 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
675 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000676 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000677 }
Jamie Madill46131a32013-06-20 11:55:50 -0400678 else
679 {
680 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
681
682 out << "static float4 gl_Color[" << numColorValues << "] =\n"
683 "{\n";
684 for (unsigned int i = 0; i < numColorValues; i++)
685 {
686 out << " float4(0, 0, 0, 0)";
687 if (i + 1 != numColorValues)
688 {
689 out << ",";
690 }
691 out << "\n";
692 }
693
694 out << "};\n";
695 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000696
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400697 if (mUsesFragDepth)
698 {
699 out << "static float gl_Depth = 0.0;\n";
700 }
701
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000702 if (mUsesFragCoord)
703 {
704 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
705 }
706
707 if (mUsesPointCoord)
708 {
709 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
710 }
711
712 if (mUsesFrontFacing)
713 {
714 out << "static bool gl_FrontFacing = false;\n";
715 }
716
717 out << "\n";
718
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000719 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000720 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000721 out << "struct gl_DepthRangeParameters\n"
722 "{\n"
723 " float near;\n"
724 " float far;\n"
725 " float diff;\n"
726 "};\n"
727 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000728 }
729
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000730 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000731 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000732 out << "cbuffer DriverConstants : register(b1)\n"
733 "{\n";
734
735 if (mUsesDepthRange)
736 {
737 out << " float3 dx_DepthRange : packoffset(c0);\n";
738 }
739
740 if (mUsesFragCoord)
741 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000742 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000743 }
744
745 if (mUsesFragCoord || mUsesFrontFacing)
746 {
747 out << " float3 dx_DepthFront : packoffset(c2);\n";
748 }
749
750 out << "};\n";
751 }
752 else
753 {
754 if (mUsesDepthRange)
755 {
756 out << "uniform float3 dx_DepthRange : register(c0);";
757 }
758
759 if (mUsesFragCoord)
760 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000761 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000762 }
763
764 if (mUsesFragCoord || mUsesFrontFacing)
765 {
766 out << "uniform float3 dx_DepthFront : register(c2);\n";
767 }
768 }
769
770 out << "\n";
771
772 if (mUsesDepthRange)
773 {
774 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
775 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000776 }
777
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000778 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000779 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000780
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000781 if (!interfaceBlocks.empty())
782 {
783 out << interfaceBlocks;
784 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400785
786 if (!flaggedStructs.empty())
787 {
788 out << "// Std140 Structures accessed by value\n";
789 out << "\n";
790 out << flaggedStructs;
791 out << "\n";
792 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000793 }
794
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000795 if (usingMRTExtension && mNumRenderTargets > 1)
796 {
797 out << "#define GL_USES_MRT\n";
798 }
799
800 if (mUsesFragColor)
801 {
802 out << "#define GL_USES_FRAG_COLOR\n";
803 }
804
805 if (mUsesFragData)
806 {
807 out << "#define GL_USES_FRAG_DATA\n";
808 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000809 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000810 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000811 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000812 out << "// Attributes\n";
813 out << attributes;
814 out << "\n"
815 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
816
817 if (mUsesPointSize)
818 {
819 out << "static float gl_PointSize = float(1);\n";
820 }
821
822 out << "\n"
823 "// Varyings\n";
824 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000825 out << "\n";
826
827 if (mUsesDepthRange)
828 {
829 out << "struct gl_DepthRangeParameters\n"
830 "{\n"
831 " float near;\n"
832 " float far;\n"
833 " float diff;\n"
834 "};\n"
835 "\n";
836 }
837
838 if (mOutputType == SH_HLSL11_OUTPUT)
839 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000840 if (mUsesDepthRange)
841 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000842 out << "cbuffer DriverConstants : register(b1)\n"
843 "{\n"
844 " float3 dx_DepthRange : packoffset(c0);\n"
845 "};\n"
846 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000847 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000848 }
849 else
850 {
851 if (mUsesDepthRange)
852 {
853 out << "uniform float3 dx_DepthRange : register(c0);\n";
854 }
855
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000856 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000857 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000858 }
859
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000860 if (mUsesDepthRange)
861 {
862 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
863 "\n";
864 }
865
866 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000867 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000868
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000869 if (!interfaceBlocks.empty())
870 {
871 out << interfaceBlocks;
872 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400873
874 if (!flaggedStructs.empty())
875 {
876 out << "// Std140 Structures accessed by value\n";
877 out << "\n";
878 out << flaggedStructs;
879 out << "\n";
880 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000881 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400882 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000883
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400884 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
885 {
886 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400887 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000888 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400889 switch(textureFunction->sampler)
890 {
891 case EbtSampler2D: out << "int2 "; break;
892 case EbtSampler3D: out << "int3 "; break;
893 case EbtSamplerCube: out << "int2 "; break;
894 case EbtSampler2DArray: out << "int3 "; break;
895 case EbtISampler2D: out << "int2 "; break;
896 case EbtISampler3D: out << "int3 "; break;
897 case EbtISamplerCube: out << "int2 "; break;
898 case EbtISampler2DArray: out << "int3 "; break;
899 case EbtUSampler2D: out << "int2 "; break;
900 case EbtUSampler3D: out << "int3 "; break;
901 case EbtUSamplerCube: out << "int2 "; break;
902 case EbtUSampler2DArray: out << "int3 "; break;
903 default: UNREACHABLE();
904 }
905 }
906 else // Sampling function
907 {
908 switch(textureFunction->sampler)
909 {
910 case EbtSampler2D: out << "float4 "; break;
911 case EbtSampler3D: out << "float4 "; break;
912 case EbtSamplerCube: out << "float4 "; break;
913 case EbtSampler2DArray: out << "float4 "; break;
914 case EbtISampler2D: out << "int4 "; break;
915 case EbtISampler3D: out << "int4 "; break;
916 case EbtISamplerCube: out << "int4 "; break;
917 case EbtISampler2DArray: out << "int4 "; break;
918 case EbtUSampler2D: out << "uint4 "; break;
919 case EbtUSampler3D: out << "uint4 "; break;
920 case EbtUSamplerCube: out << "uint4 "; break;
921 case EbtUSampler2DArray: out << "uint4 "; break;
922 default: UNREACHABLE();
923 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000924 }
925
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400926 // Function name
927 out << textureFunction->name();
928
929 // Argument list
930 int hlslCoords = 4;
931
932 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000933 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400934 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000935 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400936 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
937 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
938 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000939 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400940
Nicolas Capens75fb4752013-07-10 15:14:47 -0400941 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000942 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400943 case TextureFunction::IMPLICIT: break;
944 case TextureFunction::BIAS: hlslCoords = 4; break;
945 case TextureFunction::LOD: hlslCoords = 4; break;
946 case TextureFunction::LOD0: hlslCoords = 4; break;
947 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000948 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400949 }
950 else if (mOutputType == SH_HLSL11_OUTPUT)
951 {
952 switch(textureFunction->sampler)
953 {
Nicolas Capensfb50dff2013-06-24 16:16:23 -0400954 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
955 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
956 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens93e50de2013-07-09 13:46:28 -0400957 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capensfb50dff2013-06-24 16:16:23 -0400958 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
959 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
960 case EbtISamplerCube: out << "TextureCube<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens93e50de2013-07-09 13:46:28 -0400961 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capensfb50dff2013-06-24 16:16:23 -0400962 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
963 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
964 case EbtUSamplerCube: out << "TextureCube<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens93e50de2013-07-09 13:46:28 -0400965 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400966 default: UNREACHABLE();
967 }
968 }
969 else UNREACHABLE();
970
971 switch(textureFunction->coords)
972 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400973 case 1: out << ", int lod"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400974 case 2: out << ", float2 t"; break;
975 case 3: out << ", float3 t"; break;
976 case 4: out << ", float4 t"; break;
977 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000978 }
979
Nicolas Capens75fb4752013-07-10 15:14:47 -0400980 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000981 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400982 case TextureFunction::IMPLICIT: break;
983 case TextureFunction::BIAS: out << ", float bias"; break;
984 case TextureFunction::LOD: out << ", float lod"; break;
985 case TextureFunction::LOD0: break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400986 case TextureFunction::SIZE: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400987 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000988 }
989
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400990 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400991 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400992
Nicolas Capens75fb4752013-07-10 15:14:47 -0400993 if (textureFunction->method == TextureFunction::SIZE)
994 {
995 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
996 {
997 if (IsSamplerArray(textureFunction->sampler))
998 {
999 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
1000 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
1001 }
1002 else
1003 {
1004 out << " uint width; uint height; uint numberOfLevels;\n"
1005 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
1006 }
1007 }
1008 else if (IsSampler3D(textureFunction->sampler))
1009 {
1010 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
1011 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
1012 }
1013 else UNREACHABLE();
1014
1015 switch(textureFunction->sampler)
1016 {
1017 case EbtSampler2D: out << " return int2(width, height);"; break;
1018 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
1019 case EbtSamplerCube: out << " return int2(width, height);"; break;
1020 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
1021 case EbtISampler2D: out << " return int2(width, height);"; break;
1022 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
1023 case EbtISamplerCube: out << " return int2(width, height);"; break;
1024 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
1025 case EbtUSampler2D: out << " return int2(width, height);"; break;
1026 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
1027 case EbtUSamplerCube: out << " return int2(width, height);"; break;
1028 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
1029 default: UNREACHABLE();
1030 }
1031 }
1032 else if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
daniel@transgaming.com15795192011-05-11 15:36:20 +00001033 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001034 // Currently unsupported because TextureCube does not support Load
1035 // This will require emulation using a Texture2DArray with 6 faces
1036 if (textureFunction->sampler == EbtISamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001037 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001038 out << " return int4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001039 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001040 else if (textureFunction->sampler == EbtUSamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001041 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001042 out << " return uint4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001043 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001044 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001045 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001046 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001047 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001048 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001049 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001050 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001051 {
Nicolas Capens93e50de2013-07-09 13:46:28 -04001052 if (IsSamplerArray(textureFunction->sampler))
1053 {
Nicolas Capensc98406a2013-07-10 14:52:44 -04001054 out << " float width; float height; float layers;\n"
1055 " x.GetDimensions(width, height, layers);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001056 }
1057 else
1058 {
Nicolas Capensc98406a2013-07-10 14:52:44 -04001059 out << " float width; float height;\n"
1060 " x.GetDimensions(width, height);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001061 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001062 }
1063 else if (IsSampler3D(textureFunction->sampler))
1064 {
Nicolas Capensc98406a2013-07-10 14:52:44 -04001065 out << " float width; float height; float depth;\n"
1066 " x.GetDimensions(width, height, depth);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001067 }
1068 else UNREACHABLE();
1069 }
1070
1071 out << " return ";
1072
1073 // HLSL intrinsic
1074 if (mOutputType == SH_HLSL9_OUTPUT)
1075 {
1076 switch(textureFunction->sampler)
1077 {
1078 case EbtSampler2D: out << "tex2D"; break;
1079 case EbtSamplerCube: out << "texCUBE"; break;
1080 default: UNREACHABLE();
1081 }
1082
Nicolas Capens75fb4752013-07-10 15:14:47 -04001083 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001084 {
1085 case TextureFunction::IMPLICIT: out << "(s, "; break;
1086 case TextureFunction::BIAS: out << "bias(s, "; break;
1087 case TextureFunction::LOD: out << "lod(s, "; break;
1088 case TextureFunction::LOD0: out << "lod(s, "; break;
1089 default: UNREACHABLE();
1090 }
1091 }
1092 else if (mOutputType == SH_HLSL11_OUTPUT)
1093 {
1094 if (IsIntegerSampler(textureFunction->sampler))
1095 {
1096 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001097 }
1098 else
1099 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001100 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001101 {
1102 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1103 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1104 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1105 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
1106 default: UNREACHABLE();
1107 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001108 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001109 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001110 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001111
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001112 // Integer sampling requires integer addresses
1113 TString addressx = "";
1114 TString addressy = "";
1115 TString addressz = "";
1116 TString close = "";
1117
1118 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001119 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001120 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001121 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001122 case 2: out << "int3("; break;
1123 case 3: out << "int4("; break;
1124 default: UNREACHABLE();
1125 }
1126
Nicolas Capensc98406a2013-07-10 14:52:44 -04001127 addressx = "int(floor(width * frac((";
1128 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001129
1130 if (IsSamplerArray(textureFunction->sampler))
1131 {
1132 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1133 }
1134 else
1135 {
Nicolas Capensc98406a2013-07-10 14:52:44 -04001136 addressz = "int(floor(depth * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001137 }
1138
1139 close = "))))";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001140 }
1141 else
1142 {
1143 switch(hlslCoords)
1144 {
1145 case 2: out << "float2("; break;
1146 case 3: out << "float3("; break;
1147 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001148 default: UNREACHABLE();
1149 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001150 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001151
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001152 TString proj = ""; // Only used for projected textures
1153
1154 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001155 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001156 switch(textureFunction->coords)
1157 {
1158 case 3: proj = " / t.z"; break;
1159 case 4: proj = " / t.w"; break;
1160 default: UNREACHABLE();
1161 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001162 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001163
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001164 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001165
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001166 if (mOutputType == SH_HLSL9_OUTPUT)
1167 {
1168 if (hlslCoords >= 3)
1169 {
1170 if (textureFunction->coords < 3)
1171 {
1172 out << ", 0";
1173 }
1174 else
1175 {
1176 out << ", t.z" + proj;
1177 }
1178 }
1179
1180 if (hlslCoords == 4)
1181 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001182 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001183 {
1184 case TextureFunction::BIAS: out << ", bias"; break;
1185 case TextureFunction::LOD: out << ", lod"; break;
1186 case TextureFunction::LOD0: out << ", 0"; break;
1187 default: UNREACHABLE();
1188 }
1189 }
1190
1191 out << "));\n";
1192 }
1193 else if (mOutputType == SH_HLSL11_OUTPUT)
1194 {
1195 if (hlslCoords >= 3)
1196 {
1197 out << ", " + addressz + ("t.z" + proj) + close;
1198 }
1199
1200 if (!IsIntegerSampler(textureFunction->sampler))
1201 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001202 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001203 {
1204 case TextureFunction::IMPLICIT: out << "));"; break;
1205 case TextureFunction::BIAS: out << "), bias);"; break;
1206 case TextureFunction::LOD: out << "), lod);"; break;
1207 case TextureFunction::LOD0: out << "), 0);"; break;
1208 default: UNREACHABLE();
1209 }
1210 }
1211 else
1212 {
1213 out << ", 0));"; // Sample from the top level
1214 }
1215 }
1216 else UNREACHABLE();
1217 }
1218
1219 out << "\n"
1220 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001221 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001222 }
1223
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001224 if (mUsesFragCoord)
1225 {
1226 out << "#define GL_USES_FRAG_COORD\n";
1227 }
1228
1229 if (mUsesPointCoord)
1230 {
1231 out << "#define GL_USES_POINT_COORD\n";
1232 }
1233
1234 if (mUsesFrontFacing)
1235 {
1236 out << "#define GL_USES_FRONT_FACING\n";
1237 }
1238
1239 if (mUsesPointSize)
1240 {
1241 out << "#define GL_USES_POINT_SIZE\n";
1242 }
1243
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001244 if (mUsesFragDepth)
1245 {
1246 out << "#define GL_USES_FRAG_DEPTH\n";
1247 }
1248
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001249 if (mUsesDepthRange)
1250 {
1251 out << "#define GL_USES_DEPTH_RANGE\n";
1252 }
1253
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001254 if (mUsesXor)
1255 {
1256 out << "bool xor(bool p, bool q)\n"
1257 "{\n"
1258 " return (p || q) && !(p && q);\n"
1259 "}\n"
1260 "\n";
1261 }
1262
1263 if (mUsesMod1)
1264 {
1265 out << "float mod(float x, float y)\n"
1266 "{\n"
1267 " return x - y * floor(x / y);\n"
1268 "}\n"
1269 "\n";
1270 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001271
1272 if (mUsesMod2v)
1273 {
1274 out << "float2 mod(float2 x, float2 y)\n"
1275 "{\n"
1276 " return x - y * floor(x / y);\n"
1277 "}\n"
1278 "\n";
1279 }
1280
1281 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001282 {
1283 out << "float2 mod(float2 x, float y)\n"
1284 "{\n"
1285 " return x - y * floor(x / y);\n"
1286 "}\n"
1287 "\n";
1288 }
1289
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001290 if (mUsesMod3v)
1291 {
1292 out << "float3 mod(float3 x, float3 y)\n"
1293 "{\n"
1294 " return x - y * floor(x / y);\n"
1295 "}\n"
1296 "\n";
1297 }
1298
1299 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001300 {
1301 out << "float3 mod(float3 x, float y)\n"
1302 "{\n"
1303 " return x - y * floor(x / y);\n"
1304 "}\n"
1305 "\n";
1306 }
1307
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001308 if (mUsesMod4v)
1309 {
1310 out << "float4 mod(float4 x, float4 y)\n"
1311 "{\n"
1312 " return x - y * floor(x / y);\n"
1313 "}\n"
1314 "\n";
1315 }
1316
1317 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001318 {
1319 out << "float4 mod(float4 x, float y)\n"
1320 "{\n"
1321 " return x - y * floor(x / y);\n"
1322 "}\n"
1323 "\n";
1324 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001325
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001326 if (mUsesFaceforward1)
1327 {
1328 out << "float faceforward(float N, float I, float Nref)\n"
1329 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001330 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001331 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001332 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001333 " }\n"
1334 " else\n"
1335 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001336 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001337 " }\n"
1338 "}\n"
1339 "\n";
1340 }
1341
1342 if (mUsesFaceforward2)
1343 {
1344 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1345 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001346 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001347 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001348 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001349 " }\n"
1350 " else\n"
1351 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001352 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001353 " }\n"
1354 "}\n"
1355 "\n";
1356 }
1357
1358 if (mUsesFaceforward3)
1359 {
1360 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1361 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001362 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001363 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001364 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001365 " }\n"
1366 " else\n"
1367 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001368 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001369 " }\n"
1370 "}\n"
1371 "\n";
1372 }
1373
1374 if (mUsesFaceforward4)
1375 {
1376 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1377 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001378 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001379 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001380 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001381 " }\n"
1382 " else\n"
1383 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001384 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001385 " }\n"
1386 "}\n"
1387 "\n";
1388 }
1389
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001390 for (unsigned int cols = 2; cols <= 4; cols++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001391 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001392 for (unsigned int rows = 2; rows <= 4; rows++)
1393 {
1394 if (mUsesEqualMat[cols][rows])
1395 {
1396 TString matrixType = "float" + str(cols) + "x" + str(rows);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001397
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001398 out << "bool equal(" + matrixType + " m, " + matrixType + " n)\n"
1399 "{\n";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001400
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001401 for (unsigned int row = 0; row < rows; row++)
1402 {
1403 if (row == 0)
1404 {
1405 out << " return ";
1406 }
1407 else
1408 {
1409 out << " ";
1410 }
1411
1412 for (unsigned int col = 0; col < cols; col++)
1413 {
1414 TString index = "[" + str(col) + "][" + str(row) + "]";
1415 out << "m" + index + " == n" + index;
1416
1417 if (col == cols-1 && row == rows-1)
1418 {
1419 out << ";\n";
1420 }
1421 else if (col == cols-1)
1422 {
1423 out << " &&\n";
1424 }
1425 else
1426 {
1427 out << " && ";
1428 }
1429 }
1430 }
1431
1432 out << "}\n";
1433 }
1434 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001435 }
1436
1437 if (mUsesEqualVec2)
1438 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001439 out << "bool equal(float2 v, float2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001440 "{\n"
1441 " return v.x == u.x && v.y == u.y;\n"
1442 "}\n";
1443 }
1444
1445 if (mUsesEqualVec3)
1446 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001447 out << "bool equal(float3 v, float3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001448 "{\n"
1449 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1450 "}\n";
1451 }
1452
1453 if (mUsesEqualVec4)
1454 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001455 out << "bool equal(float4 v, float4 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 && v.w == u.w;\n"
1458 "}\n";
1459 }
1460
1461 if (mUsesEqualIVec2)
1462 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001463 out << "bool equal(int2 v, int2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001464 "{\n"
1465 " return v.x == u.x && v.y == u.y;\n"
1466 "}\n";
1467 }
1468
1469 if (mUsesEqualIVec3)
1470 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001471 out << "bool equal(int3 v, int3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001472 "{\n"
1473 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1474 "}\n";
1475 }
1476
1477 if (mUsesEqualIVec4)
1478 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001479 out << "bool equal(int4 v, int4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001480 "{\n"
1481 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1482 "}\n";
1483 }
1484
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001485 if (mUsesEqualUVec2)
1486 {
1487 out << "bool equal(uint2 v, uint2 u)\n"
1488 "{\n"
1489 " return v.x == u.x && v.y == u.y;\n"
1490 "}\n";
1491 }
1492
1493 if (mUsesEqualUVec3)
1494 {
1495 out << "bool equal(uint3 v, uint3 u)\n"
1496 "{\n"
1497 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1498 "}\n";
1499 }
1500
1501 if (mUsesEqualUVec4)
1502 {
1503 out << "bool equal(uint4 v, uint4 u)\n"
1504 "{\n"
1505 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1506 "}\n";
1507 }
1508
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001509 if (mUsesEqualBVec2)
1510 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001511 out << "bool equal(bool2 v, bool2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001512 "{\n"
1513 " return v.x == u.x && v.y == u.y;\n"
1514 "}\n";
1515 }
1516
1517 if (mUsesEqualBVec3)
1518 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001519 out << "bool equal(bool3 v, bool3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001520 "{\n"
1521 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1522 "}\n";
1523 }
1524
1525 if (mUsesEqualBVec4)
1526 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001527 out << "bool equal(bool4 v, bool4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001528 "{\n"
1529 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1530 "}\n";
1531 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001532
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001533 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001534 {
1535 out << "float atanyx(float y, float x)\n"
1536 "{\n"
1537 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1538 " return atan2(y, x);\n"
1539 "}\n";
1540 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001541
1542 if (mUsesAtan2_2)
1543 {
1544 out << "float2 atanyx(float2 y, float2 x)\n"
1545 "{\n"
1546 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1547 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1548 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1549 "}\n";
1550 }
1551
1552 if (mUsesAtan2_3)
1553 {
1554 out << "float3 atanyx(float3 y, float3 x)\n"
1555 "{\n"
1556 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1557 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1558 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1559 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1560 "}\n";
1561 }
1562
1563 if (mUsesAtan2_4)
1564 {
1565 out << "float4 atanyx(float4 y, float4 x)\n"
1566 "{\n"
1567 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1568 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1569 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1570 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1571 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1572 "}\n";
1573 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001574}
1575
1576void OutputHLSL::visitSymbol(TIntermSymbol *node)
1577{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001578 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001579
Jamie Madill570e04d2013-06-21 09:15:33 -04001580 // Handle accessing std140 structs by value
1581 if (mFlaggedStructMappedNames.count(node) > 0)
1582 {
1583 out << mFlaggedStructMappedNames[node];
1584 return;
1585 }
1586
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001587 TString name = node->getSymbol();
1588
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001589 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001590 {
1591 mUsesDepthRange = true;
1592 out << name;
1593 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001594 else
1595 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001596 TQualifier qualifier = node->getQualifier();
1597
1598 if (qualifier == EvqUniform)
1599 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001600 const TType& nodeType = node->getType();
1601 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1602
1603 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001604 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001605 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001606 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001607 else
1608 {
1609 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001610 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001611
1612 out << decorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001613 }
Jamie Madillb120eac2013-06-12 14:08:13 -04001614 else if (qualifier == EvqAttribute || qualifier == EvqVertexInput)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001615 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001616 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001617 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001618 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001619 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001620 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001621 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001622 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001623 }
Jamie Madill46131a32013-06-20 11:55:50 -04001624 else if (qualifier == EvqFragmentOutput)
1625 {
1626 mReferencedOutputVariables[name] = node;
1627 out << "out_" << name;
1628 }
1629 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001630 {
1631 out << "gl_Color[0]";
1632 mUsesFragColor = true;
1633 }
1634 else if (qualifier == EvqFragData)
1635 {
1636 out << "gl_Color";
1637 mUsesFragData = true;
1638 }
1639 else if (qualifier == EvqFragCoord)
1640 {
1641 mUsesFragCoord = true;
1642 out << name;
1643 }
1644 else if (qualifier == EvqPointCoord)
1645 {
1646 mUsesPointCoord = true;
1647 out << name;
1648 }
1649 else if (qualifier == EvqFrontFacing)
1650 {
1651 mUsesFrontFacing = true;
1652 out << name;
1653 }
1654 else if (qualifier == EvqPointSize)
1655 {
1656 mUsesPointSize = true;
1657 out << name;
1658 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001659 else if (name == "gl_FragDepthEXT")
1660 {
1661 mUsesFragDepth = true;
1662 out << "gl_Depth";
1663 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001664 else
1665 {
1666 out << decorate(name);
1667 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001668 }
1669}
1670
1671bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1672{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001673 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001674
Jamie Madill570e04d2013-06-21 09:15:33 -04001675 // Handle accessing std140 structs by value
1676 if (mFlaggedStructMappedNames.count(node) > 0)
1677 {
1678 out << mFlaggedStructMappedNames[node];
1679 return false;
1680 }
1681
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001682 switch (node->getOp())
1683 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001684 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001685 case EOpInitialize:
1686 if (visit == PreVisit)
1687 {
1688 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1689 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1690 // new variable is created before the assignment is evaluated), so we need to convert
1691 // this to "float t = x, x = t;".
1692
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001693 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1694 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001695
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001696 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1697 expression->traverse(&searchSymbol);
1698 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001699
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001700 if (sameSymbol)
1701 {
1702 // Type already printed
1703 out << "t" + str(mUniqueIndex) + " = ";
1704 expression->traverse(this);
1705 out << ", ";
1706 symbolNode->traverse(this);
1707 out << " = t" + str(mUniqueIndex);
1708
1709 mUniqueIndex++;
1710 return false;
1711 }
1712 }
1713 else if (visit == InVisit)
1714 {
1715 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001716 }
1717 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001718 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1719 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1720 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1721 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1722 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1723 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001724 if (visit == PreVisit)
1725 {
1726 out << "(";
1727 }
1728 else if (visit == InVisit)
1729 {
1730 out << " = mul(";
1731 node->getLeft()->traverse(this);
1732 out << ", transpose(";
1733 }
1734 else
1735 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001736 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001737 }
1738 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001739 case EOpMatrixTimesMatrixAssign:
1740 if (visit == PreVisit)
1741 {
1742 out << "(";
1743 }
1744 else if (visit == InVisit)
1745 {
1746 out << " = mul(";
1747 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001748 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001749 }
1750 else
1751 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001752 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001753 }
1754 break;
1755 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001756 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001757 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001758 const TType& leftType = node->getLeft()->getType();
1759 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001760 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001761 if (visit == PreVisit)
1762 {
1763 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1764 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1765
1766 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1767 out << interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1768
1769 return false;
1770 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001771 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001772 else
1773 {
1774 outputTriplet(visit, "", "[", "]");
1775 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001776 }
1777 break;
1778 case EOpIndexIndirect:
1779 // We do not currently support indirect references to interface blocks
1780 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1781 outputTriplet(visit, "", "[", "]");
1782 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001783 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001784 if (visit == InVisit)
1785 {
1786 const TStructure* structure = node->getLeft()->getType().getStruct();
1787 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1788 const TField* field = structure->fields()[index->getIConst(0)];
1789 out << "." + decorateField(field->name(), *structure);
1790
1791 return false;
1792 }
1793 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001794 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001795 if (visit == InVisit)
1796 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001797 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1798 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1799 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
1800 out << "." + decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001801
1802 return false;
1803 }
1804 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001805 case EOpVectorSwizzle:
1806 if (visit == InVisit)
1807 {
1808 out << ".";
1809
1810 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1811
1812 if (swizzle)
1813 {
1814 TIntermSequence &sequence = swizzle->getSequence();
1815
1816 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1817 {
1818 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1819
1820 if (element)
1821 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001822 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001823
1824 switch (i)
1825 {
1826 case 0: out << "x"; break;
1827 case 1: out << "y"; break;
1828 case 2: out << "z"; break;
1829 case 3: out << "w"; break;
1830 default: UNREACHABLE();
1831 }
1832 }
1833 else UNREACHABLE();
1834 }
1835 }
1836 else UNREACHABLE();
1837
1838 return false; // Fully processed
1839 }
1840 break;
1841 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1842 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1843 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1844 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001845 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001846 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001847 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001848 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001849 if (node->getOp() == EOpEqual)
1850 {
1851 outputTriplet(visit, "(", " == ", ")");
1852 }
1853 else
1854 {
1855 outputTriplet(visit, "(", " != ", ")");
1856 }
1857 }
1858 else if (node->getLeft()->getBasicType() == EbtStruct)
1859 {
1860 if (node->getOp() == EOpEqual)
1861 {
1862 out << "(";
1863 }
1864 else
1865 {
1866 out << "!(";
1867 }
1868
Jamie Madill98493dd2013-07-08 14:39:03 -04001869 const TStructure &structure = *node->getLeft()->getType().getStruct();
1870 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001871
Jamie Madill98493dd2013-07-08 14:39:03 -04001872 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001873 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001874 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001875
1876 node->getLeft()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001877 out << "." + decorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001878 node->getRight()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001879 out << "." + decorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001880
Jamie Madill98493dd2013-07-08 14:39:03 -04001881 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001882 {
1883 out << " && ";
1884 }
1885 }
1886
1887 out << ")";
1888
1889 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001890 }
1891 else
1892 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001893 if (node->getLeft()->isMatrix())
1894 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001895 mUsesEqualMat[node->getLeft()->getCols()][node->getLeft()->getRows()] = true;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001896 }
1897 else if (node->getLeft()->isVector())
1898 {
1899 switch (node->getLeft()->getBasicType())
1900 {
1901 case EbtFloat:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001902 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001903 {
1904 case 2: mUsesEqualVec2 = true; break;
1905 case 3: mUsesEqualVec3 = true; break;
1906 case 4: mUsesEqualVec4 = true; break;
1907 default: UNREACHABLE();
1908 }
1909 break;
1910 case EbtInt:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001911 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001912 {
1913 case 2: mUsesEqualIVec2 = true; break;
1914 case 3: mUsesEqualIVec3 = true; break;
1915 case 4: mUsesEqualIVec4 = true; break;
1916 default: UNREACHABLE();
1917 }
1918 break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001919 case EbtUInt:
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001920 switch (node->getLeft()->getNominalSize())
1921 {
1922 case 2: mUsesEqualUVec2 = true; break;
1923 case 3: mUsesEqualUVec3 = true; break;
1924 case 4: mUsesEqualUVec4 = true; break;
1925 default: UNREACHABLE();
1926 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001927 break;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001928 case EbtBool:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001929 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001930 {
1931 case 2: mUsesEqualBVec2 = true; break;
1932 case 3: mUsesEqualBVec3 = true; break;
1933 case 4: mUsesEqualBVec4 = true; break;
1934 default: UNREACHABLE();
1935 }
1936 break;
1937 default: UNREACHABLE();
1938 }
1939 }
1940 else UNREACHABLE();
1941
1942 if (node->getOp() == EOpEqual)
1943 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001944 outputTriplet(visit, "equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001945 }
1946 else
1947 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001948 outputTriplet(visit, "!equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001949 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001950 }
1951 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001952 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1953 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1954 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1955 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1956 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001957 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001958 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1959 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001960 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001961 case EOpLogicalOr:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001962 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001963 return false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001964 case EOpLogicalXor:
1965 mUsesXor = true;
1966 outputTriplet(visit, "xor(", ", ", ")");
1967 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001968 case EOpLogicalAnd:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001969 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001970 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001971 default: UNREACHABLE();
1972 }
1973
1974 return true;
1975}
1976
1977bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1978{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001979 switch (node->getOp())
1980 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001981 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1982 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1983 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1984 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1985 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1986 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1987 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001988 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04001989 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001990 case EOpConvFloatToBool:
1991 switch (node->getOperand()->getType().getNominalSize())
1992 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001993 case 1: outputTriplet(visit, "bool(", "", ")"); break;
1994 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
1995 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
1996 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001997 default: UNREACHABLE();
1998 }
1999 break;
2000 case EOpConvBoolToFloat:
2001 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04002002 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002003 switch (node->getOperand()->getType().getNominalSize())
2004 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002005 case 1: outputTriplet(visit, "float(", "", ")"); break;
2006 case 2: outputTriplet(visit, "float2(", "", ")"); break;
2007 case 3: outputTriplet(visit, "float3(", "", ")"); break;
2008 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002009 default: UNREACHABLE();
2010 }
2011 break;
2012 case EOpConvFloatToInt:
2013 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04002014 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002015 switch (node->getOperand()->getType().getNominalSize())
2016 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002017 case 1: outputTriplet(visit, "int(", "", ")"); break;
2018 case 2: outputTriplet(visit, "int2(", "", ")"); break;
2019 case 3: outputTriplet(visit, "int3(", "", ")"); break;
2020 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002021 default: UNREACHABLE();
2022 }
2023 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002024 case EOpConvFloatToUInt:
2025 case EOpConvBoolToUInt:
2026 case EOpConvIntToUInt:
Jamie Madilla16f1672013-07-03 15:15:17 -04002027 switch (node->getOperand()->getType().getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002028 {
2029 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002030 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
2031 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
2032 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002033 default: UNREACHABLE();
2034 }
2035 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002036 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
2037 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
2038 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
2039 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
2040 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
2041 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
2042 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
2043 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
2044 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
2045 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
2046 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
2047 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
2048 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
2049 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
2050 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
2051 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
2052 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
2053 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
2054 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
2055 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
2056 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002057 case EOpDFdx:
2058 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2059 {
2060 outputTriplet(visit, "(", "", ", 0.0)");
2061 }
2062 else
2063 {
2064 outputTriplet(visit, "ddx(", "", ")");
2065 }
2066 break;
2067 case EOpDFdy:
2068 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2069 {
2070 outputTriplet(visit, "(", "", ", 0.0)");
2071 }
2072 else
2073 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00002074 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002075 }
2076 break;
2077 case EOpFwidth:
2078 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2079 {
2080 outputTriplet(visit, "(", "", ", 0.0)");
2081 }
2082 else
2083 {
2084 outputTriplet(visit, "fwidth(", "", ")");
2085 }
2086 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002087 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
2088 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002089 default: UNREACHABLE();
2090 }
2091
2092 return true;
2093}
2094
2095bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2096{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002097 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002098
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002099 switch (node->getOp())
2100 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002101 case EOpSequence:
2102 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002103 if (mInsideFunction)
2104 {
Jamie Madill075edd82013-07-08 13:30:19 -04002105 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002106 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002107
2108 mScopeDepth++;
2109
2110 if (mScopeBracket.size() < mScopeDepth)
2111 {
2112 mScopeBracket.push_back(0); // New scope level
2113 }
2114 else
2115 {
2116 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
2117 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002118 }
2119
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002120 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
2121 {
Jamie Madill075edd82013-07-08 13:30:19 -04002122 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002123
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002124 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002125
2126 out << ";\n";
2127 }
2128
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002129 if (mInsideFunction)
2130 {
Jamie Madill075edd82013-07-08 13:30:19 -04002131 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002132 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002133
2134 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002135 }
2136
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002137 return false;
2138 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002139 case EOpDeclaration:
2140 if (visit == PreVisit)
2141 {
2142 TIntermSequence &sequence = node->getSequence();
2143 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002144
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00002145 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002146 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002147 if (variable->getType().getStruct())
2148 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002149 addConstructor(variable->getType(), scopedStruct(variable->getType().getStruct()->name()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00002150 }
2151
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002152 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002153 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00002154 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002155 {
2156 out << "static ";
2157 }
2158
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002159 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002160
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002161 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002162 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002163 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002164
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002165 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002166 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002167 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002168 out << arrayString(symbol->getType());
daniel@transgaming.com7127f202010-04-15 20:45:22 +00002169 out << " = " + initializer(variable->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002170 }
2171 else
2172 {
2173 (*sit)->traverse(this);
2174 }
2175
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002176 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002177 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002178 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002179 }
2180 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002181 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002182 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2183 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002184 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002185 }
2186 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002187 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002188 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002189 {
2190 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2191 {
2192 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2193
2194 if (symbol)
2195 {
2196 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2197 mReferencedVaryings[symbol->getSymbol()] = symbol;
2198 }
2199 else
2200 {
2201 (*sit)->traverse(this);
2202 }
2203 }
2204 }
2205
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002206 return false;
2207 }
2208 else if (visit == InVisit)
2209 {
2210 out << ", ";
2211 }
2212 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002213 case EOpPrototype:
2214 if (visit == PreVisit)
2215 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002216 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002217
2218 TIntermSequence &arguments = node->getSequence();
2219
2220 for (unsigned int i = 0; i < arguments.size(); i++)
2221 {
2222 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2223
2224 if (symbol)
2225 {
2226 out << argumentString(symbol);
2227
2228 if (i < arguments.size() - 1)
2229 {
2230 out << ", ";
2231 }
2232 }
2233 else UNREACHABLE();
2234 }
2235
2236 out << ");\n";
2237
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002238 // Also prototype the Lod0 variant if needed
2239 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2240 {
2241 mOutputLod0Function = true;
2242 node->traverse(this);
2243 mOutputLod0Function = false;
2244 }
2245
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002246 return false;
2247 }
2248 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002249 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002250 case EOpFunction:
2251 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002252 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002253
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002254 out << typeString(node->getType()) << " ";
2255
2256 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002257 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002258 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002259 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002260 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002261 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002262 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002263 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002264
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002265 TIntermSequence &sequence = node->getSequence();
2266 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2267
2268 for (unsigned int i = 0; i < arguments.size(); i++)
2269 {
2270 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2271
2272 if (symbol)
2273 {
2274 if (symbol->getType().getStruct())
2275 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002276 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getStruct()->name()), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002277 }
2278
2279 out << argumentString(symbol);
2280
2281 if (i < arguments.size() - 1)
2282 {
2283 out << ", ";
2284 }
2285 }
2286 else UNREACHABLE();
2287 }
2288
2289 out << ")\n"
2290 "{\n";
2291
2292 if (sequence.size() > 1)
2293 {
2294 mInsideFunction = true;
2295 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002296 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002297 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002298
2299 out << "}\n";
2300
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002301 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2302 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002303 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002304 {
2305 mOutputLod0Function = true;
2306 node->traverse(this);
2307 mOutputLod0Function = false;
2308 }
2309 }
2310
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002311 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002312 }
2313 break;
2314 case EOpFunctionCall:
2315 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002316 TString name = TFunction::unmangleName(node->getName());
2317 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002318 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002319
2320 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002321 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002322 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002323 }
2324 else
2325 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002326 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2327
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002328 TextureFunction textureFunction;
2329 textureFunction.sampler = samplerType;
2330 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002331 textureFunction.method = TextureFunction::IMPLICIT;
2332 textureFunction.proj = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002333
2334 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002335 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002336 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002337 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002338 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002339 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002340 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002341 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002342 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002343 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002344 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002345 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002346 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002347 else if (name == "texture2DProjLod" || name == "textureProjLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002348 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002349 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002350 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002351 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002352 else if (name == "textureSize")
2353 {
2354 textureFunction.method = TextureFunction::SIZE;
2355 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002356 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002357
Nicolas Capens75fb4752013-07-10 15:14:47 -04002358 if (textureFunction.method != TextureFunction::LOD &&
2359 textureFunction.method != TextureFunction::SIZE)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002360 {
2361 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2362 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002363 textureFunction.method = TextureFunction::LOD0;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002364 }
2365 else if (arguments.size() == 3)
2366 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002367 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002368 }
2369 }
2370
2371 mUsesTexture.insert(textureFunction);
2372
2373 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002374 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002375
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002376 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2377 {
2378 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2379 {
2380 out << "texture_";
2381 (*arg)->traverse(this);
2382 out << ", sampler_";
2383 }
2384
2385 (*arg)->traverse(this);
2386
2387 if (arg < arguments.end() - 1)
2388 {
2389 out << ", ";
2390 }
2391 }
2392
2393 out << ")";
2394
2395 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002396 }
2397 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002398 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002399 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002400 addConstructor(node->getType(), "vec1", &node->getSequence());
2401 outputTriplet(visit, "vec1(", "", ")");
2402 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002403 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002404 addConstructor(node->getType(), "vec2", &node->getSequence());
2405 outputTriplet(visit, "vec2(", ", ", ")");
2406 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002407 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002408 addConstructor(node->getType(), "vec3", &node->getSequence());
2409 outputTriplet(visit, "vec3(", ", ", ")");
2410 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002411 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002412 addConstructor(node->getType(), "vec4", &node->getSequence());
2413 outputTriplet(visit, "vec4(", ", ", ")");
2414 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002415 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002416 addConstructor(node->getType(), "bvec1", &node->getSequence());
2417 outputTriplet(visit, "bvec1(", "", ")");
2418 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002419 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002420 addConstructor(node->getType(), "bvec2", &node->getSequence());
2421 outputTriplet(visit, "bvec2(", ", ", ")");
2422 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002423 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002424 addConstructor(node->getType(), "bvec3", &node->getSequence());
2425 outputTriplet(visit, "bvec3(", ", ", ")");
2426 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002427 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002428 addConstructor(node->getType(), "bvec4", &node->getSequence());
2429 outputTriplet(visit, "bvec4(", ", ", ")");
2430 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002431 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002432 addConstructor(node->getType(), "ivec1", &node->getSequence());
2433 outputTriplet(visit, "ivec1(", "", ")");
2434 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002435 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002436 addConstructor(node->getType(), "ivec2", &node->getSequence());
2437 outputTriplet(visit, "ivec2(", ", ", ")");
2438 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002439 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002440 addConstructor(node->getType(), "ivec3", &node->getSequence());
2441 outputTriplet(visit, "ivec3(", ", ", ")");
2442 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002443 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002444 addConstructor(node->getType(), "ivec4", &node->getSequence());
2445 outputTriplet(visit, "ivec4(", ", ", ")");
2446 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002447 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002448 addConstructor(node->getType(), "uvec1", &node->getSequence());
2449 outputTriplet(visit, "uvec1(", "", ")");
2450 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002451 case EOpConstructUVec2:
2452 addConstructor(node->getType(), "uvec2", &node->getSequence());
2453 outputTriplet(visit, "uvec2(", ", ", ")");
2454 break;
2455 case EOpConstructUVec3:
2456 addConstructor(node->getType(), "uvec3", &node->getSequence());
2457 outputTriplet(visit, "uvec3(", ", ", ")");
2458 break;
2459 case EOpConstructUVec4:
2460 addConstructor(node->getType(), "uvec4", &node->getSequence());
2461 outputTriplet(visit, "uvec4(", ", ", ")");
2462 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002463 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002464 addConstructor(node->getType(), "mat2", &node->getSequence());
2465 outputTriplet(visit, "mat2(", ", ", ")");
2466 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002467 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002468 addConstructor(node->getType(), "mat3", &node->getSequence());
2469 outputTriplet(visit, "mat3(", ", ", ")");
2470 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002471 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002472 addConstructor(node->getType(), "mat4", &node->getSequence());
2473 outputTriplet(visit, "mat4(", ", ", ")");
2474 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002475 case EOpConstructStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04002476 addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence());
2477 outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002478 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002479 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2480 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2481 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2482 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2483 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2484 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002485 case EOpMod:
2486 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002487 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002488 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2489 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2490 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002491 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002492 case 11: mUsesMod1 = true; break;
2493 case 22: mUsesMod2v = true; break;
2494 case 21: mUsesMod2f = true; break;
2495 case 33: mUsesMod3v = true; break;
2496 case 31: mUsesMod3f = true; break;
2497 case 44: mUsesMod4v = true; break;
2498 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002499 default: UNREACHABLE();
2500 }
2501
2502 outputTriplet(visit, "mod(", ", ", ")");
2503 }
2504 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002505 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002506 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002507 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002508 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2509 {
2510 case 1: mUsesAtan2_1 = true; break;
2511 case 2: mUsesAtan2_2 = true; break;
2512 case 3: mUsesAtan2_3 = true; break;
2513 case 4: mUsesAtan2_4 = true; break;
2514 default: UNREACHABLE();
2515 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002516 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002517 break;
2518 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2519 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2520 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2521 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2522 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2523 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2524 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2525 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2526 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002527 case EOpFaceForward:
2528 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002529 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002530 {
2531 case 1: mUsesFaceforward1 = true; break;
2532 case 2: mUsesFaceforward2 = true; break;
2533 case 3: mUsesFaceforward3 = true; break;
2534 case 4: mUsesFaceforward4 = true; break;
2535 default: UNREACHABLE();
2536 }
2537
2538 outputTriplet(visit, "faceforward(", ", ", ")");
2539 }
2540 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002541 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2542 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2543 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002544 default: UNREACHABLE();
2545 }
2546
2547 return true;
2548}
2549
2550bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2551{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002552 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002553
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002554 if (node->usesTernaryOperator())
2555 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002556 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002557 }
2558 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002559 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002560 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002561
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002562 out << "if(";
2563
2564 node->getCondition()->traverse(this);
2565
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002566 out << ")\n";
2567
Jamie Madill075edd82013-07-08 13:30:19 -04002568 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002569 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002570
daniel@transgaming.combb885322010-04-15 20:45:24 +00002571 if (node->getTrueBlock())
2572 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002573 traverseStatements(node->getTrueBlock());
daniel@transgaming.combb885322010-04-15 20:45:24 +00002574 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002575
Jamie Madill075edd82013-07-08 13:30:19 -04002576 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002577 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002578
2579 if (node->getFalseBlock())
2580 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002581 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002582
Jamie Madill075edd82013-07-08 13:30:19 -04002583 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002584 out << "{\n";
2585
Jamie Madill075edd82013-07-08 13:30:19 -04002586 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002587 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002588
Jamie Madill075edd82013-07-08 13:30:19 -04002589 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002590 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002591 }
2592 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002593
2594 return false;
2595}
2596
2597void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2598{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002599 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002600}
2601
2602bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2603{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002604 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2605
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002606 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002607 {
2608 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2609 }
2610
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002611 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002612 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002613 if (handleExcessiveLoop(node))
2614 {
2615 return false;
2616 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002617 }
2618
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002619 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002620
alokp@chromium.org52813552010-11-16 18:36:09 +00002621 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002622 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002623 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002624
Jamie Madill075edd82013-07-08 13:30:19 -04002625 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002626 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002627 }
2628 else
2629 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002630 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002631
2632 if (node->getInit())
2633 {
2634 node->getInit()->traverse(this);
2635 }
2636
2637 out << "; ";
2638
alokp@chromium.org52813552010-11-16 18:36:09 +00002639 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002640 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002641 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002642 }
2643
2644 out << "; ";
2645
alokp@chromium.org52813552010-11-16 18:36:09 +00002646 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002647 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002648 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002649 }
2650
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002651 out << ")\n";
2652
Jamie Madill075edd82013-07-08 13:30:19 -04002653 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002654 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002655 }
2656
2657 if (node->getBody())
2658 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002659 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002660 }
2661
Jamie Madill075edd82013-07-08 13:30:19 -04002662 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002663 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002664
alokp@chromium.org52813552010-11-16 18:36:09 +00002665 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002666 {
Jamie Madill075edd82013-07-08 13:30:19 -04002667 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002668 out << "while(\n";
2669
alokp@chromium.org52813552010-11-16 18:36:09 +00002670 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002671
daniel@transgaming.com73536982012-03-21 20:45:49 +00002672 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002673 }
2674
daniel@transgaming.com73536982012-03-21 20:45:49 +00002675 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002676
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002677 mInsideDiscontinuousLoop = wasDiscontinuous;
2678
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002679 return false;
2680}
2681
2682bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2683{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002684 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002685
2686 switch (node->getFlowOp())
2687 {
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002688 case EOpKill: outputTriplet(visit, "discard;\n", "", ""); break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002689 case EOpBreak:
2690 if (visit == PreVisit)
2691 {
2692 if (mExcessiveLoopIndex)
2693 {
2694 out << "{Break";
2695 mExcessiveLoopIndex->traverse(this);
2696 out << " = true; break;}\n";
2697 }
2698 else
2699 {
2700 out << "break;\n";
2701 }
2702 }
2703 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002704 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002705 case EOpReturn:
2706 if (visit == PreVisit)
2707 {
2708 if (node->getExpression())
2709 {
2710 out << "return ";
2711 }
2712 else
2713 {
2714 out << "return;\n";
2715 }
2716 }
2717 else if (visit == PostVisit)
2718 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002719 if (node->getExpression())
2720 {
2721 out << ";\n";
2722 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002723 }
2724 break;
2725 default: UNREACHABLE();
2726 }
2727
2728 return true;
2729}
2730
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002731void OutputHLSL::traverseStatements(TIntermNode *node)
2732{
2733 if (isSingleStatement(node))
2734 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002735 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002736 }
2737
2738 node->traverse(this);
2739}
2740
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002741bool OutputHLSL::isSingleStatement(TIntermNode *node)
2742{
2743 TIntermAggregate *aggregate = node->getAsAggregate();
2744
2745 if (aggregate)
2746 {
2747 if (aggregate->getOp() == EOpSequence)
2748 {
2749 return false;
2750 }
2751 else
2752 {
2753 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2754 {
2755 if (!isSingleStatement(*sit))
2756 {
2757 return false;
2758 }
2759 }
2760
2761 return true;
2762 }
2763 }
2764
2765 return true;
2766}
2767
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002768// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2769// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002770bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2771{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002772 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002773 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002774
2775 // Parse loops of the form:
2776 // for(int index = initial; index [comparator] limit; index += increment)
2777 TIntermSymbol *index = NULL;
2778 TOperator comparator = EOpNull;
2779 int initial = 0;
2780 int limit = 0;
2781 int increment = 0;
2782
2783 // Parse index name and intial value
2784 if (node->getInit())
2785 {
2786 TIntermAggregate *init = node->getInit()->getAsAggregate();
2787
2788 if (init)
2789 {
2790 TIntermSequence &sequence = init->getSequence();
2791 TIntermTyped *variable = sequence[0]->getAsTyped();
2792
2793 if (variable && variable->getQualifier() == EvqTemporary)
2794 {
2795 TIntermBinary *assign = variable->getAsBinaryNode();
2796
2797 if (assign->getOp() == EOpInitialize)
2798 {
2799 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2800 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2801
2802 if (symbol && constant)
2803 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002804 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002805 {
2806 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002807 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002808 }
2809 }
2810 }
2811 }
2812 }
2813 }
2814
2815 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002816 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002817 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002818 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002819
2820 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2821 {
2822 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2823
2824 if (constant)
2825 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002826 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002827 {
2828 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002829 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002830 }
2831 }
2832 }
2833 }
2834
2835 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002836 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002837 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002838 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2839 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002840
2841 if (binaryTerminal)
2842 {
2843 TOperator op = binaryTerminal->getOp();
2844 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2845
2846 if (constant)
2847 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002848 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002849 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002850 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002851
2852 switch (op)
2853 {
2854 case EOpAddAssign: increment = value; break;
2855 case EOpSubAssign: increment = -value; break;
2856 default: UNIMPLEMENTED();
2857 }
2858 }
2859 }
2860 }
2861 else if (unaryTerminal)
2862 {
2863 TOperator op = unaryTerminal->getOp();
2864
2865 switch (op)
2866 {
2867 case EOpPostIncrement: increment = 1; break;
2868 case EOpPostDecrement: increment = -1; break;
2869 case EOpPreIncrement: increment = 1; break;
2870 case EOpPreDecrement: increment = -1; break;
2871 default: UNIMPLEMENTED();
2872 }
2873 }
2874 }
2875
2876 if (index != NULL && comparator != EOpNull && increment != 0)
2877 {
2878 if (comparator == EOpLessThanEqual)
2879 {
2880 comparator = EOpLessThan;
2881 limit += 1;
2882 }
2883
2884 if (comparator == EOpLessThan)
2885 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002886 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002887
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002888 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002889 {
2890 return false; // Not an excessive loop
2891 }
2892
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002893 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2894 mExcessiveLoopIndex = index;
2895
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002896 out << "{int ";
2897 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002898 out << ";\n"
2899 "bool Break";
2900 index->traverse(this);
2901 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002902
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002903 bool firstLoopFragment = true;
2904
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002905 while (iterations > 0)
2906 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002907 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002908
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002909 if (!firstLoopFragment)
2910 {
2911 out << "if(!Break";
2912 index->traverse(this);
2913 out << ") {\n";
2914 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002915
2916 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2917 {
2918 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2919 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002920
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002921 // for(int index = initial; index < clampedLimit; index += increment)
2922
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002923 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002924 index->traverse(this);
2925 out << " = ";
2926 out << initial;
2927
2928 out << "; ";
2929 index->traverse(this);
2930 out << " < ";
2931 out << clampedLimit;
2932
2933 out << "; ";
2934 index->traverse(this);
2935 out << " += ";
2936 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002937 out << ")\n";
2938
Jamie Madill075edd82013-07-08 13:30:19 -04002939 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002940 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002941
2942 if (node->getBody())
2943 {
2944 node->getBody()->traverse(this);
2945 }
2946
Jamie Madill075edd82013-07-08 13:30:19 -04002947 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002948 out << ";}\n";
2949
2950 if (!firstLoopFragment)
2951 {
2952 out << "}\n";
2953 }
2954
2955 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002956
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002957 initial += MAX_LOOP_ITERATIONS * increment;
2958 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002959 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002960
2961 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002962
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002963 mExcessiveLoopIndex = restoreIndex;
2964
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002965 return true;
2966 }
2967 else UNIMPLEMENTED();
2968 }
2969
2970 return false; // Not handled as an excessive loop
2971}
2972
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002973void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002974{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002975 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002976
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002977 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002978 {
2979 out << preString;
2980 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002981 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002982 {
2983 out << inString;
2984 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002985 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002986 {
2987 out << postString;
2988 }
2989}
2990
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002991void OutputHLSL::outputLineDirective(int line)
2992{
2993 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2994 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002995 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002996 mBody << "#line " << line;
2997
2998 if (mContext.sourcePath)
2999 {
3000 mBody << " \"" << mContext.sourcePath << "\"";
3001 }
3002
3003 mBody << "\n";
3004 }
3005}
3006
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003007TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3008{
3009 TQualifier qualifier = symbol->getQualifier();
3010 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003011 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003012
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003013 if (name.empty()) // HLSL demands named arguments, also for prototypes
3014 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00003015 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003016 }
3017 else
3018 {
3019 name = decorate(name);
3020 }
3021
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003022 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
3023 {
3024 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
3025 qualifierString(qualifier) + " SamplerState sampler_" + name + arrayString(type);
3026 }
3027
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003028 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003029}
3030
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003031TString OutputHLSL::interpolationString(TQualifier qualifier)
3032{
3033 switch(qualifier)
3034 {
3035 case EvqVaryingIn: return "";
3036 case EvqInvariantVaryingIn: return "";
3037 case EvqSmoothIn: return "linear";
3038 case EvqFlatIn: return "nointerpolation";
3039 case EvqCentroidIn: return "centroid";
3040 case EvqVaryingOut: return "";
3041 case EvqInvariantVaryingOut: return "";
3042 case EvqSmoothOut: return "linear";
3043 case EvqFlatOut: return "nointerpolation";
3044 case EvqCentroidOut: return "centroid";
3045 default: UNREACHABLE();
3046 }
3047
3048 return "";
3049}
3050
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003051TString OutputHLSL::qualifierString(TQualifier qualifier)
3052{
3053 switch(qualifier)
3054 {
3055 case EvqIn: return "in";
3056 case EvqOut: return "out";
3057 case EvqInOut: return "inout";
3058 case EvqConstReadOnly: return "const";
3059 default: UNREACHABLE();
3060 }
3061
3062 return "";
3063}
3064
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003065TString OutputHLSL::typeString(const TType &type)
3066{
Jamie Madill98493dd2013-07-08 14:39:03 -04003067 const TStructure* structure = type.getStruct();
3068 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003069 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003070 const TString& typeName = structure->name();
3071 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003072 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003073 return structLookup(typeName);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003074 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003075 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003076 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003077 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003078 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003079 }
3080 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003081 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003082 int cols = type.getCols();
3083 int rows = type.getRows();
3084 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003085 }
3086 else
3087 {
3088 switch (type.getBasicType())
3089 {
3090 case EbtFloat:
3091 switch (type.getNominalSize())
3092 {
3093 case 1: return "float";
3094 case 2: return "float2";
3095 case 3: return "float3";
3096 case 4: return "float4";
3097 }
3098 case EbtInt:
3099 switch (type.getNominalSize())
3100 {
3101 case 1: return "int";
3102 case 2: return "int2";
3103 case 3: return "int3";
3104 case 4: return "int4";
3105 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003106 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04003107 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003108 {
3109 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003110 case 2: return "uint2";
3111 case 3: return "uint3";
3112 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003113 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003114 case EbtBool:
3115 switch (type.getNominalSize())
3116 {
3117 case 1: return "bool";
3118 case 2: return "bool2";
3119 case 3: return "bool3";
3120 case 4: return "bool4";
3121 }
3122 case EbtVoid:
3123 return "void";
3124 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003125 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04003126 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003127 case EbtSampler2DArray:
3128 case EbtISampler2DArray:
3129 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003130 return "sampler2D";
3131 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003132 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04003133 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003134 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00003135 case EbtSamplerExternalOES:
3136 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00003137 default:
3138 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003139 }
3140 }
3141
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003142 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003143 return "<unknown type>";
3144}
3145
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003146TString OutputHLSL::textureString(const TType &type)
3147{
3148 switch (type.getBasicType())
3149 {
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003150 case EbtSampler2D: return "Texture2D";
3151 case EbtSamplerCube: return "TextureCube";
3152 case EbtSamplerExternalOES: return "Texture2D";
3153 case EbtSampler2DArray: return "Texture2DArray";
Jamie Madill94c3f422013-07-03 15:16:13 -04003154 case EbtSampler3D: return "Texture3D";
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003155 case EbtISampler2D: return "Texture2D<int4>";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04003156 case EbtISampler3D: return "Texture3D<int4>";
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003157 case EbtISamplerCube: return "TextureCube<int4>";
3158 case EbtISampler2DArray: return "Texture2DArray<int4>";
3159 case EbtUSampler2D: return "Texture2D<uint4>";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04003160 case EbtUSampler3D: return "Texture3D<uint4>";
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003161 case EbtUSamplerCube: return "TextureCube<uint4>";
3162 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003163 default:
3164 break;
3165 }
3166
3167 UNREACHABLE();
3168 return "<unknown texture type>";
3169}
3170
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003171TString OutputHLSL::arrayString(const TType &type)
3172{
3173 if (!type.isArray())
3174 {
3175 return "";
3176 }
3177
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003178 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003179}
3180
3181TString OutputHLSL::initializer(const TType &type)
3182{
3183 TString string;
3184
Jamie Madill94bf7f22013-07-08 13:31:15 -04003185 size_t size = type.getObjectSize();
3186 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003187 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003188 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003189
Jamie Madill94bf7f22013-07-08 13:31:15 -04003190 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003191 {
3192 string += ", ";
3193 }
3194 }
3195
daniel@transgaming.comead23042010-04-29 03:35:36 +00003196 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003197}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003198
Jamie Madill98493dd2013-07-08 14:39:03 -04003199TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003200{
Jamie Madill98493dd2013-07-08 14:39:03 -04003201 const TFieldList &fields = structure.fields();
3202 const bool isNameless = (structure.name() == "");
3203 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003204 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3205
Jamie Madill98493dd2013-07-08 14:39:03 -04003206 TString string;
3207 string += declareString + "\n"
3208 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003209
Jamie Madillc835df62013-06-21 09:15:32 -04003210 int elementIndex = 0;
3211
Jamie Madill9cf6c072013-06-20 11:55:53 -04003212 for (unsigned int i = 0; i < fields.size(); i++)
3213 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003214 const TField &field = *fields[i];
3215 const TType &fieldType = *field.type();
3216 const TStructure *fieldStruct = fieldType.getStruct();
3217 const TString &fieldTypeString = fieldStruct ? structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) : typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003218
Jamie Madillc835df62013-06-21 09:15:32 -04003219 if (useStd140Packing)
3220 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003221 string += std140PrePaddingString(*field.type(), &elementIndex);
Jamie Madillc835df62013-06-21 09:15:32 -04003222 }
3223
Jamie Madill98493dd2013-07-08 14:39:03 -04003224 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003225
3226 if (useStd140Packing)
3227 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003228 string += std140PostPaddingString(*field.type(), useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003229 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003230 }
3231
3232 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003233 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003234
Jamie Madille4075c92013-06-21 09:15:32 -04003235 // Add remaining element index to the global map, for use with nested structs in standard layouts
3236 if (useStd140Packing)
3237 {
3238 mStd140StructElementIndexes[structName] = elementIndex;
3239 }
3240
Jamie Madill98493dd2013-07-08 14:39:03 -04003241 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003242}
3243
Jamie Madill98493dd2013-07-08 14:39:03 -04003244TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003245{
Jamie Madill98493dd2013-07-08 14:39:03 -04003246 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003247 {
3248 return "";
3249 }
3250
3251 TString prefix = "";
3252
3253 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3254 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003255
3256 if (useStd140Packing)
3257 {
3258 prefix += "std";
3259 }
3260
Jamie Madill9cf6c072013-06-20 11:55:53 -04003261 if (useHLSLRowMajorPacking)
3262 {
Jamie Madillc835df62013-06-21 09:15:32 -04003263 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003264 prefix += "rm";
3265 }
3266
Jamie Madill98493dd2013-07-08 14:39:03 -04003267 return prefix + structLookup(structure.name());
Jamie Madill9cf6c072013-06-20 11:55:53 -04003268}
3269
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003270void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003271{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003272 if (name == "")
3273 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003274 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003275 }
3276
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003277 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
3278 {
3279 return; // Already added
3280 }
3281
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003282 TType ctorType = type;
3283 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003284 ctorType.setPrecision(EbpHigh);
3285 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003286
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003287 TString ctorName = type.getStruct() ? decorate(name) : name;
3288
3289 typedef std::vector<TType> ParameterArray;
3290 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003291
Jamie Madill98493dd2013-07-08 14:39:03 -04003292 const TStructure* structure = type.getStruct();
3293 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003294 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003295 mStructNames.insert(decorate(name));
3296
Jamie Madill98493dd2013-07-08 14:39:03 -04003297 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003298
Jamie Madill98493dd2013-07-08 14:39:03 -04003299 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003300 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003301 // Add row-major packed struct for interface blocks
3302 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003303 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003304 "#pragma pack_matrix(column_major)\n";
3305
Jamie Madillc835df62013-06-21 09:15:32 -04003306 const TString &std140Prefix = "std";
Jamie Madill98493dd2013-07-08 14:39:03 -04003307 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003308
3309 const TString &std140RowMajorPrefix = "std_rm";
3310 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003311 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003312 "#pragma pack_matrix(column_major)\n";
3313
Jamie Madill98493dd2013-07-08 14:39:03 -04003314 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003315 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003316 mStructDeclarations.push_back(std140String);
3317 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003318 }
3319
Jamie Madill98493dd2013-07-08 14:39:03 -04003320 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003321 for (unsigned int i = 0; i < fields.size(); i++)
3322 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003323 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003324 }
3325 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003326 else if (parameters)
3327 {
3328 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3329 {
3330 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3331 }
3332 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003333 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003334
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003335 TString constructor;
3336
3337 if (ctorType.getStruct())
3338 {
3339 constructor += ctorName + " " + ctorName + "_ctor(";
3340 }
3341 else // Built-in type
3342 {
3343 constructor += typeString(ctorType) + " " + ctorName + "(";
3344 }
3345
3346 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3347 {
3348 const TType &type = ctorParameters[parameter];
3349
3350 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3351
3352 if (parameter < ctorParameters.size() - 1)
3353 {
3354 constructor += ", ";
3355 }
3356 }
3357
3358 constructor += ")\n"
3359 "{\n";
3360
3361 if (ctorType.getStruct())
3362 {
3363 constructor += " " + ctorName + " structure = {";
3364 }
3365 else
3366 {
3367 constructor += " return " + typeString(ctorType) + "(";
3368 }
3369
3370 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3371 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003372 int rows = ctorType.getRows();
3373 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003374 const TType &parameter = ctorParameters[0];
3375
3376 if (parameter.isScalar())
3377 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003378 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003379 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003380 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003381 {
3382 constructor += TString((row == col) ? "x0" : "0.0");
3383
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003384 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003385 {
3386 constructor += ", ";
3387 }
3388 }
3389 }
3390 }
3391 else if (parameter.isMatrix())
3392 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003393 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003394 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003395 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003396 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003397 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003398 {
3399 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3400 }
3401 else
3402 {
3403 constructor += TString((row == col) ? "1.0" : "0.0");
3404 }
3405
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003406 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003407 {
3408 constructor += ", ";
3409 }
3410 }
3411 }
3412 }
3413 else UNREACHABLE();
3414 }
3415 else
3416 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003417 size_t remainingComponents = ctorType.getObjectSize();
3418 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003419
3420 while (remainingComponents > 0)
3421 {
3422 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003423 const size_t parameterSize = parameter.getObjectSize();
3424 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003425
3426 constructor += "x" + str(parameterIndex);
3427
3428 if (parameter.isScalar())
3429 {
3430 remainingComponents -= parameter.getObjectSize();
3431 }
3432 else if (parameter.isVector())
3433 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003434 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003435 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003436 ASSERT(parameterSize <= remainingComponents);
3437 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003438 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003439 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003440 {
3441 switch (remainingComponents)
3442 {
3443 case 1: constructor += ".x"; break;
3444 case 2: constructor += ".xy"; break;
3445 case 3: constructor += ".xyz"; break;
3446 case 4: constructor += ".xyzw"; break;
3447 default: UNREACHABLE();
3448 }
3449
3450 remainingComponents = 0;
3451 }
3452 else UNREACHABLE();
3453 }
3454 else if (parameter.isMatrix() || parameter.getStruct())
3455 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003456 ASSERT(remainingComponents == parameterSize || moreParameters);
3457 ASSERT(parameterSize <= remainingComponents);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003458
Jamie Madill94bf7f22013-07-08 13:31:15 -04003459 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003460 }
3461 else UNREACHABLE();
3462
3463 if (moreParameters)
3464 {
3465 parameterIndex++;
3466 }
3467
3468 if (remainingComponents)
3469 {
3470 constructor += ", ";
3471 }
3472 }
3473 }
3474
3475 if (ctorType.getStruct())
3476 {
3477 constructor += "};\n"
3478 " return structure;\n"
3479 "}\n";
3480 }
3481 else
3482 {
3483 constructor += ");\n"
3484 "}\n";
3485 }
3486
daniel@transgaming.com63691862010-04-29 03:32:42 +00003487 mConstructors.insert(constructor);
3488}
3489
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003490const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3491{
3492 TInfoSinkBase &out = mBody;
3493
Jamie Madill98493dd2013-07-08 14:39:03 -04003494 const TStructure* structure = type.getStruct();
3495 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003496 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003497 out << structLookup(structure->name()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003498
Jamie Madill98493dd2013-07-08 14:39:03 -04003499 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003500
Jamie Madill98493dd2013-07-08 14:39:03 -04003501 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003502 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003503 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003504
3505 constUnion = writeConstantUnion(*fieldType, constUnion);
3506
Jamie Madill98493dd2013-07-08 14:39:03 -04003507 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003508 {
3509 out << ", ";
3510 }
3511 }
3512
3513 out << ")";
3514 }
3515 else
3516 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003517 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003518 bool writeType = size > 1;
3519
3520 if (writeType)
3521 {
3522 out << typeString(type) << "(";
3523 }
3524
Jamie Madill94bf7f22013-07-08 13:31:15 -04003525 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003526 {
3527 switch (constUnion->getType())
3528 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003529 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003530 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003531 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003532 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003533 default: UNREACHABLE();
3534 }
3535
3536 if (i != size - 1)
3537 {
3538 out << ", ";
3539 }
3540 }
3541
3542 if (writeType)
3543 {
3544 out << ")";
3545 }
3546 }
3547
3548 return constUnion;
3549}
3550
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003551TString OutputHLSL::scopeString(unsigned int depthLimit)
3552{
3553 TString string;
3554
3555 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3556 {
3557 string += "_" + str(i);
3558 }
3559
3560 return string;
3561}
3562
3563TString OutputHLSL::scopedStruct(const TString &typeName)
3564{
3565 if (typeName == "")
3566 {
3567 return typeName;
3568 }
3569
3570 return typeName + scopeString(mScopeDepth);
3571}
3572
3573TString OutputHLSL::structLookup(const TString &typeName)
3574{
3575 for (int depth = mScopeDepth; depth >= 0; depth--)
3576 {
3577 TString scopedName = decorate(typeName + scopeString(depth));
3578
3579 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3580 {
3581 if (*structName == scopedName)
3582 {
3583 return scopedName;
3584 }
3585 }
3586 }
3587
3588 UNREACHABLE(); // Should have found a matching constructor
3589
3590 return typeName;
3591}
3592
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003593TString OutputHLSL::decorate(const TString &string)
3594{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003595 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003596 {
3597 return "_" + string;
3598 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003599
3600 return string;
3601}
3602
apatrick@chromium.org65756022012-01-17 21:45:38 +00003603TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003604{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003605 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003606 {
3607 return "ex_" + string;
3608 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003609
3610 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003611}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003612
Jamie Madill98493dd2013-07-08 14:39:03 -04003613TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003614{
Jamie Madill98493dd2013-07-08 14:39:03 -04003615 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003616 {
3617 return decorate(string);
3618 }
3619
3620 return string;
3621}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003622
3623TString OutputHLSL::registerString(TIntermSymbol *operand)
3624{
3625 ASSERT(operand->getQualifier() == EvqUniform);
3626
3627 if (IsSampler(operand->getBasicType()))
3628 {
3629 return "s" + str(samplerRegister(operand));
3630 }
3631
3632 return "c" + str(uniformRegister(operand));
3633}
3634
3635int OutputHLSL::samplerRegister(TIntermSymbol *sampler)
3636{
3637 const TType &type = sampler->getType();
3638 ASSERT(IsSampler(type.getBasicType()));
3639
3640 int index = mSamplerRegister;
3641 mSamplerRegister += sampler->totalRegisterCount();
3642
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003643 declareUniform(type, sampler->getSymbol(), index);
3644
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003645 return index;
3646}
3647
3648int OutputHLSL::uniformRegister(TIntermSymbol *uniform)
3649{
3650 const TType &type = uniform->getType();
3651 ASSERT(!IsSampler(type.getBasicType()));
3652
3653 int index = mUniformRegister;
3654 mUniformRegister += uniform->totalRegisterCount();
3655
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003656 declareUniform(type, uniform->getSymbol(), index);
3657
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003658 return index;
3659}
3660
Jamie Madill98493dd2013-07-08 14:39:03 -04003661void OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, ActiveUniforms& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003662{
Jamie Madill98493dd2013-07-08 14:39:03 -04003663 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003664
3665 if (!structure)
3666 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003667 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill98493dd2013-07-08 14:39:03 -04003668 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 +00003669 }
3670 else
3671 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003672 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 +00003673
Jamie Madill98493dd2013-07-08 14:39:03 -04003674 int fieldRegister = registerIndex;
3675 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003676
Jamie Madill98493dd2013-07-08 14:39:03 -04003677 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003678 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003679 TField *field = fields[fieldIndex];
3680 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003681
Jamie Madill010fffa2013-06-20 11:55:53 -04003682 // make sure to copy matrix packing information
Jamie Madill98493dd2013-07-08 14:39:03 -04003683 fieldType->setLayoutQualifier(type.getLayoutQualifier());
Jamie Madill010fffa2013-06-20 11:55:53 -04003684
Jamie Madill98493dd2013-07-08 14:39:03 -04003685 declareUniformToList(*fieldType, field->name(), fieldRegister, structUniform.fields);
3686 fieldRegister += fieldType->totalRegisterCount();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003687 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003688
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003689 output.push_back(structUniform);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003690 }
3691}
3692
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003693void OutputHLSL::declareUniform(const TType &type, const TString &name, int index)
3694{
3695 declareUniformToList(type, name, index, mActiveUniforms);
3696}
3697
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003698GLenum OutputHLSL::glVariableType(const TType &type)
3699{
3700 if (type.getBasicType() == EbtFloat)
3701 {
3702 if (type.isScalar())
3703 {
3704 return GL_FLOAT;
3705 }
3706 else if (type.isVector())
3707 {
3708 switch(type.getNominalSize())
3709 {
3710 case 2: return GL_FLOAT_VEC2;
3711 case 3: return GL_FLOAT_VEC3;
3712 case 4: return GL_FLOAT_VEC4;
3713 default: UNREACHABLE();
3714 }
3715 }
3716 else if (type.isMatrix())
3717 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003718 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003719 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003720 case 2:
3721 switch(type.getRows())
3722 {
3723 case 2: return GL_FLOAT_MAT2;
3724 case 3: return GL_FLOAT_MAT2x3;
3725 case 4: return GL_FLOAT_MAT2x4;
3726 default: UNREACHABLE();
3727 }
3728
3729 case 3:
3730 switch(type.getRows())
3731 {
3732 case 2: return GL_FLOAT_MAT3x2;
3733 case 3: return GL_FLOAT_MAT3;
3734 case 4: return GL_FLOAT_MAT3x4;
3735 default: UNREACHABLE();
3736 }
3737
3738 case 4:
3739 switch(type.getRows())
3740 {
3741 case 2: return GL_FLOAT_MAT4x2;
3742 case 3: return GL_FLOAT_MAT4x3;
3743 case 4: return GL_FLOAT_MAT4;
3744 default: UNREACHABLE();
3745 }
3746
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003747 default: UNREACHABLE();
3748 }
3749 }
3750 else UNREACHABLE();
3751 }
3752 else if (type.getBasicType() == EbtInt)
3753 {
3754 if (type.isScalar())
3755 {
3756 return GL_INT;
3757 }
3758 else if (type.isVector())
3759 {
3760 switch(type.getNominalSize())
3761 {
3762 case 2: return GL_INT_VEC2;
3763 case 3: return GL_INT_VEC3;
3764 case 4: return GL_INT_VEC4;
3765 default: UNREACHABLE();
3766 }
3767 }
3768 else UNREACHABLE();
3769 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003770 else if (type.getBasicType() == EbtUInt)
3771 {
3772 if (type.isScalar())
3773 {
3774 return GL_UNSIGNED_INT;
3775 }
3776 else if (type.isVector())
3777 {
Jamie Madill22d63da2013-06-07 12:45:12 -04003778 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003779 {
3780 case 2: return GL_UNSIGNED_INT_VEC2;
3781 case 3: return GL_UNSIGNED_INT_VEC3;
3782 case 4: return GL_UNSIGNED_INT_VEC4;
3783 default: UNREACHABLE();
3784 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003785 }
3786 else UNREACHABLE();
3787 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003788 else if (type.getBasicType() == EbtBool)
3789 {
3790 if (type.isScalar())
3791 {
3792 return GL_BOOL;
3793 }
3794 else if (type.isVector())
3795 {
3796 switch(type.getNominalSize())
3797 {
3798 case 2: return GL_BOOL_VEC2;
3799 case 3: return GL_BOOL_VEC3;
3800 case 4: return GL_BOOL_VEC4;
3801 default: UNREACHABLE();
3802 }
3803 }
3804 else UNREACHABLE();
3805 }
3806 else if (type.getBasicType() == EbtSampler2D)
3807 {
3808 return GL_SAMPLER_2D;
3809 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04003810 else if (type.getBasicType() == EbtSampler3D)
3811 {
3812 return GL_SAMPLER_3D;
3813 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003814 else if (type.getBasicType() == EbtSamplerCube)
3815 {
3816 return GL_SAMPLER_CUBE;
3817 }
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003818 else if (type.getBasicType() == EbtSampler2DArray)
3819 {
3820 return GL_SAMPLER_2D_ARRAY;
3821 }
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003822 else if (type.getBasicType() == EbtISampler2D)
3823 {
3824 return GL_INT_SAMPLER_2D;
3825 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04003826 else if (type.getBasicType() == EbtISampler3D)
3827 {
3828 return GL_INT_SAMPLER_3D;
3829 }
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003830 else if (type.getBasicType() == EbtISamplerCube)
3831 {
3832 return GL_INT_SAMPLER_CUBE;
3833 }
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003834 else if (type.getBasicType() == EbtISampler2DArray)
3835 {
3836 return GL_INT_SAMPLER_2D_ARRAY;
3837 }
Nicolas Capens075368e2013-06-24 15:58:30 -04003838 else if (type.getBasicType() == EbtUSampler2D)
3839 {
3840 return GL_UNSIGNED_INT_SAMPLER_2D;
3841 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04003842 else if (type.getBasicType() == EbtUSampler3D)
3843 {
3844 return GL_UNSIGNED_INT_SAMPLER_3D;
3845 }
Nicolas Capens075368e2013-06-24 15:58:30 -04003846 else if (type.getBasicType() == EbtUSamplerCube)
3847 {
3848 return GL_UNSIGNED_INT_SAMPLER_CUBE;
3849 }
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003850 else if (type.getBasicType() == EbtUSampler2DArray)
3851 {
3852 return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3853 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003854 else UNREACHABLE();
3855
3856 return GL_NONE;
3857}
3858
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003859GLenum OutputHLSL::glVariablePrecision(const TType &type)
3860{
3861 if (type.getBasicType() == EbtFloat)
3862 {
3863 switch (type.getPrecision())
3864 {
3865 case EbpHigh: return GL_HIGH_FLOAT;
3866 case EbpMedium: return GL_MEDIUM_FLOAT;
3867 case EbpLow: return GL_LOW_FLOAT;
3868 case EbpUndefined:
3869 // Should be defined as the default precision by the parser
3870 default: UNREACHABLE();
3871 }
3872 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003873 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003874 {
3875 switch (type.getPrecision())
3876 {
3877 case EbpHigh: return GL_HIGH_INT;
3878 case EbpMedium: return GL_MEDIUM_INT;
3879 case EbpLow: return GL_LOW_INT;
3880 case EbpUndefined:
3881 // Should be defined as the default precision by the parser
3882 default: UNREACHABLE();
3883 }
3884 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003885
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00003886 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003887 return GL_NONE;
3888}
3889
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003890bool OutputHLSL::isVaryingOut(TQualifier qualifier)
3891{
3892 switch(qualifier)
3893 {
3894 case EvqVaryingOut:
3895 case EvqInvariantVaryingOut:
3896 case EvqSmoothOut:
3897 case EvqFlatOut:
3898 case EvqCentroidOut:
3899 return true;
3900 }
3901
3902 return false;
3903}
3904
3905bool OutputHLSL::isVaryingIn(TQualifier qualifier)
3906{
3907 switch(qualifier)
3908 {
3909 case EvqVaryingIn:
3910 case EvqInvariantVaryingIn:
3911 case EvqSmoothIn:
3912 case EvqFlatIn:
3913 case EvqCentroidIn:
3914 return true;
3915 }
3916
3917 return false;
3918}
3919
3920bool OutputHLSL::isVarying(TQualifier qualifier)
3921{
3922 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
3923}
3924
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003925}