blob: 503d10d07064d95ae17de285a4400693e36fca09 [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 {
58 case IMPLICIT: break;
59 case BIAS: break;
60 case LOD: name += "Lod"; break;
61 case LOD0: name += "Lod0"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -040062 case SIZE: name += "Size"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040063 default: UNREACHABLE();
64 }
65
66 return name + "(";
67}
68
Jamie Madillc2141fb2013-08-30 13:21:08 -040069const char *RegisterPrefix(const TType &type)
70{
71 if (IsSampler(type.getBasicType()))
72 {
73 return "s";
74 }
75 else
76 {
77 return "c";
78 }
79}
80
Nicolas Capense0ba27a2013-06-24 16:10:52 -040081bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
82{
83 if (sampler < rhs.sampler) return true;
84 if (coords < rhs.coords) return true;
85 if (!proj && rhs.proj) return true;
Nicolas Capens75fb4752013-07-10 15:14:47 -040086 if (method < rhs.method) return true;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040087
88 return false;
89}
90
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +000091OutputHLSL::OutputHLSL(TParseContext &context, const ShBuiltInResources& resources, ShShaderOutput outputType)
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +000092 : TIntermTraverser(true, true, true), mContext(context), mOutputType(outputType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000093{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000094 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +000095 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000096
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +000097 mUsesFragColor = false;
98 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +000099 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000100 mUsesFragCoord = false;
101 mUsesPointCoord = false;
102 mUsesFrontFacing = false;
103 mUsesPointSize = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400104 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000105 mUsesXor = false;
106 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000107 mUsesMod2v = false;
108 mUsesMod2f = false;
109 mUsesMod3v = false;
110 mUsesMod3f = false;
111 mUsesMod4v = false;
112 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000113 mUsesFaceforward1 = false;
114 mUsesFaceforward2 = false;
115 mUsesFaceforward3 = false;
116 mUsesFaceforward4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000117 mUsesAtan2_1 = false;
118 mUsesAtan2_2 = false;
119 mUsesAtan2_3 = false;
120 mUsesAtan2_4 = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500121 mUsesDiscardRewriting = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000122
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000123 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
124
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000125 mScopeDepth = 0;
126
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000127 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000128
129 mContainsLoopDiscontinuity = false;
130 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000131 mInsideDiscontinuousLoop = false;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000132
133 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000134
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000135 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000136 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000137 if (mContext.shaderType == SH_FRAGMENT_SHADER)
138 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000139 mUniformRegister = 3; // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000140 }
141 else
142 {
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000143 mUniformRegister = 2; // Reserve registers for dx_DepthRange and dx_ViewAdjust
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000144 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000145 }
146 else
147 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000148 mUniformRegister = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000149 }
150
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000151 mSamplerRegister = 0;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000152 mInterfaceBlockRegister = 2; // Reserve registers for the default uniform block and driver constants
Jamie Madill574d9dd2013-06-20 11:55:56 -0400153 mPaddingCounter = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000154}
155
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000156OutputHLSL::~OutputHLSL()
157{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000158 delete mUnfoldShortCircuit;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000159}
160
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000161void OutputHLSL::output()
162{
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +0000163 mContainsLoopDiscontinuity = mContext.shaderType == SH_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400164 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
165 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000166
Jamie Madille53c98b2014-02-03 11:57:13 -0500167 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
168 // use a vertex attribute as a condition, and some related computation in the else block.
169 if (mOutputType == SH_HLSL9_OUTPUT && mContext.shaderType == SH_VERTEX_SHADER)
170 {
171 RewriteElseBlocks(mContext.treeRoot);
172 }
173
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000174 mContext.treeRoot->traverse(this); // Output the body first to determine what has to go in the header
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000175 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000176
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000177 mContext.infoSink().obj << mHeader.c_str();
178 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000179}
180
Jamie Madill570e04d2013-06-21 09:15:33 -0400181void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
182{
183 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
184 {
185 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
186
187 // This will mark the necessary block elements as referenced
188 flaggedNode->traverse(this);
189 TString structName(mBody.c_str());
190 mBody.erase();
191
192 mFlaggedStructOriginalNames[flaggedNode] = structName;
193
194 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
195 {
196 structName.erase(pos, 1);
197 }
198
199 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
200 }
201}
202
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000203TInfoSinkBase &OutputHLSL::getBodyStream()
204{
205 return mBody;
206}
207
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400208const std::vector<Uniform> &OutputHLSL::getUniforms()
daniel@transgaming.com043da132012-12-20 21:12:22 +0000209{
210 return mActiveUniforms;
211}
212
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000213const ActiveInterfaceBlocks &OutputHLSL::getInterfaceBlocks() const
214{
215 return mActiveInterfaceBlocks;
216}
217
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400218const std::vector<Attribute> &OutputHLSL::getOutputVariables() const
Jamie Madill46131a32013-06-20 11:55:50 -0400219{
220 return mActiveOutputVariables;
221}
222
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400223const std::vector<Attribute> &OutputHLSL::getAttributes() const
Jamie Madilldefb6742013-06-20 11:55:51 -0400224{
225 return mActiveAttributes;
226}
227
Jamie Madill47fdd132013-08-30 13:21:04 -0400228const std::vector<Varying> &OutputHLSL::getVaryings() const
229{
230 return mActiveVaryings;
231}
232
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000233int OutputHLSL::vectorSize(const TType &type) const
234{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000235 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000236 int arraySize = type.isArray() ? type.getArraySize() : 1;
237
238 return elementSize * arraySize;
239}
240
Jamie Madill98493dd2013-07-08 14:39:03 -0400241TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, const TField &field)
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000242{
Jamie Madill98493dd2013-07-08 14:39:03 -0400243 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000244 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400245 return interfaceBlock.name() + "." + field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000246 }
247 else
248 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400249 return field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000250 }
251}
252
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000253TString OutputHLSL::decoratePrivate(const TString &privateText)
254{
255 return "dx_" + privateText;
256}
257
Jamie Madill98493dd2013-07-08 14:39:03 -0400258TString OutputHLSL::interfaceBlockStructNameString(const TInterfaceBlock &interfaceBlock)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000259{
Jamie Madill98493dd2013-07-08 14:39:03 -0400260 return decoratePrivate(interfaceBlock.name()) + "_type";
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000261}
262
Jamie Madill98493dd2013-07-08 14:39:03 -0400263TString OutputHLSL::interfaceBlockInstanceString(const TInterfaceBlock& interfaceBlock, unsigned int arrayIndex)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000264{
Jamie Madill98493dd2013-07-08 14:39:03 -0400265 if (!interfaceBlock.hasInstanceName())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000266 {
267 return "";
268 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400269 else if (interfaceBlock.isArray())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000270 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400271 return decoratePrivate(interfaceBlock.instanceName()) + "_" + str(arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000272 }
273 else
274 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400275 return decorate(interfaceBlock.instanceName());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000276 }
277}
278
Jamie Madill98493dd2013-07-08 14:39:03 -0400279TString OutputHLSL::interfaceBlockFieldTypeString(const TField &field, TLayoutBlockStorage blockStorage)
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000280{
Jamie Madill98493dd2013-07-08 14:39:03 -0400281 const TType &fieldType = *field.type();
282 const TLayoutMatrixPacking matrixPacking = fieldType.getLayoutQualifier().matrixPacking;
Jamie Madill529077d2013-06-20 11:55:54 -0400283 ASSERT(matrixPacking != EmpUnspecified);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000284
Jamie Madill98493dd2013-07-08 14:39:03 -0400285 if (fieldType.isMatrix())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000286 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400287 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill529077d2013-06-20 11:55:54 -0400288 const TString &matrixPackString = (matrixPacking == EmpRowMajor ? "column_major" : "row_major");
Jamie Madill98493dd2013-07-08 14:39:03 -0400289 return matrixPackString + " " + typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000290 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400291 else if (fieldType.getStruct())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000292 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400293 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill98493dd2013-07-08 14:39:03 -0400294 return structureTypeName(*fieldType.getStruct(), matrixPacking == EmpColumnMajor, blockStorage == EbsStd140);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000295 }
296 else
297 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400298 return typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000299 }
300}
301
Jamie Madill98493dd2013-07-08 14:39:03 -0400302TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, TLayoutBlockStorage blockStorage)
303{
304 TString hlsl;
305
306 int elementIndex = 0;
307
308 for (unsigned int typeIndex = 0; typeIndex < interfaceBlock.fields().size(); typeIndex++)
309 {
310 const TField &field = *interfaceBlock.fields()[typeIndex];
311 const TType &fieldType = *field.type();
312
313 if (blockStorage == EbsStd140)
314 {
315 // 2 and 3 component vector types in some cases need pre-padding
316 hlsl += std140PrePaddingString(fieldType, &elementIndex);
317 }
318
319 hlsl += " " + interfaceBlockFieldTypeString(field, blockStorage) +
320 " " + decorate(field.name()) + arrayString(fieldType) + ";\n";
321
322 // must pad out after matrices and arrays, where HLSL usually allows itself room to pack stuff
323 if (blockStorage == EbsStd140)
324 {
325 const bool useHLSLRowMajorPacking = (fieldType.getLayoutQualifier().matrixPacking == EmpColumnMajor);
326 hlsl += std140PostPaddingString(fieldType, useHLSLRowMajorPacking);
327 }
328 }
329
330 return hlsl;
331}
332
333TString OutputHLSL::interfaceBlockStructString(const TInterfaceBlock &interfaceBlock)
334{
335 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
336
337 return "struct " + interfaceBlockStructNameString(interfaceBlock) + "\n"
338 "{\n" +
339 interfaceBlockFieldString(interfaceBlock, blockStorage) +
340 "};\n\n";
341}
342
343TString OutputHLSL::interfaceBlockString(const TInterfaceBlock &interfaceBlock, unsigned int registerIndex, unsigned int arrayIndex)
344{
345 const TString &arrayIndexString = (arrayIndex != GL_INVALID_INDEX ? decorate(str(arrayIndex)) : "");
346 const TString &blockName = interfaceBlock.name() + arrayIndexString;
347 TString hlsl;
348
349 hlsl += "cbuffer " + blockName + " : register(b" + str(registerIndex) + ")\n"
350 "{\n";
351
352 if (interfaceBlock.hasInstanceName())
353 {
354 hlsl += " " + interfaceBlockStructNameString(interfaceBlock) + " " + interfaceBlockInstanceString(interfaceBlock, arrayIndex) + ";\n";
355 }
356 else
357 {
358 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
359 hlsl += interfaceBlockFieldString(interfaceBlock, blockStorage);
360 }
361
362 hlsl += "};\n\n";
363
364 return hlsl;
365}
366
Jamie Madill574d9dd2013-06-20 11:55:56 -0400367TString OutputHLSL::std140PrePaddingString(const TType &type, int *elementIndex)
368{
369 if (type.getBasicType() == EbtStruct || type.isMatrix() || type.isArray())
370 {
371 // no padding needed, HLSL will align the field to a new register
372 *elementIndex = 0;
373 return "";
374 }
375
376 const GLenum glType = glVariableType(type);
377 const int numComponents = gl::UniformComponentCount(glType);
378
379 if (numComponents >= 4)
380 {
381 // no padding needed, HLSL will align the field to a new register
382 *elementIndex = 0;
383 return "";
384 }
385
386 if (*elementIndex + numComponents > 4)
387 {
388 // no padding needed, HLSL will align the field to a new register
389 *elementIndex = numComponents;
390 return "";
391 }
392
393 TString padding;
394
395 const int alignment = numComponents == 3 ? 4 : numComponents;
396 const int paddingOffset = (*elementIndex % alignment);
397
398 if (paddingOffset != 0)
399 {
400 // padding is neccessary
401 for (int paddingIndex = paddingOffset; paddingIndex < alignment; paddingIndex++)
402 {
403 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
404 }
405
406 *elementIndex += (alignment - paddingOffset);
407 }
408
409 *elementIndex += numComponents;
410 *elementIndex %= 4;
411
412 return padding;
413}
414
Jamie Madille4075c92013-06-21 09:15:32 -0400415TString OutputHLSL::std140PostPaddingString(const TType &type, bool useHLSLRowMajorPacking)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400416{
Jamie Madillc835df62013-06-21 09:15:32 -0400417 if (!type.isMatrix() && !type.isArray() && type.getBasicType() != EbtStruct)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400418 {
419 return "";
420 }
421
Jamie Madill574d9dd2013-06-20 11:55:56 -0400422 int numComponents = 0;
423
424 if (type.isMatrix())
425 {
Jamie Madille4075c92013-06-21 09:15:32 -0400426 // This method can also be called from structureString, which does not use layout qualifiers.
427 // Thus, use the method parameter for determining the matrix packing.
428 //
429 // Note HLSL row major packing corresponds to GL API column-major, and vice-versa, since we
430 // wish to always transpose GL matrices to play well with HLSL's matrix array indexing.
431 //
432 const bool isRowMajorMatrix = !useHLSLRowMajorPacking;
Jamie Madillc835df62013-06-21 09:15:32 -0400433 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400434 numComponents = gl::MatrixComponentCount(glType, isRowMajorMatrix);
435 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400436 else if (type.getStruct())
Jamie Madillc835df62013-06-21 09:15:32 -0400437 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400438 const TString &structName = structureTypeName(*type.getStruct(), useHLSLRowMajorPacking, true);
Jamie Madille4075c92013-06-21 09:15:32 -0400439 numComponents = mStd140StructElementIndexes[structName];
440
441 if (numComponents == 0)
442 {
443 return "";
444 }
Jamie Madillc835df62013-06-21 09:15:32 -0400445 }
Jamie Madill574d9dd2013-06-20 11:55:56 -0400446 else
447 {
Jamie Madillc835df62013-06-21 09:15:32 -0400448 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400449 numComponents = gl::UniformComponentCount(glType);
450 }
451
452 TString padding;
453 for (int paddingOffset = numComponents; paddingOffset < 4; paddingOffset++)
454 {
455 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
456 }
457 return padding;
458}
459
Jamie Madill440dc742013-06-20 11:55:55 -0400460// Use the same layout for packed and shared
461void setBlockLayout(InterfaceBlock *interfaceBlock, BlockLayoutType newLayout)
462{
463 interfaceBlock->layout = newLayout;
464 interfaceBlock->blockInfo.clear();
465
466 switch (newLayout)
467 {
468 case BLOCKLAYOUT_SHARED:
469 case BLOCKLAYOUT_PACKED:
470 {
471 HLSLBlockEncoder hlslEncoder(&interfaceBlock->blockInfo);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400472 hlslEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400473 interfaceBlock->dataSize = hlslEncoder.getBlockSize();
474 }
475 break;
476
477 case BLOCKLAYOUT_STANDARD:
478 {
479 Std140BlockEncoder stdEncoder(&interfaceBlock->blockInfo);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400480 stdEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400481 interfaceBlock->dataSize = stdEncoder.getBlockSize();
482 }
483 break;
484
485 default:
486 UNREACHABLE();
487 break;
488 }
489}
490
Jamie Madill574d9dd2013-06-20 11:55:56 -0400491BlockLayoutType convertBlockLayoutType(TLayoutBlockStorage blockStorage)
492{
493 switch (blockStorage)
494 {
495 case EbsPacked: return BLOCKLAYOUT_PACKED;
496 case EbsShared: return BLOCKLAYOUT_SHARED;
497 case EbsStd140: return BLOCKLAYOUT_STANDARD;
498 default: UNREACHABLE(); return BLOCKLAYOUT_SHARED;
499 }
500}
501
Jamie Madill98493dd2013-07-08 14:39:03 -0400502TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400503{
504 TString init;
505
506 TString preIndentString;
507 TString fullIndentString;
508
509 for (int spaces = 0; spaces < (indent * 4); spaces++)
510 {
511 preIndentString += ' ';
512 }
513
514 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
515 {
516 fullIndentString += ' ';
517 }
518
519 init += preIndentString + "{\n";
520
Jamie Madill98493dd2013-07-08 14:39:03 -0400521 const TFieldList &fields = structure.fields();
522 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400523 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400524 const TField &field = *fields[fieldIndex];
525 const TString &fieldName = rhsStructName + "." + decorate(field.name());
526 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400527
Jamie Madill98493dd2013-07-08 14:39:03 -0400528 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400529 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400530 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400531 }
532 else
533 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400534 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400535 }
536 }
537
538 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
539
540 return init;
541}
542
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000543void OutputHLSL::header()
544{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000545 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000546
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000547 TString uniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000548 TString interfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000549 TString varyings;
550 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400551 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000552
Jamie Madillc2141fb2013-08-30 13:21:08 -0400553 for (ReferencedSymbols::const_iterator uniformIt = mReferencedUniforms.begin(); uniformIt != mReferencedUniforms.end(); uniformIt++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000554 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400555 const TIntermSymbol &uniform = *uniformIt->second;
556 const TType &type = uniform.getType();
557 const TString &name = uniform.getSymbol();
558
559 int registerIndex = declareUniformAndAssignRegister(type, name);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000560
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000561 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture
562 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400563 uniforms += "uniform " + samplerString(type) + " sampler_" + decorateUniform(name, type) + arrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400564 " : register(s" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000565
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000566 uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400567 " : register(t" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000568 }
569 else
570 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400571 const TStructure *structure = type.getStruct();
572 const TString &typeName = (structure ? structureTypeName(*structure, false, false) : typeString(type));
573
574 const TString &registerString = TString("register(") + RegisterPrefix(type) + str(registerIndex) + ")";
575
576 uniforms += "uniform " + typeName + " " + decorateUniform(name, type) + arrayString(type) + " : " + registerString + ";\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000577 }
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000578 }
579
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000580 for (ReferencedSymbols::const_iterator interfaceBlockIt = mReferencedInterfaceBlocks.begin(); interfaceBlockIt != mReferencedInterfaceBlocks.end(); interfaceBlockIt++)
581 {
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000582 const TType &nodeType = interfaceBlockIt->second->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -0400583 const TInterfaceBlock &interfaceBlock = *nodeType.getInterfaceBlock();
584 const TFieldList &fieldList = interfaceBlock.fields();
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000585
Jamie Madill98493dd2013-07-08 14:39:03 -0400586 unsigned int arraySize = static_cast<unsigned int>(interfaceBlock.arraySize());
587 sh::InterfaceBlock activeBlock(interfaceBlock.name().c_str(), arraySize, mInterfaceBlockRegister);
588 for (unsigned int typeIndex = 0; typeIndex < fieldList.size(); typeIndex++)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000589 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400590 const TField &field = *fieldList[typeIndex];
591 const TString &fullUniformName = interfaceBlockFieldString(interfaceBlock, field);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400592 declareInterfaceBlockField(*field.type(), fullUniformName, activeBlock.fields);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000593 }
594
Jamie Madill98493dd2013-07-08 14:39:03 -0400595 mInterfaceBlockRegister += std::max(1u, arraySize);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000596
Jamie Madill98493dd2013-07-08 14:39:03 -0400597 BlockLayoutType blockLayoutType = convertBlockLayoutType(interfaceBlock.blockStorage());
598 setBlockLayout(&activeBlock, blockLayoutType);
Jamie Madill9060a4e2013-08-12 16:22:57 -0700599
600 if (interfaceBlock.matrixPacking() == EmpRowMajor)
601 {
602 activeBlock.isRowMajorLayout = true;
603 }
604
Jamie Madill98493dd2013-07-08 14:39:03 -0400605 mActiveInterfaceBlocks.push_back(activeBlock);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000606
Jamie Madill98493dd2013-07-08 14:39:03 -0400607 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000608 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400609 interfaceBlocks += interfaceBlockStructString(interfaceBlock);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000610 }
611
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000612 if (arraySize > 0)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000613 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000614 for (unsigned int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
615 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400616 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex + arrayIndex, arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000617 }
618 }
619 else
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000620 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400621 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000622 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000623 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000624
Jamie Madill829f59e2013-11-13 19:40:54 -0500625 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400626 {
627 TIntermTyped *structNode = flaggedStructIt->first;
628 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400629 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400630 const TString &originalName = mFlaggedStructOriginalNames[structNode];
631
Jamie Madill98493dd2013-07-08 14:39:03 -0400632 flaggedStructs += "static " + decorate(structure.name()) + " " + mappedName + " =\n";
633 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400634 flaggedStructs += "\n";
635 }
636
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000637 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
638 {
639 const TType &type = varying->second->getType();
640 const TString &name = varying->second->getSymbol();
641
642 // Program linking depends on this exact format
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +0000643 varyings += "static " + interpolationString(type.getQualifier()) + " " + typeString(type) + " " +
644 decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madill47fdd132013-08-30 13:21:04 -0400645
Jamie Madill94599662013-08-30 13:21:10 -0400646 declareVaryingToList(type, type.getQualifier(), name, mActiveVaryings);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000647 }
648
649 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
650 {
651 const TType &type = attribute->second->getType();
652 const TString &name = attribute->second->getSymbol();
653
654 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400655
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400656 Attribute attributeVar(glVariableType(type), glVariablePrecision(type), name.c_str(),
657 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
658 mActiveAttributes.push_back(attributeVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000659 }
660
Jamie Madill529077d2013-06-20 11:55:54 -0400661 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
662 {
663 out << *structDeclaration;
664 }
665
666 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
667 {
668 out << *constructor;
669 }
670
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500671 if (mUsesDiscardRewriting)
672 {
673 out << "#define ANGLE_USES_DISCARD_REWRITING" << "\n";
674 }
675
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400676 if (mContext.shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000677 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000678 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000679 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000680
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000681 out << "// Varyings\n";
682 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400683 out << "\n";
684
685 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000686 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500687 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000688 {
Jamie Madill46131a32013-06-20 11:55:50 -0400689 const TString &variableName = outputVariableIt->first;
690 const TType &variableType = outputVariableIt->second->getType();
691 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
692
693 out << "static " + typeString(variableType) + " out_" + variableName + arrayString(variableType) +
694 " = " + initializer(variableType) + ";\n";
695
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400696 Attribute outputVar(glVariableType(variableType), glVariablePrecision(variableType), variableName.c_str(),
697 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
Jamie Madill46131a32013-06-20 11:55:50 -0400698 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000699 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000700 }
Jamie Madill46131a32013-06-20 11:55:50 -0400701 else
702 {
703 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
704
705 out << "static float4 gl_Color[" << numColorValues << "] =\n"
706 "{\n";
707 for (unsigned int i = 0; i < numColorValues; i++)
708 {
709 out << " float4(0, 0, 0, 0)";
710 if (i + 1 != numColorValues)
711 {
712 out << ",";
713 }
714 out << "\n";
715 }
716
717 out << "};\n";
718 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000719
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400720 if (mUsesFragDepth)
721 {
722 out << "static float gl_Depth = 0.0;\n";
723 }
724
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000725 if (mUsesFragCoord)
726 {
727 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
728 }
729
730 if (mUsesPointCoord)
731 {
732 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
733 }
734
735 if (mUsesFrontFacing)
736 {
737 out << "static bool gl_FrontFacing = false;\n";
738 }
739
740 out << "\n";
741
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000742 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000743 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000744 out << "struct gl_DepthRangeParameters\n"
745 "{\n"
746 " float near;\n"
747 " float far;\n"
748 " float diff;\n"
749 "};\n"
750 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000751 }
752
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000753 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000754 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000755 out << "cbuffer DriverConstants : register(b1)\n"
756 "{\n";
757
758 if (mUsesDepthRange)
759 {
760 out << " float3 dx_DepthRange : packoffset(c0);\n";
761 }
762
763 if (mUsesFragCoord)
764 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000765 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000766 }
767
768 if (mUsesFragCoord || mUsesFrontFacing)
769 {
770 out << " float3 dx_DepthFront : packoffset(c2);\n";
771 }
772
773 out << "};\n";
774 }
775 else
776 {
777 if (mUsesDepthRange)
778 {
779 out << "uniform float3 dx_DepthRange : register(c0);";
780 }
781
782 if (mUsesFragCoord)
783 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000784 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000785 }
786
787 if (mUsesFragCoord || mUsesFrontFacing)
788 {
789 out << "uniform float3 dx_DepthFront : register(c2);\n";
790 }
791 }
792
793 out << "\n";
794
795 if (mUsesDepthRange)
796 {
797 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
798 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000799 }
800
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000801 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000802 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000803
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000804 if (!interfaceBlocks.empty())
805 {
806 out << interfaceBlocks;
807 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400808
809 if (!flaggedStructs.empty())
810 {
811 out << "// Std140 Structures accessed by value\n";
812 out << "\n";
813 out << flaggedStructs;
814 out << "\n";
815 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000816 }
817
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000818 if (usingMRTExtension && mNumRenderTargets > 1)
819 {
820 out << "#define GL_USES_MRT\n";
821 }
822
823 if (mUsesFragColor)
824 {
825 out << "#define GL_USES_FRAG_COLOR\n";
826 }
827
828 if (mUsesFragData)
829 {
830 out << "#define GL_USES_FRAG_DATA\n";
831 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000832 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000833 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000834 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000835 out << "// Attributes\n";
836 out << attributes;
837 out << "\n"
838 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
839
840 if (mUsesPointSize)
841 {
842 out << "static float gl_PointSize = float(1);\n";
843 }
844
845 out << "\n"
846 "// Varyings\n";
847 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000848 out << "\n";
849
850 if (mUsesDepthRange)
851 {
852 out << "struct gl_DepthRangeParameters\n"
853 "{\n"
854 " float near;\n"
855 " float far;\n"
856 " float diff;\n"
857 "};\n"
858 "\n";
859 }
860
861 if (mOutputType == SH_HLSL11_OUTPUT)
862 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000863 if (mUsesDepthRange)
864 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000865 out << "cbuffer DriverConstants : register(b1)\n"
866 "{\n"
867 " float3 dx_DepthRange : packoffset(c0);\n"
868 "};\n"
869 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000870 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000871 }
872 else
873 {
874 if (mUsesDepthRange)
875 {
876 out << "uniform float3 dx_DepthRange : register(c0);\n";
877 }
878
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000879 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000880 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000881 }
882
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000883 if (mUsesDepthRange)
884 {
885 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
886 "\n";
887 }
888
889 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000890 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000891
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000892 if (!interfaceBlocks.empty())
893 {
894 out << interfaceBlocks;
895 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400896
897 if (!flaggedStructs.empty())
898 {
899 out << "// Std140 Structures accessed by value\n";
900 out << "\n";
901 out << flaggedStructs;
902 out << "\n";
903 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000904 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400905 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000906
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400907 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
908 {
909 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400910 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000911 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400912 switch(textureFunction->sampler)
913 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400914 case EbtSampler2D: out << "int2 "; break;
915 case EbtSampler3D: out << "int3 "; break;
916 case EbtSamplerCube: out << "int2 "; break;
917 case EbtSampler2DArray: out << "int3 "; break;
918 case EbtISampler2D: out << "int2 "; break;
919 case EbtISampler3D: out << "int3 "; break;
920 case EbtISamplerCube: out << "int2 "; break;
921 case EbtISampler2DArray: out << "int3 "; break;
922 case EbtUSampler2D: out << "int2 "; break;
923 case EbtUSampler3D: out << "int3 "; break;
924 case EbtUSamplerCube: out << "int2 "; break;
925 case EbtUSampler2DArray: out << "int3 "; break;
926 case EbtSampler2DShadow: out << "int2 "; break;
927 case EbtSamplerCubeShadow: out << "int2 "; break;
928 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400929 default: UNREACHABLE();
930 }
931 }
932 else // Sampling function
933 {
934 switch(textureFunction->sampler)
935 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400936 case EbtSampler2D: out << "float4 "; break;
937 case EbtSampler3D: out << "float4 "; break;
938 case EbtSamplerCube: out << "float4 "; break;
939 case EbtSampler2DArray: out << "float4 "; break;
940 case EbtISampler2D: out << "int4 "; break;
941 case EbtISampler3D: out << "int4 "; break;
942 case EbtISamplerCube: out << "int4 "; break;
943 case EbtISampler2DArray: out << "int4 "; break;
944 case EbtUSampler2D: out << "uint4 "; break;
945 case EbtUSampler3D: out << "uint4 "; break;
946 case EbtUSamplerCube: out << "uint4 "; break;
947 case EbtUSampler2DArray: out << "uint4 "; break;
948 case EbtSampler2DShadow: out << "float "; break;
949 case EbtSamplerCubeShadow: out << "float "; break;
950 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400951 default: UNREACHABLE();
952 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000953 }
954
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400955 // Function name
956 out << textureFunction->name();
957
958 // Argument list
959 int hlslCoords = 4;
960
961 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000962 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400963 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000964 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400965 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
966 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
967 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000968 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400969
Nicolas Capens75fb4752013-07-10 15:14:47 -0400970 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000971 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400972 case TextureFunction::IMPLICIT: break;
973 case TextureFunction::BIAS: hlslCoords = 4; break;
974 case TextureFunction::LOD: hlslCoords = 4; break;
975 case TextureFunction::LOD0: hlslCoords = 4; break;
976 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000977 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400978 }
979 else if (mOutputType == SH_HLSL11_OUTPUT)
980 {
981 switch(textureFunction->sampler)
982 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400983 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
984 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
985 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
986 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
987 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
988 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
989 case EbtISamplerCube: out << "TextureCube<int4> x, SamplerState s"; hlslCoords = 3; break;
990 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
991 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
992 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
993 case EbtUSamplerCube: out << "TextureCube<uint4> x, SamplerState s"; hlslCoords = 3; break;
994 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
995 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
996 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
997 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400998 default: UNREACHABLE();
999 }
1000 }
1001 else UNREACHABLE();
1002
1003 switch(textureFunction->coords)
1004 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001005 case 1: out << ", int lod"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001006 case 2: out << ", float2 t"; break;
1007 case 3: out << ", float3 t"; break;
1008 case 4: out << ", float4 t"; break;
1009 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +00001010 }
1011
Nicolas Capens75fb4752013-07-10 15:14:47 -04001012 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +00001013 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001014 case TextureFunction::IMPLICIT: break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001015 case TextureFunction::BIAS: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001016 case TextureFunction::LOD: out << ", float lod"; break;
1017 case TextureFunction::LOD0: break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001018 case TextureFunction::SIZE: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001019 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +00001020 }
1021
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001022 if (textureFunction->offset)
1023 {
1024 switch(textureFunction->sampler)
1025 {
1026 case EbtSampler2D: out << ", int2 offset"; break;
1027 case EbtSampler3D: out << ", int3 offset"; break;
1028 case EbtSampler2DArray: out << ", int2 offset"; break;
1029 case EbtISampler2D: out << ", int2 offset"; break;
1030 case EbtISampler3D: out << ", int3 offset"; break;
1031 case EbtISampler2DArray: out << ", int2 offset"; break;
1032 case EbtUSampler2D: out << ", int2 offset"; break;
1033 case EbtUSampler3D: out << ", int3 offset"; break;
1034 case EbtUSampler2DArray: out << ", int2 offset"; break;
1035 case EbtSampler2DShadow: out << ", int2 offset"; break;
1036 default: UNREACHABLE();
1037 }
1038 }
1039
1040 if (textureFunction->method == TextureFunction::BIAS)
1041 {
1042 out << ", float bias";
1043 }
1044
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001045 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001046 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001047
Nicolas Capens75fb4752013-07-10 15:14:47 -04001048 if (textureFunction->method == TextureFunction::SIZE)
1049 {
1050 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
1051 {
1052 if (IsSamplerArray(textureFunction->sampler))
1053 {
1054 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
1055 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
1056 }
1057 else
1058 {
1059 out << " uint width; uint height; uint numberOfLevels;\n"
1060 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
1061 }
1062 }
1063 else if (IsSampler3D(textureFunction->sampler))
1064 {
1065 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
1066 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
1067 }
1068 else UNREACHABLE();
1069
1070 switch(textureFunction->sampler)
1071 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04001072 case EbtSampler2D: out << " return int2(width, height);"; break;
1073 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
1074 case EbtSamplerCube: out << " return int2(width, height);"; break;
1075 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
1076 case EbtISampler2D: out << " return int2(width, height);"; break;
1077 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
1078 case EbtISamplerCube: out << " return int2(width, height);"; break;
1079 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
1080 case EbtUSampler2D: out << " return int2(width, height);"; break;
1081 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
1082 case EbtUSamplerCube: out << " return int2(width, height);"; break;
1083 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
1084 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
1085 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
1086 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001087 default: UNREACHABLE();
1088 }
1089 }
1090 else if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
daniel@transgaming.com15795192011-05-11 15:36:20 +00001091 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001092 // Currently unsupported because TextureCube does not support Load
1093 // This will require emulation using a Texture2DArray with 6 faces
1094 if (textureFunction->sampler == EbtISamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001095 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001096 out << " return int4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001097 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001098 else if (textureFunction->sampler == EbtUSamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001099 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001100 out << " return uint4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001101 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001102 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001103 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001104 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001105 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001106 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001107 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001108 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001109 {
Nicolas Capens93e50de2013-07-09 13:46:28 -04001110 if (IsSamplerArray(textureFunction->sampler))
1111 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001112 out << " float width; float height; float layers; float levels;\n";
1113
1114 if (textureFunction->method == TextureFunction::LOD0)
1115 {
1116 out << " uint mip = 0;\n";
1117 }
1118 else
1119 {
1120 if (textureFunction->method == TextureFunction::IMPLICIT ||
1121 textureFunction->method == TextureFunction::BIAS)
1122 {
1123 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1124 " float2 tSized = float2(t.x * width, t.y * height);\n"
1125 " float dx = length(ddx(tSized));\n"
1126 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001127 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001128
1129 if (textureFunction->method == TextureFunction::BIAS)
1130 {
1131 out << " lod += bias;\n";
1132 }
1133 }
1134
1135 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1136 }
1137
1138 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001139 }
1140 else
1141 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001142 out << " float width; float height; float levels;\n";
1143
1144 if (textureFunction->method == TextureFunction::LOD0)
1145 {
1146 out << " uint mip = 0;\n";
1147 }
1148 else
1149 {
1150 if (textureFunction->method == TextureFunction::IMPLICIT ||
1151 textureFunction->method == TextureFunction::BIAS)
1152 {
1153 out << " x.GetDimensions(0, width, height, levels);\n"
1154 " float2 tSized = float2(t.x * width, t.y * height);\n"
1155 " float dx = length(ddx(tSized));\n"
1156 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001157 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001158
1159 if (textureFunction->method == TextureFunction::BIAS)
1160 {
1161 out << " lod += bias;\n";
1162 }
1163 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05001164 else if (textureFunction->method == TextureFunction::LOD)
1165 {
1166 out << " x.GetDimensions(0, width, height, levels);\n";
1167 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001168
1169 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1170 }
1171
1172 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001173 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001174 }
1175 else if (IsSampler3D(textureFunction->sampler))
1176 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001177 out << " float width; float height; float depth; float levels;\n";
1178
1179 if (textureFunction->method == TextureFunction::LOD0)
1180 {
1181 out << " uint mip = 0;\n";
1182 }
1183 else
1184 {
1185 if (textureFunction->method == TextureFunction::IMPLICIT ||
1186 textureFunction->method == TextureFunction::BIAS)
1187 {
1188 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1189 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
1190 " float dx = length(ddx(tSized));\n"
1191 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001192 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001193
1194 if (textureFunction->method == TextureFunction::BIAS)
1195 {
1196 out << " lod += bias;\n";
1197 }
1198 }
1199
1200 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1201 }
1202
1203 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001204 }
1205 else UNREACHABLE();
1206 }
1207
1208 out << " return ";
1209
1210 // HLSL intrinsic
1211 if (mOutputType == SH_HLSL9_OUTPUT)
1212 {
1213 switch(textureFunction->sampler)
1214 {
1215 case EbtSampler2D: out << "tex2D"; break;
1216 case EbtSamplerCube: out << "texCUBE"; break;
1217 default: UNREACHABLE();
1218 }
1219
Nicolas Capens75fb4752013-07-10 15:14:47 -04001220 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001221 {
1222 case TextureFunction::IMPLICIT: out << "(s, "; break;
1223 case TextureFunction::BIAS: out << "bias(s, "; break;
1224 case TextureFunction::LOD: out << "lod(s, "; break;
1225 case TextureFunction::LOD0: out << "lod(s, "; break;
1226 default: UNREACHABLE();
1227 }
1228 }
1229 else if (mOutputType == SH_HLSL11_OUTPUT)
1230 {
1231 if (IsIntegerSampler(textureFunction->sampler))
1232 {
1233 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001234 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001235 else if (IsShadowSampler(textureFunction->sampler))
1236 {
1237 out << "x.SampleCmp(s, ";
1238 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001239 else
1240 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001241 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001242 {
1243 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1244 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1245 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1246 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
1247 default: UNREACHABLE();
1248 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001249 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001250 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001251 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001252
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001253 // Integer sampling requires integer addresses
1254 TString addressx = "";
1255 TString addressy = "";
1256 TString addressz = "";
1257 TString close = "";
1258
1259 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001260 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001261 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001262 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001263 case 2: out << "int3("; break;
1264 case 3: out << "int4("; break;
1265 default: UNREACHABLE();
1266 }
1267
Nicolas Capensc98406a2013-07-10 14:52:44 -04001268 addressx = "int(floor(width * frac((";
1269 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001270
1271 if (IsSamplerArray(textureFunction->sampler))
1272 {
1273 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1274 }
1275 else
1276 {
Nicolas Capensc98406a2013-07-10 14:52:44 -04001277 addressz = "int(floor(depth * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001278 }
1279
1280 close = "))))";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001281 }
1282 else
1283 {
1284 switch(hlslCoords)
1285 {
1286 case 2: out << "float2("; break;
1287 case 3: out << "float3("; break;
1288 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001289 default: UNREACHABLE();
1290 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001291 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001292
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001293 TString proj = ""; // Only used for projected textures
1294
1295 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001296 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001297 switch(textureFunction->coords)
1298 {
1299 case 3: proj = " / t.z"; break;
1300 case 4: proj = " / t.w"; break;
1301 default: UNREACHABLE();
1302 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001303 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001304
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001305 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001306
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001307 if (mOutputType == SH_HLSL9_OUTPUT)
1308 {
1309 if (hlslCoords >= 3)
1310 {
1311 if (textureFunction->coords < 3)
1312 {
1313 out << ", 0";
1314 }
1315 else
1316 {
1317 out << ", t.z" + proj;
1318 }
1319 }
1320
1321 if (hlslCoords == 4)
1322 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001323 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001324 {
1325 case TextureFunction::BIAS: out << ", bias"; break;
1326 case TextureFunction::LOD: out << ", lod"; break;
1327 case TextureFunction::LOD0: out << ", 0"; break;
1328 default: UNREACHABLE();
1329 }
1330 }
1331
1332 out << "));\n";
1333 }
1334 else if (mOutputType == SH_HLSL11_OUTPUT)
1335 {
1336 if (hlslCoords >= 3)
1337 {
1338 out << ", " + addressz + ("t.z" + proj) + close;
1339 }
1340
Nicolas Capenscb127d32013-07-15 17:26:18 -04001341 if (IsIntegerSampler(textureFunction->sampler))
1342 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001343 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001344 }
1345 else if (IsShadowSampler(textureFunction->sampler))
1346 {
1347 // Compare value
1348 switch(textureFunction->coords)
1349 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001350 case 3: out << "), t.z"; break;
1351 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001352 default: UNREACHABLE();
1353 }
1354 }
1355 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001356 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001357 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001358 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001359 case TextureFunction::IMPLICIT: out << ")"; break;
1360 case TextureFunction::BIAS: out << "), bias"; break;
1361 case TextureFunction::LOD: out << "), lod"; break;
1362 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001363 default: UNREACHABLE();
1364 }
1365 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001366
1367 if (textureFunction->offset)
1368 {
1369 out << ", offset";
1370 }
1371
1372 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001373 }
1374 else UNREACHABLE();
1375 }
1376
1377 out << "\n"
1378 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001379 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001380 }
1381
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001382 if (mUsesFragCoord)
1383 {
1384 out << "#define GL_USES_FRAG_COORD\n";
1385 }
1386
1387 if (mUsesPointCoord)
1388 {
1389 out << "#define GL_USES_POINT_COORD\n";
1390 }
1391
1392 if (mUsesFrontFacing)
1393 {
1394 out << "#define GL_USES_FRONT_FACING\n";
1395 }
1396
1397 if (mUsesPointSize)
1398 {
1399 out << "#define GL_USES_POINT_SIZE\n";
1400 }
1401
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001402 if (mUsesFragDepth)
1403 {
1404 out << "#define GL_USES_FRAG_DEPTH\n";
1405 }
1406
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001407 if (mUsesDepthRange)
1408 {
1409 out << "#define GL_USES_DEPTH_RANGE\n";
1410 }
1411
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001412 if (mUsesXor)
1413 {
1414 out << "bool xor(bool p, bool q)\n"
1415 "{\n"
1416 " return (p || q) && !(p && q);\n"
1417 "}\n"
1418 "\n";
1419 }
1420
1421 if (mUsesMod1)
1422 {
1423 out << "float mod(float x, float y)\n"
1424 "{\n"
1425 " return x - y * floor(x / y);\n"
1426 "}\n"
1427 "\n";
1428 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001429
1430 if (mUsesMod2v)
1431 {
1432 out << "float2 mod(float2 x, float2 y)\n"
1433 "{\n"
1434 " return x - y * floor(x / y);\n"
1435 "}\n"
1436 "\n";
1437 }
1438
1439 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001440 {
1441 out << "float2 mod(float2 x, float y)\n"
1442 "{\n"
1443 " return x - y * floor(x / y);\n"
1444 "}\n"
1445 "\n";
1446 }
1447
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001448 if (mUsesMod3v)
1449 {
1450 out << "float3 mod(float3 x, float3 y)\n"
1451 "{\n"
1452 " return x - y * floor(x / y);\n"
1453 "}\n"
1454 "\n";
1455 }
1456
1457 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001458 {
1459 out << "float3 mod(float3 x, float y)\n"
1460 "{\n"
1461 " return x - y * floor(x / y);\n"
1462 "}\n"
1463 "\n";
1464 }
1465
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001466 if (mUsesMod4v)
1467 {
1468 out << "float4 mod(float4 x, float4 y)\n"
1469 "{\n"
1470 " return x - y * floor(x / y);\n"
1471 "}\n"
1472 "\n";
1473 }
1474
1475 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001476 {
1477 out << "float4 mod(float4 x, float y)\n"
1478 "{\n"
1479 " return x - y * floor(x / y);\n"
1480 "}\n"
1481 "\n";
1482 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001483
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001484 if (mUsesFaceforward1)
1485 {
1486 out << "float faceforward(float N, float I, float Nref)\n"
1487 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001488 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001489 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001490 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001491 " }\n"
1492 " else\n"
1493 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001494 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001495 " }\n"
1496 "}\n"
1497 "\n";
1498 }
1499
1500 if (mUsesFaceforward2)
1501 {
1502 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1503 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001504 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001505 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001506 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001507 " }\n"
1508 " else\n"
1509 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001510 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001511 " }\n"
1512 "}\n"
1513 "\n";
1514 }
1515
1516 if (mUsesFaceforward3)
1517 {
1518 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1519 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001520 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001521 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001522 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001523 " }\n"
1524 " else\n"
1525 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001526 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001527 " }\n"
1528 "}\n"
1529 "\n";
1530 }
1531
1532 if (mUsesFaceforward4)
1533 {
1534 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1535 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001536 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001537 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001538 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001539 " }\n"
1540 " else\n"
1541 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001542 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001543 " }\n"
1544 "}\n"
1545 "\n";
1546 }
1547
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001548 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001549 {
1550 out << "float atanyx(float y, float x)\n"
1551 "{\n"
1552 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1553 " return atan2(y, x);\n"
1554 "}\n";
1555 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001556
1557 if (mUsesAtan2_2)
1558 {
1559 out << "float2 atanyx(float2 y, float2 x)\n"
1560 "{\n"
1561 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1562 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1563 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1564 "}\n";
1565 }
1566
1567 if (mUsesAtan2_3)
1568 {
1569 out << "float3 atanyx(float3 y, float3 x)\n"
1570 "{\n"
1571 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1572 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1573 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1574 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1575 "}\n";
1576 }
1577
1578 if (mUsesAtan2_4)
1579 {
1580 out << "float4 atanyx(float4 y, float4 x)\n"
1581 "{\n"
1582 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1583 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1584 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1585 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1586 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1587 "}\n";
1588 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001589}
1590
1591void OutputHLSL::visitSymbol(TIntermSymbol *node)
1592{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001593 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001594
Jamie Madill570e04d2013-06-21 09:15:33 -04001595 // Handle accessing std140 structs by value
1596 if (mFlaggedStructMappedNames.count(node) > 0)
1597 {
1598 out << mFlaggedStructMappedNames[node];
1599 return;
1600 }
1601
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001602 TString name = node->getSymbol();
1603
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001604 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001605 {
1606 mUsesDepthRange = true;
1607 out << name;
1608 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001609 else
1610 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001611 TQualifier qualifier = node->getQualifier();
1612
1613 if (qualifier == EvqUniform)
1614 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001615 const TType& nodeType = node->getType();
1616 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1617
1618 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001619 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001620 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001621 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001622 else
1623 {
1624 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001625 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001626
1627 out << decorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001628 }
Jamie Madill19571812013-08-12 15:26:34 -07001629 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001630 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001631 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001632 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001633 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001634 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001635 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001636 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001637 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001638 }
Jamie Madill19571812013-08-12 15:26:34 -07001639 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001640 {
1641 mReferencedOutputVariables[name] = node;
1642 out << "out_" << name;
1643 }
1644 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001645 {
1646 out << "gl_Color[0]";
1647 mUsesFragColor = true;
1648 }
1649 else if (qualifier == EvqFragData)
1650 {
1651 out << "gl_Color";
1652 mUsesFragData = true;
1653 }
1654 else if (qualifier == EvqFragCoord)
1655 {
1656 mUsesFragCoord = true;
1657 out << name;
1658 }
1659 else if (qualifier == EvqPointCoord)
1660 {
1661 mUsesPointCoord = true;
1662 out << name;
1663 }
1664 else if (qualifier == EvqFrontFacing)
1665 {
1666 mUsesFrontFacing = true;
1667 out << name;
1668 }
1669 else if (qualifier == EvqPointSize)
1670 {
1671 mUsesPointSize = true;
1672 out << name;
1673 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001674 else if (name == "gl_FragDepthEXT")
1675 {
1676 mUsesFragDepth = true;
1677 out << "gl_Depth";
1678 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001679 else if (qualifier == EvqInternal)
1680 {
1681 out << name;
1682 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001683 else
1684 {
1685 out << decorate(name);
1686 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001687 }
1688}
1689
1690bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1691{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001692 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001693
Jamie Madill570e04d2013-06-21 09:15:33 -04001694 // Handle accessing std140 structs by value
1695 if (mFlaggedStructMappedNames.count(node) > 0)
1696 {
1697 out << mFlaggedStructMappedNames[node];
1698 return false;
1699 }
1700
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001701 switch (node->getOp())
1702 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001703 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001704 case EOpInitialize:
1705 if (visit == PreVisit)
1706 {
1707 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1708 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1709 // new variable is created before the assignment is evaluated), so we need to convert
1710 // this to "float t = x, x = t;".
1711
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001712 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1713 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001714
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001715 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1716 expression->traverse(&searchSymbol);
1717 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001718
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001719 if (sameSymbol)
1720 {
1721 // Type already printed
1722 out << "t" + str(mUniqueIndex) + " = ";
1723 expression->traverse(this);
1724 out << ", ";
1725 symbolNode->traverse(this);
1726 out << " = t" + str(mUniqueIndex);
1727
1728 mUniqueIndex++;
1729 return false;
1730 }
1731 }
1732 else if (visit == InVisit)
1733 {
1734 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001735 }
1736 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001737 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1738 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1739 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1740 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1741 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1742 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001743 if (visit == PreVisit)
1744 {
1745 out << "(";
1746 }
1747 else if (visit == InVisit)
1748 {
1749 out << " = mul(";
1750 node->getLeft()->traverse(this);
1751 out << ", transpose(";
1752 }
1753 else
1754 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001755 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001756 }
1757 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001758 case EOpMatrixTimesMatrixAssign:
1759 if (visit == PreVisit)
1760 {
1761 out << "(";
1762 }
1763 else if (visit == InVisit)
1764 {
1765 out << " = mul(";
1766 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001767 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001768 }
1769 else
1770 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001771 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001772 }
1773 break;
1774 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001775 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001776 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001777 const TType& leftType = node->getLeft()->getType();
1778 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001779 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001780 if (visit == PreVisit)
1781 {
1782 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1783 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1784
1785 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1786 out << interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1787
1788 return false;
1789 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001790 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001791 else
1792 {
1793 outputTriplet(visit, "", "[", "]");
1794 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001795 }
1796 break;
1797 case EOpIndexIndirect:
1798 // We do not currently support indirect references to interface blocks
1799 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1800 outputTriplet(visit, "", "[", "]");
1801 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001802 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001803 if (visit == InVisit)
1804 {
1805 const TStructure* structure = node->getLeft()->getType().getStruct();
1806 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1807 const TField* field = structure->fields()[index->getIConst(0)];
1808 out << "." + decorateField(field->name(), *structure);
1809
1810 return false;
1811 }
1812 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001813 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001814 if (visit == InVisit)
1815 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001816 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1817 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1818 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
1819 out << "." + decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001820
1821 return false;
1822 }
1823 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001824 case EOpVectorSwizzle:
1825 if (visit == InVisit)
1826 {
1827 out << ".";
1828
1829 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1830
1831 if (swizzle)
1832 {
1833 TIntermSequence &sequence = swizzle->getSequence();
1834
1835 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1836 {
1837 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1838
1839 if (element)
1840 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001841 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001842
1843 switch (i)
1844 {
1845 case 0: out << "x"; break;
1846 case 1: out << "y"; break;
1847 case 2: out << "z"; break;
1848 case 3: out << "w"; break;
1849 default: UNREACHABLE();
1850 }
1851 }
1852 else UNREACHABLE();
1853 }
1854 }
1855 else UNREACHABLE();
1856
1857 return false; // Fully processed
1858 }
1859 break;
1860 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1861 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1862 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1863 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001864 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001865 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001866 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001867 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001868 if (node->getOp() == EOpEqual)
1869 {
1870 outputTriplet(visit, "(", " == ", ")");
1871 }
1872 else
1873 {
1874 outputTriplet(visit, "(", " != ", ")");
1875 }
1876 }
1877 else if (node->getLeft()->getBasicType() == EbtStruct)
1878 {
1879 if (node->getOp() == EOpEqual)
1880 {
1881 out << "(";
1882 }
1883 else
1884 {
1885 out << "!(";
1886 }
1887
Jamie Madill98493dd2013-07-08 14:39:03 -04001888 const TStructure &structure = *node->getLeft()->getType().getStruct();
1889 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001890
Jamie Madill98493dd2013-07-08 14:39:03 -04001891 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001892 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001893 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001894
1895 node->getLeft()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001896 out << "." + decorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001897 node->getRight()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001898 out << "." + decorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001899
Jamie Madill98493dd2013-07-08 14:39:03 -04001900 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001901 {
1902 out << " && ";
1903 }
1904 }
1905
1906 out << ")";
1907
1908 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001909 }
1910 else
1911 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001912 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001913
1914 if (node->getOp() == EOpEqual)
1915 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001916 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001917 }
1918 else
1919 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001920 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001921 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001922 }
1923 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001924 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1925 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1926 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1927 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1928 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001929 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001930 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1931 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001932 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001933 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001934 if (node->getRight()->hasSideEffects())
1935 {
1936 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1937 return false;
1938 }
1939 else
1940 {
1941 outputTriplet(visit, "(", " || ", ")");
1942 return true;
1943 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001944 case EOpLogicalXor:
1945 mUsesXor = true;
1946 outputTriplet(visit, "xor(", ", ", ")");
1947 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001948 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001949 if (node->getRight()->hasSideEffects())
1950 {
1951 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1952 return false;
1953 }
1954 else
1955 {
1956 outputTriplet(visit, "(", " && ", ")");
1957 return true;
1958 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001959 default: UNREACHABLE();
1960 }
1961
1962 return true;
1963}
1964
1965bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1966{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001967 switch (node->getOp())
1968 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001969 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1970 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1971 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1972 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1973 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1974 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1975 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001976 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04001977 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001978 case EOpConvFloatToBool:
1979 switch (node->getOperand()->getType().getNominalSize())
1980 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001981 case 1: outputTriplet(visit, "bool(", "", ")"); break;
1982 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
1983 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
1984 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001985 default: UNREACHABLE();
1986 }
1987 break;
1988 case EOpConvBoolToFloat:
1989 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04001990 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001991 switch (node->getOperand()->getType().getNominalSize())
1992 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001993 case 1: outputTriplet(visit, "float(", "", ")"); break;
1994 case 2: outputTriplet(visit, "float2(", "", ")"); break;
1995 case 3: outputTriplet(visit, "float3(", "", ")"); break;
1996 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001997 default: UNREACHABLE();
1998 }
1999 break;
2000 case EOpConvFloatToInt:
2001 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04002002 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002003 switch (node->getOperand()->getType().getNominalSize())
2004 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002005 case 1: outputTriplet(visit, "int(", "", ")"); break;
2006 case 2: outputTriplet(visit, "int2(", "", ")"); break;
2007 case 3: outputTriplet(visit, "int3(", "", ")"); break;
2008 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002009 default: UNREACHABLE();
2010 }
2011 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002012 case EOpConvFloatToUInt:
2013 case EOpConvBoolToUInt:
2014 case EOpConvIntToUInt:
Jamie Madilla16f1672013-07-03 15:15:17 -04002015 switch (node->getOperand()->getType().getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002016 {
2017 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002018 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
2019 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
2020 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002021 default: UNREACHABLE();
2022 }
2023 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002024 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
2025 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
2026 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
2027 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
2028 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
2029 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
2030 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
2031 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
2032 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
2033 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
2034 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
2035 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
2036 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
2037 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
2038 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
2039 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
2040 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
2041 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
2042 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
2043 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
2044 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002045 case EOpDFdx:
2046 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2047 {
2048 outputTriplet(visit, "(", "", ", 0.0)");
2049 }
2050 else
2051 {
2052 outputTriplet(visit, "ddx(", "", ")");
2053 }
2054 break;
2055 case EOpDFdy:
2056 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2057 {
2058 outputTriplet(visit, "(", "", ", 0.0)");
2059 }
2060 else
2061 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00002062 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002063 }
2064 break;
2065 case EOpFwidth:
2066 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2067 {
2068 outputTriplet(visit, "(", "", ", 0.0)");
2069 }
2070 else
2071 {
2072 outputTriplet(visit, "fwidth(", "", ")");
2073 }
2074 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002075 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
2076 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002077 default: UNREACHABLE();
2078 }
2079
2080 return true;
2081}
2082
2083bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2084{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002085 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002086
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002087 switch (node->getOp())
2088 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002089 case EOpSequence:
2090 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002091 if (mInsideFunction)
2092 {
Jamie Madill075edd82013-07-08 13:30:19 -04002093 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002094 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002095
2096 mScopeDepth++;
2097
2098 if (mScopeBracket.size() < mScopeDepth)
2099 {
2100 mScopeBracket.push_back(0); // New scope level
2101 }
2102 else
2103 {
2104 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
2105 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002106 }
2107
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002108 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
2109 {
Jamie Madill075edd82013-07-08 13:30:19 -04002110 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002111
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002112 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002113
2114 out << ";\n";
2115 }
2116
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002117 if (mInsideFunction)
2118 {
Jamie Madill075edd82013-07-08 13:30:19 -04002119 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002120 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002121
2122 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002123 }
2124
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002125 return false;
2126 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002127 case EOpDeclaration:
2128 if (visit == PreVisit)
2129 {
2130 TIntermSequence &sequence = node->getSequence();
2131 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002132
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00002133 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002134 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002135 if (variable->getType().getStruct())
2136 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002137 addConstructor(variable->getType(), scopedStruct(variable->getType().getStruct()->name()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00002138 }
2139
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002140 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002141 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00002142 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002143 {
2144 out << "static ";
2145 }
2146
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002147 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002148
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002149 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002150 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002151 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002152
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002153 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002154 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002155 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002156 out << arrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05002157 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002158 }
2159 else
2160 {
2161 (*sit)->traverse(this);
2162 }
2163
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002164 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002165 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002166 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002167 }
2168 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002169 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002170 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2171 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002172 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002173 }
2174 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002175 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002176 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002177 {
2178 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2179 {
2180 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2181
2182 if (symbol)
2183 {
2184 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2185 mReferencedVaryings[symbol->getSymbol()] = symbol;
2186 }
2187 else
2188 {
2189 (*sit)->traverse(this);
2190 }
2191 }
2192 }
2193
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002194 return false;
2195 }
2196 else if (visit == InVisit)
2197 {
2198 out << ", ";
2199 }
2200 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002201 case EOpPrototype:
2202 if (visit == PreVisit)
2203 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002204 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002205
2206 TIntermSequence &arguments = node->getSequence();
2207
2208 for (unsigned int i = 0; i < arguments.size(); i++)
2209 {
2210 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2211
2212 if (symbol)
2213 {
2214 out << argumentString(symbol);
2215
2216 if (i < arguments.size() - 1)
2217 {
2218 out << ", ";
2219 }
2220 }
2221 else UNREACHABLE();
2222 }
2223
2224 out << ");\n";
2225
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002226 // Also prototype the Lod0 variant if needed
2227 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2228 {
2229 mOutputLod0Function = true;
2230 node->traverse(this);
2231 mOutputLod0Function = false;
2232 }
2233
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002234 return false;
2235 }
2236 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002237 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002238 case EOpFunction:
2239 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002240 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002241
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002242 out << typeString(node->getType()) << " ";
2243
2244 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002245 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002246 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002247 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002248 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002249 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002250 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002251 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002252
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002253 TIntermSequence &sequence = node->getSequence();
2254 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2255
2256 for (unsigned int i = 0; i < arguments.size(); i++)
2257 {
2258 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2259
2260 if (symbol)
2261 {
2262 if (symbol->getType().getStruct())
2263 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002264 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getStruct()->name()), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002265 }
2266
2267 out << argumentString(symbol);
2268
2269 if (i < arguments.size() - 1)
2270 {
2271 out << ", ";
2272 }
2273 }
2274 else UNREACHABLE();
2275 }
2276
2277 out << ")\n"
2278 "{\n";
2279
2280 if (sequence.size() > 1)
2281 {
2282 mInsideFunction = true;
2283 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002284 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002285 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002286
2287 out << "}\n";
2288
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002289 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2290 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002291 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002292 {
2293 mOutputLod0Function = true;
2294 node->traverse(this);
2295 mOutputLod0Function = false;
2296 }
2297 }
2298
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002299 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002300 }
2301 break;
2302 case EOpFunctionCall:
2303 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002304 TString name = TFunction::unmangleName(node->getName());
2305 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002306 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002307
2308 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002309 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002310 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002311 }
2312 else
2313 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002314 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2315
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002316 TextureFunction textureFunction;
2317 textureFunction.sampler = samplerType;
2318 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002319 textureFunction.method = TextureFunction::IMPLICIT;
2320 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002321 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002322
2323 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002324 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002325 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002326 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002327 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002328 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002329 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002330 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002331 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002332 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002333 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002334 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002335 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002336 else if (name == "texture2DProjLod" || name == "textureProjLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002337 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002338 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002339 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002340 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002341 else if (name == "textureSize")
2342 {
2343 textureFunction.method = TextureFunction::SIZE;
2344 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002345 else if (name == "textureOffset")
2346 {
2347 textureFunction.method = TextureFunction::IMPLICIT;
2348 textureFunction.offset = true;
2349 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002350 else if (name == "textureProjOffset")
2351 {
2352 textureFunction.method = TextureFunction::IMPLICIT;
2353 textureFunction.offset = true;
2354 textureFunction.proj = true;
2355 }
2356 else if (name == "textureLodOffset")
2357 {
2358 textureFunction.method = TextureFunction::LOD;
2359 textureFunction.offset = true;
2360 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002361 else if (name == "textureProjLod")
2362 {
2363 textureFunction.method = TextureFunction::LOD;
2364 textureFunction.proj = true;
2365 }
2366 else if (name == "textureProjLodOffset")
2367 {
2368 textureFunction.method = TextureFunction::LOD;
2369 textureFunction.proj = true;
2370 textureFunction.offset = true;
2371 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002372 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002373
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002374 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002375 {
2376 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2377 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002378 textureFunction.method = TextureFunction::LOD0;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002379 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002380 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002381 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002382 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2383
2384 if (textureFunction.offset)
2385 {
2386 mandatoryArgumentCount++;
2387 }
2388
2389 if (arguments.size() > mandatoryArgumentCount) // Bias argument is optional
2390 {
2391 textureFunction.method = TextureFunction::BIAS;
2392 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002393 }
2394 }
2395
2396 mUsesTexture.insert(textureFunction);
2397
2398 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002399 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002400
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002401 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2402 {
2403 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2404 {
2405 out << "texture_";
2406 (*arg)->traverse(this);
2407 out << ", sampler_";
2408 }
2409
2410 (*arg)->traverse(this);
2411
2412 if (arg < arguments.end() - 1)
2413 {
2414 out << ", ";
2415 }
2416 }
2417
2418 out << ")";
2419
2420 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002421 }
2422 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002423 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002424 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002425 addConstructor(node->getType(), "vec1", &node->getSequence());
2426 outputTriplet(visit, "vec1(", "", ")");
2427 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002428 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002429 addConstructor(node->getType(), "vec2", &node->getSequence());
2430 outputTriplet(visit, "vec2(", ", ", ")");
2431 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002432 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002433 addConstructor(node->getType(), "vec3", &node->getSequence());
2434 outputTriplet(visit, "vec3(", ", ", ")");
2435 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002436 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002437 addConstructor(node->getType(), "vec4", &node->getSequence());
2438 outputTriplet(visit, "vec4(", ", ", ")");
2439 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002440 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002441 addConstructor(node->getType(), "bvec1", &node->getSequence());
2442 outputTriplet(visit, "bvec1(", "", ")");
2443 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002444 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002445 addConstructor(node->getType(), "bvec2", &node->getSequence());
2446 outputTriplet(visit, "bvec2(", ", ", ")");
2447 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002448 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002449 addConstructor(node->getType(), "bvec3", &node->getSequence());
2450 outputTriplet(visit, "bvec3(", ", ", ")");
2451 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002452 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002453 addConstructor(node->getType(), "bvec4", &node->getSequence());
2454 outputTriplet(visit, "bvec4(", ", ", ")");
2455 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002456 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002457 addConstructor(node->getType(), "ivec1", &node->getSequence());
2458 outputTriplet(visit, "ivec1(", "", ")");
2459 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002460 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002461 addConstructor(node->getType(), "ivec2", &node->getSequence());
2462 outputTriplet(visit, "ivec2(", ", ", ")");
2463 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002464 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002465 addConstructor(node->getType(), "ivec3", &node->getSequence());
2466 outputTriplet(visit, "ivec3(", ", ", ")");
2467 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002468 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002469 addConstructor(node->getType(), "ivec4", &node->getSequence());
2470 outputTriplet(visit, "ivec4(", ", ", ")");
2471 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002472 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002473 addConstructor(node->getType(), "uvec1", &node->getSequence());
2474 outputTriplet(visit, "uvec1(", "", ")");
2475 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002476 case EOpConstructUVec2:
2477 addConstructor(node->getType(), "uvec2", &node->getSequence());
2478 outputTriplet(visit, "uvec2(", ", ", ")");
2479 break;
2480 case EOpConstructUVec3:
2481 addConstructor(node->getType(), "uvec3", &node->getSequence());
2482 outputTriplet(visit, "uvec3(", ", ", ")");
2483 break;
2484 case EOpConstructUVec4:
2485 addConstructor(node->getType(), "uvec4", &node->getSequence());
2486 outputTriplet(visit, "uvec4(", ", ", ")");
2487 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002488 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002489 addConstructor(node->getType(), "mat2", &node->getSequence());
2490 outputTriplet(visit, "mat2(", ", ", ")");
2491 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002492 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002493 addConstructor(node->getType(), "mat3", &node->getSequence());
2494 outputTriplet(visit, "mat3(", ", ", ")");
2495 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002496 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002497 addConstructor(node->getType(), "mat4", &node->getSequence());
2498 outputTriplet(visit, "mat4(", ", ", ")");
2499 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002500 case EOpConstructStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04002501 addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence());
2502 outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002503 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002504 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2505 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2506 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2507 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2508 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2509 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002510 case EOpMod:
2511 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002512 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002513 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2514 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2515 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002516 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002517 case 11: mUsesMod1 = true; break;
2518 case 22: mUsesMod2v = true; break;
2519 case 21: mUsesMod2f = true; break;
2520 case 33: mUsesMod3v = true; break;
2521 case 31: mUsesMod3f = true; break;
2522 case 44: mUsesMod4v = true; break;
2523 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002524 default: UNREACHABLE();
2525 }
2526
2527 outputTriplet(visit, "mod(", ", ", ")");
2528 }
2529 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002530 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002531 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002532 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002533 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2534 {
2535 case 1: mUsesAtan2_1 = true; break;
2536 case 2: mUsesAtan2_2 = true; break;
2537 case 3: mUsesAtan2_3 = true; break;
2538 case 4: mUsesAtan2_4 = true; break;
2539 default: UNREACHABLE();
2540 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002541 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002542 break;
2543 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2544 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2545 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2546 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2547 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2548 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2549 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2550 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2551 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002552 case EOpFaceForward:
2553 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002554 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002555 {
2556 case 1: mUsesFaceforward1 = true; break;
2557 case 2: mUsesFaceforward2 = true; break;
2558 case 3: mUsesFaceforward3 = true; break;
2559 case 4: mUsesFaceforward4 = true; break;
2560 default: UNREACHABLE();
2561 }
2562
2563 outputTriplet(visit, "faceforward(", ", ", ")");
2564 }
2565 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002566 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2567 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2568 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002569 default: UNREACHABLE();
2570 }
2571
2572 return true;
2573}
2574
2575bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2576{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002577 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002578
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002579 if (node->usesTernaryOperator())
2580 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002581 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002582 }
2583 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002584 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002585 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002586
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002587 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002588
2589 node->getCondition()->traverse(this);
2590
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002591 out << ")\n";
2592
Jamie Madill075edd82013-07-08 13:30:19 -04002593 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002594 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002595
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002596 bool discard = false;
2597
daniel@transgaming.combb885322010-04-15 20:45:24 +00002598 if (node->getTrueBlock())
2599 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002600 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002601
2602 // Detect true discard
2603 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002604 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002605
Jamie Madill075edd82013-07-08 13:30:19 -04002606 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002607 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002608
2609 if (node->getFalseBlock())
2610 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002611 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002612
Jamie Madill075edd82013-07-08 13:30:19 -04002613 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002614 out << "{\n";
2615
Jamie Madill075edd82013-07-08 13:30:19 -04002616 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002617 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002618
Jamie Madill075edd82013-07-08 13:30:19 -04002619 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002620 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002621
2622 // Detect false discard
2623 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2624 }
2625
2626 // ANGLE issue 486: Detect problematic conditional discard
2627 if (discard && FindSideEffectRewriting::search(node))
2628 {
2629 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002630 }
2631 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002632
2633 return false;
2634}
2635
2636void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2637{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002638 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002639}
2640
2641bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2642{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002643 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2644
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002645 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002646 {
2647 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2648 }
2649
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002650 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002651 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002652 if (handleExcessiveLoop(node))
2653 {
2654 return false;
2655 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002656 }
2657
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002658 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002659
alokp@chromium.org52813552010-11-16 18:36:09 +00002660 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002661 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002662 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002663
Jamie Madill075edd82013-07-08 13:30:19 -04002664 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002665 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002666 }
2667 else
2668 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002669 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002670
2671 if (node->getInit())
2672 {
2673 node->getInit()->traverse(this);
2674 }
2675
2676 out << "; ";
2677
alokp@chromium.org52813552010-11-16 18:36:09 +00002678 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002679 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002680 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002681 }
2682
2683 out << "; ";
2684
alokp@chromium.org52813552010-11-16 18:36:09 +00002685 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002686 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002687 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002688 }
2689
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002690 out << ")\n";
2691
Jamie Madill075edd82013-07-08 13:30:19 -04002692 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002693 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002694 }
2695
2696 if (node->getBody())
2697 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002698 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002699 }
2700
Jamie Madill075edd82013-07-08 13:30:19 -04002701 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002702 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002703
alokp@chromium.org52813552010-11-16 18:36:09 +00002704 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002705 {
Jamie Madill075edd82013-07-08 13:30:19 -04002706 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002707 out << "while(\n";
2708
alokp@chromium.org52813552010-11-16 18:36:09 +00002709 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002710
daniel@transgaming.com73536982012-03-21 20:45:49 +00002711 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002712 }
2713
daniel@transgaming.com73536982012-03-21 20:45:49 +00002714 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002715
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002716 mInsideDiscontinuousLoop = wasDiscontinuous;
2717
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002718 return false;
2719}
2720
2721bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2722{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002723 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002724
2725 switch (node->getFlowOp())
2726 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002727 case EOpKill:
2728 outputTriplet(visit, "discard;\n", "", "");
2729 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002730 case EOpBreak:
2731 if (visit == PreVisit)
2732 {
2733 if (mExcessiveLoopIndex)
2734 {
2735 out << "{Break";
2736 mExcessiveLoopIndex->traverse(this);
2737 out << " = true; break;}\n";
2738 }
2739 else
2740 {
2741 out << "break;\n";
2742 }
2743 }
2744 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002745 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002746 case EOpReturn:
2747 if (visit == PreVisit)
2748 {
2749 if (node->getExpression())
2750 {
2751 out << "return ";
2752 }
2753 else
2754 {
2755 out << "return;\n";
2756 }
2757 }
2758 else if (visit == PostVisit)
2759 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002760 if (node->getExpression())
2761 {
2762 out << ";\n";
2763 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002764 }
2765 break;
2766 default: UNREACHABLE();
2767 }
2768
2769 return true;
2770}
2771
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002772void OutputHLSL::traverseStatements(TIntermNode *node)
2773{
2774 if (isSingleStatement(node))
2775 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002776 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002777 }
2778
2779 node->traverse(this);
2780}
2781
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002782bool OutputHLSL::isSingleStatement(TIntermNode *node)
2783{
2784 TIntermAggregate *aggregate = node->getAsAggregate();
2785
2786 if (aggregate)
2787 {
2788 if (aggregate->getOp() == EOpSequence)
2789 {
2790 return false;
2791 }
2792 else
2793 {
2794 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2795 {
2796 if (!isSingleStatement(*sit))
2797 {
2798 return false;
2799 }
2800 }
2801
2802 return true;
2803 }
2804 }
2805
2806 return true;
2807}
2808
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002809// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2810// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002811bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2812{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002813 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002814 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002815
2816 // Parse loops of the form:
2817 // for(int index = initial; index [comparator] limit; index += increment)
2818 TIntermSymbol *index = NULL;
2819 TOperator comparator = EOpNull;
2820 int initial = 0;
2821 int limit = 0;
2822 int increment = 0;
2823
2824 // Parse index name and intial value
2825 if (node->getInit())
2826 {
2827 TIntermAggregate *init = node->getInit()->getAsAggregate();
2828
2829 if (init)
2830 {
2831 TIntermSequence &sequence = init->getSequence();
2832 TIntermTyped *variable = sequence[0]->getAsTyped();
2833
2834 if (variable && variable->getQualifier() == EvqTemporary)
2835 {
2836 TIntermBinary *assign = variable->getAsBinaryNode();
2837
2838 if (assign->getOp() == EOpInitialize)
2839 {
2840 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2841 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2842
2843 if (symbol && constant)
2844 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002845 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002846 {
2847 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002848 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002849 }
2850 }
2851 }
2852 }
2853 }
2854 }
2855
2856 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002857 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002858 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002859 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002860
2861 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2862 {
2863 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2864
2865 if (constant)
2866 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002867 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002868 {
2869 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002870 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002871 }
2872 }
2873 }
2874 }
2875
2876 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002877 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002878 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002879 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2880 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002881
2882 if (binaryTerminal)
2883 {
2884 TOperator op = binaryTerminal->getOp();
2885 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2886
2887 if (constant)
2888 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002889 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002890 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002891 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002892
2893 switch (op)
2894 {
2895 case EOpAddAssign: increment = value; break;
2896 case EOpSubAssign: increment = -value; break;
2897 default: UNIMPLEMENTED();
2898 }
2899 }
2900 }
2901 }
2902 else if (unaryTerminal)
2903 {
2904 TOperator op = unaryTerminal->getOp();
2905
2906 switch (op)
2907 {
2908 case EOpPostIncrement: increment = 1; break;
2909 case EOpPostDecrement: increment = -1; break;
2910 case EOpPreIncrement: increment = 1; break;
2911 case EOpPreDecrement: increment = -1; break;
2912 default: UNIMPLEMENTED();
2913 }
2914 }
2915 }
2916
2917 if (index != NULL && comparator != EOpNull && increment != 0)
2918 {
2919 if (comparator == EOpLessThanEqual)
2920 {
2921 comparator = EOpLessThan;
2922 limit += 1;
2923 }
2924
2925 if (comparator == EOpLessThan)
2926 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002927 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002928
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002929 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002930 {
2931 return false; // Not an excessive loop
2932 }
2933
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002934 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2935 mExcessiveLoopIndex = index;
2936
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002937 out << "{int ";
2938 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002939 out << ";\n"
2940 "bool Break";
2941 index->traverse(this);
2942 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002943
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002944 bool firstLoopFragment = true;
2945
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002946 while (iterations > 0)
2947 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002948 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002949
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002950 if (!firstLoopFragment)
2951 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002952 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002953 index->traverse(this);
2954 out << ") {\n";
2955 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002956
2957 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2958 {
2959 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2960 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002961
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002962 // for(int index = initial; index < clampedLimit; index += increment)
2963
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002964 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002965 index->traverse(this);
2966 out << " = ";
2967 out << initial;
2968
2969 out << "; ";
2970 index->traverse(this);
2971 out << " < ";
2972 out << clampedLimit;
2973
2974 out << "; ";
2975 index->traverse(this);
2976 out << " += ";
2977 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002978 out << ")\n";
2979
Jamie Madill075edd82013-07-08 13:30:19 -04002980 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002981 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002982
2983 if (node->getBody())
2984 {
2985 node->getBody()->traverse(this);
2986 }
2987
Jamie Madill075edd82013-07-08 13:30:19 -04002988 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002989 out << ";}\n";
2990
2991 if (!firstLoopFragment)
2992 {
2993 out << "}\n";
2994 }
2995
2996 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002997
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002998 initial += MAX_LOOP_ITERATIONS * increment;
2999 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003000 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003001
3002 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003003
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003004 mExcessiveLoopIndex = restoreIndex;
3005
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003006 return true;
3007 }
3008 else UNIMPLEMENTED();
3009 }
3010
3011 return false; // Not handled as an excessive loop
3012}
3013
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003014void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003015{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00003016 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003017
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003018 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003019 {
3020 out << preString;
3021 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003022 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003023 {
3024 out << inString;
3025 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003026 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003027 {
3028 out << postString;
3029 }
3030}
3031
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003032void OutputHLSL::outputLineDirective(int line)
3033{
3034 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
3035 {
baustin@google.com8ab69842011-06-02 21:53:45 +00003036 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003037 mBody << "#line " << line;
3038
3039 if (mContext.sourcePath)
3040 {
3041 mBody << " \"" << mContext.sourcePath << "\"";
3042 }
3043
3044 mBody << "\n";
3045 }
3046}
3047
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003048TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3049{
3050 TQualifier qualifier = symbol->getQualifier();
3051 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003052 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003053
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003054 if (name.empty()) // HLSL demands named arguments, also for prototypes
3055 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00003056 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003057 }
3058 else
3059 {
3060 name = decorate(name);
3061 }
3062
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003063 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
3064 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003065 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
3066 qualifierString(qualifier) + " " + samplerString(type) + " sampler_" + name + arrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003067 }
3068
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003069 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003070}
3071
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003072TString OutputHLSL::interpolationString(TQualifier qualifier)
3073{
3074 switch(qualifier)
3075 {
3076 case EvqVaryingIn: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003077 case EvqFragmentIn: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003078 case EvqInvariantVaryingIn: return "";
3079 case EvqSmoothIn: return "linear";
3080 case EvqFlatIn: return "nointerpolation";
3081 case EvqCentroidIn: return "centroid";
3082 case EvqVaryingOut: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003083 case EvqVertexOut: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003084 case EvqInvariantVaryingOut: return "";
3085 case EvqSmoothOut: return "linear";
3086 case EvqFlatOut: return "nointerpolation";
3087 case EvqCentroidOut: return "centroid";
3088 default: UNREACHABLE();
3089 }
3090
3091 return "";
3092}
3093
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003094TString OutputHLSL::qualifierString(TQualifier qualifier)
3095{
3096 switch(qualifier)
3097 {
3098 case EvqIn: return "in";
3099 case EvqOut: return "out";
3100 case EvqInOut: return "inout";
3101 case EvqConstReadOnly: return "const";
3102 default: UNREACHABLE();
3103 }
3104
3105 return "";
3106}
3107
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003108TString OutputHLSL::typeString(const TType &type)
3109{
Jamie Madill98493dd2013-07-08 14:39:03 -04003110 const TStructure* structure = type.getStruct();
3111 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003112 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003113 const TString& typeName = structure->name();
3114 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003115 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003116 return structLookup(typeName);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003117 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003118 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003119 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003120 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003121 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003122 }
3123 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003124 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003125 int cols = type.getCols();
3126 int rows = type.getRows();
3127 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003128 }
3129 else
3130 {
3131 switch (type.getBasicType())
3132 {
3133 case EbtFloat:
3134 switch (type.getNominalSize())
3135 {
3136 case 1: return "float";
3137 case 2: return "float2";
3138 case 3: return "float3";
3139 case 4: return "float4";
3140 }
3141 case EbtInt:
3142 switch (type.getNominalSize())
3143 {
3144 case 1: return "int";
3145 case 2: return "int2";
3146 case 3: return "int3";
3147 case 4: return "int4";
3148 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003149 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04003150 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003151 {
3152 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003153 case 2: return "uint2";
3154 case 3: return "uint3";
3155 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003156 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003157 case EbtBool:
3158 switch (type.getNominalSize())
3159 {
3160 case 1: return "bool";
3161 case 2: return "bool2";
3162 case 3: return "bool3";
3163 case 4: return "bool4";
3164 }
3165 case EbtVoid:
3166 return "void";
3167 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003168 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04003169 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003170 case EbtSampler2DArray:
3171 case EbtISampler2DArray:
3172 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003173 return "sampler2D";
3174 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003175 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04003176 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003177 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00003178 case EbtSamplerExternalOES:
3179 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00003180 default:
3181 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003182 }
3183 }
3184
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003185 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003186 return "<unknown type>";
3187}
3188
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003189TString OutputHLSL::textureString(const TType &type)
3190{
3191 switch (type.getBasicType())
3192 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003193 case EbtSampler2D: return "Texture2D";
3194 case EbtSamplerCube: return "TextureCube";
3195 case EbtSamplerExternalOES: return "Texture2D";
3196 case EbtSampler2DArray: return "Texture2DArray";
3197 case EbtSampler3D: return "Texture3D";
3198 case EbtISampler2D: return "Texture2D<int4>";
3199 case EbtISampler3D: return "Texture3D<int4>";
3200 case EbtISamplerCube: return "TextureCube<int4>";
3201 case EbtISampler2DArray: return "Texture2DArray<int4>";
3202 case EbtUSampler2D: return "Texture2D<uint4>";
3203 case EbtUSampler3D: return "Texture3D<uint4>";
3204 case EbtUSamplerCube: return "TextureCube<uint4>";
3205 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
3206 case EbtSampler2DShadow: return "Texture2D";
3207 case EbtSamplerCubeShadow: return "TextureCube";
3208 case EbtSampler2DArrayShadow: return "Texture2DArray";
3209 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003210 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04003211
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003212 return "<unknown texture type>";
3213}
3214
Nicolas Capenscb127d32013-07-15 17:26:18 -04003215TString OutputHLSL::samplerString(const TType &type)
3216{
3217 if (IsShadowSampler(type.getBasicType()))
3218 {
3219 return "SamplerComparisonState";
3220 }
3221 else
3222 {
3223 return "SamplerState";
3224 }
3225}
3226
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003227TString OutputHLSL::arrayString(const TType &type)
3228{
3229 if (!type.isArray())
3230 {
3231 return "";
3232 }
3233
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003234 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003235}
3236
3237TString OutputHLSL::initializer(const TType &type)
3238{
3239 TString string;
3240
Jamie Madill94bf7f22013-07-08 13:31:15 -04003241 size_t size = type.getObjectSize();
3242 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003243 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003244 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003245
Jamie Madill94bf7f22013-07-08 13:31:15 -04003246 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003247 {
3248 string += ", ";
3249 }
3250 }
3251
daniel@transgaming.comead23042010-04-29 03:35:36 +00003252 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003253}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003254
Jamie Madill98493dd2013-07-08 14:39:03 -04003255TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003256{
Jamie Madill98493dd2013-07-08 14:39:03 -04003257 const TFieldList &fields = structure.fields();
3258 const bool isNameless = (structure.name() == "");
3259 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003260 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3261
Jamie Madill98493dd2013-07-08 14:39:03 -04003262 TString string;
3263 string += declareString + "\n"
3264 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003265
Jamie Madillc835df62013-06-21 09:15:32 -04003266 int elementIndex = 0;
3267
Jamie Madill9cf6c072013-06-20 11:55:53 -04003268 for (unsigned int i = 0; i < fields.size(); i++)
3269 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003270 const TField &field = *fields[i];
3271 const TType &fieldType = *field.type();
3272 const TStructure *fieldStruct = fieldType.getStruct();
3273 const TString &fieldTypeString = fieldStruct ? structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) : typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003274
Jamie Madillc835df62013-06-21 09:15:32 -04003275 if (useStd140Packing)
3276 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003277 string += std140PrePaddingString(*field.type(), &elementIndex);
Jamie Madillc835df62013-06-21 09:15:32 -04003278 }
3279
Jamie Madill98493dd2013-07-08 14:39:03 -04003280 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003281
3282 if (useStd140Packing)
3283 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003284 string += std140PostPaddingString(*field.type(), useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003285 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003286 }
3287
3288 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003289 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003290
Jamie Madille4075c92013-06-21 09:15:32 -04003291 // Add remaining element index to the global map, for use with nested structs in standard layouts
3292 if (useStd140Packing)
3293 {
3294 mStd140StructElementIndexes[structName] = elementIndex;
3295 }
3296
Jamie Madill98493dd2013-07-08 14:39:03 -04003297 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003298}
3299
Jamie Madill98493dd2013-07-08 14:39:03 -04003300TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003301{
Jamie Madill98493dd2013-07-08 14:39:03 -04003302 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003303 {
3304 return "";
3305 }
3306
3307 TString prefix = "";
3308
3309 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3310 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003311
3312 if (useStd140Packing)
3313 {
3314 prefix += "std";
3315 }
3316
Jamie Madill9cf6c072013-06-20 11:55:53 -04003317 if (useHLSLRowMajorPacking)
3318 {
Jamie Madillc835df62013-06-21 09:15:32 -04003319 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003320 prefix += "rm";
3321 }
3322
Jamie Madill98493dd2013-07-08 14:39:03 -04003323 return prefix + structLookup(structure.name());
Jamie Madill9cf6c072013-06-20 11:55:53 -04003324}
3325
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003326void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003327{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003328 if (name == "")
3329 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003330 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003331 }
3332
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003333 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
3334 {
3335 return; // Already added
3336 }
3337
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003338 TType ctorType = type;
3339 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003340 ctorType.setPrecision(EbpHigh);
3341 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003342
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003343 TString ctorName = type.getStruct() ? decorate(name) : name;
3344
3345 typedef std::vector<TType> ParameterArray;
3346 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003347
Jamie Madill98493dd2013-07-08 14:39:03 -04003348 const TStructure* structure = type.getStruct();
3349 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003350 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003351 mStructNames.insert(decorate(name));
3352
Jamie Madill98493dd2013-07-08 14:39:03 -04003353 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003354
Jamie Madill98493dd2013-07-08 14:39:03 -04003355 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003356 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003357 // Add row-major packed struct for interface blocks
3358 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003359 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003360 "#pragma pack_matrix(column_major)\n";
3361
Jamie Madillc835df62013-06-21 09:15:32 -04003362 const TString &std140Prefix = "std";
Jamie Madill98493dd2013-07-08 14:39:03 -04003363 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003364
3365 const TString &std140RowMajorPrefix = "std_rm";
3366 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003367 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003368 "#pragma pack_matrix(column_major)\n";
3369
Jamie Madill98493dd2013-07-08 14:39:03 -04003370 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003371 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003372 mStructDeclarations.push_back(std140String);
3373 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003374 }
3375
Jamie Madill98493dd2013-07-08 14:39:03 -04003376 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003377 for (unsigned int i = 0; i < fields.size(); i++)
3378 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003379 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003380 }
3381 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003382 else if (parameters)
3383 {
3384 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3385 {
3386 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3387 }
3388 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003389 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003390
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003391 TString constructor;
3392
3393 if (ctorType.getStruct())
3394 {
3395 constructor += ctorName + " " + ctorName + "_ctor(";
3396 }
3397 else // Built-in type
3398 {
3399 constructor += typeString(ctorType) + " " + ctorName + "(";
3400 }
3401
3402 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3403 {
3404 const TType &type = ctorParameters[parameter];
3405
3406 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3407
3408 if (parameter < ctorParameters.size() - 1)
3409 {
3410 constructor += ", ";
3411 }
3412 }
3413
3414 constructor += ")\n"
3415 "{\n";
3416
3417 if (ctorType.getStruct())
3418 {
3419 constructor += " " + ctorName + " structure = {";
3420 }
3421 else
3422 {
3423 constructor += " return " + typeString(ctorType) + "(";
3424 }
3425
3426 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3427 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003428 int rows = ctorType.getRows();
3429 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003430 const TType &parameter = ctorParameters[0];
3431
3432 if (parameter.isScalar())
3433 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003434 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003435 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003436 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003437 {
3438 constructor += TString((row == col) ? "x0" : "0.0");
3439
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003440 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003441 {
3442 constructor += ", ";
3443 }
3444 }
3445 }
3446 }
3447 else if (parameter.isMatrix())
3448 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003449 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003450 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003451 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003452 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003453 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003454 {
3455 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3456 }
3457 else
3458 {
3459 constructor += TString((row == col) ? "1.0" : "0.0");
3460 }
3461
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003462 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003463 {
3464 constructor += ", ";
3465 }
3466 }
3467 }
3468 }
3469 else UNREACHABLE();
3470 }
3471 else
3472 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003473 size_t remainingComponents = ctorType.getObjectSize();
3474 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003475
3476 while (remainingComponents > 0)
3477 {
3478 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003479 const size_t parameterSize = parameter.getObjectSize();
3480 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003481
3482 constructor += "x" + str(parameterIndex);
3483
3484 if (parameter.isScalar())
3485 {
3486 remainingComponents -= parameter.getObjectSize();
3487 }
3488 else if (parameter.isVector())
3489 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003490 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003491 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003492 ASSERT(parameterSize <= remainingComponents);
3493 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003494 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003495 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003496 {
3497 switch (remainingComponents)
3498 {
3499 case 1: constructor += ".x"; break;
3500 case 2: constructor += ".xy"; break;
3501 case 3: constructor += ".xyz"; break;
3502 case 4: constructor += ".xyzw"; break;
3503 default: UNREACHABLE();
3504 }
3505
3506 remainingComponents = 0;
3507 }
3508 else UNREACHABLE();
3509 }
3510 else if (parameter.isMatrix() || parameter.getStruct())
3511 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003512 ASSERT(remainingComponents == parameterSize || moreParameters);
3513 ASSERT(parameterSize <= remainingComponents);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003514
Jamie Madill94bf7f22013-07-08 13:31:15 -04003515 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003516 }
3517 else UNREACHABLE();
3518
3519 if (moreParameters)
3520 {
3521 parameterIndex++;
3522 }
3523
3524 if (remainingComponents)
3525 {
3526 constructor += ", ";
3527 }
3528 }
3529 }
3530
3531 if (ctorType.getStruct())
3532 {
3533 constructor += "};\n"
3534 " return structure;\n"
3535 "}\n";
3536 }
3537 else
3538 {
3539 constructor += ");\n"
3540 "}\n";
3541 }
3542
daniel@transgaming.com63691862010-04-29 03:32:42 +00003543 mConstructors.insert(constructor);
3544}
3545
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003546const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3547{
3548 TInfoSinkBase &out = mBody;
3549
Jamie Madill98493dd2013-07-08 14:39:03 -04003550 const TStructure* structure = type.getStruct();
3551 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003552 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003553 out << structLookup(structure->name()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003554
Jamie Madill98493dd2013-07-08 14:39:03 -04003555 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003556
Jamie Madill98493dd2013-07-08 14:39:03 -04003557 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003558 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003559 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003560
3561 constUnion = writeConstantUnion(*fieldType, constUnion);
3562
Jamie Madill98493dd2013-07-08 14:39:03 -04003563 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003564 {
3565 out << ", ";
3566 }
3567 }
3568
3569 out << ")";
3570 }
3571 else
3572 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003573 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003574 bool writeType = size > 1;
3575
3576 if (writeType)
3577 {
3578 out << typeString(type) << "(";
3579 }
3580
Jamie Madill94bf7f22013-07-08 13:31:15 -04003581 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003582 {
3583 switch (constUnion->getType())
3584 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003585 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003586 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003587 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003588 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003589 default: UNREACHABLE();
3590 }
3591
3592 if (i != size - 1)
3593 {
3594 out << ", ";
3595 }
3596 }
3597
3598 if (writeType)
3599 {
3600 out << ")";
3601 }
3602 }
3603
3604 return constUnion;
3605}
3606
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003607TString OutputHLSL::scopeString(unsigned int depthLimit)
3608{
3609 TString string;
3610
3611 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3612 {
3613 string += "_" + str(i);
3614 }
3615
3616 return string;
3617}
3618
3619TString OutputHLSL::scopedStruct(const TString &typeName)
3620{
3621 if (typeName == "")
3622 {
3623 return typeName;
3624 }
3625
3626 return typeName + scopeString(mScopeDepth);
3627}
3628
3629TString OutputHLSL::structLookup(const TString &typeName)
3630{
3631 for (int depth = mScopeDepth; depth >= 0; depth--)
3632 {
3633 TString scopedName = decorate(typeName + scopeString(depth));
3634
3635 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3636 {
3637 if (*structName == scopedName)
3638 {
3639 return scopedName;
3640 }
3641 }
3642 }
3643
3644 UNREACHABLE(); // Should have found a matching constructor
3645
3646 return typeName;
3647}
3648
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003649TString OutputHLSL::decorate(const TString &string)
3650{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003651 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003652 {
3653 return "_" + string;
3654 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003655
3656 return string;
3657}
3658
apatrick@chromium.org65756022012-01-17 21:45:38 +00003659TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003660{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003661 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003662 {
3663 return "ex_" + string;
3664 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003665
3666 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003667}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003668
Jamie Madill98493dd2013-07-08 14:39:03 -04003669TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003670{
Jamie Madill98493dd2013-07-08 14:39:03 -04003671 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003672 {
3673 return decorate(string);
3674 }
3675
3676 return string;
3677}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003678
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003679void OutputHLSL::declareInterfaceBlockField(const TType &type, const TString &name, std::vector<InterfaceBlockField>& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003680{
Jamie Madill98493dd2013-07-08 14:39:03 -04003681 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003682
3683 if (!structure)
3684 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003685 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003686 InterfaceBlockField field(glVariableType(type), glVariablePrecision(type), name.c_str(),
3687 (unsigned int)type.getArraySize(), isRowMajorMatrix);
3688 output.push_back(field);
3689 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003690 else
3691 {
Jamie Madill28167c62013-08-30 13:21:10 -04003692 InterfaceBlockField structField(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), false);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003693
3694 const TFieldList &fields = structure->fields();
3695
3696 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3697 {
3698 TField *field = fields[fieldIndex];
3699 TType *fieldType = field->type();
3700
3701 // make sure to copy matrix packing information
3702 fieldType->setLayoutQualifier(type.getLayoutQualifier());
3703
3704 declareInterfaceBlockField(*fieldType, field->name(), structField.fields);
3705 }
3706
3707 output.push_back(structField);
3708 }
3709}
3710
Jamie Madillc2141fb2013-08-30 13:21:08 -04003711Uniform OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, std::vector<Uniform>& output)
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003712{
3713 const TStructure *structure = type.getStruct();
3714
3715 if (!structure)
3716 {
3717 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
3718 Uniform uniform(glVariableType(type), glVariablePrecision(type), name.c_str(),
Jamie Madill56093782013-08-30 13:21:11 -04003719 (unsigned int)type.getArraySize(), (unsigned int)registerIndex, 0);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003720 output.push_back(uniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003721
3722 return uniform;
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003723 }
3724 else
3725 {
Jamie Madill56093782013-08-30 13:21:11 -04003726 Uniform structUniform(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(),
3727 (unsigned int)registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003728
Jamie Madill98493dd2013-07-08 14:39:03 -04003729 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003730
Jamie Madill98493dd2013-07-08 14:39:03 -04003731 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003732 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003733 TField *field = fields[fieldIndex];
3734 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003735
Jamie Madill56093782013-08-30 13:21:11 -04003736 declareUniformToList(*fieldType, field->name(), GL_INVALID_INDEX, structUniform.fields);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003737 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003738
Jamie Madill56093782013-08-30 13:21:11 -04003739 // assign register offset information -- this will override the information in any sub-structures.
3740 HLSLVariableGetRegisterInfo(registerIndex, &structUniform);
3741
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003742 output.push_back(structUniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003743
3744 return structUniform;
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003745 }
3746}
3747
Jamie Madill139b9092013-08-30 13:21:06 -04003748InterpolationType getInterpolationType(TQualifier qualifier)
3749{
3750 switch (qualifier)
3751 {
3752 case EvqFlatIn:
3753 case EvqFlatOut:
3754 return INTERPOLATION_FLAT;
3755
3756 case EvqSmoothIn:
3757 case EvqSmoothOut:
3758 case EvqVertexOut:
3759 case EvqFragmentIn:
Jamie Madill384b6042013-09-13 10:06:24 -04003760 case EvqVaryingIn:
3761 case EvqVaryingOut:
Jamie Madill139b9092013-08-30 13:21:06 -04003762 return INTERPOLATION_SMOOTH;
3763
3764 case EvqCentroidIn:
3765 case EvqCentroidOut:
3766 return INTERPOLATION_CENTROID;
3767
3768 default: UNREACHABLE();
3769 return INTERPOLATION_SMOOTH;
3770 }
3771}
3772
Jamie Madill94599662013-08-30 13:21:10 -04003773void OutputHLSL::declareVaryingToList(const TType &type, TQualifier baseTypeQualifier, const TString &name, std::vector<Varying>& fieldsOut)
Jamie Madill47fdd132013-08-30 13:21:04 -04003774{
3775 const TStructure *structure = type.getStruct();
3776
Jamie Madill94599662013-08-30 13:21:10 -04003777 InterpolationType interpolation = getInterpolationType(baseTypeQualifier);
Jamie Madill47fdd132013-08-30 13:21:04 -04003778 if (!structure)
3779 {
Jamie Madill139b9092013-08-30 13:21:06 -04003780 Varying varying(glVariableType(type), glVariablePrecision(type), name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003781 fieldsOut.push_back(varying);
3782 }
3783 else
3784 {
Jamie Madill28167c62013-08-30 13:21:10 -04003785 Varying structVarying(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003786 const TFieldList &fields = structure->fields();
3787
Jamie Madill28167c62013-08-30 13:21:10 -04003788 structVarying.structName = structure->name().c_str();
3789
Jamie Madill47fdd132013-08-30 13:21:04 -04003790 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3791 {
3792 const TField &field = *fields[fieldIndex];
Jamie Madill94599662013-08-30 13:21:10 -04003793 declareVaryingToList(*field.type(), baseTypeQualifier, field.name(), structVarying.fields);
Jamie Madill47fdd132013-08-30 13:21:04 -04003794 }
3795
3796 fieldsOut.push_back(structVarying);
3797 }
3798}
3799
Jamie Madillc2141fb2013-08-30 13:21:08 -04003800int OutputHLSL::declareUniformAndAssignRegister(const TType &type, const TString &name)
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003801{
Jamie Madillc2141fb2013-08-30 13:21:08 -04003802 int registerIndex = (IsSampler(type.getBasicType()) ? mSamplerRegister : mUniformRegister);
3803
3804 const Uniform &uniform = declareUniformToList(type, name, registerIndex, mActiveUniforms);
3805
3806 if (IsSampler(type.getBasicType()))
3807 {
3808 mSamplerRegister += HLSLVariableRegisterCount(uniform);
3809 }
3810 else
3811 {
3812 mUniformRegister += HLSLVariableRegisterCount(uniform);
3813 }
3814
3815 return registerIndex;
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003816}
3817
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003818GLenum OutputHLSL::glVariableType(const TType &type)
3819{
3820 if (type.getBasicType() == EbtFloat)
3821 {
3822 if (type.isScalar())
3823 {
3824 return GL_FLOAT;
3825 }
3826 else if (type.isVector())
3827 {
3828 switch(type.getNominalSize())
3829 {
3830 case 2: return GL_FLOAT_VEC2;
3831 case 3: return GL_FLOAT_VEC3;
3832 case 4: return GL_FLOAT_VEC4;
3833 default: UNREACHABLE();
3834 }
3835 }
3836 else if (type.isMatrix())
3837 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003838 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003839 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003840 case 2:
3841 switch(type.getRows())
3842 {
3843 case 2: return GL_FLOAT_MAT2;
3844 case 3: return GL_FLOAT_MAT2x3;
3845 case 4: return GL_FLOAT_MAT2x4;
3846 default: UNREACHABLE();
3847 }
3848
3849 case 3:
3850 switch(type.getRows())
3851 {
3852 case 2: return GL_FLOAT_MAT3x2;
3853 case 3: return GL_FLOAT_MAT3;
3854 case 4: return GL_FLOAT_MAT3x4;
3855 default: UNREACHABLE();
3856 }
3857
3858 case 4:
3859 switch(type.getRows())
3860 {
3861 case 2: return GL_FLOAT_MAT4x2;
3862 case 3: return GL_FLOAT_MAT4x3;
3863 case 4: return GL_FLOAT_MAT4;
3864 default: UNREACHABLE();
3865 }
3866
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003867 default: UNREACHABLE();
3868 }
3869 }
3870 else UNREACHABLE();
3871 }
3872 else if (type.getBasicType() == EbtInt)
3873 {
3874 if (type.isScalar())
3875 {
3876 return GL_INT;
3877 }
3878 else if (type.isVector())
3879 {
3880 switch(type.getNominalSize())
3881 {
3882 case 2: return GL_INT_VEC2;
3883 case 3: return GL_INT_VEC3;
3884 case 4: return GL_INT_VEC4;
3885 default: UNREACHABLE();
3886 }
3887 }
3888 else UNREACHABLE();
3889 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003890 else if (type.getBasicType() == EbtUInt)
3891 {
3892 if (type.isScalar())
3893 {
3894 return GL_UNSIGNED_INT;
3895 }
3896 else if (type.isVector())
3897 {
Jamie Madill22d63da2013-06-07 12:45:12 -04003898 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003899 {
3900 case 2: return GL_UNSIGNED_INT_VEC2;
3901 case 3: return GL_UNSIGNED_INT_VEC3;
3902 case 4: return GL_UNSIGNED_INT_VEC4;
3903 default: UNREACHABLE();
3904 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003905 }
3906 else UNREACHABLE();
3907 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003908 else if (type.getBasicType() == EbtBool)
3909 {
3910 if (type.isScalar())
3911 {
3912 return GL_BOOL;
3913 }
3914 else if (type.isVector())
3915 {
3916 switch(type.getNominalSize())
3917 {
3918 case 2: return GL_BOOL_VEC2;
3919 case 3: return GL_BOOL_VEC3;
3920 case 4: return GL_BOOL_VEC4;
3921 default: UNREACHABLE();
3922 }
3923 }
3924 else UNREACHABLE();
3925 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003926
3927 switch(type.getBasicType())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003928 {
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003929 case EbtSampler2D: return GL_SAMPLER_2D;
3930 case EbtSampler3D: return GL_SAMPLER_3D;
3931 case EbtSamplerCube: return GL_SAMPLER_CUBE;
3932 case EbtSampler2DArray: return GL_SAMPLER_2D_ARRAY;
3933 case EbtISampler2D: return GL_INT_SAMPLER_2D;
3934 case EbtISampler3D: return GL_INT_SAMPLER_3D;
3935 case EbtISamplerCube: return GL_INT_SAMPLER_CUBE;
3936 case EbtISampler2DArray: return GL_INT_SAMPLER_2D_ARRAY;
3937 case EbtUSampler2D: return GL_UNSIGNED_INT_SAMPLER_2D;
3938 case EbtUSampler3D: return GL_UNSIGNED_INT_SAMPLER_3D;
3939 case EbtUSamplerCube: return GL_UNSIGNED_INT_SAMPLER_CUBE;
3940 case EbtUSampler2DArray: return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3941 case EbtSampler2DShadow: return GL_SAMPLER_2D_SHADOW;
3942 case EbtSamplerCubeShadow: return GL_SAMPLER_CUBE_SHADOW;
3943 case EbtSampler2DArrayShadow: return GL_SAMPLER_2D_ARRAY_SHADOW;
3944 default: UNREACHABLE();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003945 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003946
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003947 return GL_NONE;
3948}
3949
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003950GLenum OutputHLSL::glVariablePrecision(const TType &type)
3951{
3952 if (type.getBasicType() == EbtFloat)
3953 {
3954 switch (type.getPrecision())
3955 {
3956 case EbpHigh: return GL_HIGH_FLOAT;
3957 case EbpMedium: return GL_MEDIUM_FLOAT;
3958 case EbpLow: return GL_LOW_FLOAT;
3959 case EbpUndefined:
3960 // Should be defined as the default precision by the parser
3961 default: UNREACHABLE();
3962 }
3963 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003964 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003965 {
3966 switch (type.getPrecision())
3967 {
3968 case EbpHigh: return GL_HIGH_INT;
3969 case EbpMedium: return GL_MEDIUM_INT;
3970 case EbpLow: return GL_LOW_INT;
3971 case EbpUndefined:
3972 // Should be defined as the default precision by the parser
3973 default: UNREACHABLE();
3974 }
3975 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003976
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00003977 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003978 return GL_NONE;
3979}
3980
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003981bool OutputHLSL::isVaryingOut(TQualifier qualifier)
3982{
3983 switch(qualifier)
3984 {
3985 case EvqVaryingOut:
3986 case EvqInvariantVaryingOut:
3987 case EvqSmoothOut:
3988 case EvqFlatOut:
3989 case EvqCentroidOut:
Jamie Madill19571812013-08-12 15:26:34 -07003990 case EvqVertexOut:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003991 return true;
3992 }
3993
3994 return false;
3995}
3996
3997bool OutputHLSL::isVaryingIn(TQualifier qualifier)
3998{
3999 switch(qualifier)
4000 {
4001 case EvqVaryingIn:
4002 case EvqInvariantVaryingIn:
4003 case EvqSmoothIn:
4004 case EvqFlatIn:
4005 case EvqCentroidIn:
Jamie Madill19571812013-08-12 15:26:34 -07004006 case EvqFragmentIn:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004007 return true;
4008 }
4009
4010 return false;
4011}
4012
4013bool OutputHLSL::isVarying(TQualifier qualifier)
4014{
4015 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
4016}
4017
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004018}