blob: 03ff3fc8c00bae026b060b7e83b9656eba1d06bf [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002// Copyright (c) 2002-2014 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
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/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"
Geoff Lang17732822013-08-29 13:46:49 -040011#include "compiler/translator/compilerdebug.h"
12#include "compiler/translator/InfoSink.h"
13#include "compiler/translator/DetectDiscontinuity.h"
14#include "compiler/translator/SearchSymbol.h"
15#include "compiler/translator/UnfoldShortCircuit.h"
16#include "compiler/translator/HLSLLayoutEncoder.h"
17#include "compiler/translator/FlagStd140Structs.h"
Jamie Madill3c9eeb92013-11-04 11:09:26 -050018#include "compiler/translator/NodeSearch.h"
Jamie Madille53c98b2014-02-03 11:57:13 -050019#include "compiler/translator/RewriteElseBlocks.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000020
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000021#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000022#include <cfloat>
23#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000024
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000025namespace sh
26{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000027
Nicolas Capense0ba27a2013-06-24 16:10:52 -040028TString OutputHLSL::TextureFunction::name() const
29{
30 TString name = "gl_texture";
31
Nicolas Capens6d232bb2013-07-08 15:56:38 -040032 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040033 {
34 name += "2D";
35 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040036 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040037 {
38 name += "3D";
39 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040040 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040041 {
42 name += "Cube";
43 }
44 else UNREACHABLE();
45
46 if (proj)
47 {
48 name += "Proj";
49 }
50
Nicolas Capensb1f45b72013-12-19 17:37:19 -050051 if (offset)
52 {
53 name += "Offset";
54 }
55
Nicolas Capens75fb4752013-07-10 15:14:47 -040056 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040057 {
Nicolas Capensfc014542014-02-18 14:47:13 -050058 case IMPLICIT: break;
59 case BIAS: break;
60 case LOD: name += "Lod"; break;
61 case LOD0: name += "Lod0"; break;
62 case SIZE: name += "Size"; break;
63 case FETCH: name += "Fetch"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040064 default: UNREACHABLE();
65 }
66
67 return name + "(";
68}
69
Jamie Madillc2141fb2013-08-30 13:21:08 -040070const char *RegisterPrefix(const TType &type)
71{
72 if (IsSampler(type.getBasicType()))
73 {
74 return "s";
75 }
76 else
77 {
78 return "c";
79 }
80}
81
Nicolas Capense0ba27a2013-06-24 16:10:52 -040082bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
83{
84 if (sampler < rhs.sampler) return true;
85 if (coords < rhs.coords) return true;
86 if (!proj && rhs.proj) return true;
Nicolas Capens75fb4752013-07-10 15:14:47 -040087 if (method < rhs.method) return true;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040088
89 return false;
90}
91
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +000092OutputHLSL::OutputHLSL(TParseContext &context, const ShBuiltInResources& resources, ShShaderOutput outputType)
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +000093 : TIntermTraverser(true, true, true), mContext(context), mOutputType(outputType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000094{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000095 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +000096 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000097
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +000098 mUsesFragColor = false;
99 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000100 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000101 mUsesFragCoord = false;
102 mUsesPointCoord = false;
103 mUsesFrontFacing = false;
104 mUsesPointSize = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400105 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000106 mUsesXor = false;
107 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000108 mUsesMod2v = false;
109 mUsesMod2f = false;
110 mUsesMod3v = false;
111 mUsesMod3f = false;
112 mUsesMod4v = false;
113 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000114 mUsesFaceforward1 = false;
115 mUsesFaceforward2 = false;
116 mUsesFaceforward3 = false;
117 mUsesFaceforward4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000118 mUsesAtan2_1 = false;
119 mUsesAtan2_2 = false;
120 mUsesAtan2_3 = false;
121 mUsesAtan2_4 = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500122 mUsesDiscardRewriting = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000123
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000124 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
125
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000126 mScopeDepth = 0;
127
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000128 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000129
130 mContainsLoopDiscontinuity = false;
131 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000132 mInsideDiscontinuousLoop = false;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000133
134 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000135
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000136 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000137 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000138 if (mContext.shaderType == SH_FRAGMENT_SHADER)
139 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000140 mUniformRegister = 3; // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000141 }
142 else
143 {
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000144 mUniformRegister = 2; // Reserve registers for dx_DepthRange and dx_ViewAdjust
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000145 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000146 }
147 else
148 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000149 mUniformRegister = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000150 }
151
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000152 mSamplerRegister = 0;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000153 mInterfaceBlockRegister = 2; // Reserve registers for the default uniform block and driver constants
Jamie Madill574d9dd2013-06-20 11:55:56 -0400154 mPaddingCounter = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000155}
156
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000157OutputHLSL::~OutputHLSL()
158{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000159 delete mUnfoldShortCircuit;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000160}
161
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000162void OutputHLSL::output()
163{
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +0000164 mContainsLoopDiscontinuity = mContext.shaderType == SH_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400165 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
166 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000167
Jamie Madille53c98b2014-02-03 11:57:13 -0500168 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
169 // use a vertex attribute as a condition, and some related computation in the else block.
170 if (mOutputType == SH_HLSL9_OUTPUT && mContext.shaderType == SH_VERTEX_SHADER)
171 {
172 RewriteElseBlocks(mContext.treeRoot);
173 }
174
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000175 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 +0000176 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000177
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000178 mContext.infoSink().obj << mHeader.c_str();
179 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000180}
181
Jamie Madill570e04d2013-06-21 09:15:33 -0400182void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
183{
184 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
185 {
186 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
187
188 // This will mark the necessary block elements as referenced
189 flaggedNode->traverse(this);
190 TString structName(mBody.c_str());
191 mBody.erase();
192
193 mFlaggedStructOriginalNames[flaggedNode] = structName;
194
195 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
196 {
197 structName.erase(pos, 1);
198 }
199
200 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
201 }
202}
203
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000204TInfoSinkBase &OutputHLSL::getBodyStream()
205{
206 return mBody;
207}
208
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400209const std::vector<Uniform> &OutputHLSL::getUniforms()
daniel@transgaming.com043da132012-12-20 21:12:22 +0000210{
211 return mActiveUniforms;
212}
213
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000214const ActiveInterfaceBlocks &OutputHLSL::getInterfaceBlocks() const
215{
216 return mActiveInterfaceBlocks;
217}
218
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400219const std::vector<Attribute> &OutputHLSL::getOutputVariables() const
Jamie Madill46131a32013-06-20 11:55:50 -0400220{
221 return mActiveOutputVariables;
222}
223
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400224const std::vector<Attribute> &OutputHLSL::getAttributes() const
Jamie Madilldefb6742013-06-20 11:55:51 -0400225{
226 return mActiveAttributes;
227}
228
Jamie Madill47fdd132013-08-30 13:21:04 -0400229const std::vector<Varying> &OutputHLSL::getVaryings() const
230{
231 return mActiveVaryings;
232}
233
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000234int OutputHLSL::vectorSize(const TType &type) const
235{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000236 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000237 int arraySize = type.isArray() ? type.getArraySize() : 1;
238
239 return elementSize * arraySize;
240}
241
Jamie Madill98493dd2013-07-08 14:39:03 -0400242TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, const TField &field)
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000243{
Jamie Madill98493dd2013-07-08 14:39:03 -0400244 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000245 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400246 return interfaceBlock.name() + "." + field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000247 }
248 else
249 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400250 return field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000251 }
252}
253
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000254TString OutputHLSL::decoratePrivate(const TString &privateText)
255{
256 return "dx_" + privateText;
257}
258
Jamie Madill98493dd2013-07-08 14:39:03 -0400259TString OutputHLSL::interfaceBlockStructNameString(const TInterfaceBlock &interfaceBlock)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000260{
Jamie Madill98493dd2013-07-08 14:39:03 -0400261 return decoratePrivate(interfaceBlock.name()) + "_type";
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000262}
263
Jamie Madill98493dd2013-07-08 14:39:03 -0400264TString OutputHLSL::interfaceBlockInstanceString(const TInterfaceBlock& interfaceBlock, unsigned int arrayIndex)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000265{
Jamie Madill98493dd2013-07-08 14:39:03 -0400266 if (!interfaceBlock.hasInstanceName())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000267 {
268 return "";
269 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400270 else if (interfaceBlock.isArray())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000271 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400272 return decoratePrivate(interfaceBlock.instanceName()) + "_" + str(arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000273 }
274 else
275 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400276 return decorate(interfaceBlock.instanceName());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000277 }
278}
279
Jamie Madill98493dd2013-07-08 14:39:03 -0400280TString OutputHLSL::interfaceBlockFieldTypeString(const TField &field, TLayoutBlockStorage blockStorage)
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000281{
Jamie Madill98493dd2013-07-08 14:39:03 -0400282 const TType &fieldType = *field.type();
283 const TLayoutMatrixPacking matrixPacking = fieldType.getLayoutQualifier().matrixPacking;
Jamie Madill529077d2013-06-20 11:55:54 -0400284 ASSERT(matrixPacking != EmpUnspecified);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000285
Jamie Madill98493dd2013-07-08 14:39:03 -0400286 if (fieldType.isMatrix())
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 Madill529077d2013-06-20 11:55:54 -0400289 const TString &matrixPackString = (matrixPacking == EmpRowMajor ? "column_major" : "row_major");
Jamie Madill98493dd2013-07-08 14:39:03 -0400290 return matrixPackString + " " + typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000291 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400292 else if (fieldType.getStruct())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000293 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400294 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill98493dd2013-07-08 14:39:03 -0400295 return structureTypeName(*fieldType.getStruct(), matrixPacking == EmpColumnMajor, blockStorage == EbsStd140);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000296 }
297 else
298 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400299 return typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000300 }
301}
302
Jamie Madill98493dd2013-07-08 14:39:03 -0400303TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, TLayoutBlockStorage blockStorage)
304{
305 TString hlsl;
306
307 int elementIndex = 0;
308
309 for (unsigned int typeIndex = 0; typeIndex < interfaceBlock.fields().size(); typeIndex++)
310 {
311 const TField &field = *interfaceBlock.fields()[typeIndex];
312 const TType &fieldType = *field.type();
313
314 if (blockStorage == EbsStd140)
315 {
316 // 2 and 3 component vector types in some cases need pre-padding
317 hlsl += std140PrePaddingString(fieldType, &elementIndex);
318 }
319
320 hlsl += " " + interfaceBlockFieldTypeString(field, blockStorage) +
321 " " + decorate(field.name()) + arrayString(fieldType) + ";\n";
322
323 // must pad out after matrices and arrays, where HLSL usually allows itself room to pack stuff
324 if (blockStorage == EbsStd140)
325 {
326 const bool useHLSLRowMajorPacking = (fieldType.getLayoutQualifier().matrixPacking == EmpColumnMajor);
327 hlsl += std140PostPaddingString(fieldType, useHLSLRowMajorPacking);
328 }
329 }
330
331 return hlsl;
332}
333
334TString OutputHLSL::interfaceBlockStructString(const TInterfaceBlock &interfaceBlock)
335{
336 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
337
338 return "struct " + interfaceBlockStructNameString(interfaceBlock) + "\n"
339 "{\n" +
340 interfaceBlockFieldString(interfaceBlock, blockStorage) +
341 "};\n\n";
342}
343
344TString OutputHLSL::interfaceBlockString(const TInterfaceBlock &interfaceBlock, unsigned int registerIndex, unsigned int arrayIndex)
345{
346 const TString &arrayIndexString = (arrayIndex != GL_INVALID_INDEX ? decorate(str(arrayIndex)) : "");
347 const TString &blockName = interfaceBlock.name() + arrayIndexString;
348 TString hlsl;
349
350 hlsl += "cbuffer " + blockName + " : register(b" + str(registerIndex) + ")\n"
351 "{\n";
352
353 if (interfaceBlock.hasInstanceName())
354 {
355 hlsl += " " + interfaceBlockStructNameString(interfaceBlock) + " " + interfaceBlockInstanceString(interfaceBlock, arrayIndex) + ";\n";
356 }
357 else
358 {
359 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
360 hlsl += interfaceBlockFieldString(interfaceBlock, blockStorage);
361 }
362
363 hlsl += "};\n\n";
364
365 return hlsl;
366}
367
Jamie Madill574d9dd2013-06-20 11:55:56 -0400368TString OutputHLSL::std140PrePaddingString(const TType &type, int *elementIndex)
369{
370 if (type.getBasicType() == EbtStruct || type.isMatrix() || type.isArray())
371 {
372 // no padding needed, HLSL will align the field to a new register
373 *elementIndex = 0;
374 return "";
375 }
376
377 const GLenum glType = glVariableType(type);
378 const int numComponents = gl::UniformComponentCount(glType);
379
380 if (numComponents >= 4)
381 {
382 // no padding needed, HLSL will align the field to a new register
383 *elementIndex = 0;
384 return "";
385 }
386
387 if (*elementIndex + numComponents > 4)
388 {
389 // no padding needed, HLSL will align the field to a new register
390 *elementIndex = numComponents;
391 return "";
392 }
393
394 TString padding;
395
396 const int alignment = numComponents == 3 ? 4 : numComponents;
397 const int paddingOffset = (*elementIndex % alignment);
398
399 if (paddingOffset != 0)
400 {
401 // padding is neccessary
402 for (int paddingIndex = paddingOffset; paddingIndex < alignment; paddingIndex++)
403 {
404 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
405 }
406
407 *elementIndex += (alignment - paddingOffset);
408 }
409
410 *elementIndex += numComponents;
411 *elementIndex %= 4;
412
413 return padding;
414}
415
Jamie Madille4075c92013-06-21 09:15:32 -0400416TString OutputHLSL::std140PostPaddingString(const TType &type, bool useHLSLRowMajorPacking)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400417{
Jamie Madillc835df62013-06-21 09:15:32 -0400418 if (!type.isMatrix() && !type.isArray() && type.getBasicType() != EbtStruct)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400419 {
420 return "";
421 }
422
Jamie Madill574d9dd2013-06-20 11:55:56 -0400423 int numComponents = 0;
424
425 if (type.isMatrix())
426 {
Jamie Madille4075c92013-06-21 09:15:32 -0400427 // This method can also be called from structureString, which does not use layout qualifiers.
428 // Thus, use the method parameter for determining the matrix packing.
429 //
430 // Note HLSL row major packing corresponds to GL API column-major, and vice-versa, since we
431 // wish to always transpose GL matrices to play well with HLSL's matrix array indexing.
432 //
433 const bool isRowMajorMatrix = !useHLSLRowMajorPacking;
Jamie Madillc835df62013-06-21 09:15:32 -0400434 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400435 numComponents = gl::MatrixComponentCount(glType, isRowMajorMatrix);
436 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400437 else if (type.getStruct())
Jamie Madillc835df62013-06-21 09:15:32 -0400438 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400439 const TString &structName = structureTypeName(*type.getStruct(), useHLSLRowMajorPacking, true);
Jamie Madille4075c92013-06-21 09:15:32 -0400440 numComponents = mStd140StructElementIndexes[structName];
441
442 if (numComponents == 0)
443 {
444 return "";
445 }
Jamie Madillc835df62013-06-21 09:15:32 -0400446 }
Jamie Madill574d9dd2013-06-20 11:55:56 -0400447 else
448 {
Jamie Madillc835df62013-06-21 09:15:32 -0400449 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400450 numComponents = gl::UniformComponentCount(glType);
451 }
452
453 TString padding;
454 for (int paddingOffset = numComponents; paddingOffset < 4; paddingOffset++)
455 {
456 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
457 }
458 return padding;
459}
460
Jamie Madill440dc742013-06-20 11:55:55 -0400461// Use the same layout for packed and shared
462void setBlockLayout(InterfaceBlock *interfaceBlock, BlockLayoutType newLayout)
463{
464 interfaceBlock->layout = newLayout;
465 interfaceBlock->blockInfo.clear();
466
467 switch (newLayout)
468 {
469 case BLOCKLAYOUT_SHARED:
470 case BLOCKLAYOUT_PACKED:
471 {
472 HLSLBlockEncoder hlslEncoder(&interfaceBlock->blockInfo);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400473 hlslEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400474 interfaceBlock->dataSize = hlslEncoder.getBlockSize();
475 }
476 break;
477
478 case BLOCKLAYOUT_STANDARD:
479 {
480 Std140BlockEncoder stdEncoder(&interfaceBlock->blockInfo);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400481 stdEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400482 interfaceBlock->dataSize = stdEncoder.getBlockSize();
483 }
484 break;
485
486 default:
487 UNREACHABLE();
488 break;
489 }
490}
491
Jamie Madill574d9dd2013-06-20 11:55:56 -0400492BlockLayoutType convertBlockLayoutType(TLayoutBlockStorage blockStorage)
493{
494 switch (blockStorage)
495 {
496 case EbsPacked: return BLOCKLAYOUT_PACKED;
497 case EbsShared: return BLOCKLAYOUT_SHARED;
498 case EbsStd140: return BLOCKLAYOUT_STANDARD;
499 default: UNREACHABLE(); return BLOCKLAYOUT_SHARED;
500 }
501}
502
Jamie Madill98493dd2013-07-08 14:39:03 -0400503TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400504{
505 TString init;
506
507 TString preIndentString;
508 TString fullIndentString;
509
510 for (int spaces = 0; spaces < (indent * 4); spaces++)
511 {
512 preIndentString += ' ';
513 }
514
515 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
516 {
517 fullIndentString += ' ';
518 }
519
520 init += preIndentString + "{\n";
521
Jamie Madill98493dd2013-07-08 14:39:03 -0400522 const TFieldList &fields = structure.fields();
523 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400524 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400525 const TField &field = *fields[fieldIndex];
526 const TString &fieldName = rhsStructName + "." + decorate(field.name());
527 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400528
Jamie Madill98493dd2013-07-08 14:39:03 -0400529 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400530 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400531 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400532 }
533 else
534 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400535 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400536 }
537 }
538
539 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
540
541 return init;
542}
543
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000544void OutputHLSL::header()
545{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000546 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000547
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000548 TString uniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000549 TString interfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000550 TString varyings;
551 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400552 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000553
Jamie Madillc2141fb2013-08-30 13:21:08 -0400554 for (ReferencedSymbols::const_iterator uniformIt = mReferencedUniforms.begin(); uniformIt != mReferencedUniforms.end(); uniformIt++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000555 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400556 const TIntermSymbol &uniform = *uniformIt->second;
557 const TType &type = uniform.getType();
558 const TString &name = uniform.getSymbol();
559
560 int registerIndex = declareUniformAndAssignRegister(type, name);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000561
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000562 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture
563 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400564 uniforms += "uniform " + samplerString(type) + " sampler_" + decorateUniform(name, type) + arrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400565 " : register(s" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000566
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000567 uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400568 " : register(t" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000569 }
570 else
571 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400572 const TStructure *structure = type.getStruct();
573 const TString &typeName = (structure ? structureTypeName(*structure, false, false) : typeString(type));
574
575 const TString &registerString = TString("register(") + RegisterPrefix(type) + str(registerIndex) + ")";
576
577 uniforms += "uniform " + typeName + " " + decorateUniform(name, type) + arrayString(type) + " : " + registerString + ";\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000578 }
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000579 }
580
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000581 for (ReferencedSymbols::const_iterator interfaceBlockIt = mReferencedInterfaceBlocks.begin(); interfaceBlockIt != mReferencedInterfaceBlocks.end(); interfaceBlockIt++)
582 {
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000583 const TType &nodeType = interfaceBlockIt->second->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -0400584 const TInterfaceBlock &interfaceBlock = *nodeType.getInterfaceBlock();
585 const TFieldList &fieldList = interfaceBlock.fields();
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000586
Jamie Madill98493dd2013-07-08 14:39:03 -0400587 unsigned int arraySize = static_cast<unsigned int>(interfaceBlock.arraySize());
588 sh::InterfaceBlock activeBlock(interfaceBlock.name().c_str(), arraySize, mInterfaceBlockRegister);
589 for (unsigned int typeIndex = 0; typeIndex < fieldList.size(); typeIndex++)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000590 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400591 const TField &field = *fieldList[typeIndex];
592 const TString &fullUniformName = interfaceBlockFieldString(interfaceBlock, field);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400593 declareInterfaceBlockField(*field.type(), fullUniformName, activeBlock.fields);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000594 }
595
Jamie Madill98493dd2013-07-08 14:39:03 -0400596 mInterfaceBlockRegister += std::max(1u, arraySize);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000597
Jamie Madill98493dd2013-07-08 14:39:03 -0400598 BlockLayoutType blockLayoutType = convertBlockLayoutType(interfaceBlock.blockStorage());
599 setBlockLayout(&activeBlock, blockLayoutType);
Jamie Madill9060a4e2013-08-12 16:22:57 -0700600
601 if (interfaceBlock.matrixPacking() == EmpRowMajor)
602 {
603 activeBlock.isRowMajorLayout = true;
604 }
605
Jamie Madill98493dd2013-07-08 14:39:03 -0400606 mActiveInterfaceBlocks.push_back(activeBlock);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000607
Jamie Madill98493dd2013-07-08 14:39:03 -0400608 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000609 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400610 interfaceBlocks += interfaceBlockStructString(interfaceBlock);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000611 }
612
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000613 if (arraySize > 0)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000614 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000615 for (unsigned int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
616 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400617 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex + arrayIndex, arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000618 }
619 }
620 else
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000621 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400622 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000623 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000624 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000625
Jamie Madill829f59e2013-11-13 19:40:54 -0500626 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400627 {
628 TIntermTyped *structNode = flaggedStructIt->first;
629 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400630 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400631 const TString &originalName = mFlaggedStructOriginalNames[structNode];
632
Jamie Madill98493dd2013-07-08 14:39:03 -0400633 flaggedStructs += "static " + decorate(structure.name()) + " " + mappedName + " =\n";
634 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400635 flaggedStructs += "\n";
636 }
637
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000638 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
639 {
640 const TType &type = varying->second->getType();
641 const TString &name = varying->second->getSymbol();
642
643 // Program linking depends on this exact format
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +0000644 varyings += "static " + interpolationString(type.getQualifier()) + " " + typeString(type) + " " +
645 decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madill47fdd132013-08-30 13:21:04 -0400646
Jamie Madill94599662013-08-30 13:21:10 -0400647 declareVaryingToList(type, type.getQualifier(), name, mActiveVaryings);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000648 }
649
650 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
651 {
652 const TType &type = attribute->second->getType();
653 const TString &name = attribute->second->getSymbol();
654
655 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400656
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400657 Attribute attributeVar(glVariableType(type), glVariablePrecision(type), name.c_str(),
658 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
659 mActiveAttributes.push_back(attributeVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000660 }
661
Jamie Madill529077d2013-06-20 11:55:54 -0400662 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
663 {
664 out << *structDeclaration;
665 }
666
667 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
668 {
669 out << *constructor;
670 }
671
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500672 if (mUsesDiscardRewriting)
673 {
674 out << "#define ANGLE_USES_DISCARD_REWRITING" << "\n";
675 }
676
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400677 if (mContext.shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000678 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000679 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000680 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000681
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000682 out << "// Varyings\n";
683 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400684 out << "\n";
685
686 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000687 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500688 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000689 {
Jamie Madill46131a32013-06-20 11:55:50 -0400690 const TString &variableName = outputVariableIt->first;
691 const TType &variableType = outputVariableIt->second->getType();
692 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
693
694 out << "static " + typeString(variableType) + " out_" + variableName + arrayString(variableType) +
695 " = " + initializer(variableType) + ";\n";
696
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400697 Attribute outputVar(glVariableType(variableType), glVariablePrecision(variableType), variableName.c_str(),
698 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
Jamie Madill46131a32013-06-20 11:55:50 -0400699 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000700 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000701 }
Jamie Madill46131a32013-06-20 11:55:50 -0400702 else
703 {
704 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
705
706 out << "static float4 gl_Color[" << numColorValues << "] =\n"
707 "{\n";
708 for (unsigned int i = 0; i < numColorValues; i++)
709 {
710 out << " float4(0, 0, 0, 0)";
711 if (i + 1 != numColorValues)
712 {
713 out << ",";
714 }
715 out << "\n";
716 }
717
718 out << "};\n";
719 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000720
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400721 if (mUsesFragDepth)
722 {
723 out << "static float gl_Depth = 0.0;\n";
724 }
725
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000726 if (mUsesFragCoord)
727 {
728 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
729 }
730
731 if (mUsesPointCoord)
732 {
733 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
734 }
735
736 if (mUsesFrontFacing)
737 {
738 out << "static bool gl_FrontFacing = false;\n";
739 }
740
741 out << "\n";
742
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000743 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000744 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000745 out << "struct gl_DepthRangeParameters\n"
746 "{\n"
747 " float near;\n"
748 " float far;\n"
749 " float diff;\n"
750 "};\n"
751 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000752 }
753
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000754 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000755 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000756 out << "cbuffer DriverConstants : register(b1)\n"
757 "{\n";
758
759 if (mUsesDepthRange)
760 {
761 out << " float3 dx_DepthRange : packoffset(c0);\n";
762 }
763
764 if (mUsesFragCoord)
765 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000766 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000767 }
768
769 if (mUsesFragCoord || mUsesFrontFacing)
770 {
771 out << " float3 dx_DepthFront : packoffset(c2);\n";
772 }
773
774 out << "};\n";
775 }
776 else
777 {
778 if (mUsesDepthRange)
779 {
780 out << "uniform float3 dx_DepthRange : register(c0);";
781 }
782
783 if (mUsesFragCoord)
784 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000785 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000786 }
787
788 if (mUsesFragCoord || mUsesFrontFacing)
789 {
790 out << "uniform float3 dx_DepthFront : register(c2);\n";
791 }
792 }
793
794 out << "\n";
795
796 if (mUsesDepthRange)
797 {
798 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
799 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000800 }
801
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000802 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000803 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000804
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000805 if (!interfaceBlocks.empty())
806 {
807 out << interfaceBlocks;
808 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400809
810 if (!flaggedStructs.empty())
811 {
812 out << "// Std140 Structures accessed by value\n";
813 out << "\n";
814 out << flaggedStructs;
815 out << "\n";
816 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000817 }
818
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000819 if (usingMRTExtension && mNumRenderTargets > 1)
820 {
821 out << "#define GL_USES_MRT\n";
822 }
823
824 if (mUsesFragColor)
825 {
826 out << "#define GL_USES_FRAG_COLOR\n";
827 }
828
829 if (mUsesFragData)
830 {
831 out << "#define GL_USES_FRAG_DATA\n";
832 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000833 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000834 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000835 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000836 out << "// Attributes\n";
837 out << attributes;
838 out << "\n"
839 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
840
841 if (mUsesPointSize)
842 {
843 out << "static float gl_PointSize = float(1);\n";
844 }
845
846 out << "\n"
847 "// Varyings\n";
848 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000849 out << "\n";
850
851 if (mUsesDepthRange)
852 {
853 out << "struct gl_DepthRangeParameters\n"
854 "{\n"
855 " float near;\n"
856 " float far;\n"
857 " float diff;\n"
858 "};\n"
859 "\n";
860 }
861
862 if (mOutputType == SH_HLSL11_OUTPUT)
863 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000864 if (mUsesDepthRange)
865 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000866 out << "cbuffer DriverConstants : register(b1)\n"
867 "{\n"
868 " float3 dx_DepthRange : packoffset(c0);\n"
869 "};\n"
870 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000871 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000872 }
873 else
874 {
875 if (mUsesDepthRange)
876 {
877 out << "uniform float3 dx_DepthRange : register(c0);\n";
878 }
879
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000880 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000881 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000882 }
883
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000884 if (mUsesDepthRange)
885 {
886 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
887 "\n";
888 }
889
890 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000891 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000892
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000893 if (!interfaceBlocks.empty())
894 {
895 out << interfaceBlocks;
896 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400897
898 if (!flaggedStructs.empty())
899 {
900 out << "// Std140 Structures accessed by value\n";
901 out << "\n";
902 out << flaggedStructs;
903 out << "\n";
904 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000905 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400906 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000907
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400908 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
909 {
910 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400911 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000912 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400913 switch(textureFunction->sampler)
914 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400915 case EbtSampler2D: out << "int2 "; break;
916 case EbtSampler3D: out << "int3 "; break;
917 case EbtSamplerCube: out << "int2 "; break;
918 case EbtSampler2DArray: out << "int3 "; break;
919 case EbtISampler2D: out << "int2 "; break;
920 case EbtISampler3D: out << "int3 "; break;
921 case EbtISamplerCube: out << "int2 "; break;
922 case EbtISampler2DArray: out << "int3 "; break;
923 case EbtUSampler2D: out << "int2 "; break;
924 case EbtUSampler3D: out << "int3 "; break;
925 case EbtUSamplerCube: out << "int2 "; break;
926 case EbtUSampler2DArray: out << "int3 "; break;
927 case EbtSampler2DShadow: out << "int2 "; break;
928 case EbtSamplerCubeShadow: out << "int2 "; break;
929 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400930 default: UNREACHABLE();
931 }
932 }
933 else // Sampling function
934 {
935 switch(textureFunction->sampler)
936 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400937 case EbtSampler2D: out << "float4 "; break;
938 case EbtSampler3D: out << "float4 "; break;
939 case EbtSamplerCube: out << "float4 "; break;
940 case EbtSampler2DArray: out << "float4 "; break;
941 case EbtISampler2D: out << "int4 "; break;
942 case EbtISampler3D: out << "int4 "; break;
943 case EbtISamplerCube: out << "int4 "; break;
944 case EbtISampler2DArray: out << "int4 "; break;
945 case EbtUSampler2D: out << "uint4 "; break;
946 case EbtUSampler3D: out << "uint4 "; break;
947 case EbtUSamplerCube: out << "uint4 "; break;
948 case EbtUSampler2DArray: out << "uint4 "; break;
949 case EbtSampler2DShadow: out << "float "; break;
950 case EbtSamplerCubeShadow: out << "float "; break;
951 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400952 default: UNREACHABLE();
953 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000954 }
955
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400956 // Function name
957 out << textureFunction->name();
958
959 // Argument list
960 int hlslCoords = 4;
961
962 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000963 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400964 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000965 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400966 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
967 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
968 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000969 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400970
Nicolas Capens75fb4752013-07-10 15:14:47 -0400971 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000972 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400973 case TextureFunction::IMPLICIT: break;
974 case TextureFunction::BIAS: hlslCoords = 4; break;
975 case TextureFunction::LOD: hlslCoords = 4; break;
976 case TextureFunction::LOD0: hlslCoords = 4; break;
977 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000978 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400979 }
980 else if (mOutputType == SH_HLSL11_OUTPUT)
981 {
982 switch(textureFunction->sampler)
983 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400984 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
985 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
986 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
987 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
988 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
989 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
990 case EbtISamplerCube: out << "TextureCube<int4> x, SamplerState s"; hlslCoords = 3; break;
991 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
992 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
993 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
994 case EbtUSamplerCube: out << "TextureCube<uint4> x, SamplerState s"; hlslCoords = 3; break;
995 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
996 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
997 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
998 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400999 default: UNREACHABLE();
1000 }
1001 }
1002 else UNREACHABLE();
1003
Nicolas Capensfc014542014-02-18 14:47:13 -05001004 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001005 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001006 switch(textureFunction->coords)
1007 {
1008 case 2: out << ", int2 t"; break;
1009 case 3: out << ", int3 t"; break;
1010 default: UNREACHABLE();
1011 }
1012 }
1013 else // Floating-point coordinates (except textureSize)
1014 {
1015 switch(textureFunction->coords)
1016 {
1017 case 1: out << ", int lod"; break; // textureSize()
1018 case 2: out << ", float2 t"; break;
1019 case 3: out << ", float3 t"; break;
1020 case 4: out << ", float4 t"; break;
1021 default: UNREACHABLE();
1022 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001023 }
1024
Nicolas Capens75fb4752013-07-10 15:14:47 -04001025 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +00001026 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001027 case TextureFunction::IMPLICIT: break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001028 case TextureFunction::BIAS: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001029 case TextureFunction::LOD: out << ", float lod"; break;
1030 case TextureFunction::LOD0: break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001031 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -05001032 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001033 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +00001034 }
1035
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001036 if (textureFunction->offset)
1037 {
1038 switch(textureFunction->sampler)
1039 {
1040 case EbtSampler2D: out << ", int2 offset"; break;
1041 case EbtSampler3D: out << ", int3 offset"; break;
1042 case EbtSampler2DArray: out << ", int2 offset"; break;
1043 case EbtISampler2D: out << ", int2 offset"; break;
1044 case EbtISampler3D: out << ", int3 offset"; break;
1045 case EbtISampler2DArray: out << ", int2 offset"; break;
1046 case EbtUSampler2D: out << ", int2 offset"; break;
1047 case EbtUSampler3D: out << ", int3 offset"; break;
1048 case EbtUSampler2DArray: out << ", int2 offset"; break;
1049 case EbtSampler2DShadow: out << ", int2 offset"; break;
1050 default: UNREACHABLE();
1051 }
1052 }
1053
1054 if (textureFunction->method == TextureFunction::BIAS)
1055 {
1056 out << ", float bias";
1057 }
1058
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001059 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001060 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001061
Nicolas Capens75fb4752013-07-10 15:14:47 -04001062 if (textureFunction->method == TextureFunction::SIZE)
1063 {
1064 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
1065 {
1066 if (IsSamplerArray(textureFunction->sampler))
1067 {
1068 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
1069 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
1070 }
1071 else
1072 {
1073 out << " uint width; uint height; uint numberOfLevels;\n"
1074 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
1075 }
1076 }
1077 else if (IsSampler3D(textureFunction->sampler))
1078 {
1079 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
1080 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
1081 }
1082 else UNREACHABLE();
1083
1084 switch(textureFunction->sampler)
1085 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04001086 case EbtSampler2D: out << " return int2(width, height);"; break;
1087 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
1088 case EbtSamplerCube: out << " return int2(width, height);"; break;
1089 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
1090 case EbtISampler2D: out << " return int2(width, height);"; break;
1091 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
1092 case EbtISamplerCube: out << " return int2(width, height);"; break;
1093 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
1094 case EbtUSampler2D: out << " return int2(width, height);"; break;
1095 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
1096 case EbtUSamplerCube: out << " return int2(width, height);"; break;
1097 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
1098 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
1099 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
1100 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001101 default: UNREACHABLE();
1102 }
1103 }
1104 else if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
daniel@transgaming.com15795192011-05-11 15:36:20 +00001105 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001106 // Currently unsupported because TextureCube does not support Load
1107 // This will require emulation using a Texture2DArray with 6 faces
1108 if (textureFunction->sampler == EbtISamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001109 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001110 out << " return int4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001111 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001112 else if (textureFunction->sampler == EbtUSamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001113 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001114 out << " return uint4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001115 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001116 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001117 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001118 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001119 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001120 if (IsIntegerSampler(textureFunction->sampler) &&
1121 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001122 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001123 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001124 {
Nicolas Capens93e50de2013-07-09 13:46:28 -04001125 if (IsSamplerArray(textureFunction->sampler))
1126 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001127 out << " float width; float height; float layers; float levels;\n";
1128
1129 if (textureFunction->method == TextureFunction::LOD0)
1130 {
1131 out << " uint mip = 0;\n";
1132 }
1133 else
1134 {
1135 if (textureFunction->method == TextureFunction::IMPLICIT ||
1136 textureFunction->method == TextureFunction::BIAS)
1137 {
1138 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1139 " float2 tSized = float2(t.x * width, t.y * height);\n"
1140 " float dx = length(ddx(tSized));\n"
1141 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001142 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001143
1144 if (textureFunction->method == TextureFunction::BIAS)
1145 {
1146 out << " lod += bias;\n";
1147 }
1148 }
1149
1150 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1151 }
1152
1153 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001154 }
1155 else
1156 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001157 out << " float width; float height; float levels;\n";
1158
1159 if (textureFunction->method == TextureFunction::LOD0)
1160 {
1161 out << " uint mip = 0;\n";
1162 }
1163 else
1164 {
1165 if (textureFunction->method == TextureFunction::IMPLICIT ||
1166 textureFunction->method == TextureFunction::BIAS)
1167 {
1168 out << " x.GetDimensions(0, width, height, levels);\n"
1169 " float2 tSized = float2(t.x * width, t.y * height);\n"
1170 " float dx = length(ddx(tSized));\n"
1171 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001172 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001173
1174 if (textureFunction->method == TextureFunction::BIAS)
1175 {
1176 out << " lod += bias;\n";
1177 }
1178 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05001179 else if (textureFunction->method == TextureFunction::LOD)
1180 {
1181 out << " x.GetDimensions(0, width, height, levels);\n";
1182 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001183
1184 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1185 }
1186
1187 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001188 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001189 }
1190 else if (IsSampler3D(textureFunction->sampler))
1191 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001192 out << " float width; float height; float depth; float levels;\n";
1193
1194 if (textureFunction->method == TextureFunction::LOD0)
1195 {
1196 out << " uint mip = 0;\n";
1197 }
1198 else
1199 {
1200 if (textureFunction->method == TextureFunction::IMPLICIT ||
1201 textureFunction->method == TextureFunction::BIAS)
1202 {
1203 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1204 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
1205 " float dx = length(ddx(tSized));\n"
1206 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001207 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001208
1209 if (textureFunction->method == TextureFunction::BIAS)
1210 {
1211 out << " lod += bias;\n";
1212 }
1213 }
1214
1215 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1216 }
1217
1218 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001219 }
1220 else UNREACHABLE();
1221 }
1222
1223 out << " return ";
1224
1225 // HLSL intrinsic
1226 if (mOutputType == SH_HLSL9_OUTPUT)
1227 {
1228 switch(textureFunction->sampler)
1229 {
1230 case EbtSampler2D: out << "tex2D"; break;
1231 case EbtSamplerCube: out << "texCUBE"; break;
1232 default: UNREACHABLE();
1233 }
1234
Nicolas Capens75fb4752013-07-10 15:14:47 -04001235 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001236 {
1237 case TextureFunction::IMPLICIT: out << "(s, "; break;
1238 case TextureFunction::BIAS: out << "bias(s, "; break;
1239 case TextureFunction::LOD: out << "lod(s, "; break;
1240 case TextureFunction::LOD0: out << "lod(s, "; break;
1241 default: UNREACHABLE();
1242 }
1243 }
1244 else if (mOutputType == SH_HLSL11_OUTPUT)
1245 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001246 if (IsIntegerSampler(textureFunction->sampler) ||
1247 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001248 {
1249 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001250 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001251 else if (IsShadowSampler(textureFunction->sampler))
1252 {
1253 out << "x.SampleCmp(s, ";
1254 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001255 else
1256 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001257 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001258 {
1259 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1260 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1261 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1262 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
1263 default: UNREACHABLE();
1264 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001265 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001266 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001267 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001268
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001269 // Integer sampling requires integer addresses
1270 TString addressx = "";
1271 TString addressy = "";
1272 TString addressz = "";
1273 TString close = "";
1274
Nicolas Capensfc014542014-02-18 14:47:13 -05001275 if (IsIntegerSampler(textureFunction->sampler) ||
1276 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001277 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001278 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001279 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001280 case 2: out << "int3("; break;
1281 case 3: out << "int4("; break;
1282 default: UNREACHABLE();
1283 }
1284
Nicolas Capensfc014542014-02-18 14:47:13 -05001285 // Convert from normalized floating-point to integer
1286 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001287 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001288 addressx = "int(floor(width * frac((";
1289 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001290
Nicolas Capensfc014542014-02-18 14:47:13 -05001291 if (IsSamplerArray(textureFunction->sampler))
1292 {
1293 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1294 }
1295 else
1296 {
1297 addressz = "int(floor(depth * frac((";
1298 }
1299
1300 close = "))))";
1301 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001302 }
1303 else
1304 {
1305 switch(hlslCoords)
1306 {
1307 case 2: out << "float2("; break;
1308 case 3: out << "float3("; break;
1309 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001310 default: UNREACHABLE();
1311 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001312 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001313
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001314 TString proj = ""; // Only used for projected textures
1315
1316 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001317 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001318 switch(textureFunction->coords)
1319 {
1320 case 3: proj = " / t.z"; break;
1321 case 4: proj = " / t.w"; break;
1322 default: UNREACHABLE();
1323 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001324 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001325
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001326 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001327
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001328 if (mOutputType == SH_HLSL9_OUTPUT)
1329 {
1330 if (hlslCoords >= 3)
1331 {
1332 if (textureFunction->coords < 3)
1333 {
1334 out << ", 0";
1335 }
1336 else
1337 {
1338 out << ", t.z" + proj;
1339 }
1340 }
1341
1342 if (hlslCoords == 4)
1343 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001344 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001345 {
1346 case TextureFunction::BIAS: out << ", bias"; break;
1347 case TextureFunction::LOD: out << ", lod"; break;
1348 case TextureFunction::LOD0: out << ", 0"; break;
1349 default: UNREACHABLE();
1350 }
1351 }
1352
1353 out << "));\n";
1354 }
1355 else if (mOutputType == SH_HLSL11_OUTPUT)
1356 {
1357 if (hlslCoords >= 3)
1358 {
1359 out << ", " + addressz + ("t.z" + proj) + close;
1360 }
1361
Nicolas Capensfc014542014-02-18 14:47:13 -05001362 if (IsIntegerSampler(textureFunction->sampler) ||
1363 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001364 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001365 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001366 }
1367 else if (IsShadowSampler(textureFunction->sampler))
1368 {
1369 // Compare value
1370 switch(textureFunction->coords)
1371 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001372 case 3: out << "), t.z"; break;
1373 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001374 default: UNREACHABLE();
1375 }
1376 }
1377 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001378 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001379 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001380 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001381 case TextureFunction::IMPLICIT: out << ")"; break;
1382 case TextureFunction::BIAS: out << "), bias"; break;
1383 case TextureFunction::LOD: out << "), lod"; break;
1384 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001385 default: UNREACHABLE();
1386 }
1387 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001388
1389 if (textureFunction->offset)
1390 {
1391 out << ", offset";
1392 }
1393
1394 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001395 }
1396 else UNREACHABLE();
1397 }
1398
1399 out << "\n"
1400 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001401 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001402 }
1403
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001404 if (mUsesFragCoord)
1405 {
1406 out << "#define GL_USES_FRAG_COORD\n";
1407 }
1408
1409 if (mUsesPointCoord)
1410 {
1411 out << "#define GL_USES_POINT_COORD\n";
1412 }
1413
1414 if (mUsesFrontFacing)
1415 {
1416 out << "#define GL_USES_FRONT_FACING\n";
1417 }
1418
1419 if (mUsesPointSize)
1420 {
1421 out << "#define GL_USES_POINT_SIZE\n";
1422 }
1423
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001424 if (mUsesFragDepth)
1425 {
1426 out << "#define GL_USES_FRAG_DEPTH\n";
1427 }
1428
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001429 if (mUsesDepthRange)
1430 {
1431 out << "#define GL_USES_DEPTH_RANGE\n";
1432 }
1433
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001434 if (mUsesXor)
1435 {
1436 out << "bool xor(bool p, bool q)\n"
1437 "{\n"
1438 " return (p || q) && !(p && q);\n"
1439 "}\n"
1440 "\n";
1441 }
1442
1443 if (mUsesMod1)
1444 {
1445 out << "float mod(float x, float y)\n"
1446 "{\n"
1447 " return x - y * floor(x / y);\n"
1448 "}\n"
1449 "\n";
1450 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001451
1452 if (mUsesMod2v)
1453 {
1454 out << "float2 mod(float2 x, float2 y)\n"
1455 "{\n"
1456 " return x - y * floor(x / y);\n"
1457 "}\n"
1458 "\n";
1459 }
1460
1461 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001462 {
1463 out << "float2 mod(float2 x, float y)\n"
1464 "{\n"
1465 " return x - y * floor(x / y);\n"
1466 "}\n"
1467 "\n";
1468 }
1469
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001470 if (mUsesMod3v)
1471 {
1472 out << "float3 mod(float3 x, float3 y)\n"
1473 "{\n"
1474 " return x - y * floor(x / y);\n"
1475 "}\n"
1476 "\n";
1477 }
1478
1479 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001480 {
1481 out << "float3 mod(float3 x, float y)\n"
1482 "{\n"
1483 " return x - y * floor(x / y);\n"
1484 "}\n"
1485 "\n";
1486 }
1487
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001488 if (mUsesMod4v)
1489 {
1490 out << "float4 mod(float4 x, float4 y)\n"
1491 "{\n"
1492 " return x - y * floor(x / y);\n"
1493 "}\n"
1494 "\n";
1495 }
1496
1497 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001498 {
1499 out << "float4 mod(float4 x, float y)\n"
1500 "{\n"
1501 " return x - y * floor(x / y);\n"
1502 "}\n"
1503 "\n";
1504 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001505
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001506 if (mUsesFaceforward1)
1507 {
1508 out << "float faceforward(float N, float I, float Nref)\n"
1509 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001510 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001511 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001512 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001513 " }\n"
1514 " else\n"
1515 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001516 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001517 " }\n"
1518 "}\n"
1519 "\n";
1520 }
1521
1522 if (mUsesFaceforward2)
1523 {
1524 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1525 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001526 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001527 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001528 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001529 " }\n"
1530 " else\n"
1531 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001532 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001533 " }\n"
1534 "}\n"
1535 "\n";
1536 }
1537
1538 if (mUsesFaceforward3)
1539 {
1540 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1541 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001542 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001543 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001544 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001545 " }\n"
1546 " else\n"
1547 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001548 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001549 " }\n"
1550 "}\n"
1551 "\n";
1552 }
1553
1554 if (mUsesFaceforward4)
1555 {
1556 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1557 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001558 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001559 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001560 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001561 " }\n"
1562 " else\n"
1563 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001564 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001565 " }\n"
1566 "}\n"
1567 "\n";
1568 }
1569
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001570 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001571 {
1572 out << "float atanyx(float y, float x)\n"
1573 "{\n"
1574 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1575 " return atan2(y, x);\n"
1576 "}\n";
1577 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001578
1579 if (mUsesAtan2_2)
1580 {
1581 out << "float2 atanyx(float2 y, float2 x)\n"
1582 "{\n"
1583 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1584 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1585 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1586 "}\n";
1587 }
1588
1589 if (mUsesAtan2_3)
1590 {
1591 out << "float3 atanyx(float3 y, float3 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 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1597 "}\n";
1598 }
1599
1600 if (mUsesAtan2_4)
1601 {
1602 out << "float4 atanyx(float4 y, float4 x)\n"
1603 "{\n"
1604 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1605 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1606 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1607 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1608 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1609 "}\n";
1610 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001611}
1612
1613void OutputHLSL::visitSymbol(TIntermSymbol *node)
1614{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001615 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001616
Jamie Madill570e04d2013-06-21 09:15:33 -04001617 // Handle accessing std140 structs by value
1618 if (mFlaggedStructMappedNames.count(node) > 0)
1619 {
1620 out << mFlaggedStructMappedNames[node];
1621 return;
1622 }
1623
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001624 TString name = node->getSymbol();
1625
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001626 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001627 {
1628 mUsesDepthRange = true;
1629 out << name;
1630 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001631 else
1632 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001633 TQualifier qualifier = node->getQualifier();
1634
1635 if (qualifier == EvqUniform)
1636 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001637 const TType& nodeType = node->getType();
1638 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1639
1640 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001641 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001642 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001643 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001644 else
1645 {
1646 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001647 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001648
1649 out << decorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001650 }
Jamie Madill19571812013-08-12 15:26:34 -07001651 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001652 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001653 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001654 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001655 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001656 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001657 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001658 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001659 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001660 }
Jamie Madill19571812013-08-12 15:26:34 -07001661 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001662 {
1663 mReferencedOutputVariables[name] = node;
1664 out << "out_" << name;
1665 }
1666 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001667 {
1668 out << "gl_Color[0]";
1669 mUsesFragColor = true;
1670 }
1671 else if (qualifier == EvqFragData)
1672 {
1673 out << "gl_Color";
1674 mUsesFragData = true;
1675 }
1676 else if (qualifier == EvqFragCoord)
1677 {
1678 mUsesFragCoord = true;
1679 out << name;
1680 }
1681 else if (qualifier == EvqPointCoord)
1682 {
1683 mUsesPointCoord = true;
1684 out << name;
1685 }
1686 else if (qualifier == EvqFrontFacing)
1687 {
1688 mUsesFrontFacing = true;
1689 out << name;
1690 }
1691 else if (qualifier == EvqPointSize)
1692 {
1693 mUsesPointSize = true;
1694 out << name;
1695 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001696 else if (name == "gl_FragDepthEXT")
1697 {
1698 mUsesFragDepth = true;
1699 out << "gl_Depth";
1700 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001701 else if (qualifier == EvqInternal)
1702 {
1703 out << name;
1704 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001705 else
1706 {
1707 out << decorate(name);
1708 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001709 }
1710}
1711
1712bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1713{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001714 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001715
Jamie Madill570e04d2013-06-21 09:15:33 -04001716 // Handle accessing std140 structs by value
1717 if (mFlaggedStructMappedNames.count(node) > 0)
1718 {
1719 out << mFlaggedStructMappedNames[node];
1720 return false;
1721 }
1722
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001723 switch (node->getOp())
1724 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001725 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001726 case EOpInitialize:
1727 if (visit == PreVisit)
1728 {
1729 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1730 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1731 // new variable is created before the assignment is evaluated), so we need to convert
1732 // this to "float t = x, x = t;".
1733
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001734 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1735 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001736
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001737 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1738 expression->traverse(&searchSymbol);
1739 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001740
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001741 if (sameSymbol)
1742 {
1743 // Type already printed
1744 out << "t" + str(mUniqueIndex) + " = ";
1745 expression->traverse(this);
1746 out << ", ";
1747 symbolNode->traverse(this);
1748 out << " = t" + str(mUniqueIndex);
1749
1750 mUniqueIndex++;
1751 return false;
1752 }
1753 }
1754 else if (visit == InVisit)
1755 {
1756 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001757 }
1758 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001759 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1760 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1761 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1762 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1763 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1764 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001765 if (visit == PreVisit)
1766 {
1767 out << "(";
1768 }
1769 else if (visit == InVisit)
1770 {
1771 out << " = mul(";
1772 node->getLeft()->traverse(this);
1773 out << ", transpose(";
1774 }
1775 else
1776 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001777 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001778 }
1779 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001780 case EOpMatrixTimesMatrixAssign:
1781 if (visit == PreVisit)
1782 {
1783 out << "(";
1784 }
1785 else if (visit == InVisit)
1786 {
1787 out << " = mul(";
1788 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001789 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001790 }
1791 else
1792 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001793 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001794 }
1795 break;
1796 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001797 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001798 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001799 const TType& leftType = node->getLeft()->getType();
1800 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001801 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001802 if (visit == PreVisit)
1803 {
1804 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1805 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1806
1807 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1808 out << interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1809
1810 return false;
1811 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001812 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001813 else
1814 {
1815 outputTriplet(visit, "", "[", "]");
1816 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001817 }
1818 break;
1819 case EOpIndexIndirect:
1820 // We do not currently support indirect references to interface blocks
1821 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1822 outputTriplet(visit, "", "[", "]");
1823 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001824 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001825 if (visit == InVisit)
1826 {
1827 const TStructure* structure = node->getLeft()->getType().getStruct();
1828 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1829 const TField* field = structure->fields()[index->getIConst(0)];
1830 out << "." + decorateField(field->name(), *structure);
1831
1832 return false;
1833 }
1834 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001835 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001836 if (visit == InVisit)
1837 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001838 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1839 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1840 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
1841 out << "." + decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001842
1843 return false;
1844 }
1845 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001846 case EOpVectorSwizzle:
1847 if (visit == InVisit)
1848 {
1849 out << ".";
1850
1851 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1852
1853 if (swizzle)
1854 {
1855 TIntermSequence &sequence = swizzle->getSequence();
1856
1857 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1858 {
1859 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1860
1861 if (element)
1862 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001863 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001864
1865 switch (i)
1866 {
1867 case 0: out << "x"; break;
1868 case 1: out << "y"; break;
1869 case 2: out << "z"; break;
1870 case 3: out << "w"; break;
1871 default: UNREACHABLE();
1872 }
1873 }
1874 else UNREACHABLE();
1875 }
1876 }
1877 else UNREACHABLE();
1878
1879 return false; // Fully processed
1880 }
1881 break;
1882 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1883 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1884 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1885 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001886 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001887 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001888 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001889 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001890 if (node->getOp() == EOpEqual)
1891 {
1892 outputTriplet(visit, "(", " == ", ")");
1893 }
1894 else
1895 {
1896 outputTriplet(visit, "(", " != ", ")");
1897 }
1898 }
1899 else if (node->getLeft()->getBasicType() == EbtStruct)
1900 {
1901 if (node->getOp() == EOpEqual)
1902 {
1903 out << "(";
1904 }
1905 else
1906 {
1907 out << "!(";
1908 }
1909
Jamie Madill98493dd2013-07-08 14:39:03 -04001910 const TStructure &structure = *node->getLeft()->getType().getStruct();
1911 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001912
Jamie Madill98493dd2013-07-08 14:39:03 -04001913 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001914 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001915 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001916
1917 node->getLeft()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001918 out << "." + decorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001919 node->getRight()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001920 out << "." + decorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001921
Jamie Madill98493dd2013-07-08 14:39:03 -04001922 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001923 {
1924 out << " && ";
1925 }
1926 }
1927
1928 out << ")";
1929
1930 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001931 }
1932 else
1933 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001934 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001935
1936 if (node->getOp() == EOpEqual)
1937 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001938 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001939 }
1940 else
1941 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001942 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001943 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001944 }
1945 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001946 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1947 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1948 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1949 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1950 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001951 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001952 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1953 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001954 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001955 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001956 if (node->getRight()->hasSideEffects())
1957 {
1958 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1959 return false;
1960 }
1961 else
1962 {
1963 outputTriplet(visit, "(", " || ", ")");
1964 return true;
1965 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001966 case EOpLogicalXor:
1967 mUsesXor = true;
1968 outputTriplet(visit, "xor(", ", ", ")");
1969 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001970 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001971 if (node->getRight()->hasSideEffects())
1972 {
1973 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1974 return false;
1975 }
1976 else
1977 {
1978 outputTriplet(visit, "(", " && ", ")");
1979 return true;
1980 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001981 default: UNREACHABLE();
1982 }
1983
1984 return true;
1985}
1986
1987bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1988{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001989 switch (node->getOp())
1990 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001991 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1992 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1993 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1994 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1995 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1996 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1997 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001998 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04001999 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002000 case EOpConvFloatToBool:
2001 switch (node->getOperand()->getType().getNominalSize())
2002 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002003 case 1: outputTriplet(visit, "bool(", "", ")"); break;
2004 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
2005 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
2006 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002007 default: UNREACHABLE();
2008 }
2009 break;
2010 case EOpConvBoolToFloat:
2011 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04002012 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002013 switch (node->getOperand()->getType().getNominalSize())
2014 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002015 case 1: outputTriplet(visit, "float(", "", ")"); break;
2016 case 2: outputTriplet(visit, "float2(", "", ")"); break;
2017 case 3: outputTriplet(visit, "float3(", "", ")"); break;
2018 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002019 default: UNREACHABLE();
2020 }
2021 break;
2022 case EOpConvFloatToInt:
2023 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04002024 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002025 switch (node->getOperand()->getType().getNominalSize())
2026 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002027 case 1: outputTriplet(visit, "int(", "", ")"); break;
2028 case 2: outputTriplet(visit, "int2(", "", ")"); break;
2029 case 3: outputTriplet(visit, "int3(", "", ")"); break;
2030 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002031 default: UNREACHABLE();
2032 }
2033 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002034 case EOpConvFloatToUInt:
2035 case EOpConvBoolToUInt:
2036 case EOpConvIntToUInt:
Jamie Madilla16f1672013-07-03 15:15:17 -04002037 switch (node->getOperand()->getType().getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002038 {
2039 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002040 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
2041 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
2042 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002043 default: UNREACHABLE();
2044 }
2045 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002046 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
2047 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
2048 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
2049 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
2050 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
2051 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
2052 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
2053 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
2054 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
2055 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
2056 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
2057 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
2058 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
2059 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
2060 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
2061 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
2062 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
2063 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
2064 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
2065 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
2066 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002067 case EOpDFdx:
2068 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2069 {
2070 outputTriplet(visit, "(", "", ", 0.0)");
2071 }
2072 else
2073 {
2074 outputTriplet(visit, "ddx(", "", ")");
2075 }
2076 break;
2077 case EOpDFdy:
2078 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2079 {
2080 outputTriplet(visit, "(", "", ", 0.0)");
2081 }
2082 else
2083 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00002084 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002085 }
2086 break;
2087 case EOpFwidth:
2088 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2089 {
2090 outputTriplet(visit, "(", "", ", 0.0)");
2091 }
2092 else
2093 {
2094 outputTriplet(visit, "fwidth(", "", ")");
2095 }
2096 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002097 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
2098 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002099 default: UNREACHABLE();
2100 }
2101
2102 return true;
2103}
2104
2105bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2106{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002107 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002108
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002109 switch (node->getOp())
2110 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002111 case EOpSequence:
2112 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002113 if (mInsideFunction)
2114 {
Jamie Madill075edd82013-07-08 13:30:19 -04002115 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002116 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002117
2118 mScopeDepth++;
2119
2120 if (mScopeBracket.size() < mScopeDepth)
2121 {
2122 mScopeBracket.push_back(0); // New scope level
2123 }
2124 else
2125 {
2126 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
2127 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002128 }
2129
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002130 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
2131 {
Jamie Madill075edd82013-07-08 13:30:19 -04002132 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002133
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002134 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002135
2136 out << ";\n";
2137 }
2138
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002139 if (mInsideFunction)
2140 {
Jamie Madill075edd82013-07-08 13:30:19 -04002141 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002142 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002143
2144 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002145 }
2146
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002147 return false;
2148 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002149 case EOpDeclaration:
2150 if (visit == PreVisit)
2151 {
2152 TIntermSequence &sequence = node->getSequence();
2153 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002154
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00002155 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002156 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002157 if (variable->getType().getStruct())
2158 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002159 addConstructor(variable->getType(), scopedStruct(variable->getType().getStruct()->name()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00002160 }
2161
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002162 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002163 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00002164 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002165 {
2166 out << "static ";
2167 }
2168
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002169 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002170
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002171 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002172 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002173 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002174
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002175 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002176 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002177 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002178 out << arrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05002179 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002180 }
2181 else
2182 {
2183 (*sit)->traverse(this);
2184 }
2185
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002186 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002187 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002188 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002189 }
2190 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002191 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002192 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2193 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002194 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002195 }
2196 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002197 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002198 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002199 {
2200 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2201 {
2202 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2203
2204 if (symbol)
2205 {
2206 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2207 mReferencedVaryings[symbol->getSymbol()] = symbol;
2208 }
2209 else
2210 {
2211 (*sit)->traverse(this);
2212 }
2213 }
2214 }
2215
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002216 return false;
2217 }
2218 else if (visit == InVisit)
2219 {
2220 out << ", ";
2221 }
2222 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002223 case EOpPrototype:
2224 if (visit == PreVisit)
2225 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002226 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002227
2228 TIntermSequence &arguments = node->getSequence();
2229
2230 for (unsigned int i = 0; i < arguments.size(); i++)
2231 {
2232 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2233
2234 if (symbol)
2235 {
2236 out << argumentString(symbol);
2237
2238 if (i < arguments.size() - 1)
2239 {
2240 out << ", ";
2241 }
2242 }
2243 else UNREACHABLE();
2244 }
2245
2246 out << ");\n";
2247
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002248 // Also prototype the Lod0 variant if needed
2249 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2250 {
2251 mOutputLod0Function = true;
2252 node->traverse(this);
2253 mOutputLod0Function = false;
2254 }
2255
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002256 return false;
2257 }
2258 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002259 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002260 case EOpFunction:
2261 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002262 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002263
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002264 out << typeString(node->getType()) << " ";
2265
2266 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002267 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002268 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002269 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002270 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002271 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002272 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002273 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002274
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002275 TIntermSequence &sequence = node->getSequence();
2276 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2277
2278 for (unsigned int i = 0; i < arguments.size(); i++)
2279 {
2280 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2281
2282 if (symbol)
2283 {
2284 if (symbol->getType().getStruct())
2285 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002286 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getStruct()->name()), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002287 }
2288
2289 out << argumentString(symbol);
2290
2291 if (i < arguments.size() - 1)
2292 {
2293 out << ", ";
2294 }
2295 }
2296 else UNREACHABLE();
2297 }
2298
2299 out << ")\n"
2300 "{\n";
2301
2302 if (sequence.size() > 1)
2303 {
2304 mInsideFunction = true;
2305 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002306 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002307 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002308
2309 out << "}\n";
2310
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002311 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2312 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002313 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002314 {
2315 mOutputLod0Function = true;
2316 node->traverse(this);
2317 mOutputLod0Function = false;
2318 }
2319 }
2320
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002321 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002322 }
2323 break;
2324 case EOpFunctionCall:
2325 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002326 TString name = TFunction::unmangleName(node->getName());
2327 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002328 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002329
2330 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002331 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002332 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002333 }
2334 else
2335 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002336 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2337
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002338 TextureFunction textureFunction;
2339 textureFunction.sampler = samplerType;
2340 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002341 textureFunction.method = TextureFunction::IMPLICIT;
2342 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002343 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002344
2345 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002346 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002347 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002348 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002349 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002350 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002351 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002352 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002353 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002354 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002355 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002356 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002357 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002358 else if (name == "texture2DProjLod" || name == "textureProjLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002359 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002360 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002361 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002362 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002363 else if (name == "textureSize")
2364 {
2365 textureFunction.method = TextureFunction::SIZE;
2366 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002367 else if (name == "textureOffset")
2368 {
2369 textureFunction.method = TextureFunction::IMPLICIT;
2370 textureFunction.offset = true;
2371 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002372 else if (name == "textureProjOffset")
2373 {
2374 textureFunction.method = TextureFunction::IMPLICIT;
2375 textureFunction.offset = true;
2376 textureFunction.proj = true;
2377 }
2378 else if (name == "textureLodOffset")
2379 {
2380 textureFunction.method = TextureFunction::LOD;
2381 textureFunction.offset = true;
2382 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002383 else if (name == "textureProjLod")
2384 {
2385 textureFunction.method = TextureFunction::LOD;
2386 textureFunction.proj = true;
2387 }
2388 else if (name == "textureProjLodOffset")
2389 {
2390 textureFunction.method = TextureFunction::LOD;
2391 textureFunction.proj = true;
2392 textureFunction.offset = true;
2393 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002394 else if (name == "texelFetch")
2395 {
2396 textureFunction.method = TextureFunction::FETCH;
2397 }
2398 else if (name == "texelFetchOffset")
2399 {
2400 textureFunction.method = TextureFunction::FETCH;
2401 textureFunction.offset = true;
2402 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002403 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002404
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002405 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002406 {
2407 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2408 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002409 textureFunction.method = TextureFunction::LOD0;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002410 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002411 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002412 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002413 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2414
2415 if (textureFunction.offset)
2416 {
2417 mandatoryArgumentCount++;
2418 }
2419
2420 if (arguments.size() > mandatoryArgumentCount) // Bias argument is optional
2421 {
2422 textureFunction.method = TextureFunction::BIAS;
2423 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002424 }
2425 }
2426
2427 mUsesTexture.insert(textureFunction);
2428
2429 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002430 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002431
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002432 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2433 {
2434 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2435 {
2436 out << "texture_";
2437 (*arg)->traverse(this);
2438 out << ", sampler_";
2439 }
2440
2441 (*arg)->traverse(this);
2442
2443 if (arg < arguments.end() - 1)
2444 {
2445 out << ", ";
2446 }
2447 }
2448
2449 out << ")";
2450
2451 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002452 }
2453 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002454 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002455 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002456 addConstructor(node->getType(), "vec1", &node->getSequence());
2457 outputTriplet(visit, "vec1(", "", ")");
2458 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002459 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002460 addConstructor(node->getType(), "vec2", &node->getSequence());
2461 outputTriplet(visit, "vec2(", ", ", ")");
2462 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002463 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002464 addConstructor(node->getType(), "vec3", &node->getSequence());
2465 outputTriplet(visit, "vec3(", ", ", ")");
2466 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002467 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002468 addConstructor(node->getType(), "vec4", &node->getSequence());
2469 outputTriplet(visit, "vec4(", ", ", ")");
2470 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002471 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002472 addConstructor(node->getType(), "bvec1", &node->getSequence());
2473 outputTriplet(visit, "bvec1(", "", ")");
2474 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002475 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002476 addConstructor(node->getType(), "bvec2", &node->getSequence());
2477 outputTriplet(visit, "bvec2(", ", ", ")");
2478 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002479 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002480 addConstructor(node->getType(), "bvec3", &node->getSequence());
2481 outputTriplet(visit, "bvec3(", ", ", ")");
2482 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002483 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002484 addConstructor(node->getType(), "bvec4", &node->getSequence());
2485 outputTriplet(visit, "bvec4(", ", ", ")");
2486 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002487 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002488 addConstructor(node->getType(), "ivec1", &node->getSequence());
2489 outputTriplet(visit, "ivec1(", "", ")");
2490 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002491 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002492 addConstructor(node->getType(), "ivec2", &node->getSequence());
2493 outputTriplet(visit, "ivec2(", ", ", ")");
2494 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002495 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002496 addConstructor(node->getType(), "ivec3", &node->getSequence());
2497 outputTriplet(visit, "ivec3(", ", ", ")");
2498 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002499 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002500 addConstructor(node->getType(), "ivec4", &node->getSequence());
2501 outputTriplet(visit, "ivec4(", ", ", ")");
2502 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002503 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002504 addConstructor(node->getType(), "uvec1", &node->getSequence());
2505 outputTriplet(visit, "uvec1(", "", ")");
2506 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002507 case EOpConstructUVec2:
2508 addConstructor(node->getType(), "uvec2", &node->getSequence());
2509 outputTriplet(visit, "uvec2(", ", ", ")");
2510 break;
2511 case EOpConstructUVec3:
2512 addConstructor(node->getType(), "uvec3", &node->getSequence());
2513 outputTriplet(visit, "uvec3(", ", ", ")");
2514 break;
2515 case EOpConstructUVec4:
2516 addConstructor(node->getType(), "uvec4", &node->getSequence());
2517 outputTriplet(visit, "uvec4(", ", ", ")");
2518 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002519 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002520 addConstructor(node->getType(), "mat2", &node->getSequence());
2521 outputTriplet(visit, "mat2(", ", ", ")");
2522 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002523 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002524 addConstructor(node->getType(), "mat3", &node->getSequence());
2525 outputTriplet(visit, "mat3(", ", ", ")");
2526 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002527 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002528 addConstructor(node->getType(), "mat4", &node->getSequence());
2529 outputTriplet(visit, "mat4(", ", ", ")");
2530 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002531 case EOpConstructStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04002532 addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence());
2533 outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002534 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002535 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2536 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2537 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2538 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2539 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2540 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002541 case EOpMod:
2542 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002543 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002544 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2545 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2546 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002547 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002548 case 11: mUsesMod1 = true; break;
2549 case 22: mUsesMod2v = true; break;
2550 case 21: mUsesMod2f = true; break;
2551 case 33: mUsesMod3v = true; break;
2552 case 31: mUsesMod3f = true; break;
2553 case 44: mUsesMod4v = true; break;
2554 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002555 default: UNREACHABLE();
2556 }
2557
2558 outputTriplet(visit, "mod(", ", ", ")");
2559 }
2560 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002561 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002562 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002563 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002564 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2565 {
2566 case 1: mUsesAtan2_1 = true; break;
2567 case 2: mUsesAtan2_2 = true; break;
2568 case 3: mUsesAtan2_3 = true; break;
2569 case 4: mUsesAtan2_4 = true; break;
2570 default: UNREACHABLE();
2571 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002572 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002573 break;
2574 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2575 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2576 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2577 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2578 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2579 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2580 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2581 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2582 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002583 case EOpFaceForward:
2584 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002585 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002586 {
2587 case 1: mUsesFaceforward1 = true; break;
2588 case 2: mUsesFaceforward2 = true; break;
2589 case 3: mUsesFaceforward3 = true; break;
2590 case 4: mUsesFaceforward4 = true; break;
2591 default: UNREACHABLE();
2592 }
2593
2594 outputTriplet(visit, "faceforward(", ", ", ")");
2595 }
2596 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002597 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2598 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2599 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002600 default: UNREACHABLE();
2601 }
2602
2603 return true;
2604}
2605
2606bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2607{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002608 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002609
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002610 if (node->usesTernaryOperator())
2611 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002612 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002613 }
2614 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002615 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002616 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002617
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002618 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002619
2620 node->getCondition()->traverse(this);
2621
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002622 out << ")\n";
2623
Jamie Madill075edd82013-07-08 13:30:19 -04002624 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002625 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002626
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002627 bool discard = false;
2628
daniel@transgaming.combb885322010-04-15 20:45:24 +00002629 if (node->getTrueBlock())
2630 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002631 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002632
2633 // Detect true discard
2634 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002635 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002636
Jamie Madill075edd82013-07-08 13:30:19 -04002637 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002638 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002639
2640 if (node->getFalseBlock())
2641 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002642 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002643
Jamie Madill075edd82013-07-08 13:30:19 -04002644 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002645 out << "{\n";
2646
Jamie Madill075edd82013-07-08 13:30:19 -04002647 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002648 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002649
Jamie Madill075edd82013-07-08 13:30:19 -04002650 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002651 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002652
2653 // Detect false discard
2654 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2655 }
2656
2657 // ANGLE issue 486: Detect problematic conditional discard
2658 if (discard && FindSideEffectRewriting::search(node))
2659 {
2660 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002661 }
2662 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002663
2664 return false;
2665}
2666
2667void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2668{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002669 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002670}
2671
2672bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2673{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002674 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2675
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002676 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002677 {
2678 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2679 }
2680
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002681 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002682 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002683 if (handleExcessiveLoop(node))
2684 {
2685 return false;
2686 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002687 }
2688
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002689 TInfoSinkBase &out = mBody;
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 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002693 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002694
Jamie Madill075edd82013-07-08 13:30:19 -04002695 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002696 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002697 }
2698 else
2699 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002700 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002701
2702 if (node->getInit())
2703 {
2704 node->getInit()->traverse(this);
2705 }
2706
2707 out << "; ";
2708
alokp@chromium.org52813552010-11-16 18:36:09 +00002709 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002710 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002711 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002712 }
2713
2714 out << "; ";
2715
alokp@chromium.org52813552010-11-16 18:36:09 +00002716 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002717 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002718 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002719 }
2720
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002721 out << ")\n";
2722
Jamie Madill075edd82013-07-08 13:30:19 -04002723 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002724 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002725 }
2726
2727 if (node->getBody())
2728 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002729 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002730 }
2731
Jamie Madill075edd82013-07-08 13:30:19 -04002732 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002733 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002734
alokp@chromium.org52813552010-11-16 18:36:09 +00002735 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002736 {
Jamie Madill075edd82013-07-08 13:30:19 -04002737 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002738 out << "while(\n";
2739
alokp@chromium.org52813552010-11-16 18:36:09 +00002740 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002741
daniel@transgaming.com73536982012-03-21 20:45:49 +00002742 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002743 }
2744
daniel@transgaming.com73536982012-03-21 20:45:49 +00002745 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002746
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002747 mInsideDiscontinuousLoop = wasDiscontinuous;
2748
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002749 return false;
2750}
2751
2752bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2753{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002754 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002755
2756 switch (node->getFlowOp())
2757 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002758 case EOpKill:
2759 outputTriplet(visit, "discard;\n", "", "");
2760 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002761 case EOpBreak:
2762 if (visit == PreVisit)
2763 {
2764 if (mExcessiveLoopIndex)
2765 {
2766 out << "{Break";
2767 mExcessiveLoopIndex->traverse(this);
2768 out << " = true; break;}\n";
2769 }
2770 else
2771 {
2772 out << "break;\n";
2773 }
2774 }
2775 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002776 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002777 case EOpReturn:
2778 if (visit == PreVisit)
2779 {
2780 if (node->getExpression())
2781 {
2782 out << "return ";
2783 }
2784 else
2785 {
2786 out << "return;\n";
2787 }
2788 }
2789 else if (visit == PostVisit)
2790 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002791 if (node->getExpression())
2792 {
2793 out << ";\n";
2794 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002795 }
2796 break;
2797 default: UNREACHABLE();
2798 }
2799
2800 return true;
2801}
2802
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002803void OutputHLSL::traverseStatements(TIntermNode *node)
2804{
2805 if (isSingleStatement(node))
2806 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002807 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002808 }
2809
2810 node->traverse(this);
2811}
2812
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002813bool OutputHLSL::isSingleStatement(TIntermNode *node)
2814{
2815 TIntermAggregate *aggregate = node->getAsAggregate();
2816
2817 if (aggregate)
2818 {
2819 if (aggregate->getOp() == EOpSequence)
2820 {
2821 return false;
2822 }
2823 else
2824 {
2825 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2826 {
2827 if (!isSingleStatement(*sit))
2828 {
2829 return false;
2830 }
2831 }
2832
2833 return true;
2834 }
2835 }
2836
2837 return true;
2838}
2839
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002840// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2841// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002842bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2843{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002844 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002845 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002846
2847 // Parse loops of the form:
2848 // for(int index = initial; index [comparator] limit; index += increment)
2849 TIntermSymbol *index = NULL;
2850 TOperator comparator = EOpNull;
2851 int initial = 0;
2852 int limit = 0;
2853 int increment = 0;
2854
2855 // Parse index name and intial value
2856 if (node->getInit())
2857 {
2858 TIntermAggregate *init = node->getInit()->getAsAggregate();
2859
2860 if (init)
2861 {
2862 TIntermSequence &sequence = init->getSequence();
2863 TIntermTyped *variable = sequence[0]->getAsTyped();
2864
2865 if (variable && variable->getQualifier() == EvqTemporary)
2866 {
2867 TIntermBinary *assign = variable->getAsBinaryNode();
2868
2869 if (assign->getOp() == EOpInitialize)
2870 {
2871 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2872 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2873
2874 if (symbol && constant)
2875 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002876 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002877 {
2878 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002879 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002880 }
2881 }
2882 }
2883 }
2884 }
2885 }
2886
2887 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002888 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002889 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002890 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002891
2892 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2893 {
2894 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2895
2896 if (constant)
2897 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002898 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002899 {
2900 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002901 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002902 }
2903 }
2904 }
2905 }
2906
2907 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002908 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002909 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002910 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2911 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002912
2913 if (binaryTerminal)
2914 {
2915 TOperator op = binaryTerminal->getOp();
2916 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2917
2918 if (constant)
2919 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002920 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002921 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002922 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002923
2924 switch (op)
2925 {
2926 case EOpAddAssign: increment = value; break;
2927 case EOpSubAssign: increment = -value; break;
2928 default: UNIMPLEMENTED();
2929 }
2930 }
2931 }
2932 }
2933 else if (unaryTerminal)
2934 {
2935 TOperator op = unaryTerminal->getOp();
2936
2937 switch (op)
2938 {
2939 case EOpPostIncrement: increment = 1; break;
2940 case EOpPostDecrement: increment = -1; break;
2941 case EOpPreIncrement: increment = 1; break;
2942 case EOpPreDecrement: increment = -1; break;
2943 default: UNIMPLEMENTED();
2944 }
2945 }
2946 }
2947
2948 if (index != NULL && comparator != EOpNull && increment != 0)
2949 {
2950 if (comparator == EOpLessThanEqual)
2951 {
2952 comparator = EOpLessThan;
2953 limit += 1;
2954 }
2955
2956 if (comparator == EOpLessThan)
2957 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002958 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002959
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002960 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002961 {
2962 return false; // Not an excessive loop
2963 }
2964
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002965 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2966 mExcessiveLoopIndex = index;
2967
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002968 out << "{int ";
2969 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002970 out << ";\n"
2971 "bool Break";
2972 index->traverse(this);
2973 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002974
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002975 bool firstLoopFragment = true;
2976
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002977 while (iterations > 0)
2978 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002979 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002980
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002981 if (!firstLoopFragment)
2982 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002983 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002984 index->traverse(this);
2985 out << ") {\n";
2986 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002987
2988 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2989 {
2990 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2991 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002992
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002993 // for(int index = initial; index < clampedLimit; index += increment)
2994
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002995 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002996 index->traverse(this);
2997 out << " = ";
2998 out << initial;
2999
3000 out << "; ";
3001 index->traverse(this);
3002 out << " < ";
3003 out << clampedLimit;
3004
3005 out << "; ";
3006 index->traverse(this);
3007 out << " += ";
3008 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003009 out << ")\n";
3010
Jamie Madill075edd82013-07-08 13:30:19 -04003011 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003012 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003013
3014 if (node->getBody())
3015 {
3016 node->getBody()->traverse(this);
3017 }
3018
Jamie Madill075edd82013-07-08 13:30:19 -04003019 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003020 out << ";}\n";
3021
3022 if (!firstLoopFragment)
3023 {
3024 out << "}\n";
3025 }
3026
3027 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003028
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003029 initial += MAX_LOOP_ITERATIONS * increment;
3030 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003031 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003032
3033 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003034
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003035 mExcessiveLoopIndex = restoreIndex;
3036
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003037 return true;
3038 }
3039 else UNIMPLEMENTED();
3040 }
3041
3042 return false; // Not handled as an excessive loop
3043}
3044
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003045void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003046{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00003047 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003048
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003049 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003050 {
3051 out << preString;
3052 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003053 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003054 {
3055 out << inString;
3056 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003057 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003058 {
3059 out << postString;
3060 }
3061}
3062
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003063void OutputHLSL::outputLineDirective(int line)
3064{
3065 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
3066 {
baustin@google.com8ab69842011-06-02 21:53:45 +00003067 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003068 mBody << "#line " << line;
3069
3070 if (mContext.sourcePath)
3071 {
3072 mBody << " \"" << mContext.sourcePath << "\"";
3073 }
3074
3075 mBody << "\n";
3076 }
3077}
3078
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003079TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3080{
3081 TQualifier qualifier = symbol->getQualifier();
3082 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003083 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003084
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003085 if (name.empty()) // HLSL demands named arguments, also for prototypes
3086 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00003087 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003088 }
3089 else
3090 {
3091 name = decorate(name);
3092 }
3093
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003094 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
3095 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003096 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
3097 qualifierString(qualifier) + " " + samplerString(type) + " sampler_" + name + arrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003098 }
3099
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003100 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003101}
3102
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003103TString OutputHLSL::interpolationString(TQualifier qualifier)
3104{
3105 switch(qualifier)
3106 {
3107 case EvqVaryingIn: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003108 case EvqFragmentIn: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003109 case EvqInvariantVaryingIn: return "";
3110 case EvqSmoothIn: return "linear";
3111 case EvqFlatIn: return "nointerpolation";
3112 case EvqCentroidIn: return "centroid";
3113 case EvqVaryingOut: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003114 case EvqVertexOut: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003115 case EvqInvariantVaryingOut: return "";
3116 case EvqSmoothOut: return "linear";
3117 case EvqFlatOut: return "nointerpolation";
3118 case EvqCentroidOut: return "centroid";
3119 default: UNREACHABLE();
3120 }
3121
3122 return "";
3123}
3124
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003125TString OutputHLSL::qualifierString(TQualifier qualifier)
3126{
3127 switch(qualifier)
3128 {
3129 case EvqIn: return "in";
3130 case EvqOut: return "out";
3131 case EvqInOut: return "inout";
3132 case EvqConstReadOnly: return "const";
3133 default: UNREACHABLE();
3134 }
3135
3136 return "";
3137}
3138
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003139TString OutputHLSL::typeString(const TType &type)
3140{
Jamie Madill98493dd2013-07-08 14:39:03 -04003141 const TStructure* structure = type.getStruct();
3142 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003143 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003144 const TString& typeName = structure->name();
3145 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003146 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003147 return structLookup(typeName);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003148 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003149 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003150 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003151 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003152 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003153 }
3154 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003155 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003156 int cols = type.getCols();
3157 int rows = type.getRows();
3158 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003159 }
3160 else
3161 {
3162 switch (type.getBasicType())
3163 {
3164 case EbtFloat:
3165 switch (type.getNominalSize())
3166 {
3167 case 1: return "float";
3168 case 2: return "float2";
3169 case 3: return "float3";
3170 case 4: return "float4";
3171 }
3172 case EbtInt:
3173 switch (type.getNominalSize())
3174 {
3175 case 1: return "int";
3176 case 2: return "int2";
3177 case 3: return "int3";
3178 case 4: return "int4";
3179 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003180 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04003181 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003182 {
3183 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003184 case 2: return "uint2";
3185 case 3: return "uint3";
3186 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003187 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003188 case EbtBool:
3189 switch (type.getNominalSize())
3190 {
3191 case 1: return "bool";
3192 case 2: return "bool2";
3193 case 3: return "bool3";
3194 case 4: return "bool4";
3195 }
3196 case EbtVoid:
3197 return "void";
3198 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003199 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04003200 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003201 case EbtSampler2DArray:
3202 case EbtISampler2DArray:
3203 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003204 return "sampler2D";
3205 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003206 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04003207 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003208 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00003209 case EbtSamplerExternalOES:
3210 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00003211 default:
3212 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003213 }
3214 }
3215
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003216 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003217 return "<unknown type>";
3218}
3219
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003220TString OutputHLSL::textureString(const TType &type)
3221{
3222 switch (type.getBasicType())
3223 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003224 case EbtSampler2D: return "Texture2D";
3225 case EbtSamplerCube: return "TextureCube";
3226 case EbtSamplerExternalOES: return "Texture2D";
3227 case EbtSampler2DArray: return "Texture2DArray";
3228 case EbtSampler3D: return "Texture3D";
3229 case EbtISampler2D: return "Texture2D<int4>";
3230 case EbtISampler3D: return "Texture3D<int4>";
3231 case EbtISamplerCube: return "TextureCube<int4>";
3232 case EbtISampler2DArray: return "Texture2DArray<int4>";
3233 case EbtUSampler2D: return "Texture2D<uint4>";
3234 case EbtUSampler3D: return "Texture3D<uint4>";
3235 case EbtUSamplerCube: return "TextureCube<uint4>";
3236 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
3237 case EbtSampler2DShadow: return "Texture2D";
3238 case EbtSamplerCubeShadow: return "TextureCube";
3239 case EbtSampler2DArrayShadow: return "Texture2DArray";
3240 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003241 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04003242
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003243 return "<unknown texture type>";
3244}
3245
Nicolas Capenscb127d32013-07-15 17:26:18 -04003246TString OutputHLSL::samplerString(const TType &type)
3247{
3248 if (IsShadowSampler(type.getBasicType()))
3249 {
3250 return "SamplerComparisonState";
3251 }
3252 else
3253 {
3254 return "SamplerState";
3255 }
3256}
3257
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003258TString OutputHLSL::arrayString(const TType &type)
3259{
3260 if (!type.isArray())
3261 {
3262 return "";
3263 }
3264
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003265 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003266}
3267
3268TString OutputHLSL::initializer(const TType &type)
3269{
3270 TString string;
3271
Jamie Madill94bf7f22013-07-08 13:31:15 -04003272 size_t size = type.getObjectSize();
3273 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003274 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003275 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003276
Jamie Madill94bf7f22013-07-08 13:31:15 -04003277 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003278 {
3279 string += ", ";
3280 }
3281 }
3282
daniel@transgaming.comead23042010-04-29 03:35:36 +00003283 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003284}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003285
Jamie Madill98493dd2013-07-08 14:39:03 -04003286TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003287{
Jamie Madill98493dd2013-07-08 14:39:03 -04003288 const TFieldList &fields = structure.fields();
3289 const bool isNameless = (structure.name() == "");
3290 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003291 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3292
Jamie Madill98493dd2013-07-08 14:39:03 -04003293 TString string;
3294 string += declareString + "\n"
3295 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003296
Jamie Madillc835df62013-06-21 09:15:32 -04003297 int elementIndex = 0;
3298
Jamie Madill9cf6c072013-06-20 11:55:53 -04003299 for (unsigned int i = 0; i < fields.size(); i++)
3300 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003301 const TField &field = *fields[i];
3302 const TType &fieldType = *field.type();
3303 const TStructure *fieldStruct = fieldType.getStruct();
3304 const TString &fieldTypeString = fieldStruct ? structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) : typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003305
Jamie Madillc835df62013-06-21 09:15:32 -04003306 if (useStd140Packing)
3307 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003308 string += std140PrePaddingString(*field.type(), &elementIndex);
Jamie Madillc835df62013-06-21 09:15:32 -04003309 }
3310
Jamie Madill98493dd2013-07-08 14:39:03 -04003311 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003312
3313 if (useStd140Packing)
3314 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003315 string += std140PostPaddingString(*field.type(), useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003316 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003317 }
3318
3319 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003320 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003321
Jamie Madille4075c92013-06-21 09:15:32 -04003322 // Add remaining element index to the global map, for use with nested structs in standard layouts
3323 if (useStd140Packing)
3324 {
3325 mStd140StructElementIndexes[structName] = elementIndex;
3326 }
3327
Jamie Madill98493dd2013-07-08 14:39:03 -04003328 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003329}
3330
Jamie Madill98493dd2013-07-08 14:39:03 -04003331TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003332{
Jamie Madill98493dd2013-07-08 14:39:03 -04003333 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003334 {
3335 return "";
3336 }
3337
3338 TString prefix = "";
3339
3340 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3341 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003342
3343 if (useStd140Packing)
3344 {
3345 prefix += "std";
3346 }
3347
Jamie Madill9cf6c072013-06-20 11:55:53 -04003348 if (useHLSLRowMajorPacking)
3349 {
Jamie Madillc835df62013-06-21 09:15:32 -04003350 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003351 prefix += "rm";
3352 }
3353
Jamie Madill98493dd2013-07-08 14:39:03 -04003354 return prefix + structLookup(structure.name());
Jamie Madill9cf6c072013-06-20 11:55:53 -04003355}
3356
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003357void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003358{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003359 if (name == "")
3360 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003361 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003362 }
3363
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003364 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
3365 {
3366 return; // Already added
3367 }
3368
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003369 TType ctorType = type;
3370 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003371 ctorType.setPrecision(EbpHigh);
3372 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003373
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003374 TString ctorName = type.getStruct() ? decorate(name) : name;
3375
3376 typedef std::vector<TType> ParameterArray;
3377 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003378
Jamie Madill98493dd2013-07-08 14:39:03 -04003379 const TStructure* structure = type.getStruct();
3380 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003381 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003382 mStructNames.insert(decorate(name));
3383
Jamie Madill98493dd2013-07-08 14:39:03 -04003384 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003385
Jamie Madill98493dd2013-07-08 14:39:03 -04003386 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003387 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003388 // Add row-major packed struct for interface blocks
3389 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003390 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003391 "#pragma pack_matrix(column_major)\n";
3392
Jamie Madillc835df62013-06-21 09:15:32 -04003393 const TString &std140Prefix = "std";
Jamie Madill98493dd2013-07-08 14:39:03 -04003394 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003395
3396 const TString &std140RowMajorPrefix = "std_rm";
3397 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003398 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003399 "#pragma pack_matrix(column_major)\n";
3400
Jamie Madill98493dd2013-07-08 14:39:03 -04003401 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003402 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003403 mStructDeclarations.push_back(std140String);
3404 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003405 }
3406
Jamie Madill98493dd2013-07-08 14:39:03 -04003407 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003408 for (unsigned int i = 0; i < fields.size(); i++)
3409 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003410 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003411 }
3412 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003413 else if (parameters)
3414 {
3415 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3416 {
3417 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3418 }
3419 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003420 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003421
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003422 TString constructor;
3423
3424 if (ctorType.getStruct())
3425 {
3426 constructor += ctorName + " " + ctorName + "_ctor(";
3427 }
3428 else // Built-in type
3429 {
3430 constructor += typeString(ctorType) + " " + ctorName + "(";
3431 }
3432
3433 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3434 {
3435 const TType &type = ctorParameters[parameter];
3436
3437 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3438
3439 if (parameter < ctorParameters.size() - 1)
3440 {
3441 constructor += ", ";
3442 }
3443 }
3444
3445 constructor += ")\n"
3446 "{\n";
3447
3448 if (ctorType.getStruct())
3449 {
3450 constructor += " " + ctorName + " structure = {";
3451 }
3452 else
3453 {
3454 constructor += " return " + typeString(ctorType) + "(";
3455 }
3456
3457 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3458 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003459 int rows = ctorType.getRows();
3460 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003461 const TType &parameter = ctorParameters[0];
3462
3463 if (parameter.isScalar())
3464 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003465 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003466 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003467 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003468 {
3469 constructor += TString((row == col) ? "x0" : "0.0");
3470
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003471 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003472 {
3473 constructor += ", ";
3474 }
3475 }
3476 }
3477 }
3478 else if (parameter.isMatrix())
3479 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003480 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003481 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003482 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003483 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003484 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003485 {
3486 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3487 }
3488 else
3489 {
3490 constructor += TString((row == col) ? "1.0" : "0.0");
3491 }
3492
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003493 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003494 {
3495 constructor += ", ";
3496 }
3497 }
3498 }
3499 }
3500 else UNREACHABLE();
3501 }
3502 else
3503 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003504 size_t remainingComponents = ctorType.getObjectSize();
3505 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003506
3507 while (remainingComponents > 0)
3508 {
3509 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003510 const size_t parameterSize = parameter.getObjectSize();
3511 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003512
3513 constructor += "x" + str(parameterIndex);
3514
3515 if (parameter.isScalar())
3516 {
3517 remainingComponents -= parameter.getObjectSize();
3518 }
3519 else if (parameter.isVector())
3520 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003521 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003522 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003523 ASSERT(parameterSize <= remainingComponents);
3524 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003525 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003526 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003527 {
3528 switch (remainingComponents)
3529 {
3530 case 1: constructor += ".x"; break;
3531 case 2: constructor += ".xy"; break;
3532 case 3: constructor += ".xyz"; break;
3533 case 4: constructor += ".xyzw"; break;
3534 default: UNREACHABLE();
3535 }
3536
3537 remainingComponents = 0;
3538 }
3539 else UNREACHABLE();
3540 }
3541 else if (parameter.isMatrix() || parameter.getStruct())
3542 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003543 ASSERT(remainingComponents == parameterSize || moreParameters);
3544 ASSERT(parameterSize <= remainingComponents);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003545
Jamie Madill94bf7f22013-07-08 13:31:15 -04003546 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003547 }
3548 else UNREACHABLE();
3549
3550 if (moreParameters)
3551 {
3552 parameterIndex++;
3553 }
3554
3555 if (remainingComponents)
3556 {
3557 constructor += ", ";
3558 }
3559 }
3560 }
3561
3562 if (ctorType.getStruct())
3563 {
3564 constructor += "};\n"
3565 " return structure;\n"
3566 "}\n";
3567 }
3568 else
3569 {
3570 constructor += ");\n"
3571 "}\n";
3572 }
3573
daniel@transgaming.com63691862010-04-29 03:32:42 +00003574 mConstructors.insert(constructor);
3575}
3576
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003577const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3578{
3579 TInfoSinkBase &out = mBody;
3580
Jamie Madill98493dd2013-07-08 14:39:03 -04003581 const TStructure* structure = type.getStruct();
3582 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003583 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003584 out << structLookup(structure->name()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003585
Jamie Madill98493dd2013-07-08 14:39:03 -04003586 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003587
Jamie Madill98493dd2013-07-08 14:39:03 -04003588 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003589 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003590 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003591
3592 constUnion = writeConstantUnion(*fieldType, constUnion);
3593
Jamie Madill98493dd2013-07-08 14:39:03 -04003594 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003595 {
3596 out << ", ";
3597 }
3598 }
3599
3600 out << ")";
3601 }
3602 else
3603 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003604 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003605 bool writeType = size > 1;
3606
3607 if (writeType)
3608 {
3609 out << typeString(type) << "(";
3610 }
3611
Jamie Madill94bf7f22013-07-08 13:31:15 -04003612 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003613 {
3614 switch (constUnion->getType())
3615 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003616 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003617 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003618 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003619 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003620 default: UNREACHABLE();
3621 }
3622
3623 if (i != size - 1)
3624 {
3625 out << ", ";
3626 }
3627 }
3628
3629 if (writeType)
3630 {
3631 out << ")";
3632 }
3633 }
3634
3635 return constUnion;
3636}
3637
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003638TString OutputHLSL::scopeString(unsigned int depthLimit)
3639{
3640 TString string;
3641
3642 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3643 {
3644 string += "_" + str(i);
3645 }
3646
3647 return string;
3648}
3649
3650TString OutputHLSL::scopedStruct(const TString &typeName)
3651{
3652 if (typeName == "")
3653 {
3654 return typeName;
3655 }
3656
3657 return typeName + scopeString(mScopeDepth);
3658}
3659
3660TString OutputHLSL::structLookup(const TString &typeName)
3661{
3662 for (int depth = mScopeDepth; depth >= 0; depth--)
3663 {
3664 TString scopedName = decorate(typeName + scopeString(depth));
3665
3666 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3667 {
3668 if (*structName == scopedName)
3669 {
3670 return scopedName;
3671 }
3672 }
3673 }
3674
3675 UNREACHABLE(); // Should have found a matching constructor
3676
3677 return typeName;
3678}
3679
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003680TString OutputHLSL::decorate(const TString &string)
3681{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003682 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003683 {
3684 return "_" + string;
3685 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003686
3687 return string;
3688}
3689
apatrick@chromium.org65756022012-01-17 21:45:38 +00003690TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003691{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003692 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003693 {
3694 return "ex_" + string;
3695 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003696
3697 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003698}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003699
Jamie Madill98493dd2013-07-08 14:39:03 -04003700TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003701{
Jamie Madill98493dd2013-07-08 14:39:03 -04003702 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003703 {
3704 return decorate(string);
3705 }
3706
3707 return string;
3708}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003709
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003710void OutputHLSL::declareInterfaceBlockField(const TType &type, const TString &name, std::vector<InterfaceBlockField>& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003711{
Jamie Madill98493dd2013-07-08 14:39:03 -04003712 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003713
3714 if (!structure)
3715 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003716 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003717 InterfaceBlockField field(glVariableType(type), glVariablePrecision(type), name.c_str(),
3718 (unsigned int)type.getArraySize(), isRowMajorMatrix);
3719 output.push_back(field);
3720 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003721 else
3722 {
Jamie Madill28167c62013-08-30 13:21:10 -04003723 InterfaceBlockField structField(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), false);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003724
3725 const TFieldList &fields = structure->fields();
3726
3727 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3728 {
3729 TField *field = fields[fieldIndex];
3730 TType *fieldType = field->type();
3731
3732 // make sure to copy matrix packing information
3733 fieldType->setLayoutQualifier(type.getLayoutQualifier());
3734
3735 declareInterfaceBlockField(*fieldType, field->name(), structField.fields);
3736 }
3737
3738 output.push_back(structField);
3739 }
3740}
3741
Jamie Madillc2141fb2013-08-30 13:21:08 -04003742Uniform OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, std::vector<Uniform>& output)
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003743{
3744 const TStructure *structure = type.getStruct();
3745
3746 if (!structure)
3747 {
3748 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
3749 Uniform uniform(glVariableType(type), glVariablePrecision(type), name.c_str(),
Jamie Madill56093782013-08-30 13:21:11 -04003750 (unsigned int)type.getArraySize(), (unsigned int)registerIndex, 0);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003751 output.push_back(uniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003752
3753 return uniform;
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003754 }
3755 else
3756 {
Jamie Madill56093782013-08-30 13:21:11 -04003757 Uniform structUniform(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(),
3758 (unsigned int)registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003759
Jamie Madill98493dd2013-07-08 14:39:03 -04003760 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003761
Jamie Madill98493dd2013-07-08 14:39:03 -04003762 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003763 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003764 TField *field = fields[fieldIndex];
3765 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003766
Jamie Madill56093782013-08-30 13:21:11 -04003767 declareUniformToList(*fieldType, field->name(), GL_INVALID_INDEX, structUniform.fields);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003768 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003769
Jamie Madill56093782013-08-30 13:21:11 -04003770 // assign register offset information -- this will override the information in any sub-structures.
3771 HLSLVariableGetRegisterInfo(registerIndex, &structUniform);
3772
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003773 output.push_back(structUniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003774
3775 return structUniform;
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003776 }
3777}
3778
Jamie Madill139b9092013-08-30 13:21:06 -04003779InterpolationType getInterpolationType(TQualifier qualifier)
3780{
3781 switch (qualifier)
3782 {
3783 case EvqFlatIn:
3784 case EvqFlatOut:
3785 return INTERPOLATION_FLAT;
3786
3787 case EvqSmoothIn:
3788 case EvqSmoothOut:
3789 case EvqVertexOut:
3790 case EvqFragmentIn:
Jamie Madill384b6042013-09-13 10:06:24 -04003791 case EvqVaryingIn:
3792 case EvqVaryingOut:
Jamie Madill139b9092013-08-30 13:21:06 -04003793 return INTERPOLATION_SMOOTH;
3794
3795 case EvqCentroidIn:
3796 case EvqCentroidOut:
3797 return INTERPOLATION_CENTROID;
3798
3799 default: UNREACHABLE();
3800 return INTERPOLATION_SMOOTH;
3801 }
3802}
3803
Jamie Madill94599662013-08-30 13:21:10 -04003804void OutputHLSL::declareVaryingToList(const TType &type, TQualifier baseTypeQualifier, const TString &name, std::vector<Varying>& fieldsOut)
Jamie Madill47fdd132013-08-30 13:21:04 -04003805{
3806 const TStructure *structure = type.getStruct();
3807
Jamie Madill94599662013-08-30 13:21:10 -04003808 InterpolationType interpolation = getInterpolationType(baseTypeQualifier);
Jamie Madill47fdd132013-08-30 13:21:04 -04003809 if (!structure)
3810 {
Jamie Madill139b9092013-08-30 13:21:06 -04003811 Varying varying(glVariableType(type), glVariablePrecision(type), name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003812 fieldsOut.push_back(varying);
3813 }
3814 else
3815 {
Jamie Madill28167c62013-08-30 13:21:10 -04003816 Varying structVarying(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003817 const TFieldList &fields = structure->fields();
3818
Jamie Madill28167c62013-08-30 13:21:10 -04003819 structVarying.structName = structure->name().c_str();
3820
Jamie Madill47fdd132013-08-30 13:21:04 -04003821 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3822 {
3823 const TField &field = *fields[fieldIndex];
Jamie Madill94599662013-08-30 13:21:10 -04003824 declareVaryingToList(*field.type(), baseTypeQualifier, field.name(), structVarying.fields);
Jamie Madill47fdd132013-08-30 13:21:04 -04003825 }
3826
3827 fieldsOut.push_back(structVarying);
3828 }
3829}
3830
Jamie Madillc2141fb2013-08-30 13:21:08 -04003831int OutputHLSL::declareUniformAndAssignRegister(const TType &type, const TString &name)
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003832{
Jamie Madillc2141fb2013-08-30 13:21:08 -04003833 int registerIndex = (IsSampler(type.getBasicType()) ? mSamplerRegister : mUniformRegister);
3834
3835 const Uniform &uniform = declareUniformToList(type, name, registerIndex, mActiveUniforms);
3836
3837 if (IsSampler(type.getBasicType()))
3838 {
3839 mSamplerRegister += HLSLVariableRegisterCount(uniform);
3840 }
3841 else
3842 {
3843 mUniformRegister += HLSLVariableRegisterCount(uniform);
3844 }
3845
3846 return registerIndex;
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003847}
3848
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003849GLenum OutputHLSL::glVariableType(const TType &type)
3850{
3851 if (type.getBasicType() == EbtFloat)
3852 {
3853 if (type.isScalar())
3854 {
3855 return GL_FLOAT;
3856 }
3857 else if (type.isVector())
3858 {
3859 switch(type.getNominalSize())
3860 {
3861 case 2: return GL_FLOAT_VEC2;
3862 case 3: return GL_FLOAT_VEC3;
3863 case 4: return GL_FLOAT_VEC4;
3864 default: UNREACHABLE();
3865 }
3866 }
3867 else if (type.isMatrix())
3868 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003869 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003870 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003871 case 2:
3872 switch(type.getRows())
3873 {
3874 case 2: return GL_FLOAT_MAT2;
3875 case 3: return GL_FLOAT_MAT2x3;
3876 case 4: return GL_FLOAT_MAT2x4;
3877 default: UNREACHABLE();
3878 }
3879
3880 case 3:
3881 switch(type.getRows())
3882 {
3883 case 2: return GL_FLOAT_MAT3x2;
3884 case 3: return GL_FLOAT_MAT3;
3885 case 4: return GL_FLOAT_MAT3x4;
3886 default: UNREACHABLE();
3887 }
3888
3889 case 4:
3890 switch(type.getRows())
3891 {
3892 case 2: return GL_FLOAT_MAT4x2;
3893 case 3: return GL_FLOAT_MAT4x3;
3894 case 4: return GL_FLOAT_MAT4;
3895 default: UNREACHABLE();
3896 }
3897
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003898 default: UNREACHABLE();
3899 }
3900 }
3901 else UNREACHABLE();
3902 }
3903 else if (type.getBasicType() == EbtInt)
3904 {
3905 if (type.isScalar())
3906 {
3907 return GL_INT;
3908 }
3909 else if (type.isVector())
3910 {
3911 switch(type.getNominalSize())
3912 {
3913 case 2: return GL_INT_VEC2;
3914 case 3: return GL_INT_VEC3;
3915 case 4: return GL_INT_VEC4;
3916 default: UNREACHABLE();
3917 }
3918 }
3919 else UNREACHABLE();
3920 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003921 else if (type.getBasicType() == EbtUInt)
3922 {
3923 if (type.isScalar())
3924 {
3925 return GL_UNSIGNED_INT;
3926 }
3927 else if (type.isVector())
3928 {
Jamie Madill22d63da2013-06-07 12:45:12 -04003929 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003930 {
3931 case 2: return GL_UNSIGNED_INT_VEC2;
3932 case 3: return GL_UNSIGNED_INT_VEC3;
3933 case 4: return GL_UNSIGNED_INT_VEC4;
3934 default: UNREACHABLE();
3935 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003936 }
3937 else UNREACHABLE();
3938 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003939 else if (type.getBasicType() == EbtBool)
3940 {
3941 if (type.isScalar())
3942 {
3943 return GL_BOOL;
3944 }
3945 else if (type.isVector())
3946 {
3947 switch(type.getNominalSize())
3948 {
3949 case 2: return GL_BOOL_VEC2;
3950 case 3: return GL_BOOL_VEC3;
3951 case 4: return GL_BOOL_VEC4;
3952 default: UNREACHABLE();
3953 }
3954 }
3955 else UNREACHABLE();
3956 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003957
3958 switch(type.getBasicType())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003959 {
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003960 case EbtSampler2D: return GL_SAMPLER_2D;
3961 case EbtSampler3D: return GL_SAMPLER_3D;
3962 case EbtSamplerCube: return GL_SAMPLER_CUBE;
3963 case EbtSampler2DArray: return GL_SAMPLER_2D_ARRAY;
3964 case EbtISampler2D: return GL_INT_SAMPLER_2D;
3965 case EbtISampler3D: return GL_INT_SAMPLER_3D;
3966 case EbtISamplerCube: return GL_INT_SAMPLER_CUBE;
3967 case EbtISampler2DArray: return GL_INT_SAMPLER_2D_ARRAY;
3968 case EbtUSampler2D: return GL_UNSIGNED_INT_SAMPLER_2D;
3969 case EbtUSampler3D: return GL_UNSIGNED_INT_SAMPLER_3D;
3970 case EbtUSamplerCube: return GL_UNSIGNED_INT_SAMPLER_CUBE;
3971 case EbtUSampler2DArray: return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3972 case EbtSampler2DShadow: return GL_SAMPLER_2D_SHADOW;
3973 case EbtSamplerCubeShadow: return GL_SAMPLER_CUBE_SHADOW;
3974 case EbtSampler2DArrayShadow: return GL_SAMPLER_2D_ARRAY_SHADOW;
3975 default: UNREACHABLE();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003976 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003977
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003978 return GL_NONE;
3979}
3980
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003981GLenum OutputHLSL::glVariablePrecision(const TType &type)
3982{
3983 if (type.getBasicType() == EbtFloat)
3984 {
3985 switch (type.getPrecision())
3986 {
3987 case EbpHigh: return GL_HIGH_FLOAT;
3988 case EbpMedium: return GL_MEDIUM_FLOAT;
3989 case EbpLow: return GL_LOW_FLOAT;
3990 case EbpUndefined:
3991 // Should be defined as the default precision by the parser
3992 default: UNREACHABLE();
3993 }
3994 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003995 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003996 {
3997 switch (type.getPrecision())
3998 {
3999 case EbpHigh: return GL_HIGH_INT;
4000 case EbpMedium: return GL_MEDIUM_INT;
4001 case EbpLow: return GL_LOW_INT;
4002 case EbpUndefined:
4003 // Should be defined as the default precision by the parser
4004 default: UNREACHABLE();
4005 }
4006 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004007
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00004008 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004009 return GL_NONE;
4010}
4011
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004012bool OutputHLSL::isVaryingOut(TQualifier qualifier)
4013{
4014 switch(qualifier)
4015 {
4016 case EvqVaryingOut:
4017 case EvqInvariantVaryingOut:
4018 case EvqSmoothOut:
4019 case EvqFlatOut:
4020 case EvqCentroidOut:
Jamie Madill19571812013-08-12 15:26:34 -07004021 case EvqVertexOut:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004022 return true;
4023 }
4024
4025 return false;
4026}
4027
4028bool OutputHLSL::isVaryingIn(TQualifier qualifier)
4029{
4030 switch(qualifier)
4031 {
4032 case EvqVaryingIn:
4033 case EvqInvariantVaryingIn:
4034 case EvqSmoothIn:
4035 case EvqFlatIn:
4036 case EvqCentroidIn:
Jamie Madill19571812013-08-12 15:26:34 -07004037 case EvqFragmentIn:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004038 return true;
4039 }
4040
4041 return false;
4042}
4043
4044bool OutputHLSL::isVarying(TQualifier qualifier)
4045{
4046 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
4047}
4048
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004049}