blob: 6fdefc749e200365db3399b9fbce6252a22a819d [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 }
1164
1165 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1166 }
1167
1168 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001169 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001170 }
1171 else if (IsSampler3D(textureFunction->sampler))
1172 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001173 out << " float width; float height; float depth; float levels;\n";
1174
1175 if (textureFunction->method == TextureFunction::LOD0)
1176 {
1177 out << " uint mip = 0;\n";
1178 }
1179 else
1180 {
1181 if (textureFunction->method == TextureFunction::IMPLICIT ||
1182 textureFunction->method == TextureFunction::BIAS)
1183 {
1184 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1185 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
1186 " float dx = length(ddx(tSized));\n"
1187 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001188 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001189
1190 if (textureFunction->method == TextureFunction::BIAS)
1191 {
1192 out << " lod += bias;\n";
1193 }
1194 }
1195
1196 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1197 }
1198
1199 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001200 }
1201 else UNREACHABLE();
1202 }
1203
1204 out << " return ";
1205
1206 // HLSL intrinsic
1207 if (mOutputType == SH_HLSL9_OUTPUT)
1208 {
1209 switch(textureFunction->sampler)
1210 {
1211 case EbtSampler2D: out << "tex2D"; break;
1212 case EbtSamplerCube: out << "texCUBE"; break;
1213 default: UNREACHABLE();
1214 }
1215
Nicolas Capens75fb4752013-07-10 15:14:47 -04001216 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001217 {
1218 case TextureFunction::IMPLICIT: out << "(s, "; break;
1219 case TextureFunction::BIAS: out << "bias(s, "; break;
1220 case TextureFunction::LOD: out << "lod(s, "; break;
1221 case TextureFunction::LOD0: out << "lod(s, "; break;
1222 default: UNREACHABLE();
1223 }
1224 }
1225 else if (mOutputType == SH_HLSL11_OUTPUT)
1226 {
1227 if (IsIntegerSampler(textureFunction->sampler))
1228 {
1229 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001230 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001231 else if (IsShadowSampler(textureFunction->sampler))
1232 {
1233 out << "x.SampleCmp(s, ";
1234 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001235 else
1236 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001237 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001238 {
1239 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1240 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1241 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1242 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
1243 default: UNREACHABLE();
1244 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001245 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001246 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001247 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001248
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001249 // Integer sampling requires integer addresses
1250 TString addressx = "";
1251 TString addressy = "";
1252 TString addressz = "";
1253 TString close = "";
1254
1255 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001256 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001257 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001258 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001259 case 2: out << "int3("; break;
1260 case 3: out << "int4("; break;
1261 default: UNREACHABLE();
1262 }
1263
Nicolas Capensc98406a2013-07-10 14:52:44 -04001264 addressx = "int(floor(width * frac((";
1265 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001266
1267 if (IsSamplerArray(textureFunction->sampler))
1268 {
1269 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1270 }
1271 else
1272 {
Nicolas Capensc98406a2013-07-10 14:52:44 -04001273 addressz = "int(floor(depth * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001274 }
1275
1276 close = "))))";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001277 }
1278 else
1279 {
1280 switch(hlslCoords)
1281 {
1282 case 2: out << "float2("; break;
1283 case 3: out << "float3("; break;
1284 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001285 default: UNREACHABLE();
1286 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001287 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001288
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001289 TString proj = ""; // Only used for projected textures
1290
1291 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001292 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001293 switch(textureFunction->coords)
1294 {
1295 case 3: proj = " / t.z"; break;
1296 case 4: proj = " / t.w"; break;
1297 default: UNREACHABLE();
1298 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001299 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001300
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001301 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001302
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001303 if (mOutputType == SH_HLSL9_OUTPUT)
1304 {
1305 if (hlslCoords >= 3)
1306 {
1307 if (textureFunction->coords < 3)
1308 {
1309 out << ", 0";
1310 }
1311 else
1312 {
1313 out << ", t.z" + proj;
1314 }
1315 }
1316
1317 if (hlslCoords == 4)
1318 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001319 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001320 {
1321 case TextureFunction::BIAS: out << ", bias"; break;
1322 case TextureFunction::LOD: out << ", lod"; break;
1323 case TextureFunction::LOD0: out << ", 0"; break;
1324 default: UNREACHABLE();
1325 }
1326 }
1327
1328 out << "));\n";
1329 }
1330 else if (mOutputType == SH_HLSL11_OUTPUT)
1331 {
1332 if (hlslCoords >= 3)
1333 {
1334 out << ", " + addressz + ("t.z" + proj) + close;
1335 }
1336
Nicolas Capenscb127d32013-07-15 17:26:18 -04001337 if (IsIntegerSampler(textureFunction->sampler))
1338 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001339 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001340 }
1341 else if (IsShadowSampler(textureFunction->sampler))
1342 {
1343 // Compare value
1344 switch(textureFunction->coords)
1345 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001346 case 3: out << "), t.z"; break;
1347 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001348 default: UNREACHABLE();
1349 }
1350 }
1351 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001352 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001353 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001354 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001355 case TextureFunction::IMPLICIT: out << ")"; break;
1356 case TextureFunction::BIAS: out << "), bias"; break;
1357 case TextureFunction::LOD: out << "), lod"; break;
1358 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001359 default: UNREACHABLE();
1360 }
1361 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001362
1363 if (textureFunction->offset)
1364 {
1365 out << ", offset";
1366 }
1367
1368 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001369 }
1370 else UNREACHABLE();
1371 }
1372
1373 out << "\n"
1374 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001375 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001376 }
1377
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001378 if (mUsesFragCoord)
1379 {
1380 out << "#define GL_USES_FRAG_COORD\n";
1381 }
1382
1383 if (mUsesPointCoord)
1384 {
1385 out << "#define GL_USES_POINT_COORD\n";
1386 }
1387
1388 if (mUsesFrontFacing)
1389 {
1390 out << "#define GL_USES_FRONT_FACING\n";
1391 }
1392
1393 if (mUsesPointSize)
1394 {
1395 out << "#define GL_USES_POINT_SIZE\n";
1396 }
1397
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001398 if (mUsesFragDepth)
1399 {
1400 out << "#define GL_USES_FRAG_DEPTH\n";
1401 }
1402
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001403 if (mUsesDepthRange)
1404 {
1405 out << "#define GL_USES_DEPTH_RANGE\n";
1406 }
1407
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001408 if (mUsesXor)
1409 {
1410 out << "bool xor(bool p, bool q)\n"
1411 "{\n"
1412 " return (p || q) && !(p && q);\n"
1413 "}\n"
1414 "\n";
1415 }
1416
1417 if (mUsesMod1)
1418 {
1419 out << "float mod(float x, float y)\n"
1420 "{\n"
1421 " return x - y * floor(x / y);\n"
1422 "}\n"
1423 "\n";
1424 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001425
1426 if (mUsesMod2v)
1427 {
1428 out << "float2 mod(float2 x, float2 y)\n"
1429 "{\n"
1430 " return x - y * floor(x / y);\n"
1431 "}\n"
1432 "\n";
1433 }
1434
1435 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001436 {
1437 out << "float2 mod(float2 x, float y)\n"
1438 "{\n"
1439 " return x - y * floor(x / y);\n"
1440 "}\n"
1441 "\n";
1442 }
1443
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001444 if (mUsesMod3v)
1445 {
1446 out << "float3 mod(float3 x, float3 y)\n"
1447 "{\n"
1448 " return x - y * floor(x / y);\n"
1449 "}\n"
1450 "\n";
1451 }
1452
1453 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001454 {
1455 out << "float3 mod(float3 x, float y)\n"
1456 "{\n"
1457 " return x - y * floor(x / y);\n"
1458 "}\n"
1459 "\n";
1460 }
1461
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001462 if (mUsesMod4v)
1463 {
1464 out << "float4 mod(float4 x, float4 y)\n"
1465 "{\n"
1466 " return x - y * floor(x / y);\n"
1467 "}\n"
1468 "\n";
1469 }
1470
1471 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001472 {
1473 out << "float4 mod(float4 x, float y)\n"
1474 "{\n"
1475 " return x - y * floor(x / y);\n"
1476 "}\n"
1477 "\n";
1478 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001479
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001480 if (mUsesFaceforward1)
1481 {
1482 out << "float faceforward(float N, float I, float Nref)\n"
1483 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001484 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001485 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001486 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001487 " }\n"
1488 " else\n"
1489 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001490 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001491 " }\n"
1492 "}\n"
1493 "\n";
1494 }
1495
1496 if (mUsesFaceforward2)
1497 {
1498 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1499 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001500 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001501 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001502 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001503 " }\n"
1504 " else\n"
1505 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001506 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001507 " }\n"
1508 "}\n"
1509 "\n";
1510 }
1511
1512 if (mUsesFaceforward3)
1513 {
1514 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1515 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001516 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001517 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001518 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001519 " }\n"
1520 " else\n"
1521 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001522 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001523 " }\n"
1524 "}\n"
1525 "\n";
1526 }
1527
1528 if (mUsesFaceforward4)
1529 {
1530 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1531 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001532 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001533 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001534 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001535 " }\n"
1536 " else\n"
1537 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001538 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001539 " }\n"
1540 "}\n"
1541 "\n";
1542 }
1543
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001544 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001545 {
1546 out << "float atanyx(float y, float x)\n"
1547 "{\n"
1548 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1549 " return atan2(y, x);\n"
1550 "}\n";
1551 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001552
1553 if (mUsesAtan2_2)
1554 {
1555 out << "float2 atanyx(float2 y, float2 x)\n"
1556 "{\n"
1557 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1558 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1559 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1560 "}\n";
1561 }
1562
1563 if (mUsesAtan2_3)
1564 {
1565 out << "float3 atanyx(float3 y, float3 x)\n"
1566 "{\n"
1567 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1568 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1569 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1570 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1571 "}\n";
1572 }
1573
1574 if (mUsesAtan2_4)
1575 {
1576 out << "float4 atanyx(float4 y, float4 x)\n"
1577 "{\n"
1578 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1579 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1580 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1581 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1582 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1583 "}\n";
1584 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001585}
1586
1587void OutputHLSL::visitSymbol(TIntermSymbol *node)
1588{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001589 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001590
Jamie Madill570e04d2013-06-21 09:15:33 -04001591 // Handle accessing std140 structs by value
1592 if (mFlaggedStructMappedNames.count(node) > 0)
1593 {
1594 out << mFlaggedStructMappedNames[node];
1595 return;
1596 }
1597
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001598 TString name = node->getSymbol();
1599
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001600 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001601 {
1602 mUsesDepthRange = true;
1603 out << name;
1604 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001605 else
1606 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001607 TQualifier qualifier = node->getQualifier();
1608
1609 if (qualifier == EvqUniform)
1610 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001611 const TType& nodeType = node->getType();
1612 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1613
1614 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001615 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001616 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001617 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001618 else
1619 {
1620 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001621 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001622
1623 out << decorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001624 }
Jamie Madill19571812013-08-12 15:26:34 -07001625 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001626 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001627 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001628 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001629 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001630 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001631 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001632 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001633 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001634 }
Jamie Madill19571812013-08-12 15:26:34 -07001635 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001636 {
1637 mReferencedOutputVariables[name] = node;
1638 out << "out_" << name;
1639 }
1640 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001641 {
1642 out << "gl_Color[0]";
1643 mUsesFragColor = true;
1644 }
1645 else if (qualifier == EvqFragData)
1646 {
1647 out << "gl_Color";
1648 mUsesFragData = true;
1649 }
1650 else if (qualifier == EvqFragCoord)
1651 {
1652 mUsesFragCoord = true;
1653 out << name;
1654 }
1655 else if (qualifier == EvqPointCoord)
1656 {
1657 mUsesPointCoord = true;
1658 out << name;
1659 }
1660 else if (qualifier == EvqFrontFacing)
1661 {
1662 mUsesFrontFacing = true;
1663 out << name;
1664 }
1665 else if (qualifier == EvqPointSize)
1666 {
1667 mUsesPointSize = true;
1668 out << name;
1669 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001670 else if (name == "gl_FragDepthEXT")
1671 {
1672 mUsesFragDepth = true;
1673 out << "gl_Depth";
1674 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001675 else if (qualifier == EvqInternal)
1676 {
1677 out << name;
1678 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001679 else
1680 {
1681 out << decorate(name);
1682 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001683 }
1684}
1685
1686bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1687{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001688 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001689
Jamie Madill570e04d2013-06-21 09:15:33 -04001690 // Handle accessing std140 structs by value
1691 if (mFlaggedStructMappedNames.count(node) > 0)
1692 {
1693 out << mFlaggedStructMappedNames[node];
1694 return false;
1695 }
1696
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001697 switch (node->getOp())
1698 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001699 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001700 case EOpInitialize:
1701 if (visit == PreVisit)
1702 {
1703 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1704 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1705 // new variable is created before the assignment is evaluated), so we need to convert
1706 // this to "float t = x, x = t;".
1707
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001708 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1709 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001710
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001711 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1712 expression->traverse(&searchSymbol);
1713 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001714
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001715 if (sameSymbol)
1716 {
1717 // Type already printed
1718 out << "t" + str(mUniqueIndex) + " = ";
1719 expression->traverse(this);
1720 out << ", ";
1721 symbolNode->traverse(this);
1722 out << " = t" + str(mUniqueIndex);
1723
1724 mUniqueIndex++;
1725 return false;
1726 }
1727 }
1728 else if (visit == InVisit)
1729 {
1730 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001731 }
1732 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001733 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1734 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1735 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1736 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1737 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1738 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001739 if (visit == PreVisit)
1740 {
1741 out << "(";
1742 }
1743 else if (visit == InVisit)
1744 {
1745 out << " = mul(";
1746 node->getLeft()->traverse(this);
1747 out << ", transpose(";
1748 }
1749 else
1750 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001751 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001752 }
1753 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001754 case EOpMatrixTimesMatrixAssign:
1755 if (visit == PreVisit)
1756 {
1757 out << "(";
1758 }
1759 else if (visit == InVisit)
1760 {
1761 out << " = mul(";
1762 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001763 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001764 }
1765 else
1766 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001767 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001768 }
1769 break;
1770 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001771 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001772 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001773 const TType& leftType = node->getLeft()->getType();
1774 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001775 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001776 if (visit == PreVisit)
1777 {
1778 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1779 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1780
1781 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1782 out << interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1783
1784 return false;
1785 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001786 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001787 else
1788 {
1789 outputTriplet(visit, "", "[", "]");
1790 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001791 }
1792 break;
1793 case EOpIndexIndirect:
1794 // We do not currently support indirect references to interface blocks
1795 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1796 outputTriplet(visit, "", "[", "]");
1797 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001798 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001799 if (visit == InVisit)
1800 {
1801 const TStructure* structure = node->getLeft()->getType().getStruct();
1802 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1803 const TField* field = structure->fields()[index->getIConst(0)];
1804 out << "." + decorateField(field->name(), *structure);
1805
1806 return false;
1807 }
1808 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001809 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001810 if (visit == InVisit)
1811 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001812 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1813 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1814 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
1815 out << "." + decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001816
1817 return false;
1818 }
1819 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001820 case EOpVectorSwizzle:
1821 if (visit == InVisit)
1822 {
1823 out << ".";
1824
1825 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1826
1827 if (swizzle)
1828 {
1829 TIntermSequence &sequence = swizzle->getSequence();
1830
1831 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1832 {
1833 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1834
1835 if (element)
1836 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001837 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001838
1839 switch (i)
1840 {
1841 case 0: out << "x"; break;
1842 case 1: out << "y"; break;
1843 case 2: out << "z"; break;
1844 case 3: out << "w"; break;
1845 default: UNREACHABLE();
1846 }
1847 }
1848 else UNREACHABLE();
1849 }
1850 }
1851 else UNREACHABLE();
1852
1853 return false; // Fully processed
1854 }
1855 break;
1856 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1857 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1858 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1859 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001860 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001861 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001862 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001863 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001864 if (node->getOp() == EOpEqual)
1865 {
1866 outputTriplet(visit, "(", " == ", ")");
1867 }
1868 else
1869 {
1870 outputTriplet(visit, "(", " != ", ")");
1871 }
1872 }
1873 else if (node->getLeft()->getBasicType() == EbtStruct)
1874 {
1875 if (node->getOp() == EOpEqual)
1876 {
1877 out << "(";
1878 }
1879 else
1880 {
1881 out << "!(";
1882 }
1883
Jamie Madill98493dd2013-07-08 14:39:03 -04001884 const TStructure &structure = *node->getLeft()->getType().getStruct();
1885 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001886
Jamie Madill98493dd2013-07-08 14:39:03 -04001887 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001888 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001889 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001890
1891 node->getLeft()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001892 out << "." + decorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001893 node->getRight()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001894 out << "." + decorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001895
Jamie Madill98493dd2013-07-08 14:39:03 -04001896 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001897 {
1898 out << " && ";
1899 }
1900 }
1901
1902 out << ")";
1903
1904 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001905 }
1906 else
1907 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001908 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001909
1910 if (node->getOp() == EOpEqual)
1911 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001912 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001913 }
1914 else
1915 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001916 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001917 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001918 }
1919 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001920 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1921 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1922 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1923 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1924 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001925 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001926 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1927 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001928 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001929 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001930 if (node->getRight()->hasSideEffects())
1931 {
1932 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1933 return false;
1934 }
1935 else
1936 {
1937 outputTriplet(visit, "(", " || ", ")");
1938 return true;
1939 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001940 case EOpLogicalXor:
1941 mUsesXor = true;
1942 outputTriplet(visit, "xor(", ", ", ")");
1943 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001944 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001945 if (node->getRight()->hasSideEffects())
1946 {
1947 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1948 return false;
1949 }
1950 else
1951 {
1952 outputTriplet(visit, "(", " && ", ")");
1953 return true;
1954 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001955 default: UNREACHABLE();
1956 }
1957
1958 return true;
1959}
1960
1961bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1962{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001963 switch (node->getOp())
1964 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001965 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1966 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1967 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1968 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1969 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1970 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1971 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001972 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04001973 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001974 case EOpConvFloatToBool:
1975 switch (node->getOperand()->getType().getNominalSize())
1976 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001977 case 1: outputTriplet(visit, "bool(", "", ")"); break;
1978 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
1979 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
1980 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001981 default: UNREACHABLE();
1982 }
1983 break;
1984 case EOpConvBoolToFloat:
1985 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04001986 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001987 switch (node->getOperand()->getType().getNominalSize())
1988 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001989 case 1: outputTriplet(visit, "float(", "", ")"); break;
1990 case 2: outputTriplet(visit, "float2(", "", ")"); break;
1991 case 3: outputTriplet(visit, "float3(", "", ")"); break;
1992 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001993 default: UNREACHABLE();
1994 }
1995 break;
1996 case EOpConvFloatToInt:
1997 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04001998 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001999 switch (node->getOperand()->getType().getNominalSize())
2000 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002001 case 1: outputTriplet(visit, "int(", "", ")"); break;
2002 case 2: outputTriplet(visit, "int2(", "", ")"); break;
2003 case 3: outputTriplet(visit, "int3(", "", ")"); break;
2004 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002005 default: UNREACHABLE();
2006 }
2007 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002008 case EOpConvFloatToUInt:
2009 case EOpConvBoolToUInt:
2010 case EOpConvIntToUInt:
Jamie Madilla16f1672013-07-03 15:15:17 -04002011 switch (node->getOperand()->getType().getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002012 {
2013 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002014 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
2015 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
2016 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002017 default: UNREACHABLE();
2018 }
2019 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002020 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
2021 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
2022 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
2023 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
2024 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
2025 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
2026 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
2027 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
2028 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
2029 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
2030 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
2031 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
2032 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
2033 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
2034 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
2035 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
2036 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
2037 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
2038 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
2039 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
2040 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002041 case EOpDFdx:
2042 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2043 {
2044 outputTriplet(visit, "(", "", ", 0.0)");
2045 }
2046 else
2047 {
2048 outputTriplet(visit, "ddx(", "", ")");
2049 }
2050 break;
2051 case EOpDFdy:
2052 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2053 {
2054 outputTriplet(visit, "(", "", ", 0.0)");
2055 }
2056 else
2057 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00002058 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002059 }
2060 break;
2061 case EOpFwidth:
2062 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2063 {
2064 outputTriplet(visit, "(", "", ", 0.0)");
2065 }
2066 else
2067 {
2068 outputTriplet(visit, "fwidth(", "", ")");
2069 }
2070 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002071 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
2072 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002073 default: UNREACHABLE();
2074 }
2075
2076 return true;
2077}
2078
2079bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2080{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002081 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002082
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002083 switch (node->getOp())
2084 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002085 case EOpSequence:
2086 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002087 if (mInsideFunction)
2088 {
Jamie Madill075edd82013-07-08 13:30:19 -04002089 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002090 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002091
2092 mScopeDepth++;
2093
2094 if (mScopeBracket.size() < mScopeDepth)
2095 {
2096 mScopeBracket.push_back(0); // New scope level
2097 }
2098 else
2099 {
2100 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
2101 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002102 }
2103
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002104 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
2105 {
Jamie Madill075edd82013-07-08 13:30:19 -04002106 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002107
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002108 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002109
2110 out << ";\n";
2111 }
2112
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002113 if (mInsideFunction)
2114 {
Jamie Madill075edd82013-07-08 13:30:19 -04002115 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002116 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002117
2118 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002119 }
2120
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002121 return false;
2122 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002123 case EOpDeclaration:
2124 if (visit == PreVisit)
2125 {
2126 TIntermSequence &sequence = node->getSequence();
2127 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002128
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00002129 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002130 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002131 if (variable->getType().getStruct())
2132 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002133 addConstructor(variable->getType(), scopedStruct(variable->getType().getStruct()->name()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00002134 }
2135
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002136 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002137 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00002138 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002139 {
2140 out << "static ";
2141 }
2142
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002143 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002144
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002145 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002146 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002147 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002148
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002149 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002150 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002151 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002152 out << arrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05002153 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002154 }
2155 else
2156 {
2157 (*sit)->traverse(this);
2158 }
2159
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002160 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002161 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002162 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002163 }
2164 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002165 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002166 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2167 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002168 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002169 }
2170 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002171 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002172 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002173 {
2174 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2175 {
2176 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2177
2178 if (symbol)
2179 {
2180 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2181 mReferencedVaryings[symbol->getSymbol()] = symbol;
2182 }
2183 else
2184 {
2185 (*sit)->traverse(this);
2186 }
2187 }
2188 }
2189
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002190 return false;
2191 }
2192 else if (visit == InVisit)
2193 {
2194 out << ", ";
2195 }
2196 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002197 case EOpPrototype:
2198 if (visit == PreVisit)
2199 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002200 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002201
2202 TIntermSequence &arguments = node->getSequence();
2203
2204 for (unsigned int i = 0; i < arguments.size(); i++)
2205 {
2206 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2207
2208 if (symbol)
2209 {
2210 out << argumentString(symbol);
2211
2212 if (i < arguments.size() - 1)
2213 {
2214 out << ", ";
2215 }
2216 }
2217 else UNREACHABLE();
2218 }
2219
2220 out << ");\n";
2221
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002222 // Also prototype the Lod0 variant if needed
2223 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2224 {
2225 mOutputLod0Function = true;
2226 node->traverse(this);
2227 mOutputLod0Function = false;
2228 }
2229
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002230 return false;
2231 }
2232 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002233 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002234 case EOpFunction:
2235 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002236 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002237
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002238 out << typeString(node->getType()) << " ";
2239
2240 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002241 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002242 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002243 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002244 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002245 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002246 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002247 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002248
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002249 TIntermSequence &sequence = node->getSequence();
2250 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2251
2252 for (unsigned int i = 0; i < arguments.size(); i++)
2253 {
2254 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2255
2256 if (symbol)
2257 {
2258 if (symbol->getType().getStruct())
2259 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002260 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getStruct()->name()), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002261 }
2262
2263 out << argumentString(symbol);
2264
2265 if (i < arguments.size() - 1)
2266 {
2267 out << ", ";
2268 }
2269 }
2270 else UNREACHABLE();
2271 }
2272
2273 out << ")\n"
2274 "{\n";
2275
2276 if (sequence.size() > 1)
2277 {
2278 mInsideFunction = true;
2279 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002280 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002281 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002282
2283 out << "}\n";
2284
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002285 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2286 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002287 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002288 {
2289 mOutputLod0Function = true;
2290 node->traverse(this);
2291 mOutputLod0Function = false;
2292 }
2293 }
2294
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002295 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002296 }
2297 break;
2298 case EOpFunctionCall:
2299 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002300 TString name = TFunction::unmangleName(node->getName());
2301 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002302 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002303
2304 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002305 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002306 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002307 }
2308 else
2309 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002310 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2311
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002312 TextureFunction textureFunction;
2313 textureFunction.sampler = samplerType;
2314 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002315 textureFunction.method = TextureFunction::IMPLICIT;
2316 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002317 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002318
2319 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002320 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002321 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002322 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002323 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002324 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002325 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002326 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002327 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002328 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002329 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002330 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002331 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002332 else if (name == "texture2DProjLod" || name == "textureProjLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002333 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002334 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002335 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002336 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002337 else if (name == "textureSize")
2338 {
2339 textureFunction.method = TextureFunction::SIZE;
2340 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002341 else if (name == "textureOffset")
2342 {
2343 textureFunction.method = TextureFunction::IMPLICIT;
2344 textureFunction.offset = true;
2345 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002346 else if (name == "textureProjOffset")
2347 {
2348 textureFunction.method = TextureFunction::IMPLICIT;
2349 textureFunction.offset = true;
2350 textureFunction.proj = true;
2351 }
2352 else if (name == "textureLodOffset")
2353 {
2354 textureFunction.method = TextureFunction::LOD;
2355 textureFunction.offset = true;
2356 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002357 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002358
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002359 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002360 {
2361 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2362 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002363 textureFunction.method = TextureFunction::LOD0;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002364 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002365 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002366 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002367 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2368
2369 if (textureFunction.offset)
2370 {
2371 mandatoryArgumentCount++;
2372 }
2373
2374 if (arguments.size() > mandatoryArgumentCount) // Bias argument is optional
2375 {
2376 textureFunction.method = TextureFunction::BIAS;
2377 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002378 }
2379 }
2380
2381 mUsesTexture.insert(textureFunction);
2382
2383 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002384 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002385
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002386 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2387 {
2388 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2389 {
2390 out << "texture_";
2391 (*arg)->traverse(this);
2392 out << ", sampler_";
2393 }
2394
2395 (*arg)->traverse(this);
2396
2397 if (arg < arguments.end() - 1)
2398 {
2399 out << ", ";
2400 }
2401 }
2402
2403 out << ")";
2404
2405 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002406 }
2407 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002408 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002409 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002410 addConstructor(node->getType(), "vec1", &node->getSequence());
2411 outputTriplet(visit, "vec1(", "", ")");
2412 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002413 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002414 addConstructor(node->getType(), "vec2", &node->getSequence());
2415 outputTriplet(visit, "vec2(", ", ", ")");
2416 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002417 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002418 addConstructor(node->getType(), "vec3", &node->getSequence());
2419 outputTriplet(visit, "vec3(", ", ", ")");
2420 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002421 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002422 addConstructor(node->getType(), "vec4", &node->getSequence());
2423 outputTriplet(visit, "vec4(", ", ", ")");
2424 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002425 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002426 addConstructor(node->getType(), "bvec1", &node->getSequence());
2427 outputTriplet(visit, "bvec1(", "", ")");
2428 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002429 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002430 addConstructor(node->getType(), "bvec2", &node->getSequence());
2431 outputTriplet(visit, "bvec2(", ", ", ")");
2432 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002433 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002434 addConstructor(node->getType(), "bvec3", &node->getSequence());
2435 outputTriplet(visit, "bvec3(", ", ", ")");
2436 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002437 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002438 addConstructor(node->getType(), "bvec4", &node->getSequence());
2439 outputTriplet(visit, "bvec4(", ", ", ")");
2440 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002441 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002442 addConstructor(node->getType(), "ivec1", &node->getSequence());
2443 outputTriplet(visit, "ivec1(", "", ")");
2444 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002445 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002446 addConstructor(node->getType(), "ivec2", &node->getSequence());
2447 outputTriplet(visit, "ivec2(", ", ", ")");
2448 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002449 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002450 addConstructor(node->getType(), "ivec3", &node->getSequence());
2451 outputTriplet(visit, "ivec3(", ", ", ")");
2452 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002453 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002454 addConstructor(node->getType(), "ivec4", &node->getSequence());
2455 outputTriplet(visit, "ivec4(", ", ", ")");
2456 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002457 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002458 addConstructor(node->getType(), "uvec1", &node->getSequence());
2459 outputTriplet(visit, "uvec1(", "", ")");
2460 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002461 case EOpConstructUVec2:
2462 addConstructor(node->getType(), "uvec2", &node->getSequence());
2463 outputTriplet(visit, "uvec2(", ", ", ")");
2464 break;
2465 case EOpConstructUVec3:
2466 addConstructor(node->getType(), "uvec3", &node->getSequence());
2467 outputTriplet(visit, "uvec3(", ", ", ")");
2468 break;
2469 case EOpConstructUVec4:
2470 addConstructor(node->getType(), "uvec4", &node->getSequence());
2471 outputTriplet(visit, "uvec4(", ", ", ")");
2472 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002473 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002474 addConstructor(node->getType(), "mat2", &node->getSequence());
2475 outputTriplet(visit, "mat2(", ", ", ")");
2476 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002477 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002478 addConstructor(node->getType(), "mat3", &node->getSequence());
2479 outputTriplet(visit, "mat3(", ", ", ")");
2480 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002481 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002482 addConstructor(node->getType(), "mat4", &node->getSequence());
2483 outputTriplet(visit, "mat4(", ", ", ")");
2484 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002485 case EOpConstructStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04002486 addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence());
2487 outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002488 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002489 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2490 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2491 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2492 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2493 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2494 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002495 case EOpMod:
2496 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002497 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002498 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2499 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2500 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002501 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002502 case 11: mUsesMod1 = true; break;
2503 case 22: mUsesMod2v = true; break;
2504 case 21: mUsesMod2f = true; break;
2505 case 33: mUsesMod3v = true; break;
2506 case 31: mUsesMod3f = true; break;
2507 case 44: mUsesMod4v = true; break;
2508 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002509 default: UNREACHABLE();
2510 }
2511
2512 outputTriplet(visit, "mod(", ", ", ")");
2513 }
2514 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002515 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002516 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002517 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002518 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2519 {
2520 case 1: mUsesAtan2_1 = true; break;
2521 case 2: mUsesAtan2_2 = true; break;
2522 case 3: mUsesAtan2_3 = true; break;
2523 case 4: mUsesAtan2_4 = true; break;
2524 default: UNREACHABLE();
2525 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002526 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002527 break;
2528 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2529 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2530 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2531 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2532 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2533 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2534 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2535 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2536 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002537 case EOpFaceForward:
2538 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002539 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002540 {
2541 case 1: mUsesFaceforward1 = true; break;
2542 case 2: mUsesFaceforward2 = true; break;
2543 case 3: mUsesFaceforward3 = true; break;
2544 case 4: mUsesFaceforward4 = true; break;
2545 default: UNREACHABLE();
2546 }
2547
2548 outputTriplet(visit, "faceforward(", ", ", ")");
2549 }
2550 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002551 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2552 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2553 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002554 default: UNREACHABLE();
2555 }
2556
2557 return true;
2558}
2559
2560bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2561{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002562 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002563
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002564 if (node->usesTernaryOperator())
2565 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002566 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002567 }
2568 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002569 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002570 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002571
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002572 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002573
2574 node->getCondition()->traverse(this);
2575
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002576 out << ")\n";
2577
Jamie Madill075edd82013-07-08 13:30:19 -04002578 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002579 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002580
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002581 bool discard = false;
2582
daniel@transgaming.combb885322010-04-15 20:45:24 +00002583 if (node->getTrueBlock())
2584 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002585 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002586
2587 // Detect true discard
2588 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002589 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002590
Jamie Madill075edd82013-07-08 13:30:19 -04002591 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002592 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002593
2594 if (node->getFalseBlock())
2595 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002596 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002597
Jamie Madill075edd82013-07-08 13:30:19 -04002598 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002599 out << "{\n";
2600
Jamie Madill075edd82013-07-08 13:30:19 -04002601 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002602 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002603
Jamie Madill075edd82013-07-08 13:30:19 -04002604 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002605 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002606
2607 // Detect false discard
2608 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2609 }
2610
2611 // ANGLE issue 486: Detect problematic conditional discard
2612 if (discard && FindSideEffectRewriting::search(node))
2613 {
2614 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002615 }
2616 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002617
2618 return false;
2619}
2620
2621void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2622{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002623 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002624}
2625
2626bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2627{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002628 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2629
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002630 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002631 {
2632 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2633 }
2634
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002635 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002636 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002637 if (handleExcessiveLoop(node))
2638 {
2639 return false;
2640 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002641 }
2642
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002643 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002644
alokp@chromium.org52813552010-11-16 18:36:09 +00002645 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002646 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002647 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002648
Jamie Madill075edd82013-07-08 13:30:19 -04002649 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002650 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002651 }
2652 else
2653 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002654 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002655
2656 if (node->getInit())
2657 {
2658 node->getInit()->traverse(this);
2659 }
2660
2661 out << "; ";
2662
alokp@chromium.org52813552010-11-16 18:36:09 +00002663 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002664 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002665 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002666 }
2667
2668 out << "; ";
2669
alokp@chromium.org52813552010-11-16 18:36:09 +00002670 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002671 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002672 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002673 }
2674
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002675 out << ")\n";
2676
Jamie Madill075edd82013-07-08 13:30:19 -04002677 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002678 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002679 }
2680
2681 if (node->getBody())
2682 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002683 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002684 }
2685
Jamie Madill075edd82013-07-08 13:30:19 -04002686 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002687 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002688
alokp@chromium.org52813552010-11-16 18:36:09 +00002689 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002690 {
Jamie Madill075edd82013-07-08 13:30:19 -04002691 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002692 out << "while(\n";
2693
alokp@chromium.org52813552010-11-16 18:36:09 +00002694 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002695
daniel@transgaming.com73536982012-03-21 20:45:49 +00002696 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002697 }
2698
daniel@transgaming.com73536982012-03-21 20:45:49 +00002699 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002700
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002701 mInsideDiscontinuousLoop = wasDiscontinuous;
2702
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002703 return false;
2704}
2705
2706bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2707{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002708 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002709
2710 switch (node->getFlowOp())
2711 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002712 case EOpKill:
2713 outputTriplet(visit, "discard;\n", "", "");
2714 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002715 case EOpBreak:
2716 if (visit == PreVisit)
2717 {
2718 if (mExcessiveLoopIndex)
2719 {
2720 out << "{Break";
2721 mExcessiveLoopIndex->traverse(this);
2722 out << " = true; break;}\n";
2723 }
2724 else
2725 {
2726 out << "break;\n";
2727 }
2728 }
2729 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002730 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002731 case EOpReturn:
2732 if (visit == PreVisit)
2733 {
2734 if (node->getExpression())
2735 {
2736 out << "return ";
2737 }
2738 else
2739 {
2740 out << "return;\n";
2741 }
2742 }
2743 else if (visit == PostVisit)
2744 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002745 if (node->getExpression())
2746 {
2747 out << ";\n";
2748 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002749 }
2750 break;
2751 default: UNREACHABLE();
2752 }
2753
2754 return true;
2755}
2756
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002757void OutputHLSL::traverseStatements(TIntermNode *node)
2758{
2759 if (isSingleStatement(node))
2760 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002761 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002762 }
2763
2764 node->traverse(this);
2765}
2766
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002767bool OutputHLSL::isSingleStatement(TIntermNode *node)
2768{
2769 TIntermAggregate *aggregate = node->getAsAggregate();
2770
2771 if (aggregate)
2772 {
2773 if (aggregate->getOp() == EOpSequence)
2774 {
2775 return false;
2776 }
2777 else
2778 {
2779 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2780 {
2781 if (!isSingleStatement(*sit))
2782 {
2783 return false;
2784 }
2785 }
2786
2787 return true;
2788 }
2789 }
2790
2791 return true;
2792}
2793
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002794// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2795// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002796bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2797{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002798 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002799 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002800
2801 // Parse loops of the form:
2802 // for(int index = initial; index [comparator] limit; index += increment)
2803 TIntermSymbol *index = NULL;
2804 TOperator comparator = EOpNull;
2805 int initial = 0;
2806 int limit = 0;
2807 int increment = 0;
2808
2809 // Parse index name and intial value
2810 if (node->getInit())
2811 {
2812 TIntermAggregate *init = node->getInit()->getAsAggregate();
2813
2814 if (init)
2815 {
2816 TIntermSequence &sequence = init->getSequence();
2817 TIntermTyped *variable = sequence[0]->getAsTyped();
2818
2819 if (variable && variable->getQualifier() == EvqTemporary)
2820 {
2821 TIntermBinary *assign = variable->getAsBinaryNode();
2822
2823 if (assign->getOp() == EOpInitialize)
2824 {
2825 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2826 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2827
2828 if (symbol && constant)
2829 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002830 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002831 {
2832 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002833 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002834 }
2835 }
2836 }
2837 }
2838 }
2839 }
2840
2841 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002842 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002843 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002844 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002845
2846 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2847 {
2848 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2849
2850 if (constant)
2851 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002852 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002853 {
2854 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002855 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002856 }
2857 }
2858 }
2859 }
2860
2861 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002862 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002863 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002864 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2865 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002866
2867 if (binaryTerminal)
2868 {
2869 TOperator op = binaryTerminal->getOp();
2870 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2871
2872 if (constant)
2873 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002874 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002875 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002876 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002877
2878 switch (op)
2879 {
2880 case EOpAddAssign: increment = value; break;
2881 case EOpSubAssign: increment = -value; break;
2882 default: UNIMPLEMENTED();
2883 }
2884 }
2885 }
2886 }
2887 else if (unaryTerminal)
2888 {
2889 TOperator op = unaryTerminal->getOp();
2890
2891 switch (op)
2892 {
2893 case EOpPostIncrement: increment = 1; break;
2894 case EOpPostDecrement: increment = -1; break;
2895 case EOpPreIncrement: increment = 1; break;
2896 case EOpPreDecrement: increment = -1; break;
2897 default: UNIMPLEMENTED();
2898 }
2899 }
2900 }
2901
2902 if (index != NULL && comparator != EOpNull && increment != 0)
2903 {
2904 if (comparator == EOpLessThanEqual)
2905 {
2906 comparator = EOpLessThan;
2907 limit += 1;
2908 }
2909
2910 if (comparator == EOpLessThan)
2911 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002912 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002913
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002914 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002915 {
2916 return false; // Not an excessive loop
2917 }
2918
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002919 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2920 mExcessiveLoopIndex = index;
2921
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002922 out << "{int ";
2923 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002924 out << ";\n"
2925 "bool Break";
2926 index->traverse(this);
2927 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002928
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002929 bool firstLoopFragment = true;
2930
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002931 while (iterations > 0)
2932 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002933 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002934
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002935 if (!firstLoopFragment)
2936 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002937 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002938 index->traverse(this);
2939 out << ") {\n";
2940 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002941
2942 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2943 {
2944 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2945 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002946
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002947 // for(int index = initial; index < clampedLimit; index += increment)
2948
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002949 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002950 index->traverse(this);
2951 out << " = ";
2952 out << initial;
2953
2954 out << "; ";
2955 index->traverse(this);
2956 out << " < ";
2957 out << clampedLimit;
2958
2959 out << "; ";
2960 index->traverse(this);
2961 out << " += ";
2962 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002963 out << ")\n";
2964
Jamie Madill075edd82013-07-08 13:30:19 -04002965 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002966 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002967
2968 if (node->getBody())
2969 {
2970 node->getBody()->traverse(this);
2971 }
2972
Jamie Madill075edd82013-07-08 13:30:19 -04002973 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002974 out << ";}\n";
2975
2976 if (!firstLoopFragment)
2977 {
2978 out << "}\n";
2979 }
2980
2981 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002982
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002983 initial += MAX_LOOP_ITERATIONS * increment;
2984 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002985 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002986
2987 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002988
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002989 mExcessiveLoopIndex = restoreIndex;
2990
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002991 return true;
2992 }
2993 else UNIMPLEMENTED();
2994 }
2995
2996 return false; // Not handled as an excessive loop
2997}
2998
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002999void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003000{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00003001 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003002
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003003 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003004 {
3005 out << preString;
3006 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003007 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003008 {
3009 out << inString;
3010 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003011 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003012 {
3013 out << postString;
3014 }
3015}
3016
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003017void OutputHLSL::outputLineDirective(int line)
3018{
3019 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
3020 {
baustin@google.com8ab69842011-06-02 21:53:45 +00003021 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003022 mBody << "#line " << line;
3023
3024 if (mContext.sourcePath)
3025 {
3026 mBody << " \"" << mContext.sourcePath << "\"";
3027 }
3028
3029 mBody << "\n";
3030 }
3031}
3032
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003033TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3034{
3035 TQualifier qualifier = symbol->getQualifier();
3036 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003037 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003038
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003039 if (name.empty()) // HLSL demands named arguments, also for prototypes
3040 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00003041 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003042 }
3043 else
3044 {
3045 name = decorate(name);
3046 }
3047
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003048 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
3049 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003050 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
3051 qualifierString(qualifier) + " " + samplerString(type) + " sampler_" + name + arrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003052 }
3053
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003054 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003055}
3056
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003057TString OutputHLSL::interpolationString(TQualifier qualifier)
3058{
3059 switch(qualifier)
3060 {
3061 case EvqVaryingIn: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003062 case EvqFragmentIn: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003063 case EvqInvariantVaryingIn: return "";
3064 case EvqSmoothIn: return "linear";
3065 case EvqFlatIn: return "nointerpolation";
3066 case EvqCentroidIn: return "centroid";
3067 case EvqVaryingOut: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003068 case EvqVertexOut: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003069 case EvqInvariantVaryingOut: return "";
3070 case EvqSmoothOut: return "linear";
3071 case EvqFlatOut: return "nointerpolation";
3072 case EvqCentroidOut: return "centroid";
3073 default: UNREACHABLE();
3074 }
3075
3076 return "";
3077}
3078
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003079TString OutputHLSL::qualifierString(TQualifier qualifier)
3080{
3081 switch(qualifier)
3082 {
3083 case EvqIn: return "in";
3084 case EvqOut: return "out";
3085 case EvqInOut: return "inout";
3086 case EvqConstReadOnly: return "const";
3087 default: UNREACHABLE();
3088 }
3089
3090 return "";
3091}
3092
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003093TString OutputHLSL::typeString(const TType &type)
3094{
Jamie Madill98493dd2013-07-08 14:39:03 -04003095 const TStructure* structure = type.getStruct();
3096 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003097 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003098 const TString& typeName = structure->name();
3099 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003100 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003101 return structLookup(typeName);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003102 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003103 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003104 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003105 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003106 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003107 }
3108 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003109 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003110 int cols = type.getCols();
3111 int rows = type.getRows();
3112 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003113 }
3114 else
3115 {
3116 switch (type.getBasicType())
3117 {
3118 case EbtFloat:
3119 switch (type.getNominalSize())
3120 {
3121 case 1: return "float";
3122 case 2: return "float2";
3123 case 3: return "float3";
3124 case 4: return "float4";
3125 }
3126 case EbtInt:
3127 switch (type.getNominalSize())
3128 {
3129 case 1: return "int";
3130 case 2: return "int2";
3131 case 3: return "int3";
3132 case 4: return "int4";
3133 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003134 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04003135 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003136 {
3137 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003138 case 2: return "uint2";
3139 case 3: return "uint3";
3140 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003141 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003142 case EbtBool:
3143 switch (type.getNominalSize())
3144 {
3145 case 1: return "bool";
3146 case 2: return "bool2";
3147 case 3: return "bool3";
3148 case 4: return "bool4";
3149 }
3150 case EbtVoid:
3151 return "void";
3152 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003153 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04003154 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003155 case EbtSampler2DArray:
3156 case EbtISampler2DArray:
3157 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003158 return "sampler2D";
3159 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003160 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04003161 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003162 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00003163 case EbtSamplerExternalOES:
3164 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00003165 default:
3166 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003167 }
3168 }
3169
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003170 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003171 return "<unknown type>";
3172}
3173
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003174TString OutputHLSL::textureString(const TType &type)
3175{
3176 switch (type.getBasicType())
3177 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003178 case EbtSampler2D: return "Texture2D";
3179 case EbtSamplerCube: return "TextureCube";
3180 case EbtSamplerExternalOES: return "Texture2D";
3181 case EbtSampler2DArray: return "Texture2DArray";
3182 case EbtSampler3D: return "Texture3D";
3183 case EbtISampler2D: return "Texture2D<int4>";
3184 case EbtISampler3D: return "Texture3D<int4>";
3185 case EbtISamplerCube: return "TextureCube<int4>";
3186 case EbtISampler2DArray: return "Texture2DArray<int4>";
3187 case EbtUSampler2D: return "Texture2D<uint4>";
3188 case EbtUSampler3D: return "Texture3D<uint4>";
3189 case EbtUSamplerCube: return "TextureCube<uint4>";
3190 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
3191 case EbtSampler2DShadow: return "Texture2D";
3192 case EbtSamplerCubeShadow: return "TextureCube";
3193 case EbtSampler2DArrayShadow: return "Texture2DArray";
3194 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003195 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04003196
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003197 return "<unknown texture type>";
3198}
3199
Nicolas Capenscb127d32013-07-15 17:26:18 -04003200TString OutputHLSL::samplerString(const TType &type)
3201{
3202 if (IsShadowSampler(type.getBasicType()))
3203 {
3204 return "SamplerComparisonState";
3205 }
3206 else
3207 {
3208 return "SamplerState";
3209 }
3210}
3211
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003212TString OutputHLSL::arrayString(const TType &type)
3213{
3214 if (!type.isArray())
3215 {
3216 return "";
3217 }
3218
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003219 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003220}
3221
3222TString OutputHLSL::initializer(const TType &type)
3223{
3224 TString string;
3225
Jamie Madill94bf7f22013-07-08 13:31:15 -04003226 size_t size = type.getObjectSize();
3227 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003228 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003229 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003230
Jamie Madill94bf7f22013-07-08 13:31:15 -04003231 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003232 {
3233 string += ", ";
3234 }
3235 }
3236
daniel@transgaming.comead23042010-04-29 03:35:36 +00003237 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003238}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003239
Jamie Madill98493dd2013-07-08 14:39:03 -04003240TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003241{
Jamie Madill98493dd2013-07-08 14:39:03 -04003242 const TFieldList &fields = structure.fields();
3243 const bool isNameless = (structure.name() == "");
3244 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003245 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3246
Jamie Madill98493dd2013-07-08 14:39:03 -04003247 TString string;
3248 string += declareString + "\n"
3249 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003250
Jamie Madillc835df62013-06-21 09:15:32 -04003251 int elementIndex = 0;
3252
Jamie Madill9cf6c072013-06-20 11:55:53 -04003253 for (unsigned int i = 0; i < fields.size(); i++)
3254 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003255 const TField &field = *fields[i];
3256 const TType &fieldType = *field.type();
3257 const TStructure *fieldStruct = fieldType.getStruct();
3258 const TString &fieldTypeString = fieldStruct ? structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) : typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003259
Jamie Madillc835df62013-06-21 09:15:32 -04003260 if (useStd140Packing)
3261 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003262 string += std140PrePaddingString(*field.type(), &elementIndex);
Jamie Madillc835df62013-06-21 09:15:32 -04003263 }
3264
Jamie Madill98493dd2013-07-08 14:39:03 -04003265 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003266
3267 if (useStd140Packing)
3268 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003269 string += std140PostPaddingString(*field.type(), useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003270 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003271 }
3272
3273 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003274 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003275
Jamie Madille4075c92013-06-21 09:15:32 -04003276 // Add remaining element index to the global map, for use with nested structs in standard layouts
3277 if (useStd140Packing)
3278 {
3279 mStd140StructElementIndexes[structName] = elementIndex;
3280 }
3281
Jamie Madill98493dd2013-07-08 14:39:03 -04003282 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003283}
3284
Jamie Madill98493dd2013-07-08 14:39:03 -04003285TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003286{
Jamie Madill98493dd2013-07-08 14:39:03 -04003287 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003288 {
3289 return "";
3290 }
3291
3292 TString prefix = "";
3293
3294 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3295 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003296
3297 if (useStd140Packing)
3298 {
3299 prefix += "std";
3300 }
3301
Jamie Madill9cf6c072013-06-20 11:55:53 -04003302 if (useHLSLRowMajorPacking)
3303 {
Jamie Madillc835df62013-06-21 09:15:32 -04003304 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003305 prefix += "rm";
3306 }
3307
Jamie Madill98493dd2013-07-08 14:39:03 -04003308 return prefix + structLookup(structure.name());
Jamie Madill9cf6c072013-06-20 11:55:53 -04003309}
3310
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003311void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003312{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003313 if (name == "")
3314 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003315 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003316 }
3317
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003318 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
3319 {
3320 return; // Already added
3321 }
3322
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003323 TType ctorType = type;
3324 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003325 ctorType.setPrecision(EbpHigh);
3326 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003327
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003328 TString ctorName = type.getStruct() ? decorate(name) : name;
3329
3330 typedef std::vector<TType> ParameterArray;
3331 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003332
Jamie Madill98493dd2013-07-08 14:39:03 -04003333 const TStructure* structure = type.getStruct();
3334 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003335 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003336 mStructNames.insert(decorate(name));
3337
Jamie Madill98493dd2013-07-08 14:39:03 -04003338 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003339
Jamie Madill98493dd2013-07-08 14:39:03 -04003340 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003341 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003342 // Add row-major packed struct for interface blocks
3343 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003344 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003345 "#pragma pack_matrix(column_major)\n";
3346
Jamie Madillc835df62013-06-21 09:15:32 -04003347 const TString &std140Prefix = "std";
Jamie Madill98493dd2013-07-08 14:39:03 -04003348 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003349
3350 const TString &std140RowMajorPrefix = "std_rm";
3351 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003352 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003353 "#pragma pack_matrix(column_major)\n";
3354
Jamie Madill98493dd2013-07-08 14:39:03 -04003355 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003356 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003357 mStructDeclarations.push_back(std140String);
3358 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003359 }
3360
Jamie Madill98493dd2013-07-08 14:39:03 -04003361 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003362 for (unsigned int i = 0; i < fields.size(); i++)
3363 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003364 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003365 }
3366 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003367 else if (parameters)
3368 {
3369 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3370 {
3371 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3372 }
3373 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003374 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003375
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003376 TString constructor;
3377
3378 if (ctorType.getStruct())
3379 {
3380 constructor += ctorName + " " + ctorName + "_ctor(";
3381 }
3382 else // Built-in type
3383 {
3384 constructor += typeString(ctorType) + " " + ctorName + "(";
3385 }
3386
3387 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3388 {
3389 const TType &type = ctorParameters[parameter];
3390
3391 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3392
3393 if (parameter < ctorParameters.size() - 1)
3394 {
3395 constructor += ", ";
3396 }
3397 }
3398
3399 constructor += ")\n"
3400 "{\n";
3401
3402 if (ctorType.getStruct())
3403 {
3404 constructor += " " + ctorName + " structure = {";
3405 }
3406 else
3407 {
3408 constructor += " return " + typeString(ctorType) + "(";
3409 }
3410
3411 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3412 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003413 int rows = ctorType.getRows();
3414 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003415 const TType &parameter = ctorParameters[0];
3416
3417 if (parameter.isScalar())
3418 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003419 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003420 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003421 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003422 {
3423 constructor += TString((row == col) ? "x0" : "0.0");
3424
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003425 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003426 {
3427 constructor += ", ";
3428 }
3429 }
3430 }
3431 }
3432 else if (parameter.isMatrix())
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 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003438 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003439 {
3440 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3441 }
3442 else
3443 {
3444 constructor += TString((row == col) ? "1.0" : "0.0");
3445 }
3446
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003447 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003448 {
3449 constructor += ", ";
3450 }
3451 }
3452 }
3453 }
3454 else UNREACHABLE();
3455 }
3456 else
3457 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003458 size_t remainingComponents = ctorType.getObjectSize();
3459 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003460
3461 while (remainingComponents > 0)
3462 {
3463 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003464 const size_t parameterSize = parameter.getObjectSize();
3465 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003466
3467 constructor += "x" + str(parameterIndex);
3468
3469 if (parameter.isScalar())
3470 {
3471 remainingComponents -= parameter.getObjectSize();
3472 }
3473 else if (parameter.isVector())
3474 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003475 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003476 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003477 ASSERT(parameterSize <= remainingComponents);
3478 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003479 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003480 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003481 {
3482 switch (remainingComponents)
3483 {
3484 case 1: constructor += ".x"; break;
3485 case 2: constructor += ".xy"; break;
3486 case 3: constructor += ".xyz"; break;
3487 case 4: constructor += ".xyzw"; break;
3488 default: UNREACHABLE();
3489 }
3490
3491 remainingComponents = 0;
3492 }
3493 else UNREACHABLE();
3494 }
3495 else if (parameter.isMatrix() || parameter.getStruct())
3496 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003497 ASSERT(remainingComponents == parameterSize || moreParameters);
3498 ASSERT(parameterSize <= remainingComponents);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003499
Jamie Madill94bf7f22013-07-08 13:31:15 -04003500 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003501 }
3502 else UNREACHABLE();
3503
3504 if (moreParameters)
3505 {
3506 parameterIndex++;
3507 }
3508
3509 if (remainingComponents)
3510 {
3511 constructor += ", ";
3512 }
3513 }
3514 }
3515
3516 if (ctorType.getStruct())
3517 {
3518 constructor += "};\n"
3519 " return structure;\n"
3520 "}\n";
3521 }
3522 else
3523 {
3524 constructor += ");\n"
3525 "}\n";
3526 }
3527
daniel@transgaming.com63691862010-04-29 03:32:42 +00003528 mConstructors.insert(constructor);
3529}
3530
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003531const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3532{
3533 TInfoSinkBase &out = mBody;
3534
Jamie Madill98493dd2013-07-08 14:39:03 -04003535 const TStructure* structure = type.getStruct();
3536 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003537 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003538 out << structLookup(structure->name()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003539
Jamie Madill98493dd2013-07-08 14:39:03 -04003540 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003541
Jamie Madill98493dd2013-07-08 14:39:03 -04003542 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003543 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003544 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003545
3546 constUnion = writeConstantUnion(*fieldType, constUnion);
3547
Jamie Madill98493dd2013-07-08 14:39:03 -04003548 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003549 {
3550 out << ", ";
3551 }
3552 }
3553
3554 out << ")";
3555 }
3556 else
3557 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003558 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003559 bool writeType = size > 1;
3560
3561 if (writeType)
3562 {
3563 out << typeString(type) << "(";
3564 }
3565
Jamie Madill94bf7f22013-07-08 13:31:15 -04003566 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003567 {
3568 switch (constUnion->getType())
3569 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003570 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003571 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003572 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003573 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003574 default: UNREACHABLE();
3575 }
3576
3577 if (i != size - 1)
3578 {
3579 out << ", ";
3580 }
3581 }
3582
3583 if (writeType)
3584 {
3585 out << ")";
3586 }
3587 }
3588
3589 return constUnion;
3590}
3591
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003592TString OutputHLSL::scopeString(unsigned int depthLimit)
3593{
3594 TString string;
3595
3596 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3597 {
3598 string += "_" + str(i);
3599 }
3600
3601 return string;
3602}
3603
3604TString OutputHLSL::scopedStruct(const TString &typeName)
3605{
3606 if (typeName == "")
3607 {
3608 return typeName;
3609 }
3610
3611 return typeName + scopeString(mScopeDepth);
3612}
3613
3614TString OutputHLSL::structLookup(const TString &typeName)
3615{
3616 for (int depth = mScopeDepth; depth >= 0; depth--)
3617 {
3618 TString scopedName = decorate(typeName + scopeString(depth));
3619
3620 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3621 {
3622 if (*structName == scopedName)
3623 {
3624 return scopedName;
3625 }
3626 }
3627 }
3628
3629 UNREACHABLE(); // Should have found a matching constructor
3630
3631 return typeName;
3632}
3633
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003634TString OutputHLSL::decorate(const TString &string)
3635{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003636 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003637 {
3638 return "_" + string;
3639 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003640
3641 return string;
3642}
3643
apatrick@chromium.org65756022012-01-17 21:45:38 +00003644TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003645{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003646 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003647 {
3648 return "ex_" + string;
3649 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003650
3651 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003652}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003653
Jamie Madill98493dd2013-07-08 14:39:03 -04003654TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003655{
Jamie Madill98493dd2013-07-08 14:39:03 -04003656 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003657 {
3658 return decorate(string);
3659 }
3660
3661 return string;
3662}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003663
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003664void OutputHLSL::declareInterfaceBlockField(const TType &type, const TString &name, std::vector<InterfaceBlockField>& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003665{
Jamie Madill98493dd2013-07-08 14:39:03 -04003666 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003667
3668 if (!structure)
3669 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003670 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003671 InterfaceBlockField field(glVariableType(type), glVariablePrecision(type), name.c_str(),
3672 (unsigned int)type.getArraySize(), isRowMajorMatrix);
3673 output.push_back(field);
3674 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003675 else
3676 {
Jamie Madill28167c62013-08-30 13:21:10 -04003677 InterfaceBlockField structField(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), false);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003678
3679 const TFieldList &fields = structure->fields();
3680
3681 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3682 {
3683 TField *field = fields[fieldIndex];
3684 TType *fieldType = field->type();
3685
3686 // make sure to copy matrix packing information
3687 fieldType->setLayoutQualifier(type.getLayoutQualifier());
3688
3689 declareInterfaceBlockField(*fieldType, field->name(), structField.fields);
3690 }
3691
3692 output.push_back(structField);
3693 }
3694}
3695
Jamie Madillc2141fb2013-08-30 13:21:08 -04003696Uniform OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, std::vector<Uniform>& output)
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003697{
3698 const TStructure *structure = type.getStruct();
3699
3700 if (!structure)
3701 {
3702 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
3703 Uniform uniform(glVariableType(type), glVariablePrecision(type), name.c_str(),
Jamie Madill56093782013-08-30 13:21:11 -04003704 (unsigned int)type.getArraySize(), (unsigned int)registerIndex, 0);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003705 output.push_back(uniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003706
3707 return uniform;
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003708 }
3709 else
3710 {
Jamie Madill56093782013-08-30 13:21:11 -04003711 Uniform structUniform(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(),
3712 (unsigned int)registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003713
Jamie Madill98493dd2013-07-08 14:39:03 -04003714 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003715
Jamie Madill98493dd2013-07-08 14:39:03 -04003716 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003717 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003718 TField *field = fields[fieldIndex];
3719 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003720
Jamie Madill56093782013-08-30 13:21:11 -04003721 declareUniformToList(*fieldType, field->name(), GL_INVALID_INDEX, structUniform.fields);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003722 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003723
Jamie Madill56093782013-08-30 13:21:11 -04003724 // assign register offset information -- this will override the information in any sub-structures.
3725 HLSLVariableGetRegisterInfo(registerIndex, &structUniform);
3726
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003727 output.push_back(structUniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003728
3729 return structUniform;
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003730 }
3731}
3732
Jamie Madill139b9092013-08-30 13:21:06 -04003733InterpolationType getInterpolationType(TQualifier qualifier)
3734{
3735 switch (qualifier)
3736 {
3737 case EvqFlatIn:
3738 case EvqFlatOut:
3739 return INTERPOLATION_FLAT;
3740
3741 case EvqSmoothIn:
3742 case EvqSmoothOut:
3743 case EvqVertexOut:
3744 case EvqFragmentIn:
Jamie Madill384b6042013-09-13 10:06:24 -04003745 case EvqVaryingIn:
3746 case EvqVaryingOut:
Jamie Madill139b9092013-08-30 13:21:06 -04003747 return INTERPOLATION_SMOOTH;
3748
3749 case EvqCentroidIn:
3750 case EvqCentroidOut:
3751 return INTERPOLATION_CENTROID;
3752
3753 default: UNREACHABLE();
3754 return INTERPOLATION_SMOOTH;
3755 }
3756}
3757
Jamie Madill94599662013-08-30 13:21:10 -04003758void OutputHLSL::declareVaryingToList(const TType &type, TQualifier baseTypeQualifier, const TString &name, std::vector<Varying>& fieldsOut)
Jamie Madill47fdd132013-08-30 13:21:04 -04003759{
3760 const TStructure *structure = type.getStruct();
3761
Jamie Madill94599662013-08-30 13:21:10 -04003762 InterpolationType interpolation = getInterpolationType(baseTypeQualifier);
Jamie Madill47fdd132013-08-30 13:21:04 -04003763 if (!structure)
3764 {
Jamie Madill139b9092013-08-30 13:21:06 -04003765 Varying varying(glVariableType(type), glVariablePrecision(type), name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003766 fieldsOut.push_back(varying);
3767 }
3768 else
3769 {
Jamie Madill28167c62013-08-30 13:21:10 -04003770 Varying structVarying(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003771 const TFieldList &fields = structure->fields();
3772
Jamie Madill28167c62013-08-30 13:21:10 -04003773 structVarying.structName = structure->name().c_str();
3774
Jamie Madill47fdd132013-08-30 13:21:04 -04003775 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3776 {
3777 const TField &field = *fields[fieldIndex];
Jamie Madill94599662013-08-30 13:21:10 -04003778 declareVaryingToList(*field.type(), baseTypeQualifier, field.name(), structVarying.fields);
Jamie Madill47fdd132013-08-30 13:21:04 -04003779 }
3780
3781 fieldsOut.push_back(structVarying);
3782 }
3783}
3784
Jamie Madillc2141fb2013-08-30 13:21:08 -04003785int OutputHLSL::declareUniformAndAssignRegister(const TType &type, const TString &name)
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003786{
Jamie Madillc2141fb2013-08-30 13:21:08 -04003787 int registerIndex = (IsSampler(type.getBasicType()) ? mSamplerRegister : mUniformRegister);
3788
3789 const Uniform &uniform = declareUniformToList(type, name, registerIndex, mActiveUniforms);
3790
3791 if (IsSampler(type.getBasicType()))
3792 {
3793 mSamplerRegister += HLSLVariableRegisterCount(uniform);
3794 }
3795 else
3796 {
3797 mUniformRegister += HLSLVariableRegisterCount(uniform);
3798 }
3799
3800 return registerIndex;
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003801}
3802
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003803GLenum OutputHLSL::glVariableType(const TType &type)
3804{
3805 if (type.getBasicType() == EbtFloat)
3806 {
3807 if (type.isScalar())
3808 {
3809 return GL_FLOAT;
3810 }
3811 else if (type.isVector())
3812 {
3813 switch(type.getNominalSize())
3814 {
3815 case 2: return GL_FLOAT_VEC2;
3816 case 3: return GL_FLOAT_VEC3;
3817 case 4: return GL_FLOAT_VEC4;
3818 default: UNREACHABLE();
3819 }
3820 }
3821 else if (type.isMatrix())
3822 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003823 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003824 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003825 case 2:
3826 switch(type.getRows())
3827 {
3828 case 2: return GL_FLOAT_MAT2;
3829 case 3: return GL_FLOAT_MAT2x3;
3830 case 4: return GL_FLOAT_MAT2x4;
3831 default: UNREACHABLE();
3832 }
3833
3834 case 3:
3835 switch(type.getRows())
3836 {
3837 case 2: return GL_FLOAT_MAT3x2;
3838 case 3: return GL_FLOAT_MAT3;
3839 case 4: return GL_FLOAT_MAT3x4;
3840 default: UNREACHABLE();
3841 }
3842
3843 case 4:
3844 switch(type.getRows())
3845 {
3846 case 2: return GL_FLOAT_MAT4x2;
3847 case 3: return GL_FLOAT_MAT4x3;
3848 case 4: return GL_FLOAT_MAT4;
3849 default: UNREACHABLE();
3850 }
3851
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003852 default: UNREACHABLE();
3853 }
3854 }
3855 else UNREACHABLE();
3856 }
3857 else if (type.getBasicType() == EbtInt)
3858 {
3859 if (type.isScalar())
3860 {
3861 return GL_INT;
3862 }
3863 else if (type.isVector())
3864 {
3865 switch(type.getNominalSize())
3866 {
3867 case 2: return GL_INT_VEC2;
3868 case 3: return GL_INT_VEC3;
3869 case 4: return GL_INT_VEC4;
3870 default: UNREACHABLE();
3871 }
3872 }
3873 else UNREACHABLE();
3874 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003875 else if (type.getBasicType() == EbtUInt)
3876 {
3877 if (type.isScalar())
3878 {
3879 return GL_UNSIGNED_INT;
3880 }
3881 else if (type.isVector())
3882 {
Jamie Madill22d63da2013-06-07 12:45:12 -04003883 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003884 {
3885 case 2: return GL_UNSIGNED_INT_VEC2;
3886 case 3: return GL_UNSIGNED_INT_VEC3;
3887 case 4: return GL_UNSIGNED_INT_VEC4;
3888 default: UNREACHABLE();
3889 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003890 }
3891 else UNREACHABLE();
3892 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003893 else if (type.getBasicType() == EbtBool)
3894 {
3895 if (type.isScalar())
3896 {
3897 return GL_BOOL;
3898 }
3899 else if (type.isVector())
3900 {
3901 switch(type.getNominalSize())
3902 {
3903 case 2: return GL_BOOL_VEC2;
3904 case 3: return GL_BOOL_VEC3;
3905 case 4: return GL_BOOL_VEC4;
3906 default: UNREACHABLE();
3907 }
3908 }
3909 else UNREACHABLE();
3910 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003911
3912 switch(type.getBasicType())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003913 {
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003914 case EbtSampler2D: return GL_SAMPLER_2D;
3915 case EbtSampler3D: return GL_SAMPLER_3D;
3916 case EbtSamplerCube: return GL_SAMPLER_CUBE;
3917 case EbtSampler2DArray: return GL_SAMPLER_2D_ARRAY;
3918 case EbtISampler2D: return GL_INT_SAMPLER_2D;
3919 case EbtISampler3D: return GL_INT_SAMPLER_3D;
3920 case EbtISamplerCube: return GL_INT_SAMPLER_CUBE;
3921 case EbtISampler2DArray: return GL_INT_SAMPLER_2D_ARRAY;
3922 case EbtUSampler2D: return GL_UNSIGNED_INT_SAMPLER_2D;
3923 case EbtUSampler3D: return GL_UNSIGNED_INT_SAMPLER_3D;
3924 case EbtUSamplerCube: return GL_UNSIGNED_INT_SAMPLER_CUBE;
3925 case EbtUSampler2DArray: return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3926 case EbtSampler2DShadow: return GL_SAMPLER_2D_SHADOW;
3927 case EbtSamplerCubeShadow: return GL_SAMPLER_CUBE_SHADOW;
3928 case EbtSampler2DArrayShadow: return GL_SAMPLER_2D_ARRAY_SHADOW;
3929 default: UNREACHABLE();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003930 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003931
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003932
3933 return GL_NONE;
3934}
3935
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003936GLenum OutputHLSL::glVariablePrecision(const TType &type)
3937{
3938 if (type.getBasicType() == EbtFloat)
3939 {
3940 switch (type.getPrecision())
3941 {
3942 case EbpHigh: return GL_HIGH_FLOAT;
3943 case EbpMedium: return GL_MEDIUM_FLOAT;
3944 case EbpLow: return GL_LOW_FLOAT;
3945 case EbpUndefined:
3946 // Should be defined as the default precision by the parser
3947 default: UNREACHABLE();
3948 }
3949 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003950 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003951 {
3952 switch (type.getPrecision())
3953 {
3954 case EbpHigh: return GL_HIGH_INT;
3955 case EbpMedium: return GL_MEDIUM_INT;
3956 case EbpLow: return GL_LOW_INT;
3957 case EbpUndefined:
3958 // Should be defined as the default precision by the parser
3959 default: UNREACHABLE();
3960 }
3961 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003962
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00003963 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003964 return GL_NONE;
3965}
3966
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003967bool OutputHLSL::isVaryingOut(TQualifier qualifier)
3968{
3969 switch(qualifier)
3970 {
3971 case EvqVaryingOut:
3972 case EvqInvariantVaryingOut:
3973 case EvqSmoothOut:
3974 case EvqFlatOut:
3975 case EvqCentroidOut:
Jamie Madill19571812013-08-12 15:26:34 -07003976 case EvqVertexOut:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003977 return true;
3978 }
3979
3980 return false;
3981}
3982
3983bool OutputHLSL::isVaryingIn(TQualifier qualifier)
3984{
3985 switch(qualifier)
3986 {
3987 case EvqVaryingIn:
3988 case EvqInvariantVaryingIn:
3989 case EvqSmoothIn:
3990 case EvqFlatIn:
3991 case EvqCentroidIn:
Jamie Madill19571812013-08-12 15:26:34 -07003992 case EvqFragmentIn:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003993 return true;
3994 }
3995
3996 return false;
3997}
3998
3999bool OutputHLSL::isVarying(TQualifier qualifier)
4000{
4001 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
4002}
4003
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004004}