blob: 24ff76eeece1e362c34d8c47d31052f9f10dddb7 [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
Nicolas Capenscb127d32013-07-15 17:26:18 -0400557 uniforms += "uniform " + samplerString(type) + " sampler_" + decorateUniform(name, type) + arrayString(type) +
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000558 " : 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 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400891 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 case EbtSampler2DShadow: out << "int2 "; break;
904 case EbtSamplerCubeShadow: out << "int2 "; break;
905 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400906 default: UNREACHABLE();
907 }
908 }
909 else // Sampling function
910 {
911 switch(textureFunction->sampler)
912 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400913 case EbtSampler2D: out << "float4 "; break;
914 case EbtSampler3D: out << "float4 "; break;
915 case EbtSamplerCube: out << "float4 "; break;
916 case EbtSampler2DArray: out << "float4 "; break;
917 case EbtISampler2D: out << "int4 "; break;
918 case EbtISampler3D: out << "int4 "; break;
919 case EbtISamplerCube: out << "int4 "; break;
920 case EbtISampler2DArray: out << "int4 "; break;
921 case EbtUSampler2D: out << "uint4 "; break;
922 case EbtUSampler3D: out << "uint4 "; break;
923 case EbtUSamplerCube: out << "uint4 "; break;
924 case EbtUSampler2DArray: out << "uint4 "; break;
925 case EbtSampler2DShadow: out << "float "; break;
926 case EbtSamplerCubeShadow: out << "float "; break;
927 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400928 default: UNREACHABLE();
929 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000930 }
931
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400932 // Function name
933 out << textureFunction->name();
934
935 // Argument list
936 int hlslCoords = 4;
937
938 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000939 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400940 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000941 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400942 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
943 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
944 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000945 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400946
Nicolas Capens75fb4752013-07-10 15:14:47 -0400947 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000948 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400949 case TextureFunction::IMPLICIT: break;
950 case TextureFunction::BIAS: hlslCoords = 4; break;
951 case TextureFunction::LOD: hlslCoords = 4; break;
952 case TextureFunction::LOD0: hlslCoords = 4; break;
953 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000954 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400955 }
956 else if (mOutputType == SH_HLSL11_OUTPUT)
957 {
958 switch(textureFunction->sampler)
959 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400960 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
961 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
962 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
963 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
964 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
965 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
966 case EbtISamplerCube: out << "TextureCube<int4> x, SamplerState s"; hlslCoords = 3; break;
967 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
968 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
969 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
970 case EbtUSamplerCube: out << "TextureCube<uint4> x, SamplerState s"; hlslCoords = 3; break;
971 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
972 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
973 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
974 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400975 default: UNREACHABLE();
976 }
977 }
978 else UNREACHABLE();
979
980 switch(textureFunction->coords)
981 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400982 case 1: out << ", int lod"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400983 case 2: out << ", float2 t"; break;
984 case 3: out << ", float3 t"; break;
985 case 4: out << ", float4 t"; break;
986 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000987 }
988
Nicolas Capens75fb4752013-07-10 15:14:47 -0400989 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000990 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400991 case TextureFunction::IMPLICIT: break;
992 case TextureFunction::BIAS: out << ", float bias"; break;
993 case TextureFunction::LOD: out << ", float lod"; break;
994 case TextureFunction::LOD0: break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400995 case TextureFunction::SIZE: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400996 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000997 }
998
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400999 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001000 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001001
Nicolas Capens75fb4752013-07-10 15:14:47 -04001002 if (textureFunction->method == TextureFunction::SIZE)
1003 {
1004 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
1005 {
1006 if (IsSamplerArray(textureFunction->sampler))
1007 {
1008 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
1009 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
1010 }
1011 else
1012 {
1013 out << " uint width; uint height; uint numberOfLevels;\n"
1014 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
1015 }
1016 }
1017 else if (IsSampler3D(textureFunction->sampler))
1018 {
1019 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
1020 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
1021 }
1022 else UNREACHABLE();
1023
1024 switch(textureFunction->sampler)
1025 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04001026 case EbtSampler2D: out << " return int2(width, height);"; break;
1027 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
1028 case EbtSamplerCube: out << " return int2(width, height);"; break;
1029 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
1030 case EbtISampler2D: out << " return int2(width, height);"; break;
1031 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
1032 case EbtISamplerCube: out << " return int2(width, height);"; break;
1033 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
1034 case EbtUSampler2D: out << " return int2(width, height);"; break;
1035 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
1036 case EbtUSamplerCube: out << " return int2(width, height);"; break;
1037 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
1038 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
1039 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
1040 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001041 default: UNREACHABLE();
1042 }
1043 }
1044 else if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
daniel@transgaming.com15795192011-05-11 15:36:20 +00001045 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001046 // Currently unsupported because TextureCube does not support Load
1047 // This will require emulation using a Texture2DArray with 6 faces
1048 if (textureFunction->sampler == EbtISamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001049 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001050 out << " return int4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001051 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001052 else if (textureFunction->sampler == EbtUSamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001053 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001054 out << " return uint4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001055 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001056 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001057 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001058 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001059 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001060 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001061 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001062 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001063 {
Nicolas Capens93e50de2013-07-09 13:46:28 -04001064 if (IsSamplerArray(textureFunction->sampler))
1065 {
Nicolas Capensc98406a2013-07-10 14:52:44 -04001066 out << " float width; float height; float layers;\n"
1067 " x.GetDimensions(width, height, layers);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001068 }
1069 else
1070 {
Nicolas Capensc98406a2013-07-10 14:52:44 -04001071 out << " float width; float height;\n"
1072 " x.GetDimensions(width, height);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001073 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001074 }
1075 else if (IsSampler3D(textureFunction->sampler))
1076 {
Nicolas Capensc98406a2013-07-10 14:52:44 -04001077 out << " float width; float height; float depth;\n"
1078 " x.GetDimensions(width, height, depth);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001079 }
1080 else UNREACHABLE();
1081 }
1082
1083 out << " return ";
1084
1085 // HLSL intrinsic
1086 if (mOutputType == SH_HLSL9_OUTPUT)
1087 {
1088 switch(textureFunction->sampler)
1089 {
1090 case EbtSampler2D: out << "tex2D"; break;
1091 case EbtSamplerCube: out << "texCUBE"; break;
1092 default: UNREACHABLE();
1093 }
1094
Nicolas Capens75fb4752013-07-10 15:14:47 -04001095 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001096 {
1097 case TextureFunction::IMPLICIT: out << "(s, "; break;
1098 case TextureFunction::BIAS: out << "bias(s, "; break;
1099 case TextureFunction::LOD: out << "lod(s, "; break;
1100 case TextureFunction::LOD0: out << "lod(s, "; break;
1101 default: UNREACHABLE();
1102 }
1103 }
1104 else if (mOutputType == SH_HLSL11_OUTPUT)
1105 {
1106 if (IsIntegerSampler(textureFunction->sampler))
1107 {
1108 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001109 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001110 else if (IsShadowSampler(textureFunction->sampler))
1111 {
1112 out << "x.SampleCmp(s, ";
1113 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001114 else
1115 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001116 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001117 {
1118 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1119 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1120 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1121 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
1122 default: UNREACHABLE();
1123 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001124 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001125 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001126 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001127
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001128 // Integer sampling requires integer addresses
1129 TString addressx = "";
1130 TString addressy = "";
1131 TString addressz = "";
1132 TString close = "";
1133
1134 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001135 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001136 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001137 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001138 case 2: out << "int3("; break;
1139 case 3: out << "int4("; break;
1140 default: UNREACHABLE();
1141 }
1142
Nicolas Capensc98406a2013-07-10 14:52:44 -04001143 addressx = "int(floor(width * frac((";
1144 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001145
1146 if (IsSamplerArray(textureFunction->sampler))
1147 {
1148 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1149 }
1150 else
1151 {
Nicolas Capensc98406a2013-07-10 14:52:44 -04001152 addressz = "int(floor(depth * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001153 }
1154
1155 close = "))))";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001156 }
1157 else
1158 {
1159 switch(hlslCoords)
1160 {
1161 case 2: out << "float2("; break;
1162 case 3: out << "float3("; break;
1163 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001164 default: UNREACHABLE();
1165 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001166 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001167
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001168 TString proj = ""; // Only used for projected textures
1169
1170 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001171 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001172 switch(textureFunction->coords)
1173 {
1174 case 3: proj = " / t.z"; break;
1175 case 4: proj = " / t.w"; break;
1176 default: UNREACHABLE();
1177 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001178 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001179
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001180 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001181
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001182 if (mOutputType == SH_HLSL9_OUTPUT)
1183 {
1184 if (hlslCoords >= 3)
1185 {
1186 if (textureFunction->coords < 3)
1187 {
1188 out << ", 0";
1189 }
1190 else
1191 {
1192 out << ", t.z" + proj;
1193 }
1194 }
1195
1196 if (hlslCoords == 4)
1197 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001198 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001199 {
1200 case TextureFunction::BIAS: out << ", bias"; break;
1201 case TextureFunction::LOD: out << ", lod"; break;
1202 case TextureFunction::LOD0: out << ", 0"; break;
1203 default: UNREACHABLE();
1204 }
1205 }
1206
1207 out << "));\n";
1208 }
1209 else if (mOutputType == SH_HLSL11_OUTPUT)
1210 {
1211 if (hlslCoords >= 3)
1212 {
1213 out << ", " + addressz + ("t.z" + proj) + close;
1214 }
1215
Nicolas Capenscb127d32013-07-15 17:26:18 -04001216 if (IsIntegerSampler(textureFunction->sampler))
1217 {
1218 out << ", 0));"; // Sample from the top level
1219 }
1220 else if (IsShadowSampler(textureFunction->sampler))
1221 {
1222 // Compare value
1223 switch(textureFunction->coords)
1224 {
1225 case 3: out << "), t.z);"; break;
1226 case 4: out << "), t.w);"; break;
1227 default: UNREACHABLE();
1228 }
1229 }
1230 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001231 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001232 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001233 {
1234 case TextureFunction::IMPLICIT: out << "));"; break;
1235 case TextureFunction::BIAS: out << "), bias);"; break;
1236 case TextureFunction::LOD: out << "), lod);"; break;
1237 case TextureFunction::LOD0: out << "), 0);"; break;
1238 default: UNREACHABLE();
1239 }
1240 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001241 }
1242 else UNREACHABLE();
1243 }
1244
1245 out << "\n"
1246 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001247 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001248 }
1249
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001250 if (mUsesFragCoord)
1251 {
1252 out << "#define GL_USES_FRAG_COORD\n";
1253 }
1254
1255 if (mUsesPointCoord)
1256 {
1257 out << "#define GL_USES_POINT_COORD\n";
1258 }
1259
1260 if (mUsesFrontFacing)
1261 {
1262 out << "#define GL_USES_FRONT_FACING\n";
1263 }
1264
1265 if (mUsesPointSize)
1266 {
1267 out << "#define GL_USES_POINT_SIZE\n";
1268 }
1269
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001270 if (mUsesFragDepth)
1271 {
1272 out << "#define GL_USES_FRAG_DEPTH\n";
1273 }
1274
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001275 if (mUsesDepthRange)
1276 {
1277 out << "#define GL_USES_DEPTH_RANGE\n";
1278 }
1279
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001280 if (mUsesXor)
1281 {
1282 out << "bool xor(bool p, bool q)\n"
1283 "{\n"
1284 " return (p || q) && !(p && q);\n"
1285 "}\n"
1286 "\n";
1287 }
1288
1289 if (mUsesMod1)
1290 {
1291 out << "float mod(float x, float y)\n"
1292 "{\n"
1293 " return x - y * floor(x / y);\n"
1294 "}\n"
1295 "\n";
1296 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001297
1298 if (mUsesMod2v)
1299 {
1300 out << "float2 mod(float2 x, float2 y)\n"
1301 "{\n"
1302 " return x - y * floor(x / y);\n"
1303 "}\n"
1304 "\n";
1305 }
1306
1307 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001308 {
1309 out << "float2 mod(float2 x, float y)\n"
1310 "{\n"
1311 " return x - y * floor(x / y);\n"
1312 "}\n"
1313 "\n";
1314 }
1315
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001316 if (mUsesMod3v)
1317 {
1318 out << "float3 mod(float3 x, float3 y)\n"
1319 "{\n"
1320 " return x - y * floor(x / y);\n"
1321 "}\n"
1322 "\n";
1323 }
1324
1325 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001326 {
1327 out << "float3 mod(float3 x, float y)\n"
1328 "{\n"
1329 " return x - y * floor(x / y);\n"
1330 "}\n"
1331 "\n";
1332 }
1333
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001334 if (mUsesMod4v)
1335 {
1336 out << "float4 mod(float4 x, float4 y)\n"
1337 "{\n"
1338 " return x - y * floor(x / y);\n"
1339 "}\n"
1340 "\n";
1341 }
1342
1343 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001344 {
1345 out << "float4 mod(float4 x, float y)\n"
1346 "{\n"
1347 " return x - y * floor(x / y);\n"
1348 "}\n"
1349 "\n";
1350 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001351
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001352 if (mUsesFaceforward1)
1353 {
1354 out << "float faceforward(float N, float I, float Nref)\n"
1355 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001356 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001357 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001358 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001359 " }\n"
1360 " else\n"
1361 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001362 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001363 " }\n"
1364 "}\n"
1365 "\n";
1366 }
1367
1368 if (mUsesFaceforward2)
1369 {
1370 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1371 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001372 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001373 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001374 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001375 " }\n"
1376 " else\n"
1377 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001378 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001379 " }\n"
1380 "}\n"
1381 "\n";
1382 }
1383
1384 if (mUsesFaceforward3)
1385 {
1386 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1387 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001388 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001389 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001390 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001391 " }\n"
1392 " else\n"
1393 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001394 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001395 " }\n"
1396 "}\n"
1397 "\n";
1398 }
1399
1400 if (mUsesFaceforward4)
1401 {
1402 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1403 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001404 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001405 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001406 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001407 " }\n"
1408 " else\n"
1409 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001410 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001411 " }\n"
1412 "}\n"
1413 "\n";
1414 }
1415
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001416 for (unsigned int cols = 2; cols <= 4; cols++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001417 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001418 for (unsigned int rows = 2; rows <= 4; rows++)
1419 {
1420 if (mUsesEqualMat[cols][rows])
1421 {
1422 TString matrixType = "float" + str(cols) + "x" + str(rows);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001423
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001424 out << "bool equal(" + matrixType + " m, " + matrixType + " n)\n"
1425 "{\n";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001426
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001427 for (unsigned int row = 0; row < rows; row++)
1428 {
1429 if (row == 0)
1430 {
1431 out << " return ";
1432 }
1433 else
1434 {
1435 out << " ";
1436 }
1437
1438 for (unsigned int col = 0; col < cols; col++)
1439 {
1440 TString index = "[" + str(col) + "][" + str(row) + "]";
1441 out << "m" + index + " == n" + index;
1442
1443 if (col == cols-1 && row == rows-1)
1444 {
1445 out << ";\n";
1446 }
1447 else if (col == cols-1)
1448 {
1449 out << " &&\n";
1450 }
1451 else
1452 {
1453 out << " && ";
1454 }
1455 }
1456 }
1457
1458 out << "}\n";
1459 }
1460 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001461 }
1462
1463 if (mUsesEqualVec2)
1464 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001465 out << "bool equal(float2 v, float2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001466 "{\n"
1467 " return v.x == u.x && v.y == u.y;\n"
1468 "}\n";
1469 }
1470
1471 if (mUsesEqualVec3)
1472 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001473 out << "bool equal(float3 v, float3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001474 "{\n"
1475 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1476 "}\n";
1477 }
1478
1479 if (mUsesEqualVec4)
1480 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001481 out << "bool equal(float4 v, float4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001482 "{\n"
1483 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1484 "}\n";
1485 }
1486
1487 if (mUsesEqualIVec2)
1488 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001489 out << "bool equal(int2 v, int2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001490 "{\n"
1491 " return v.x == u.x && v.y == u.y;\n"
1492 "}\n";
1493 }
1494
1495 if (mUsesEqualIVec3)
1496 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001497 out << "bool equal(int3 v, int3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001498 "{\n"
1499 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1500 "}\n";
1501 }
1502
1503 if (mUsesEqualIVec4)
1504 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001505 out << "bool equal(int4 v, int4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001506 "{\n"
1507 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1508 "}\n";
1509 }
1510
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001511 if (mUsesEqualUVec2)
1512 {
1513 out << "bool equal(uint2 v, uint2 u)\n"
1514 "{\n"
1515 " return v.x == u.x && v.y == u.y;\n"
1516 "}\n";
1517 }
1518
1519 if (mUsesEqualUVec3)
1520 {
1521 out << "bool equal(uint3 v, uint3 u)\n"
1522 "{\n"
1523 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1524 "}\n";
1525 }
1526
1527 if (mUsesEqualUVec4)
1528 {
1529 out << "bool equal(uint4 v, uint4 u)\n"
1530 "{\n"
1531 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1532 "}\n";
1533 }
1534
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001535 if (mUsesEqualBVec2)
1536 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001537 out << "bool equal(bool2 v, bool2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001538 "{\n"
1539 " return v.x == u.x && v.y == u.y;\n"
1540 "}\n";
1541 }
1542
1543 if (mUsesEqualBVec3)
1544 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001545 out << "bool equal(bool3 v, bool3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001546 "{\n"
1547 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
1548 "}\n";
1549 }
1550
1551 if (mUsesEqualBVec4)
1552 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001553 out << "bool equal(bool4 v, bool4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001554 "{\n"
1555 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
1556 "}\n";
1557 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001558
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001559 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001560 {
1561 out << "float atanyx(float y, float x)\n"
1562 "{\n"
1563 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1564 " return atan2(y, x);\n"
1565 "}\n";
1566 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001567
1568 if (mUsesAtan2_2)
1569 {
1570 out << "float2 atanyx(float2 y, float2 x)\n"
1571 "{\n"
1572 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1573 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1574 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1575 "}\n";
1576 }
1577
1578 if (mUsesAtan2_3)
1579 {
1580 out << "float3 atanyx(float3 y, float3 x)\n"
1581 "{\n"
1582 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1583 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1584 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1585 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1586 "}\n";
1587 }
1588
1589 if (mUsesAtan2_4)
1590 {
1591 out << "float4 atanyx(float4 y, float4 x)\n"
1592 "{\n"
1593 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1594 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1595 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1596 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1597 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1598 "}\n";
1599 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001600}
1601
1602void OutputHLSL::visitSymbol(TIntermSymbol *node)
1603{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001604 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001605
Jamie Madill570e04d2013-06-21 09:15:33 -04001606 // Handle accessing std140 structs by value
1607 if (mFlaggedStructMappedNames.count(node) > 0)
1608 {
1609 out << mFlaggedStructMappedNames[node];
1610 return;
1611 }
1612
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001613 TString name = node->getSymbol();
1614
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001615 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001616 {
1617 mUsesDepthRange = true;
1618 out << name;
1619 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001620 else
1621 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001622 TQualifier qualifier = node->getQualifier();
1623
1624 if (qualifier == EvqUniform)
1625 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001626 const TType& nodeType = node->getType();
1627 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1628
1629 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001630 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001631 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001632 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001633 else
1634 {
1635 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001636 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001637
1638 out << decorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001639 }
Jamie Madillb120eac2013-06-12 14:08:13 -04001640 else if (qualifier == EvqAttribute || qualifier == EvqVertexInput)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001641 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001642 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001643 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001644 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001645 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001646 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001647 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001648 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001649 }
Jamie Madill46131a32013-06-20 11:55:50 -04001650 else if (qualifier == EvqFragmentOutput)
1651 {
1652 mReferencedOutputVariables[name] = node;
1653 out << "out_" << name;
1654 }
1655 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001656 {
1657 out << "gl_Color[0]";
1658 mUsesFragColor = true;
1659 }
1660 else if (qualifier == EvqFragData)
1661 {
1662 out << "gl_Color";
1663 mUsesFragData = true;
1664 }
1665 else if (qualifier == EvqFragCoord)
1666 {
1667 mUsesFragCoord = true;
1668 out << name;
1669 }
1670 else if (qualifier == EvqPointCoord)
1671 {
1672 mUsesPointCoord = true;
1673 out << name;
1674 }
1675 else if (qualifier == EvqFrontFacing)
1676 {
1677 mUsesFrontFacing = true;
1678 out << name;
1679 }
1680 else if (qualifier == EvqPointSize)
1681 {
1682 mUsesPointSize = true;
1683 out << name;
1684 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001685 else if (name == "gl_FragDepthEXT")
1686 {
1687 mUsesFragDepth = true;
1688 out << "gl_Depth";
1689 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001690 else
1691 {
1692 out << decorate(name);
1693 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001694 }
1695}
1696
1697bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1698{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001699 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001700
Jamie Madill570e04d2013-06-21 09:15:33 -04001701 // Handle accessing std140 structs by value
1702 if (mFlaggedStructMappedNames.count(node) > 0)
1703 {
1704 out << mFlaggedStructMappedNames[node];
1705 return false;
1706 }
1707
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001708 switch (node->getOp())
1709 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001710 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001711 case EOpInitialize:
1712 if (visit == PreVisit)
1713 {
1714 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1715 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1716 // new variable is created before the assignment is evaluated), so we need to convert
1717 // this to "float t = x, x = t;".
1718
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001719 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1720 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001721
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001722 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1723 expression->traverse(&searchSymbol);
1724 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001725
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001726 if (sameSymbol)
1727 {
1728 // Type already printed
1729 out << "t" + str(mUniqueIndex) + " = ";
1730 expression->traverse(this);
1731 out << ", ";
1732 symbolNode->traverse(this);
1733 out << " = t" + str(mUniqueIndex);
1734
1735 mUniqueIndex++;
1736 return false;
1737 }
1738 }
1739 else if (visit == InVisit)
1740 {
1741 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001742 }
1743 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001744 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1745 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1746 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1747 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1748 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1749 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001750 if (visit == PreVisit)
1751 {
1752 out << "(";
1753 }
1754 else if (visit == InVisit)
1755 {
1756 out << " = mul(";
1757 node->getLeft()->traverse(this);
1758 out << ", transpose(";
1759 }
1760 else
1761 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001762 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001763 }
1764 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001765 case EOpMatrixTimesMatrixAssign:
1766 if (visit == PreVisit)
1767 {
1768 out << "(";
1769 }
1770 else if (visit == InVisit)
1771 {
1772 out << " = mul(";
1773 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001774 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001775 }
1776 else
1777 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001778 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001779 }
1780 break;
1781 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001782 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001783 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001784 const TType& leftType = node->getLeft()->getType();
1785 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001786 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001787 if (visit == PreVisit)
1788 {
1789 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1790 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1791
1792 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1793 out << interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1794
1795 return false;
1796 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001797 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001798 else
1799 {
1800 outputTriplet(visit, "", "[", "]");
1801 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001802 }
1803 break;
1804 case EOpIndexIndirect:
1805 // We do not currently support indirect references to interface blocks
1806 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1807 outputTriplet(visit, "", "[", "]");
1808 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001809 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001810 if (visit == InVisit)
1811 {
1812 const TStructure* structure = node->getLeft()->getType().getStruct();
1813 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1814 const TField* field = structure->fields()[index->getIConst(0)];
1815 out << "." + decorateField(field->name(), *structure);
1816
1817 return false;
1818 }
1819 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001820 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001821 if (visit == InVisit)
1822 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001823 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1824 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1825 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
1826 out << "." + decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001827
1828 return false;
1829 }
1830 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001831 case EOpVectorSwizzle:
1832 if (visit == InVisit)
1833 {
1834 out << ".";
1835
1836 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1837
1838 if (swizzle)
1839 {
1840 TIntermSequence &sequence = swizzle->getSequence();
1841
1842 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1843 {
1844 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1845
1846 if (element)
1847 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001848 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001849
1850 switch (i)
1851 {
1852 case 0: out << "x"; break;
1853 case 1: out << "y"; break;
1854 case 2: out << "z"; break;
1855 case 3: out << "w"; break;
1856 default: UNREACHABLE();
1857 }
1858 }
1859 else UNREACHABLE();
1860 }
1861 }
1862 else UNREACHABLE();
1863
1864 return false; // Fully processed
1865 }
1866 break;
1867 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1868 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1869 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1870 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001871 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001872 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001873 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001874 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001875 if (node->getOp() == EOpEqual)
1876 {
1877 outputTriplet(visit, "(", " == ", ")");
1878 }
1879 else
1880 {
1881 outputTriplet(visit, "(", " != ", ")");
1882 }
1883 }
1884 else if (node->getLeft()->getBasicType() == EbtStruct)
1885 {
1886 if (node->getOp() == EOpEqual)
1887 {
1888 out << "(";
1889 }
1890 else
1891 {
1892 out << "!(";
1893 }
1894
Jamie Madill98493dd2013-07-08 14:39:03 -04001895 const TStructure &structure = *node->getLeft()->getType().getStruct();
1896 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001897
Jamie Madill98493dd2013-07-08 14:39:03 -04001898 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001899 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001900 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001901
1902 node->getLeft()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001903 out << "." + decorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001904 node->getRight()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001905 out << "." + decorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001906
Jamie Madill98493dd2013-07-08 14:39:03 -04001907 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001908 {
1909 out << " && ";
1910 }
1911 }
1912
1913 out << ")";
1914
1915 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001916 }
1917 else
1918 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001919 if (node->getLeft()->isMatrix())
1920 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00001921 mUsesEqualMat[node->getLeft()->getCols()][node->getLeft()->getRows()] = true;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001922 }
1923 else if (node->getLeft()->isVector())
1924 {
1925 switch (node->getLeft()->getBasicType())
1926 {
1927 case EbtFloat:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001928 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001929 {
1930 case 2: mUsesEqualVec2 = true; break;
1931 case 3: mUsesEqualVec3 = true; break;
1932 case 4: mUsesEqualVec4 = true; break;
1933 default: UNREACHABLE();
1934 }
1935 break;
1936 case EbtInt:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001937 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001938 {
1939 case 2: mUsesEqualIVec2 = true; break;
1940 case 3: mUsesEqualIVec3 = true; break;
1941 case 4: mUsesEqualIVec4 = true; break;
1942 default: UNREACHABLE();
1943 }
1944 break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001945 case EbtUInt:
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001946 switch (node->getLeft()->getNominalSize())
1947 {
1948 case 2: mUsesEqualUVec2 = true; break;
1949 case 3: mUsesEqualUVec3 = true; break;
1950 case 4: mUsesEqualUVec4 = true; break;
1951 default: UNREACHABLE();
1952 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001953 break;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001954 case EbtBool:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001955 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001956 {
1957 case 2: mUsesEqualBVec2 = true; break;
1958 case 3: mUsesEqualBVec3 = true; break;
1959 case 4: mUsesEqualBVec4 = true; break;
1960 default: UNREACHABLE();
1961 }
1962 break;
1963 default: UNREACHABLE();
1964 }
1965 }
1966 else UNREACHABLE();
1967
1968 if (node->getOp() == EOpEqual)
1969 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001970 outputTriplet(visit, "equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001971 }
1972 else
1973 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001974 outputTriplet(visit, "!equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001975 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001976 }
1977 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001978 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1979 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1980 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1981 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1982 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001983 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001984 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1985 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001986 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001987 case EOpLogicalOr:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001988 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001989 return false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001990 case EOpLogicalXor:
1991 mUsesXor = true;
1992 outputTriplet(visit, "xor(", ", ", ")");
1993 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001994 case EOpLogicalAnd:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001995 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001996 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001997 default: UNREACHABLE();
1998 }
1999
2000 return true;
2001}
2002
2003bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
2004{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002005 switch (node->getOp())
2006 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002007 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
2008 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
2009 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
2010 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
2011 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
2012 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
2013 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002014 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04002015 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002016 case EOpConvFloatToBool:
2017 switch (node->getOperand()->getType().getNominalSize())
2018 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002019 case 1: outputTriplet(visit, "bool(", "", ")"); break;
2020 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
2021 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
2022 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002023 default: UNREACHABLE();
2024 }
2025 break;
2026 case EOpConvBoolToFloat:
2027 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04002028 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002029 switch (node->getOperand()->getType().getNominalSize())
2030 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002031 case 1: outputTriplet(visit, "float(", "", ")"); break;
2032 case 2: outputTriplet(visit, "float2(", "", ")"); break;
2033 case 3: outputTriplet(visit, "float3(", "", ")"); break;
2034 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002035 default: UNREACHABLE();
2036 }
2037 break;
2038 case EOpConvFloatToInt:
2039 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04002040 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002041 switch (node->getOperand()->getType().getNominalSize())
2042 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002043 case 1: outputTriplet(visit, "int(", "", ")"); break;
2044 case 2: outputTriplet(visit, "int2(", "", ")"); break;
2045 case 3: outputTriplet(visit, "int3(", "", ")"); break;
2046 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002047 default: UNREACHABLE();
2048 }
2049 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002050 case EOpConvFloatToUInt:
2051 case EOpConvBoolToUInt:
2052 case EOpConvIntToUInt:
Jamie Madilla16f1672013-07-03 15:15:17 -04002053 switch (node->getOperand()->getType().getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002054 {
2055 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002056 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
2057 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
2058 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002059 default: UNREACHABLE();
2060 }
2061 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002062 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
2063 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
2064 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
2065 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
2066 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
2067 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
2068 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
2069 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
2070 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
2071 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
2072 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
2073 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
2074 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
2075 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
2076 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
2077 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
2078 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
2079 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
2080 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
2081 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
2082 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002083 case EOpDFdx:
2084 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2085 {
2086 outputTriplet(visit, "(", "", ", 0.0)");
2087 }
2088 else
2089 {
2090 outputTriplet(visit, "ddx(", "", ")");
2091 }
2092 break;
2093 case EOpDFdy:
2094 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2095 {
2096 outputTriplet(visit, "(", "", ", 0.0)");
2097 }
2098 else
2099 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00002100 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002101 }
2102 break;
2103 case EOpFwidth:
2104 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2105 {
2106 outputTriplet(visit, "(", "", ", 0.0)");
2107 }
2108 else
2109 {
2110 outputTriplet(visit, "fwidth(", "", ")");
2111 }
2112 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002113 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
2114 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002115 default: UNREACHABLE();
2116 }
2117
2118 return true;
2119}
2120
2121bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2122{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002123 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002124
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002125 switch (node->getOp())
2126 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002127 case EOpSequence:
2128 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002129 if (mInsideFunction)
2130 {
Jamie Madill075edd82013-07-08 13:30:19 -04002131 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002132 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002133
2134 mScopeDepth++;
2135
2136 if (mScopeBracket.size() < mScopeDepth)
2137 {
2138 mScopeBracket.push_back(0); // New scope level
2139 }
2140 else
2141 {
2142 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
2143 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002144 }
2145
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002146 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
2147 {
Jamie Madill075edd82013-07-08 13:30:19 -04002148 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002149
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002150 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002151
2152 out << ";\n";
2153 }
2154
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002155 if (mInsideFunction)
2156 {
Jamie Madill075edd82013-07-08 13:30:19 -04002157 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002158 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002159
2160 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002161 }
2162
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002163 return false;
2164 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002165 case EOpDeclaration:
2166 if (visit == PreVisit)
2167 {
2168 TIntermSequence &sequence = node->getSequence();
2169 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002170
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00002171 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002172 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002173 if (variable->getType().getStruct())
2174 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002175 addConstructor(variable->getType(), scopedStruct(variable->getType().getStruct()->name()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00002176 }
2177
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002178 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002179 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00002180 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002181 {
2182 out << "static ";
2183 }
2184
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002185 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002186
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002187 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002188 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002189 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002190
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002191 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002192 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002193 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002194 out << arrayString(symbol->getType());
daniel@transgaming.com7127f202010-04-15 20:45:22 +00002195 out << " = " + initializer(variable->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002196 }
2197 else
2198 {
2199 (*sit)->traverse(this);
2200 }
2201
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002202 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002203 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002204 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002205 }
2206 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002207 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002208 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2209 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002210 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002211 }
2212 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002213 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002214 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002215 {
2216 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2217 {
2218 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2219
2220 if (symbol)
2221 {
2222 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2223 mReferencedVaryings[symbol->getSymbol()] = symbol;
2224 }
2225 else
2226 {
2227 (*sit)->traverse(this);
2228 }
2229 }
2230 }
2231
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002232 return false;
2233 }
2234 else if (visit == InVisit)
2235 {
2236 out << ", ";
2237 }
2238 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002239 case EOpPrototype:
2240 if (visit == PreVisit)
2241 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002242 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002243
2244 TIntermSequence &arguments = node->getSequence();
2245
2246 for (unsigned int i = 0; i < arguments.size(); i++)
2247 {
2248 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2249
2250 if (symbol)
2251 {
2252 out << argumentString(symbol);
2253
2254 if (i < arguments.size() - 1)
2255 {
2256 out << ", ";
2257 }
2258 }
2259 else UNREACHABLE();
2260 }
2261
2262 out << ");\n";
2263
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002264 // Also prototype the Lod0 variant if needed
2265 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2266 {
2267 mOutputLod0Function = true;
2268 node->traverse(this);
2269 mOutputLod0Function = false;
2270 }
2271
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002272 return false;
2273 }
2274 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002275 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002276 case EOpFunction:
2277 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002278 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002279
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002280 out << typeString(node->getType()) << " ";
2281
2282 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002283 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002284 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002285 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002286 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002287 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002288 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002289 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002290
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002291 TIntermSequence &sequence = node->getSequence();
2292 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2293
2294 for (unsigned int i = 0; i < arguments.size(); i++)
2295 {
2296 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2297
2298 if (symbol)
2299 {
2300 if (symbol->getType().getStruct())
2301 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002302 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getStruct()->name()), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002303 }
2304
2305 out << argumentString(symbol);
2306
2307 if (i < arguments.size() - 1)
2308 {
2309 out << ", ";
2310 }
2311 }
2312 else UNREACHABLE();
2313 }
2314
2315 out << ")\n"
2316 "{\n";
2317
2318 if (sequence.size() > 1)
2319 {
2320 mInsideFunction = true;
2321 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002322 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002323 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002324
2325 out << "}\n";
2326
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002327 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2328 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002329 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002330 {
2331 mOutputLod0Function = true;
2332 node->traverse(this);
2333 mOutputLod0Function = false;
2334 }
2335 }
2336
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002337 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002338 }
2339 break;
2340 case EOpFunctionCall:
2341 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002342 TString name = TFunction::unmangleName(node->getName());
2343 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002344 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002345
2346 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002347 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002348 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002349 }
2350 else
2351 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002352 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2353
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002354 TextureFunction textureFunction;
2355 textureFunction.sampler = samplerType;
2356 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002357 textureFunction.method = TextureFunction::IMPLICIT;
2358 textureFunction.proj = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002359
2360 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002361 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002362 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002363 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002364 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002365 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002366 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002367 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002368 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002369 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002370 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002371 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002372 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002373 else if (name == "texture2DProjLod" || name == "textureProjLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002374 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002375 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002376 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002377 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002378 else if (name == "textureSize")
2379 {
2380 textureFunction.method = TextureFunction::SIZE;
2381 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002382 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002383
Nicolas Capens75fb4752013-07-10 15:14:47 -04002384 if (textureFunction.method != TextureFunction::LOD &&
2385 textureFunction.method != TextureFunction::SIZE)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002386 {
2387 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2388 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002389 textureFunction.method = TextureFunction::LOD0;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002390 }
2391 else if (arguments.size() == 3)
2392 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002393 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002394 }
2395 }
2396
2397 mUsesTexture.insert(textureFunction);
2398
2399 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002400 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002401
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002402 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2403 {
2404 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2405 {
2406 out << "texture_";
2407 (*arg)->traverse(this);
2408 out << ", sampler_";
2409 }
2410
2411 (*arg)->traverse(this);
2412
2413 if (arg < arguments.end() - 1)
2414 {
2415 out << ", ";
2416 }
2417 }
2418
2419 out << ")";
2420
2421 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002422 }
2423 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002424 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002425 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002426 addConstructor(node->getType(), "vec1", &node->getSequence());
2427 outputTriplet(visit, "vec1(", "", ")");
2428 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002429 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002430 addConstructor(node->getType(), "vec2", &node->getSequence());
2431 outputTriplet(visit, "vec2(", ", ", ")");
2432 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002433 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002434 addConstructor(node->getType(), "vec3", &node->getSequence());
2435 outputTriplet(visit, "vec3(", ", ", ")");
2436 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002437 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002438 addConstructor(node->getType(), "vec4", &node->getSequence());
2439 outputTriplet(visit, "vec4(", ", ", ")");
2440 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002441 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002442 addConstructor(node->getType(), "bvec1", &node->getSequence());
2443 outputTriplet(visit, "bvec1(", "", ")");
2444 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002445 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002446 addConstructor(node->getType(), "bvec2", &node->getSequence());
2447 outputTriplet(visit, "bvec2(", ", ", ")");
2448 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002449 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002450 addConstructor(node->getType(), "bvec3", &node->getSequence());
2451 outputTriplet(visit, "bvec3(", ", ", ")");
2452 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002453 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002454 addConstructor(node->getType(), "bvec4", &node->getSequence());
2455 outputTriplet(visit, "bvec4(", ", ", ")");
2456 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002457 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002458 addConstructor(node->getType(), "ivec1", &node->getSequence());
2459 outputTriplet(visit, "ivec1(", "", ")");
2460 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002461 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002462 addConstructor(node->getType(), "ivec2", &node->getSequence());
2463 outputTriplet(visit, "ivec2(", ", ", ")");
2464 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002465 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002466 addConstructor(node->getType(), "ivec3", &node->getSequence());
2467 outputTriplet(visit, "ivec3(", ", ", ")");
2468 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002469 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002470 addConstructor(node->getType(), "ivec4", &node->getSequence());
2471 outputTriplet(visit, "ivec4(", ", ", ")");
2472 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002473 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002474 addConstructor(node->getType(), "uvec1", &node->getSequence());
2475 outputTriplet(visit, "uvec1(", "", ")");
2476 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002477 case EOpConstructUVec2:
2478 addConstructor(node->getType(), "uvec2", &node->getSequence());
2479 outputTriplet(visit, "uvec2(", ", ", ")");
2480 break;
2481 case EOpConstructUVec3:
2482 addConstructor(node->getType(), "uvec3", &node->getSequence());
2483 outputTriplet(visit, "uvec3(", ", ", ")");
2484 break;
2485 case EOpConstructUVec4:
2486 addConstructor(node->getType(), "uvec4", &node->getSequence());
2487 outputTriplet(visit, "uvec4(", ", ", ")");
2488 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002489 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002490 addConstructor(node->getType(), "mat2", &node->getSequence());
2491 outputTriplet(visit, "mat2(", ", ", ")");
2492 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002493 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002494 addConstructor(node->getType(), "mat3", &node->getSequence());
2495 outputTriplet(visit, "mat3(", ", ", ")");
2496 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002497 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002498 addConstructor(node->getType(), "mat4", &node->getSequence());
2499 outputTriplet(visit, "mat4(", ", ", ")");
2500 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002501 case EOpConstructStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04002502 addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence());
2503 outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002504 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002505 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2506 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2507 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2508 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2509 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2510 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002511 case EOpMod:
2512 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002513 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002514 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2515 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2516 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002517 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002518 case 11: mUsesMod1 = true; break;
2519 case 22: mUsesMod2v = true; break;
2520 case 21: mUsesMod2f = true; break;
2521 case 33: mUsesMod3v = true; break;
2522 case 31: mUsesMod3f = true; break;
2523 case 44: mUsesMod4v = true; break;
2524 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002525 default: UNREACHABLE();
2526 }
2527
2528 outputTriplet(visit, "mod(", ", ", ")");
2529 }
2530 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002531 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002532 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002533 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002534 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2535 {
2536 case 1: mUsesAtan2_1 = true; break;
2537 case 2: mUsesAtan2_2 = true; break;
2538 case 3: mUsesAtan2_3 = true; break;
2539 case 4: mUsesAtan2_4 = true; break;
2540 default: UNREACHABLE();
2541 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002542 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002543 break;
2544 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2545 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2546 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2547 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2548 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2549 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2550 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2551 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2552 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002553 case EOpFaceForward:
2554 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002555 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002556 {
2557 case 1: mUsesFaceforward1 = true; break;
2558 case 2: mUsesFaceforward2 = true; break;
2559 case 3: mUsesFaceforward3 = true; break;
2560 case 4: mUsesFaceforward4 = true; break;
2561 default: UNREACHABLE();
2562 }
2563
2564 outputTriplet(visit, "faceforward(", ", ", ")");
2565 }
2566 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002567 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2568 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2569 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002570 default: UNREACHABLE();
2571 }
2572
2573 return true;
2574}
2575
2576bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2577{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002578 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002579
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002580 if (node->usesTernaryOperator())
2581 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002582 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002583 }
2584 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002585 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002586 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002587
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002588 out << "if(";
2589
2590 node->getCondition()->traverse(this);
2591
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002592 out << ")\n";
2593
Jamie Madill075edd82013-07-08 13:30:19 -04002594 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002595 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002596
daniel@transgaming.combb885322010-04-15 20:45:24 +00002597 if (node->getTrueBlock())
2598 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002599 traverseStatements(node->getTrueBlock());
daniel@transgaming.combb885322010-04-15 20:45:24 +00002600 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002601
Jamie Madill075edd82013-07-08 13:30:19 -04002602 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002603 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002604
2605 if (node->getFalseBlock())
2606 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002607 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002608
Jamie Madill075edd82013-07-08 13:30:19 -04002609 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002610 out << "{\n";
2611
Jamie Madill075edd82013-07-08 13:30:19 -04002612 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002613 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002614
Jamie Madill075edd82013-07-08 13:30:19 -04002615 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002616 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002617 }
2618 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002619
2620 return false;
2621}
2622
2623void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2624{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002625 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002626}
2627
2628bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2629{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002630 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2631
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002632 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002633 {
2634 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2635 }
2636
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002637 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002638 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002639 if (handleExcessiveLoop(node))
2640 {
2641 return false;
2642 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002643 }
2644
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002645 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002646
alokp@chromium.org52813552010-11-16 18:36:09 +00002647 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002648 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002649 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002650
Jamie Madill075edd82013-07-08 13:30:19 -04002651 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002652 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002653 }
2654 else
2655 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002656 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002657
2658 if (node->getInit())
2659 {
2660 node->getInit()->traverse(this);
2661 }
2662
2663 out << "; ";
2664
alokp@chromium.org52813552010-11-16 18:36:09 +00002665 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002666 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002667 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002668 }
2669
2670 out << "; ";
2671
alokp@chromium.org52813552010-11-16 18:36:09 +00002672 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002673 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002674 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002675 }
2676
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002677 out << ")\n";
2678
Jamie Madill075edd82013-07-08 13:30:19 -04002679 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002680 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002681 }
2682
2683 if (node->getBody())
2684 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002685 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002686 }
2687
Jamie Madill075edd82013-07-08 13:30:19 -04002688 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002689 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002690
alokp@chromium.org52813552010-11-16 18:36:09 +00002691 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002692 {
Jamie Madill075edd82013-07-08 13:30:19 -04002693 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002694 out << "while(\n";
2695
alokp@chromium.org52813552010-11-16 18:36:09 +00002696 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002697
daniel@transgaming.com73536982012-03-21 20:45:49 +00002698 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002699 }
2700
daniel@transgaming.com73536982012-03-21 20:45:49 +00002701 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002702
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002703 mInsideDiscontinuousLoop = wasDiscontinuous;
2704
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002705 return false;
2706}
2707
2708bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2709{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002710 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002711
2712 switch (node->getFlowOp())
2713 {
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002714 case EOpKill: outputTriplet(visit, "discard;\n", "", ""); break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002715 case EOpBreak:
2716 if (visit == PreVisit)
2717 {
2718 if (mExcessiveLoopIndex)
2719 {
2720 out << "{Break";
2721 mExcessiveLoopIndex->traverse(this);
2722 out << " = true; break;}\n";
2723 }
2724 else
2725 {
2726 out << "break;\n";
2727 }
2728 }
2729 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002730 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002731 case EOpReturn:
2732 if (visit == PreVisit)
2733 {
2734 if (node->getExpression())
2735 {
2736 out << "return ";
2737 }
2738 else
2739 {
2740 out << "return;\n";
2741 }
2742 }
2743 else if (visit == PostVisit)
2744 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002745 if (node->getExpression())
2746 {
2747 out << ";\n";
2748 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002749 }
2750 break;
2751 default: UNREACHABLE();
2752 }
2753
2754 return true;
2755}
2756
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002757void OutputHLSL::traverseStatements(TIntermNode *node)
2758{
2759 if (isSingleStatement(node))
2760 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002761 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002762 }
2763
2764 node->traverse(this);
2765}
2766
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002767bool OutputHLSL::isSingleStatement(TIntermNode *node)
2768{
2769 TIntermAggregate *aggregate = node->getAsAggregate();
2770
2771 if (aggregate)
2772 {
2773 if (aggregate->getOp() == EOpSequence)
2774 {
2775 return false;
2776 }
2777 else
2778 {
2779 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2780 {
2781 if (!isSingleStatement(*sit))
2782 {
2783 return false;
2784 }
2785 }
2786
2787 return true;
2788 }
2789 }
2790
2791 return true;
2792}
2793
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002794// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2795// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002796bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2797{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002798 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002799 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002800
2801 // Parse loops of the form:
2802 // for(int index = initial; index [comparator] limit; index += increment)
2803 TIntermSymbol *index = NULL;
2804 TOperator comparator = EOpNull;
2805 int initial = 0;
2806 int limit = 0;
2807 int increment = 0;
2808
2809 // Parse index name and intial value
2810 if (node->getInit())
2811 {
2812 TIntermAggregate *init = node->getInit()->getAsAggregate();
2813
2814 if (init)
2815 {
2816 TIntermSequence &sequence = init->getSequence();
2817 TIntermTyped *variable = sequence[0]->getAsTyped();
2818
2819 if (variable && variable->getQualifier() == EvqTemporary)
2820 {
2821 TIntermBinary *assign = variable->getAsBinaryNode();
2822
2823 if (assign->getOp() == EOpInitialize)
2824 {
2825 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2826 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2827
2828 if (symbol && constant)
2829 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002830 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002831 {
2832 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002833 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002834 }
2835 }
2836 }
2837 }
2838 }
2839 }
2840
2841 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002842 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002843 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002844 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002845
2846 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2847 {
2848 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2849
2850 if (constant)
2851 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002852 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002853 {
2854 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002855 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002856 }
2857 }
2858 }
2859 }
2860
2861 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002862 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002863 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002864 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2865 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002866
2867 if (binaryTerminal)
2868 {
2869 TOperator op = binaryTerminal->getOp();
2870 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2871
2872 if (constant)
2873 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002874 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002875 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002876 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002877
2878 switch (op)
2879 {
2880 case EOpAddAssign: increment = value; break;
2881 case EOpSubAssign: increment = -value; break;
2882 default: UNIMPLEMENTED();
2883 }
2884 }
2885 }
2886 }
2887 else if (unaryTerminal)
2888 {
2889 TOperator op = unaryTerminal->getOp();
2890
2891 switch (op)
2892 {
2893 case EOpPostIncrement: increment = 1; break;
2894 case EOpPostDecrement: increment = -1; break;
2895 case EOpPreIncrement: increment = 1; break;
2896 case EOpPreDecrement: increment = -1; break;
2897 default: UNIMPLEMENTED();
2898 }
2899 }
2900 }
2901
2902 if (index != NULL && comparator != EOpNull && increment != 0)
2903 {
2904 if (comparator == EOpLessThanEqual)
2905 {
2906 comparator = EOpLessThan;
2907 limit += 1;
2908 }
2909
2910 if (comparator == EOpLessThan)
2911 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002912 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002913
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002914 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002915 {
2916 return false; // Not an excessive loop
2917 }
2918
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002919 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2920 mExcessiveLoopIndex = index;
2921
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002922 out << "{int ";
2923 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002924 out << ";\n"
2925 "bool Break";
2926 index->traverse(this);
2927 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002928
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002929 bool firstLoopFragment = true;
2930
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002931 while (iterations > 0)
2932 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002933 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002934
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002935 if (!firstLoopFragment)
2936 {
2937 out << "if(!Break";
2938 index->traverse(this);
2939 out << ") {\n";
2940 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002941
2942 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2943 {
2944 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2945 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002946
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002947 // for(int index = initial; index < clampedLimit; index += increment)
2948
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002949 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002950 index->traverse(this);
2951 out << " = ";
2952 out << initial;
2953
2954 out << "; ";
2955 index->traverse(this);
2956 out << " < ";
2957 out << clampedLimit;
2958
2959 out << "; ";
2960 index->traverse(this);
2961 out << " += ";
2962 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002963 out << ")\n";
2964
Jamie Madill075edd82013-07-08 13:30:19 -04002965 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002966 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002967
2968 if (node->getBody())
2969 {
2970 node->getBody()->traverse(this);
2971 }
2972
Jamie Madill075edd82013-07-08 13:30:19 -04002973 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002974 out << ";}\n";
2975
2976 if (!firstLoopFragment)
2977 {
2978 out << "}\n";
2979 }
2980
2981 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002982
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002983 initial += MAX_LOOP_ITERATIONS * increment;
2984 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002985 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002986
2987 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002988
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002989 mExcessiveLoopIndex = restoreIndex;
2990
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002991 return true;
2992 }
2993 else UNIMPLEMENTED();
2994 }
2995
2996 return false; // Not handled as an excessive loop
2997}
2998
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002999void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003000{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00003001 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003002
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003003 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003004 {
3005 out << preString;
3006 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003007 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003008 {
3009 out << inString;
3010 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003011 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003012 {
3013 out << postString;
3014 }
3015}
3016
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003017void OutputHLSL::outputLineDirective(int line)
3018{
3019 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
3020 {
baustin@google.com8ab69842011-06-02 21:53:45 +00003021 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003022 mBody << "#line " << line;
3023
3024 if (mContext.sourcePath)
3025 {
3026 mBody << " \"" << mContext.sourcePath << "\"";
3027 }
3028
3029 mBody << "\n";
3030 }
3031}
3032
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003033TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3034{
3035 TQualifier qualifier = symbol->getQualifier();
3036 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003037 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003038
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003039 if (name.empty()) // HLSL demands named arguments, also for prototypes
3040 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00003041 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003042 }
3043 else
3044 {
3045 name = decorate(name);
3046 }
3047
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003048 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
3049 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003050 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
3051 qualifierString(qualifier) + " " + samplerString(type) + " sampler_" + name + arrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003052 }
3053
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003054 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003055}
3056
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003057TString OutputHLSL::interpolationString(TQualifier qualifier)
3058{
3059 switch(qualifier)
3060 {
3061 case EvqVaryingIn: return "";
3062 case EvqInvariantVaryingIn: return "";
3063 case EvqSmoothIn: return "linear";
3064 case EvqFlatIn: return "nointerpolation";
3065 case EvqCentroidIn: return "centroid";
3066 case EvqVaryingOut: return "";
3067 case EvqInvariantVaryingOut: return "";
3068 case EvqSmoothOut: return "linear";
3069 case EvqFlatOut: return "nointerpolation";
3070 case EvqCentroidOut: return "centroid";
3071 default: UNREACHABLE();
3072 }
3073
3074 return "";
3075}
3076
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003077TString OutputHLSL::qualifierString(TQualifier qualifier)
3078{
3079 switch(qualifier)
3080 {
3081 case EvqIn: return "in";
3082 case EvqOut: return "out";
3083 case EvqInOut: return "inout";
3084 case EvqConstReadOnly: return "const";
3085 default: UNREACHABLE();
3086 }
3087
3088 return "";
3089}
3090
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003091TString OutputHLSL::typeString(const TType &type)
3092{
Jamie Madill98493dd2013-07-08 14:39:03 -04003093 const TStructure* structure = type.getStruct();
3094 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003095 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003096 const TString& typeName = structure->name();
3097 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003098 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003099 return structLookup(typeName);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003100 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003101 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003102 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003103 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003104 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003105 }
3106 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003107 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003108 int cols = type.getCols();
3109 int rows = type.getRows();
3110 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003111 }
3112 else
3113 {
3114 switch (type.getBasicType())
3115 {
3116 case EbtFloat:
3117 switch (type.getNominalSize())
3118 {
3119 case 1: return "float";
3120 case 2: return "float2";
3121 case 3: return "float3";
3122 case 4: return "float4";
3123 }
3124 case EbtInt:
3125 switch (type.getNominalSize())
3126 {
3127 case 1: return "int";
3128 case 2: return "int2";
3129 case 3: return "int3";
3130 case 4: return "int4";
3131 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003132 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04003133 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003134 {
3135 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003136 case 2: return "uint2";
3137 case 3: return "uint3";
3138 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003139 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003140 case EbtBool:
3141 switch (type.getNominalSize())
3142 {
3143 case 1: return "bool";
3144 case 2: return "bool2";
3145 case 3: return "bool3";
3146 case 4: return "bool4";
3147 }
3148 case EbtVoid:
3149 return "void";
3150 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003151 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04003152 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003153 case EbtSampler2DArray:
3154 case EbtISampler2DArray:
3155 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003156 return "sampler2D";
3157 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003158 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04003159 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003160 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00003161 case EbtSamplerExternalOES:
3162 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00003163 default:
3164 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003165 }
3166 }
3167
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003168 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003169 return "<unknown type>";
3170}
3171
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003172TString OutputHLSL::textureString(const TType &type)
3173{
3174 switch (type.getBasicType())
3175 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003176 case EbtSampler2D: return "Texture2D";
3177 case EbtSamplerCube: return "TextureCube";
3178 case EbtSamplerExternalOES: return "Texture2D";
3179 case EbtSampler2DArray: return "Texture2DArray";
3180 case EbtSampler3D: return "Texture3D";
3181 case EbtISampler2D: return "Texture2D<int4>";
3182 case EbtISampler3D: return "Texture3D<int4>";
3183 case EbtISamplerCube: return "TextureCube<int4>";
3184 case EbtISampler2DArray: return "Texture2DArray<int4>";
3185 case EbtUSampler2D: return "Texture2D<uint4>";
3186 case EbtUSampler3D: return "Texture3D<uint4>";
3187 case EbtUSamplerCube: return "TextureCube<uint4>";
3188 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
3189 case EbtSampler2DShadow: return "Texture2D";
3190 case EbtSamplerCubeShadow: return "TextureCube";
3191 case EbtSampler2DArrayShadow: return "Texture2DArray";
3192 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003193 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04003194
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003195 return "<unknown texture type>";
3196}
3197
Nicolas Capenscb127d32013-07-15 17:26:18 -04003198TString OutputHLSL::samplerString(const TType &type)
3199{
3200 if (IsShadowSampler(type.getBasicType()))
3201 {
3202 return "SamplerComparisonState";
3203 }
3204 else
3205 {
3206 return "SamplerState";
3207 }
3208}
3209
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003210TString OutputHLSL::arrayString(const TType &type)
3211{
3212 if (!type.isArray())
3213 {
3214 return "";
3215 }
3216
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003217 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003218}
3219
3220TString OutputHLSL::initializer(const TType &type)
3221{
3222 TString string;
3223
Jamie Madill94bf7f22013-07-08 13:31:15 -04003224 size_t size = type.getObjectSize();
3225 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003226 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003227 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003228
Jamie Madill94bf7f22013-07-08 13:31:15 -04003229 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003230 {
3231 string += ", ";
3232 }
3233 }
3234
daniel@transgaming.comead23042010-04-29 03:35:36 +00003235 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003236}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003237
Jamie Madill98493dd2013-07-08 14:39:03 -04003238TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003239{
Jamie Madill98493dd2013-07-08 14:39:03 -04003240 const TFieldList &fields = structure.fields();
3241 const bool isNameless = (structure.name() == "");
3242 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003243 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3244
Jamie Madill98493dd2013-07-08 14:39:03 -04003245 TString string;
3246 string += declareString + "\n"
3247 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003248
Jamie Madillc835df62013-06-21 09:15:32 -04003249 int elementIndex = 0;
3250
Jamie Madill9cf6c072013-06-20 11:55:53 -04003251 for (unsigned int i = 0; i < fields.size(); i++)
3252 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003253 const TField &field = *fields[i];
3254 const TType &fieldType = *field.type();
3255 const TStructure *fieldStruct = fieldType.getStruct();
3256 const TString &fieldTypeString = fieldStruct ? structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) : typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003257
Jamie Madillc835df62013-06-21 09:15:32 -04003258 if (useStd140Packing)
3259 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003260 string += std140PrePaddingString(*field.type(), &elementIndex);
Jamie Madillc835df62013-06-21 09:15:32 -04003261 }
3262
Jamie Madill98493dd2013-07-08 14:39:03 -04003263 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003264
3265 if (useStd140Packing)
3266 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003267 string += std140PostPaddingString(*field.type(), useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003268 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003269 }
3270
3271 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003272 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003273
Jamie Madille4075c92013-06-21 09:15:32 -04003274 // Add remaining element index to the global map, for use with nested structs in standard layouts
3275 if (useStd140Packing)
3276 {
3277 mStd140StructElementIndexes[structName] = elementIndex;
3278 }
3279
Jamie Madill98493dd2013-07-08 14:39:03 -04003280 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003281}
3282
Jamie Madill98493dd2013-07-08 14:39:03 -04003283TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003284{
Jamie Madill98493dd2013-07-08 14:39:03 -04003285 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003286 {
3287 return "";
3288 }
3289
3290 TString prefix = "";
3291
3292 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3293 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003294
3295 if (useStd140Packing)
3296 {
3297 prefix += "std";
3298 }
3299
Jamie Madill9cf6c072013-06-20 11:55:53 -04003300 if (useHLSLRowMajorPacking)
3301 {
Jamie Madillc835df62013-06-21 09:15:32 -04003302 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003303 prefix += "rm";
3304 }
3305
Jamie Madill98493dd2013-07-08 14:39:03 -04003306 return prefix + structLookup(structure.name());
Jamie Madill9cf6c072013-06-20 11:55:53 -04003307}
3308
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003309void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003310{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003311 if (name == "")
3312 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003313 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003314 }
3315
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003316 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
3317 {
3318 return; // Already added
3319 }
3320
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003321 TType ctorType = type;
3322 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003323 ctorType.setPrecision(EbpHigh);
3324 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003325
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003326 TString ctorName = type.getStruct() ? decorate(name) : name;
3327
3328 typedef std::vector<TType> ParameterArray;
3329 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003330
Jamie Madill98493dd2013-07-08 14:39:03 -04003331 const TStructure* structure = type.getStruct();
3332 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003333 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003334 mStructNames.insert(decorate(name));
3335
Jamie Madill98493dd2013-07-08 14:39:03 -04003336 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003337
Jamie Madill98493dd2013-07-08 14:39:03 -04003338 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003339 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003340 // Add row-major packed struct for interface blocks
3341 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003342 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003343 "#pragma pack_matrix(column_major)\n";
3344
Jamie Madillc835df62013-06-21 09:15:32 -04003345 const TString &std140Prefix = "std";
Jamie Madill98493dd2013-07-08 14:39:03 -04003346 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003347
3348 const TString &std140RowMajorPrefix = "std_rm";
3349 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003350 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003351 "#pragma pack_matrix(column_major)\n";
3352
Jamie Madill98493dd2013-07-08 14:39:03 -04003353 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003354 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003355 mStructDeclarations.push_back(std140String);
3356 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003357 }
3358
Jamie Madill98493dd2013-07-08 14:39:03 -04003359 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003360 for (unsigned int i = 0; i < fields.size(); i++)
3361 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003362 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003363 }
3364 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003365 else if (parameters)
3366 {
3367 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3368 {
3369 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3370 }
3371 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003372 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003373
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003374 TString constructor;
3375
3376 if (ctorType.getStruct())
3377 {
3378 constructor += ctorName + " " + ctorName + "_ctor(";
3379 }
3380 else // Built-in type
3381 {
3382 constructor += typeString(ctorType) + " " + ctorName + "(";
3383 }
3384
3385 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3386 {
3387 const TType &type = ctorParameters[parameter];
3388
3389 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3390
3391 if (parameter < ctorParameters.size() - 1)
3392 {
3393 constructor += ", ";
3394 }
3395 }
3396
3397 constructor += ")\n"
3398 "{\n";
3399
3400 if (ctorType.getStruct())
3401 {
3402 constructor += " " + ctorName + " structure = {";
3403 }
3404 else
3405 {
3406 constructor += " return " + typeString(ctorType) + "(";
3407 }
3408
3409 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3410 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003411 int rows = ctorType.getRows();
3412 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003413 const TType &parameter = ctorParameters[0];
3414
3415 if (parameter.isScalar())
3416 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003417 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003418 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003419 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003420 {
3421 constructor += TString((row == col) ? "x0" : "0.0");
3422
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003423 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003424 {
3425 constructor += ", ";
3426 }
3427 }
3428 }
3429 }
3430 else if (parameter.isMatrix())
3431 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003432 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003433 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003434 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003435 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003436 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003437 {
3438 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3439 }
3440 else
3441 {
3442 constructor += TString((row == col) ? "1.0" : "0.0");
3443 }
3444
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003445 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003446 {
3447 constructor += ", ";
3448 }
3449 }
3450 }
3451 }
3452 else UNREACHABLE();
3453 }
3454 else
3455 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003456 size_t remainingComponents = ctorType.getObjectSize();
3457 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003458
3459 while (remainingComponents > 0)
3460 {
3461 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003462 const size_t parameterSize = parameter.getObjectSize();
3463 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003464
3465 constructor += "x" + str(parameterIndex);
3466
3467 if (parameter.isScalar())
3468 {
3469 remainingComponents -= parameter.getObjectSize();
3470 }
3471 else if (parameter.isVector())
3472 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003473 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003474 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003475 ASSERT(parameterSize <= remainingComponents);
3476 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003477 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003478 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003479 {
3480 switch (remainingComponents)
3481 {
3482 case 1: constructor += ".x"; break;
3483 case 2: constructor += ".xy"; break;
3484 case 3: constructor += ".xyz"; break;
3485 case 4: constructor += ".xyzw"; break;
3486 default: UNREACHABLE();
3487 }
3488
3489 remainingComponents = 0;
3490 }
3491 else UNREACHABLE();
3492 }
3493 else if (parameter.isMatrix() || parameter.getStruct())
3494 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003495 ASSERT(remainingComponents == parameterSize || moreParameters);
3496 ASSERT(parameterSize <= remainingComponents);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003497
Jamie Madill94bf7f22013-07-08 13:31:15 -04003498 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003499 }
3500 else UNREACHABLE();
3501
3502 if (moreParameters)
3503 {
3504 parameterIndex++;
3505 }
3506
3507 if (remainingComponents)
3508 {
3509 constructor += ", ";
3510 }
3511 }
3512 }
3513
3514 if (ctorType.getStruct())
3515 {
3516 constructor += "};\n"
3517 " return structure;\n"
3518 "}\n";
3519 }
3520 else
3521 {
3522 constructor += ");\n"
3523 "}\n";
3524 }
3525
daniel@transgaming.com63691862010-04-29 03:32:42 +00003526 mConstructors.insert(constructor);
3527}
3528
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003529const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3530{
3531 TInfoSinkBase &out = mBody;
3532
Jamie Madill98493dd2013-07-08 14:39:03 -04003533 const TStructure* structure = type.getStruct();
3534 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003535 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003536 out << structLookup(structure->name()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003537
Jamie Madill98493dd2013-07-08 14:39:03 -04003538 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003539
Jamie Madill98493dd2013-07-08 14:39:03 -04003540 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003541 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003542 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003543
3544 constUnion = writeConstantUnion(*fieldType, constUnion);
3545
Jamie Madill98493dd2013-07-08 14:39:03 -04003546 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003547 {
3548 out << ", ";
3549 }
3550 }
3551
3552 out << ")";
3553 }
3554 else
3555 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003556 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003557 bool writeType = size > 1;
3558
3559 if (writeType)
3560 {
3561 out << typeString(type) << "(";
3562 }
3563
Jamie Madill94bf7f22013-07-08 13:31:15 -04003564 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003565 {
3566 switch (constUnion->getType())
3567 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003568 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003569 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003570 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003571 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003572 default: UNREACHABLE();
3573 }
3574
3575 if (i != size - 1)
3576 {
3577 out << ", ";
3578 }
3579 }
3580
3581 if (writeType)
3582 {
3583 out << ")";
3584 }
3585 }
3586
3587 return constUnion;
3588}
3589
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003590TString OutputHLSL::scopeString(unsigned int depthLimit)
3591{
3592 TString string;
3593
3594 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3595 {
3596 string += "_" + str(i);
3597 }
3598
3599 return string;
3600}
3601
3602TString OutputHLSL::scopedStruct(const TString &typeName)
3603{
3604 if (typeName == "")
3605 {
3606 return typeName;
3607 }
3608
3609 return typeName + scopeString(mScopeDepth);
3610}
3611
3612TString OutputHLSL::structLookup(const TString &typeName)
3613{
3614 for (int depth = mScopeDepth; depth >= 0; depth--)
3615 {
3616 TString scopedName = decorate(typeName + scopeString(depth));
3617
3618 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3619 {
3620 if (*structName == scopedName)
3621 {
3622 return scopedName;
3623 }
3624 }
3625 }
3626
3627 UNREACHABLE(); // Should have found a matching constructor
3628
3629 return typeName;
3630}
3631
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003632TString OutputHLSL::decorate(const TString &string)
3633{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003634 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003635 {
3636 return "_" + string;
3637 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003638
3639 return string;
3640}
3641
apatrick@chromium.org65756022012-01-17 21:45:38 +00003642TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003643{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003644 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003645 {
3646 return "ex_" + string;
3647 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003648
3649 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003650}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003651
Jamie Madill98493dd2013-07-08 14:39:03 -04003652TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003653{
Jamie Madill98493dd2013-07-08 14:39:03 -04003654 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003655 {
3656 return decorate(string);
3657 }
3658
3659 return string;
3660}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003661
3662TString OutputHLSL::registerString(TIntermSymbol *operand)
3663{
3664 ASSERT(operand->getQualifier() == EvqUniform);
3665
3666 if (IsSampler(operand->getBasicType()))
3667 {
3668 return "s" + str(samplerRegister(operand));
3669 }
3670
3671 return "c" + str(uniformRegister(operand));
3672}
3673
3674int OutputHLSL::samplerRegister(TIntermSymbol *sampler)
3675{
3676 const TType &type = sampler->getType();
3677 ASSERT(IsSampler(type.getBasicType()));
3678
3679 int index = mSamplerRegister;
3680 mSamplerRegister += sampler->totalRegisterCount();
3681
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003682 declareUniform(type, sampler->getSymbol(), index);
3683
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003684 return index;
3685}
3686
3687int OutputHLSL::uniformRegister(TIntermSymbol *uniform)
3688{
3689 const TType &type = uniform->getType();
3690 ASSERT(!IsSampler(type.getBasicType()));
3691
3692 int index = mUniformRegister;
3693 mUniformRegister += uniform->totalRegisterCount();
3694
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003695 declareUniform(type, uniform->getSymbol(), index);
3696
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003697 return index;
3698}
3699
Jamie Madill98493dd2013-07-08 14:39:03 -04003700void OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, ActiveUniforms& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003701{
Jamie Madill98493dd2013-07-08 14:39:03 -04003702 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003703
3704 if (!structure)
3705 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003706 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill98493dd2013-07-08 14:39:03 -04003707 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 +00003708 }
3709 else
3710 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003711 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 +00003712
Jamie Madill98493dd2013-07-08 14:39:03 -04003713 int fieldRegister = registerIndex;
3714 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003715
Jamie Madill98493dd2013-07-08 14:39:03 -04003716 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003717 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003718 TField *field = fields[fieldIndex];
3719 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003720
Jamie Madill010fffa2013-06-20 11:55:53 -04003721 // make sure to copy matrix packing information
Jamie Madill98493dd2013-07-08 14:39:03 -04003722 fieldType->setLayoutQualifier(type.getLayoutQualifier());
Jamie Madill010fffa2013-06-20 11:55:53 -04003723
Jamie Madill98493dd2013-07-08 14:39:03 -04003724 declareUniformToList(*fieldType, field->name(), fieldRegister, structUniform.fields);
3725 fieldRegister += fieldType->totalRegisterCount();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003726 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003727
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003728 output.push_back(structUniform);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003729 }
3730}
3731
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003732void OutputHLSL::declareUniform(const TType &type, const TString &name, int index)
3733{
3734 declareUniformToList(type, name, index, mActiveUniforms);
3735}
3736
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003737GLenum OutputHLSL::glVariableType(const TType &type)
3738{
3739 if (type.getBasicType() == EbtFloat)
3740 {
3741 if (type.isScalar())
3742 {
3743 return GL_FLOAT;
3744 }
3745 else if (type.isVector())
3746 {
3747 switch(type.getNominalSize())
3748 {
3749 case 2: return GL_FLOAT_VEC2;
3750 case 3: return GL_FLOAT_VEC3;
3751 case 4: return GL_FLOAT_VEC4;
3752 default: UNREACHABLE();
3753 }
3754 }
3755 else if (type.isMatrix())
3756 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003757 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003758 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003759 case 2:
3760 switch(type.getRows())
3761 {
3762 case 2: return GL_FLOAT_MAT2;
3763 case 3: return GL_FLOAT_MAT2x3;
3764 case 4: return GL_FLOAT_MAT2x4;
3765 default: UNREACHABLE();
3766 }
3767
3768 case 3:
3769 switch(type.getRows())
3770 {
3771 case 2: return GL_FLOAT_MAT3x2;
3772 case 3: return GL_FLOAT_MAT3;
3773 case 4: return GL_FLOAT_MAT3x4;
3774 default: UNREACHABLE();
3775 }
3776
3777 case 4:
3778 switch(type.getRows())
3779 {
3780 case 2: return GL_FLOAT_MAT4x2;
3781 case 3: return GL_FLOAT_MAT4x3;
3782 case 4: return GL_FLOAT_MAT4;
3783 default: UNREACHABLE();
3784 }
3785
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003786 default: UNREACHABLE();
3787 }
3788 }
3789 else UNREACHABLE();
3790 }
3791 else if (type.getBasicType() == EbtInt)
3792 {
3793 if (type.isScalar())
3794 {
3795 return GL_INT;
3796 }
3797 else if (type.isVector())
3798 {
3799 switch(type.getNominalSize())
3800 {
3801 case 2: return GL_INT_VEC2;
3802 case 3: return GL_INT_VEC3;
3803 case 4: return GL_INT_VEC4;
3804 default: UNREACHABLE();
3805 }
3806 }
3807 else UNREACHABLE();
3808 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003809 else if (type.getBasicType() == EbtUInt)
3810 {
3811 if (type.isScalar())
3812 {
3813 return GL_UNSIGNED_INT;
3814 }
3815 else if (type.isVector())
3816 {
Jamie Madill22d63da2013-06-07 12:45:12 -04003817 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003818 {
3819 case 2: return GL_UNSIGNED_INT_VEC2;
3820 case 3: return GL_UNSIGNED_INT_VEC3;
3821 case 4: return GL_UNSIGNED_INT_VEC4;
3822 default: UNREACHABLE();
3823 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003824 }
3825 else UNREACHABLE();
3826 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003827 else if (type.getBasicType() == EbtBool)
3828 {
3829 if (type.isScalar())
3830 {
3831 return GL_BOOL;
3832 }
3833 else if (type.isVector())
3834 {
3835 switch(type.getNominalSize())
3836 {
3837 case 2: return GL_BOOL_VEC2;
3838 case 3: return GL_BOOL_VEC3;
3839 case 4: return GL_BOOL_VEC4;
3840 default: UNREACHABLE();
3841 }
3842 }
3843 else UNREACHABLE();
3844 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003845
3846 switch(type.getBasicType())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003847 {
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003848 case EbtSampler2D: return GL_SAMPLER_2D;
3849 case EbtSampler3D: return GL_SAMPLER_3D;
3850 case EbtSamplerCube: return GL_SAMPLER_CUBE;
3851 case EbtSampler2DArray: return GL_SAMPLER_2D_ARRAY;
3852 case EbtISampler2D: return GL_INT_SAMPLER_2D;
3853 case EbtISampler3D: return GL_INT_SAMPLER_3D;
3854 case EbtISamplerCube: return GL_INT_SAMPLER_CUBE;
3855 case EbtISampler2DArray: return GL_INT_SAMPLER_2D_ARRAY;
3856 case EbtUSampler2D: return GL_UNSIGNED_INT_SAMPLER_2D;
3857 case EbtUSampler3D: return GL_UNSIGNED_INT_SAMPLER_3D;
3858 case EbtUSamplerCube: return GL_UNSIGNED_INT_SAMPLER_CUBE;
3859 case EbtUSampler2DArray: return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3860 case EbtSampler2DShadow: return GL_SAMPLER_2D_SHADOW;
3861 case EbtSamplerCubeShadow: return GL_SAMPLER_CUBE_SHADOW;
3862 case EbtSampler2DArrayShadow: return GL_SAMPLER_2D_ARRAY_SHADOW;
3863 default: UNREACHABLE();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003864 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003865
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003866
3867 return GL_NONE;
3868}
3869
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003870GLenum OutputHLSL::glVariablePrecision(const TType &type)
3871{
3872 if (type.getBasicType() == EbtFloat)
3873 {
3874 switch (type.getPrecision())
3875 {
3876 case EbpHigh: return GL_HIGH_FLOAT;
3877 case EbpMedium: return GL_MEDIUM_FLOAT;
3878 case EbpLow: return GL_LOW_FLOAT;
3879 case EbpUndefined:
3880 // Should be defined as the default precision by the parser
3881 default: UNREACHABLE();
3882 }
3883 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003884 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003885 {
3886 switch (type.getPrecision())
3887 {
3888 case EbpHigh: return GL_HIGH_INT;
3889 case EbpMedium: return GL_MEDIUM_INT;
3890 case EbpLow: return GL_LOW_INT;
3891 case EbpUndefined:
3892 // Should be defined as the default precision by the parser
3893 default: UNREACHABLE();
3894 }
3895 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003896
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00003897 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003898 return GL_NONE;
3899}
3900
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003901bool OutputHLSL::isVaryingOut(TQualifier qualifier)
3902{
3903 switch(qualifier)
3904 {
3905 case EvqVaryingOut:
3906 case EvqInvariantVaryingOut:
3907 case EvqSmoothOut:
3908 case EvqFlatOut:
3909 case EvqCentroidOut:
3910 return true;
3911 }
3912
3913 return false;
3914}
3915
3916bool OutputHLSL::isVaryingIn(TQualifier qualifier)
3917{
3918 switch(qualifier)
3919 {
3920 case EvqVaryingIn:
3921 case EvqInvariantVaryingIn:
3922 case EvqSmoothIn:
3923 case EvqFlatIn:
3924 case EvqCentroidIn:
3925 return true;
3926 }
3927
3928 return false;
3929}
3930
3931bool OutputHLSL::isVarying(TQualifier qualifier)
3932{
3933 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
3934}
3935
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003936}