blob: 0a7b9304185a2aacb939874ea1ca4594b78993f6 [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 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002346 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002347
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002348 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002349 {
2350 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2351 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002352 textureFunction.method = TextureFunction::LOD0;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002353 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002354 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002355 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002356 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2357
2358 if (textureFunction.offset)
2359 {
2360 mandatoryArgumentCount++;
2361 }
2362
2363 if (arguments.size() > mandatoryArgumentCount) // Bias argument is optional
2364 {
2365 textureFunction.method = TextureFunction::BIAS;
2366 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002367 }
2368 }
2369
2370 mUsesTexture.insert(textureFunction);
2371
2372 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002373 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002374
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002375 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2376 {
2377 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2378 {
2379 out << "texture_";
2380 (*arg)->traverse(this);
2381 out << ", sampler_";
2382 }
2383
2384 (*arg)->traverse(this);
2385
2386 if (arg < arguments.end() - 1)
2387 {
2388 out << ", ";
2389 }
2390 }
2391
2392 out << ")";
2393
2394 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002395 }
2396 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002397 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002398 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002399 addConstructor(node->getType(), "vec1", &node->getSequence());
2400 outputTriplet(visit, "vec1(", "", ")");
2401 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002402 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002403 addConstructor(node->getType(), "vec2", &node->getSequence());
2404 outputTriplet(visit, "vec2(", ", ", ")");
2405 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002406 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002407 addConstructor(node->getType(), "vec3", &node->getSequence());
2408 outputTriplet(visit, "vec3(", ", ", ")");
2409 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002410 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002411 addConstructor(node->getType(), "vec4", &node->getSequence());
2412 outputTriplet(visit, "vec4(", ", ", ")");
2413 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002414 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002415 addConstructor(node->getType(), "bvec1", &node->getSequence());
2416 outputTriplet(visit, "bvec1(", "", ")");
2417 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002418 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002419 addConstructor(node->getType(), "bvec2", &node->getSequence());
2420 outputTriplet(visit, "bvec2(", ", ", ")");
2421 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002422 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002423 addConstructor(node->getType(), "bvec3", &node->getSequence());
2424 outputTriplet(visit, "bvec3(", ", ", ")");
2425 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002426 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002427 addConstructor(node->getType(), "bvec4", &node->getSequence());
2428 outputTriplet(visit, "bvec4(", ", ", ")");
2429 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002430 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002431 addConstructor(node->getType(), "ivec1", &node->getSequence());
2432 outputTriplet(visit, "ivec1(", "", ")");
2433 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002434 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002435 addConstructor(node->getType(), "ivec2", &node->getSequence());
2436 outputTriplet(visit, "ivec2(", ", ", ")");
2437 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002438 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002439 addConstructor(node->getType(), "ivec3", &node->getSequence());
2440 outputTriplet(visit, "ivec3(", ", ", ")");
2441 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002442 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002443 addConstructor(node->getType(), "ivec4", &node->getSequence());
2444 outputTriplet(visit, "ivec4(", ", ", ")");
2445 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002446 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002447 addConstructor(node->getType(), "uvec1", &node->getSequence());
2448 outputTriplet(visit, "uvec1(", "", ")");
2449 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002450 case EOpConstructUVec2:
2451 addConstructor(node->getType(), "uvec2", &node->getSequence());
2452 outputTriplet(visit, "uvec2(", ", ", ")");
2453 break;
2454 case EOpConstructUVec3:
2455 addConstructor(node->getType(), "uvec3", &node->getSequence());
2456 outputTriplet(visit, "uvec3(", ", ", ")");
2457 break;
2458 case EOpConstructUVec4:
2459 addConstructor(node->getType(), "uvec4", &node->getSequence());
2460 outputTriplet(visit, "uvec4(", ", ", ")");
2461 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002462 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002463 addConstructor(node->getType(), "mat2", &node->getSequence());
2464 outputTriplet(visit, "mat2(", ", ", ")");
2465 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002466 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002467 addConstructor(node->getType(), "mat3", &node->getSequence());
2468 outputTriplet(visit, "mat3(", ", ", ")");
2469 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002470 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002471 addConstructor(node->getType(), "mat4", &node->getSequence());
2472 outputTriplet(visit, "mat4(", ", ", ")");
2473 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002474 case EOpConstructStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04002475 addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence());
2476 outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002477 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002478 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2479 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2480 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2481 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2482 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2483 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002484 case EOpMod:
2485 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002486 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002487 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2488 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2489 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002490 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002491 case 11: mUsesMod1 = true; break;
2492 case 22: mUsesMod2v = true; break;
2493 case 21: mUsesMod2f = true; break;
2494 case 33: mUsesMod3v = true; break;
2495 case 31: mUsesMod3f = true; break;
2496 case 44: mUsesMod4v = true; break;
2497 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002498 default: UNREACHABLE();
2499 }
2500
2501 outputTriplet(visit, "mod(", ", ", ")");
2502 }
2503 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002504 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002505 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002506 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002507 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2508 {
2509 case 1: mUsesAtan2_1 = true; break;
2510 case 2: mUsesAtan2_2 = true; break;
2511 case 3: mUsesAtan2_3 = true; break;
2512 case 4: mUsesAtan2_4 = true; break;
2513 default: UNREACHABLE();
2514 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002515 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002516 break;
2517 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2518 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2519 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2520 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2521 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2522 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2523 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2524 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2525 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002526 case EOpFaceForward:
2527 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002528 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002529 {
2530 case 1: mUsesFaceforward1 = true; break;
2531 case 2: mUsesFaceforward2 = true; break;
2532 case 3: mUsesFaceforward3 = true; break;
2533 case 4: mUsesFaceforward4 = true; break;
2534 default: UNREACHABLE();
2535 }
2536
2537 outputTriplet(visit, "faceforward(", ", ", ")");
2538 }
2539 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002540 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2541 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2542 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002543 default: UNREACHABLE();
2544 }
2545
2546 return true;
2547}
2548
2549bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2550{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002551 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002552
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002553 if (node->usesTernaryOperator())
2554 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002555 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002556 }
2557 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002558 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002559 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002560
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002561 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002562
2563 node->getCondition()->traverse(this);
2564
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002565 out << ")\n";
2566
Jamie Madill075edd82013-07-08 13:30:19 -04002567 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002568 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002569
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002570 bool discard = false;
2571
daniel@transgaming.combb885322010-04-15 20:45:24 +00002572 if (node->getTrueBlock())
2573 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002574 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002575
2576 // Detect true discard
2577 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002578 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002579
Jamie Madill075edd82013-07-08 13:30:19 -04002580 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002581 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002582
2583 if (node->getFalseBlock())
2584 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002585 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002586
Jamie Madill075edd82013-07-08 13:30:19 -04002587 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002588 out << "{\n";
2589
Jamie Madill075edd82013-07-08 13:30:19 -04002590 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002591 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002592
Jamie Madill075edd82013-07-08 13:30:19 -04002593 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002594 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002595
2596 // Detect false discard
2597 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2598 }
2599
2600 // ANGLE issue 486: Detect problematic conditional discard
2601 if (discard && FindSideEffectRewriting::search(node))
2602 {
2603 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002604 }
2605 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002606
2607 return false;
2608}
2609
2610void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2611{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002612 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002613}
2614
2615bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2616{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002617 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2618
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002619 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002620 {
2621 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2622 }
2623
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002624 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002625 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002626 if (handleExcessiveLoop(node))
2627 {
2628 return false;
2629 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002630 }
2631
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002632 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002633
alokp@chromium.org52813552010-11-16 18:36:09 +00002634 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002635 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002636 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002637
Jamie Madill075edd82013-07-08 13:30:19 -04002638 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002639 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002640 }
2641 else
2642 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002643 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002644
2645 if (node->getInit())
2646 {
2647 node->getInit()->traverse(this);
2648 }
2649
2650 out << "; ";
2651
alokp@chromium.org52813552010-11-16 18:36:09 +00002652 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002653 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002654 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002655 }
2656
2657 out << "; ";
2658
alokp@chromium.org52813552010-11-16 18:36:09 +00002659 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002660 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002661 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002662 }
2663
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002664 out << ")\n";
2665
Jamie Madill075edd82013-07-08 13:30:19 -04002666 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002667 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002668 }
2669
2670 if (node->getBody())
2671 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002672 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002673 }
2674
Jamie Madill075edd82013-07-08 13:30:19 -04002675 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002676 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002677
alokp@chromium.org52813552010-11-16 18:36:09 +00002678 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002679 {
Jamie Madill075edd82013-07-08 13:30:19 -04002680 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002681 out << "while(\n";
2682
alokp@chromium.org52813552010-11-16 18:36:09 +00002683 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002684
daniel@transgaming.com73536982012-03-21 20:45:49 +00002685 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002686 }
2687
daniel@transgaming.com73536982012-03-21 20:45:49 +00002688 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002689
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002690 mInsideDiscontinuousLoop = wasDiscontinuous;
2691
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002692 return false;
2693}
2694
2695bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2696{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002697 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002698
2699 switch (node->getFlowOp())
2700 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002701 case EOpKill:
2702 outputTriplet(visit, "discard;\n", "", "");
2703 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002704 case EOpBreak:
2705 if (visit == PreVisit)
2706 {
2707 if (mExcessiveLoopIndex)
2708 {
2709 out << "{Break";
2710 mExcessiveLoopIndex->traverse(this);
2711 out << " = true; break;}\n";
2712 }
2713 else
2714 {
2715 out << "break;\n";
2716 }
2717 }
2718 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002719 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002720 case EOpReturn:
2721 if (visit == PreVisit)
2722 {
2723 if (node->getExpression())
2724 {
2725 out << "return ";
2726 }
2727 else
2728 {
2729 out << "return;\n";
2730 }
2731 }
2732 else if (visit == PostVisit)
2733 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002734 if (node->getExpression())
2735 {
2736 out << ";\n";
2737 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002738 }
2739 break;
2740 default: UNREACHABLE();
2741 }
2742
2743 return true;
2744}
2745
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002746void OutputHLSL::traverseStatements(TIntermNode *node)
2747{
2748 if (isSingleStatement(node))
2749 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002750 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002751 }
2752
2753 node->traverse(this);
2754}
2755
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002756bool OutputHLSL::isSingleStatement(TIntermNode *node)
2757{
2758 TIntermAggregate *aggregate = node->getAsAggregate();
2759
2760 if (aggregate)
2761 {
2762 if (aggregate->getOp() == EOpSequence)
2763 {
2764 return false;
2765 }
2766 else
2767 {
2768 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2769 {
2770 if (!isSingleStatement(*sit))
2771 {
2772 return false;
2773 }
2774 }
2775
2776 return true;
2777 }
2778 }
2779
2780 return true;
2781}
2782
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002783// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2784// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002785bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2786{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002787 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002788 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002789
2790 // Parse loops of the form:
2791 // for(int index = initial; index [comparator] limit; index += increment)
2792 TIntermSymbol *index = NULL;
2793 TOperator comparator = EOpNull;
2794 int initial = 0;
2795 int limit = 0;
2796 int increment = 0;
2797
2798 // Parse index name and intial value
2799 if (node->getInit())
2800 {
2801 TIntermAggregate *init = node->getInit()->getAsAggregate();
2802
2803 if (init)
2804 {
2805 TIntermSequence &sequence = init->getSequence();
2806 TIntermTyped *variable = sequence[0]->getAsTyped();
2807
2808 if (variable && variable->getQualifier() == EvqTemporary)
2809 {
2810 TIntermBinary *assign = variable->getAsBinaryNode();
2811
2812 if (assign->getOp() == EOpInitialize)
2813 {
2814 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2815 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2816
2817 if (symbol && constant)
2818 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002819 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002820 {
2821 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002822 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002823 }
2824 }
2825 }
2826 }
2827 }
2828 }
2829
2830 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002831 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002832 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002833 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002834
2835 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2836 {
2837 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2838
2839 if (constant)
2840 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002841 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002842 {
2843 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002844 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002845 }
2846 }
2847 }
2848 }
2849
2850 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002851 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002852 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002853 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2854 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002855
2856 if (binaryTerminal)
2857 {
2858 TOperator op = binaryTerminal->getOp();
2859 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2860
2861 if (constant)
2862 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002863 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002864 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002865 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002866
2867 switch (op)
2868 {
2869 case EOpAddAssign: increment = value; break;
2870 case EOpSubAssign: increment = -value; break;
2871 default: UNIMPLEMENTED();
2872 }
2873 }
2874 }
2875 }
2876 else if (unaryTerminal)
2877 {
2878 TOperator op = unaryTerminal->getOp();
2879
2880 switch (op)
2881 {
2882 case EOpPostIncrement: increment = 1; break;
2883 case EOpPostDecrement: increment = -1; break;
2884 case EOpPreIncrement: increment = 1; break;
2885 case EOpPreDecrement: increment = -1; break;
2886 default: UNIMPLEMENTED();
2887 }
2888 }
2889 }
2890
2891 if (index != NULL && comparator != EOpNull && increment != 0)
2892 {
2893 if (comparator == EOpLessThanEqual)
2894 {
2895 comparator = EOpLessThan;
2896 limit += 1;
2897 }
2898
2899 if (comparator == EOpLessThan)
2900 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002901 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002902
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002903 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002904 {
2905 return false; // Not an excessive loop
2906 }
2907
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002908 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2909 mExcessiveLoopIndex = index;
2910
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002911 out << "{int ";
2912 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002913 out << ";\n"
2914 "bool Break";
2915 index->traverse(this);
2916 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002917
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002918 bool firstLoopFragment = true;
2919
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002920 while (iterations > 0)
2921 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002922 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002923
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002924 if (!firstLoopFragment)
2925 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002926 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002927 index->traverse(this);
2928 out << ") {\n";
2929 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002930
2931 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2932 {
2933 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2934 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002935
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002936 // for(int index = initial; index < clampedLimit; index += increment)
2937
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002938 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002939 index->traverse(this);
2940 out << " = ";
2941 out << initial;
2942
2943 out << "; ";
2944 index->traverse(this);
2945 out << " < ";
2946 out << clampedLimit;
2947
2948 out << "; ";
2949 index->traverse(this);
2950 out << " += ";
2951 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002952 out << ")\n";
2953
Jamie Madill075edd82013-07-08 13:30:19 -04002954 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002955 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002956
2957 if (node->getBody())
2958 {
2959 node->getBody()->traverse(this);
2960 }
2961
Jamie Madill075edd82013-07-08 13:30:19 -04002962 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002963 out << ";}\n";
2964
2965 if (!firstLoopFragment)
2966 {
2967 out << "}\n";
2968 }
2969
2970 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002971
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002972 initial += MAX_LOOP_ITERATIONS * increment;
2973 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002974 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002975
2976 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002977
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002978 mExcessiveLoopIndex = restoreIndex;
2979
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002980 return true;
2981 }
2982 else UNIMPLEMENTED();
2983 }
2984
2985 return false; // Not handled as an excessive loop
2986}
2987
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002988void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002989{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002990 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002991
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002992 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002993 {
2994 out << preString;
2995 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002996 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002997 {
2998 out << inString;
2999 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003000 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003001 {
3002 out << postString;
3003 }
3004}
3005
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003006void OutputHLSL::outputLineDirective(int line)
3007{
3008 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
3009 {
baustin@google.com8ab69842011-06-02 21:53:45 +00003010 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003011 mBody << "#line " << line;
3012
3013 if (mContext.sourcePath)
3014 {
3015 mBody << " \"" << mContext.sourcePath << "\"";
3016 }
3017
3018 mBody << "\n";
3019 }
3020}
3021
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003022TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3023{
3024 TQualifier qualifier = symbol->getQualifier();
3025 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003026 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003027
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003028 if (name.empty()) // HLSL demands named arguments, also for prototypes
3029 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00003030 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003031 }
3032 else
3033 {
3034 name = decorate(name);
3035 }
3036
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003037 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
3038 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003039 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
3040 qualifierString(qualifier) + " " + samplerString(type) + " sampler_" + name + arrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003041 }
3042
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003043 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003044}
3045
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003046TString OutputHLSL::interpolationString(TQualifier qualifier)
3047{
3048 switch(qualifier)
3049 {
3050 case EvqVaryingIn: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003051 case EvqFragmentIn: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003052 case EvqInvariantVaryingIn: return "";
3053 case EvqSmoothIn: return "linear";
3054 case EvqFlatIn: return "nointerpolation";
3055 case EvqCentroidIn: return "centroid";
3056 case EvqVaryingOut: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003057 case EvqVertexOut: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003058 case EvqInvariantVaryingOut: return "";
3059 case EvqSmoothOut: return "linear";
3060 case EvqFlatOut: return "nointerpolation";
3061 case EvqCentroidOut: return "centroid";
3062 default: UNREACHABLE();
3063 }
3064
3065 return "";
3066}
3067
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003068TString OutputHLSL::qualifierString(TQualifier qualifier)
3069{
3070 switch(qualifier)
3071 {
3072 case EvqIn: return "in";
3073 case EvqOut: return "out";
3074 case EvqInOut: return "inout";
3075 case EvqConstReadOnly: return "const";
3076 default: UNREACHABLE();
3077 }
3078
3079 return "";
3080}
3081
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003082TString OutputHLSL::typeString(const TType &type)
3083{
Jamie Madill98493dd2013-07-08 14:39:03 -04003084 const TStructure* structure = type.getStruct();
3085 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003086 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003087 const TString& typeName = structure->name();
3088 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003089 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003090 return structLookup(typeName);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003091 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003092 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003093 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003094 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003095 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003096 }
3097 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003098 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003099 int cols = type.getCols();
3100 int rows = type.getRows();
3101 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003102 }
3103 else
3104 {
3105 switch (type.getBasicType())
3106 {
3107 case EbtFloat:
3108 switch (type.getNominalSize())
3109 {
3110 case 1: return "float";
3111 case 2: return "float2";
3112 case 3: return "float3";
3113 case 4: return "float4";
3114 }
3115 case EbtInt:
3116 switch (type.getNominalSize())
3117 {
3118 case 1: return "int";
3119 case 2: return "int2";
3120 case 3: return "int3";
3121 case 4: return "int4";
3122 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003123 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04003124 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003125 {
3126 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003127 case 2: return "uint2";
3128 case 3: return "uint3";
3129 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003130 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003131 case EbtBool:
3132 switch (type.getNominalSize())
3133 {
3134 case 1: return "bool";
3135 case 2: return "bool2";
3136 case 3: return "bool3";
3137 case 4: return "bool4";
3138 }
3139 case EbtVoid:
3140 return "void";
3141 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003142 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04003143 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003144 case EbtSampler2DArray:
3145 case EbtISampler2DArray:
3146 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003147 return "sampler2D";
3148 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003149 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04003150 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003151 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00003152 case EbtSamplerExternalOES:
3153 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00003154 default:
3155 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003156 }
3157 }
3158
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003159 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003160 return "<unknown type>";
3161}
3162
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003163TString OutputHLSL::textureString(const TType &type)
3164{
3165 switch (type.getBasicType())
3166 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003167 case EbtSampler2D: return "Texture2D";
3168 case EbtSamplerCube: return "TextureCube";
3169 case EbtSamplerExternalOES: return "Texture2D";
3170 case EbtSampler2DArray: return "Texture2DArray";
3171 case EbtSampler3D: return "Texture3D";
3172 case EbtISampler2D: return "Texture2D<int4>";
3173 case EbtISampler3D: return "Texture3D<int4>";
3174 case EbtISamplerCube: return "TextureCube<int4>";
3175 case EbtISampler2DArray: return "Texture2DArray<int4>";
3176 case EbtUSampler2D: return "Texture2D<uint4>";
3177 case EbtUSampler3D: return "Texture3D<uint4>";
3178 case EbtUSamplerCube: return "TextureCube<uint4>";
3179 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
3180 case EbtSampler2DShadow: return "Texture2D";
3181 case EbtSamplerCubeShadow: return "TextureCube";
3182 case EbtSampler2DArrayShadow: return "Texture2DArray";
3183 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003184 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04003185
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003186 return "<unknown texture type>";
3187}
3188
Nicolas Capenscb127d32013-07-15 17:26:18 -04003189TString OutputHLSL::samplerString(const TType &type)
3190{
3191 if (IsShadowSampler(type.getBasicType()))
3192 {
3193 return "SamplerComparisonState";
3194 }
3195 else
3196 {
3197 return "SamplerState";
3198 }
3199}
3200
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003201TString OutputHLSL::arrayString(const TType &type)
3202{
3203 if (!type.isArray())
3204 {
3205 return "";
3206 }
3207
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003208 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003209}
3210
3211TString OutputHLSL::initializer(const TType &type)
3212{
3213 TString string;
3214
Jamie Madill94bf7f22013-07-08 13:31:15 -04003215 size_t size = type.getObjectSize();
3216 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003217 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003218 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003219
Jamie Madill94bf7f22013-07-08 13:31:15 -04003220 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003221 {
3222 string += ", ";
3223 }
3224 }
3225
daniel@transgaming.comead23042010-04-29 03:35:36 +00003226 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003227}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003228
Jamie Madill98493dd2013-07-08 14:39:03 -04003229TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003230{
Jamie Madill98493dd2013-07-08 14:39:03 -04003231 const TFieldList &fields = structure.fields();
3232 const bool isNameless = (structure.name() == "");
3233 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003234 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3235
Jamie Madill98493dd2013-07-08 14:39:03 -04003236 TString string;
3237 string += declareString + "\n"
3238 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003239
Jamie Madillc835df62013-06-21 09:15:32 -04003240 int elementIndex = 0;
3241
Jamie Madill9cf6c072013-06-20 11:55:53 -04003242 for (unsigned int i = 0; i < fields.size(); i++)
3243 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003244 const TField &field = *fields[i];
3245 const TType &fieldType = *field.type();
3246 const TStructure *fieldStruct = fieldType.getStruct();
3247 const TString &fieldTypeString = fieldStruct ? structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) : typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003248
Jamie Madillc835df62013-06-21 09:15:32 -04003249 if (useStd140Packing)
3250 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003251 string += std140PrePaddingString(*field.type(), &elementIndex);
Jamie Madillc835df62013-06-21 09:15:32 -04003252 }
3253
Jamie Madill98493dd2013-07-08 14:39:03 -04003254 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003255
3256 if (useStd140Packing)
3257 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003258 string += std140PostPaddingString(*field.type(), useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003259 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003260 }
3261
3262 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003263 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003264
Jamie Madille4075c92013-06-21 09:15:32 -04003265 // Add remaining element index to the global map, for use with nested structs in standard layouts
3266 if (useStd140Packing)
3267 {
3268 mStd140StructElementIndexes[structName] = elementIndex;
3269 }
3270
Jamie Madill98493dd2013-07-08 14:39:03 -04003271 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003272}
3273
Jamie Madill98493dd2013-07-08 14:39:03 -04003274TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003275{
Jamie Madill98493dd2013-07-08 14:39:03 -04003276 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003277 {
3278 return "";
3279 }
3280
3281 TString prefix = "";
3282
3283 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3284 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003285
3286 if (useStd140Packing)
3287 {
3288 prefix += "std";
3289 }
3290
Jamie Madill9cf6c072013-06-20 11:55:53 -04003291 if (useHLSLRowMajorPacking)
3292 {
Jamie Madillc835df62013-06-21 09:15:32 -04003293 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003294 prefix += "rm";
3295 }
3296
Jamie Madill98493dd2013-07-08 14:39:03 -04003297 return prefix + structLookup(structure.name());
Jamie Madill9cf6c072013-06-20 11:55:53 -04003298}
3299
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003300void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003301{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003302 if (name == "")
3303 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003304 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003305 }
3306
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003307 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
3308 {
3309 return; // Already added
3310 }
3311
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003312 TType ctorType = type;
3313 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003314 ctorType.setPrecision(EbpHigh);
3315 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003316
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003317 TString ctorName = type.getStruct() ? decorate(name) : name;
3318
3319 typedef std::vector<TType> ParameterArray;
3320 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003321
Jamie Madill98493dd2013-07-08 14:39:03 -04003322 const TStructure* structure = type.getStruct();
3323 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003324 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003325 mStructNames.insert(decorate(name));
3326
Jamie Madill98493dd2013-07-08 14:39:03 -04003327 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003328
Jamie Madill98493dd2013-07-08 14:39:03 -04003329 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003330 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003331 // Add row-major packed struct for interface blocks
3332 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003333 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003334 "#pragma pack_matrix(column_major)\n";
3335
Jamie Madillc835df62013-06-21 09:15:32 -04003336 const TString &std140Prefix = "std";
Jamie Madill98493dd2013-07-08 14:39:03 -04003337 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003338
3339 const TString &std140RowMajorPrefix = "std_rm";
3340 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003341 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003342 "#pragma pack_matrix(column_major)\n";
3343
Jamie Madill98493dd2013-07-08 14:39:03 -04003344 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003345 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003346 mStructDeclarations.push_back(std140String);
3347 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003348 }
3349
Jamie Madill98493dd2013-07-08 14:39:03 -04003350 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003351 for (unsigned int i = 0; i < fields.size(); i++)
3352 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003353 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003354 }
3355 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003356 else if (parameters)
3357 {
3358 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3359 {
3360 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3361 }
3362 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003363 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003364
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003365 TString constructor;
3366
3367 if (ctorType.getStruct())
3368 {
3369 constructor += ctorName + " " + ctorName + "_ctor(";
3370 }
3371 else // Built-in type
3372 {
3373 constructor += typeString(ctorType) + " " + ctorName + "(";
3374 }
3375
3376 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3377 {
3378 const TType &type = ctorParameters[parameter];
3379
3380 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3381
3382 if (parameter < ctorParameters.size() - 1)
3383 {
3384 constructor += ", ";
3385 }
3386 }
3387
3388 constructor += ")\n"
3389 "{\n";
3390
3391 if (ctorType.getStruct())
3392 {
3393 constructor += " " + ctorName + " structure = {";
3394 }
3395 else
3396 {
3397 constructor += " return " + typeString(ctorType) + "(";
3398 }
3399
3400 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3401 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003402 int rows = ctorType.getRows();
3403 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003404 const TType &parameter = ctorParameters[0];
3405
3406 if (parameter.isScalar())
3407 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003408 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003409 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003410 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003411 {
3412 constructor += TString((row == col) ? "x0" : "0.0");
3413
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003414 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003415 {
3416 constructor += ", ";
3417 }
3418 }
3419 }
3420 }
3421 else if (parameter.isMatrix())
3422 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003423 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003424 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003425 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003426 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003427 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003428 {
3429 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3430 }
3431 else
3432 {
3433 constructor += TString((row == col) ? "1.0" : "0.0");
3434 }
3435
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003436 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003437 {
3438 constructor += ", ";
3439 }
3440 }
3441 }
3442 }
3443 else UNREACHABLE();
3444 }
3445 else
3446 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003447 size_t remainingComponents = ctorType.getObjectSize();
3448 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003449
3450 while (remainingComponents > 0)
3451 {
3452 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003453 const size_t parameterSize = parameter.getObjectSize();
3454 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003455
3456 constructor += "x" + str(parameterIndex);
3457
3458 if (parameter.isScalar())
3459 {
3460 remainingComponents -= parameter.getObjectSize();
3461 }
3462 else if (parameter.isVector())
3463 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003464 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003465 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003466 ASSERT(parameterSize <= remainingComponents);
3467 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003468 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003469 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003470 {
3471 switch (remainingComponents)
3472 {
3473 case 1: constructor += ".x"; break;
3474 case 2: constructor += ".xy"; break;
3475 case 3: constructor += ".xyz"; break;
3476 case 4: constructor += ".xyzw"; break;
3477 default: UNREACHABLE();
3478 }
3479
3480 remainingComponents = 0;
3481 }
3482 else UNREACHABLE();
3483 }
3484 else if (parameter.isMatrix() || parameter.getStruct())
3485 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003486 ASSERT(remainingComponents == parameterSize || moreParameters);
3487 ASSERT(parameterSize <= remainingComponents);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003488
Jamie Madill94bf7f22013-07-08 13:31:15 -04003489 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003490 }
3491 else UNREACHABLE();
3492
3493 if (moreParameters)
3494 {
3495 parameterIndex++;
3496 }
3497
3498 if (remainingComponents)
3499 {
3500 constructor += ", ";
3501 }
3502 }
3503 }
3504
3505 if (ctorType.getStruct())
3506 {
3507 constructor += "};\n"
3508 " return structure;\n"
3509 "}\n";
3510 }
3511 else
3512 {
3513 constructor += ");\n"
3514 "}\n";
3515 }
3516
daniel@transgaming.com63691862010-04-29 03:32:42 +00003517 mConstructors.insert(constructor);
3518}
3519
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003520const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3521{
3522 TInfoSinkBase &out = mBody;
3523
Jamie Madill98493dd2013-07-08 14:39:03 -04003524 const TStructure* structure = type.getStruct();
3525 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003526 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003527 out << structLookup(structure->name()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003528
Jamie Madill98493dd2013-07-08 14:39:03 -04003529 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003530
Jamie Madill98493dd2013-07-08 14:39:03 -04003531 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003532 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003533 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003534
3535 constUnion = writeConstantUnion(*fieldType, constUnion);
3536
Jamie Madill98493dd2013-07-08 14:39:03 -04003537 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003538 {
3539 out << ", ";
3540 }
3541 }
3542
3543 out << ")";
3544 }
3545 else
3546 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003547 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003548 bool writeType = size > 1;
3549
3550 if (writeType)
3551 {
3552 out << typeString(type) << "(";
3553 }
3554
Jamie Madill94bf7f22013-07-08 13:31:15 -04003555 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003556 {
3557 switch (constUnion->getType())
3558 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003559 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003560 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003561 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003562 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003563 default: UNREACHABLE();
3564 }
3565
3566 if (i != size - 1)
3567 {
3568 out << ", ";
3569 }
3570 }
3571
3572 if (writeType)
3573 {
3574 out << ")";
3575 }
3576 }
3577
3578 return constUnion;
3579}
3580
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003581TString OutputHLSL::scopeString(unsigned int depthLimit)
3582{
3583 TString string;
3584
3585 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3586 {
3587 string += "_" + str(i);
3588 }
3589
3590 return string;
3591}
3592
3593TString OutputHLSL::scopedStruct(const TString &typeName)
3594{
3595 if (typeName == "")
3596 {
3597 return typeName;
3598 }
3599
3600 return typeName + scopeString(mScopeDepth);
3601}
3602
3603TString OutputHLSL::structLookup(const TString &typeName)
3604{
3605 for (int depth = mScopeDepth; depth >= 0; depth--)
3606 {
3607 TString scopedName = decorate(typeName + scopeString(depth));
3608
3609 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3610 {
3611 if (*structName == scopedName)
3612 {
3613 return scopedName;
3614 }
3615 }
3616 }
3617
3618 UNREACHABLE(); // Should have found a matching constructor
3619
3620 return typeName;
3621}
3622
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003623TString OutputHLSL::decorate(const TString &string)
3624{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003625 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003626 {
3627 return "_" + string;
3628 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003629
3630 return string;
3631}
3632
apatrick@chromium.org65756022012-01-17 21:45:38 +00003633TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003634{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003635 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003636 {
3637 return "ex_" + string;
3638 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003639
3640 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003641}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003642
Jamie Madill98493dd2013-07-08 14:39:03 -04003643TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003644{
Jamie Madill98493dd2013-07-08 14:39:03 -04003645 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003646 {
3647 return decorate(string);
3648 }
3649
3650 return string;
3651}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003652
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003653void OutputHLSL::declareInterfaceBlockField(const TType &type, const TString &name, std::vector<InterfaceBlockField>& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003654{
Jamie Madill98493dd2013-07-08 14:39:03 -04003655 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003656
3657 if (!structure)
3658 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003659 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003660 InterfaceBlockField field(glVariableType(type), glVariablePrecision(type), name.c_str(),
3661 (unsigned int)type.getArraySize(), isRowMajorMatrix);
3662 output.push_back(field);
3663 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003664 else
3665 {
Jamie Madill28167c62013-08-30 13:21:10 -04003666 InterfaceBlockField structField(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), false);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003667
3668 const TFieldList &fields = structure->fields();
3669
3670 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3671 {
3672 TField *field = fields[fieldIndex];
3673 TType *fieldType = field->type();
3674
3675 // make sure to copy matrix packing information
3676 fieldType->setLayoutQualifier(type.getLayoutQualifier());
3677
3678 declareInterfaceBlockField(*fieldType, field->name(), structField.fields);
3679 }
3680
3681 output.push_back(structField);
3682 }
3683}
3684
Jamie Madillc2141fb2013-08-30 13:21:08 -04003685Uniform OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, std::vector<Uniform>& output)
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003686{
3687 const TStructure *structure = type.getStruct();
3688
3689 if (!structure)
3690 {
3691 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
3692 Uniform uniform(glVariableType(type), glVariablePrecision(type), name.c_str(),
Jamie Madill56093782013-08-30 13:21:11 -04003693 (unsigned int)type.getArraySize(), (unsigned int)registerIndex, 0);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003694 output.push_back(uniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003695
3696 return uniform;
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003697 }
3698 else
3699 {
Jamie Madill56093782013-08-30 13:21:11 -04003700 Uniform structUniform(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(),
3701 (unsigned int)registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003702
Jamie Madill98493dd2013-07-08 14:39:03 -04003703 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003704
Jamie Madill98493dd2013-07-08 14:39:03 -04003705 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003706 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003707 TField *field = fields[fieldIndex];
3708 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003709
Jamie Madill56093782013-08-30 13:21:11 -04003710 declareUniformToList(*fieldType, field->name(), GL_INVALID_INDEX, structUniform.fields);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003711 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003712
Jamie Madill56093782013-08-30 13:21:11 -04003713 // assign register offset information -- this will override the information in any sub-structures.
3714 HLSLVariableGetRegisterInfo(registerIndex, &structUniform);
3715
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003716 output.push_back(structUniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003717
3718 return structUniform;
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003719 }
3720}
3721
Jamie Madill139b9092013-08-30 13:21:06 -04003722InterpolationType getInterpolationType(TQualifier qualifier)
3723{
3724 switch (qualifier)
3725 {
3726 case EvqFlatIn:
3727 case EvqFlatOut:
3728 return INTERPOLATION_FLAT;
3729
3730 case EvqSmoothIn:
3731 case EvqSmoothOut:
3732 case EvqVertexOut:
3733 case EvqFragmentIn:
Jamie Madill384b6042013-09-13 10:06:24 -04003734 case EvqVaryingIn:
3735 case EvqVaryingOut:
Jamie Madill139b9092013-08-30 13:21:06 -04003736 return INTERPOLATION_SMOOTH;
3737
3738 case EvqCentroidIn:
3739 case EvqCentroidOut:
3740 return INTERPOLATION_CENTROID;
3741
3742 default: UNREACHABLE();
3743 return INTERPOLATION_SMOOTH;
3744 }
3745}
3746
Jamie Madill94599662013-08-30 13:21:10 -04003747void OutputHLSL::declareVaryingToList(const TType &type, TQualifier baseTypeQualifier, const TString &name, std::vector<Varying>& fieldsOut)
Jamie Madill47fdd132013-08-30 13:21:04 -04003748{
3749 const TStructure *structure = type.getStruct();
3750
Jamie Madill94599662013-08-30 13:21:10 -04003751 InterpolationType interpolation = getInterpolationType(baseTypeQualifier);
Jamie Madill47fdd132013-08-30 13:21:04 -04003752 if (!structure)
3753 {
Jamie Madill139b9092013-08-30 13:21:06 -04003754 Varying varying(glVariableType(type), glVariablePrecision(type), name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003755 fieldsOut.push_back(varying);
3756 }
3757 else
3758 {
Jamie Madill28167c62013-08-30 13:21:10 -04003759 Varying structVarying(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003760 const TFieldList &fields = structure->fields();
3761
Jamie Madill28167c62013-08-30 13:21:10 -04003762 structVarying.structName = structure->name().c_str();
3763
Jamie Madill47fdd132013-08-30 13:21:04 -04003764 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3765 {
3766 const TField &field = *fields[fieldIndex];
Jamie Madill94599662013-08-30 13:21:10 -04003767 declareVaryingToList(*field.type(), baseTypeQualifier, field.name(), structVarying.fields);
Jamie Madill47fdd132013-08-30 13:21:04 -04003768 }
3769
3770 fieldsOut.push_back(structVarying);
3771 }
3772}
3773
Jamie Madillc2141fb2013-08-30 13:21:08 -04003774int OutputHLSL::declareUniformAndAssignRegister(const TType &type, const TString &name)
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003775{
Jamie Madillc2141fb2013-08-30 13:21:08 -04003776 int registerIndex = (IsSampler(type.getBasicType()) ? mSamplerRegister : mUniformRegister);
3777
3778 const Uniform &uniform = declareUniformToList(type, name, registerIndex, mActiveUniforms);
3779
3780 if (IsSampler(type.getBasicType()))
3781 {
3782 mSamplerRegister += HLSLVariableRegisterCount(uniform);
3783 }
3784 else
3785 {
3786 mUniformRegister += HLSLVariableRegisterCount(uniform);
3787 }
3788
3789 return registerIndex;
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003790}
3791
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003792GLenum OutputHLSL::glVariableType(const TType &type)
3793{
3794 if (type.getBasicType() == EbtFloat)
3795 {
3796 if (type.isScalar())
3797 {
3798 return GL_FLOAT;
3799 }
3800 else if (type.isVector())
3801 {
3802 switch(type.getNominalSize())
3803 {
3804 case 2: return GL_FLOAT_VEC2;
3805 case 3: return GL_FLOAT_VEC3;
3806 case 4: return GL_FLOAT_VEC4;
3807 default: UNREACHABLE();
3808 }
3809 }
3810 else if (type.isMatrix())
3811 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003812 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003813 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003814 case 2:
3815 switch(type.getRows())
3816 {
3817 case 2: return GL_FLOAT_MAT2;
3818 case 3: return GL_FLOAT_MAT2x3;
3819 case 4: return GL_FLOAT_MAT2x4;
3820 default: UNREACHABLE();
3821 }
3822
3823 case 3:
3824 switch(type.getRows())
3825 {
3826 case 2: return GL_FLOAT_MAT3x2;
3827 case 3: return GL_FLOAT_MAT3;
3828 case 4: return GL_FLOAT_MAT3x4;
3829 default: UNREACHABLE();
3830 }
3831
3832 case 4:
3833 switch(type.getRows())
3834 {
3835 case 2: return GL_FLOAT_MAT4x2;
3836 case 3: return GL_FLOAT_MAT4x3;
3837 case 4: return GL_FLOAT_MAT4;
3838 default: UNREACHABLE();
3839 }
3840
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003841 default: UNREACHABLE();
3842 }
3843 }
3844 else UNREACHABLE();
3845 }
3846 else if (type.getBasicType() == EbtInt)
3847 {
3848 if (type.isScalar())
3849 {
3850 return GL_INT;
3851 }
3852 else if (type.isVector())
3853 {
3854 switch(type.getNominalSize())
3855 {
3856 case 2: return GL_INT_VEC2;
3857 case 3: return GL_INT_VEC3;
3858 case 4: return GL_INT_VEC4;
3859 default: UNREACHABLE();
3860 }
3861 }
3862 else UNREACHABLE();
3863 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003864 else if (type.getBasicType() == EbtUInt)
3865 {
3866 if (type.isScalar())
3867 {
3868 return GL_UNSIGNED_INT;
3869 }
3870 else if (type.isVector())
3871 {
Jamie Madill22d63da2013-06-07 12:45:12 -04003872 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003873 {
3874 case 2: return GL_UNSIGNED_INT_VEC2;
3875 case 3: return GL_UNSIGNED_INT_VEC3;
3876 case 4: return GL_UNSIGNED_INT_VEC4;
3877 default: UNREACHABLE();
3878 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003879 }
3880 else UNREACHABLE();
3881 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003882 else if (type.getBasicType() == EbtBool)
3883 {
3884 if (type.isScalar())
3885 {
3886 return GL_BOOL;
3887 }
3888 else if (type.isVector())
3889 {
3890 switch(type.getNominalSize())
3891 {
3892 case 2: return GL_BOOL_VEC2;
3893 case 3: return GL_BOOL_VEC3;
3894 case 4: return GL_BOOL_VEC4;
3895 default: UNREACHABLE();
3896 }
3897 }
3898 else UNREACHABLE();
3899 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003900
3901 switch(type.getBasicType())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003902 {
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003903 case EbtSampler2D: return GL_SAMPLER_2D;
3904 case EbtSampler3D: return GL_SAMPLER_3D;
3905 case EbtSamplerCube: return GL_SAMPLER_CUBE;
3906 case EbtSampler2DArray: return GL_SAMPLER_2D_ARRAY;
3907 case EbtISampler2D: return GL_INT_SAMPLER_2D;
3908 case EbtISampler3D: return GL_INT_SAMPLER_3D;
3909 case EbtISamplerCube: return GL_INT_SAMPLER_CUBE;
3910 case EbtISampler2DArray: return GL_INT_SAMPLER_2D_ARRAY;
3911 case EbtUSampler2D: return GL_UNSIGNED_INT_SAMPLER_2D;
3912 case EbtUSampler3D: return GL_UNSIGNED_INT_SAMPLER_3D;
3913 case EbtUSamplerCube: return GL_UNSIGNED_INT_SAMPLER_CUBE;
3914 case EbtUSampler2DArray: return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3915 case EbtSampler2DShadow: return GL_SAMPLER_2D_SHADOW;
3916 case EbtSamplerCubeShadow: return GL_SAMPLER_CUBE_SHADOW;
3917 case EbtSampler2DArrayShadow: return GL_SAMPLER_2D_ARRAY_SHADOW;
3918 default: UNREACHABLE();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003919 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003920
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003921
3922 return GL_NONE;
3923}
3924
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003925GLenum OutputHLSL::glVariablePrecision(const TType &type)
3926{
3927 if (type.getBasicType() == EbtFloat)
3928 {
3929 switch (type.getPrecision())
3930 {
3931 case EbpHigh: return GL_HIGH_FLOAT;
3932 case EbpMedium: return GL_MEDIUM_FLOAT;
3933 case EbpLow: return GL_LOW_FLOAT;
3934 case EbpUndefined:
3935 // Should be defined as the default precision by the parser
3936 default: UNREACHABLE();
3937 }
3938 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003939 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003940 {
3941 switch (type.getPrecision())
3942 {
3943 case EbpHigh: return GL_HIGH_INT;
3944 case EbpMedium: return GL_MEDIUM_INT;
3945 case EbpLow: return GL_LOW_INT;
3946 case EbpUndefined:
3947 // Should be defined as the default precision by the parser
3948 default: UNREACHABLE();
3949 }
3950 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003951
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00003952 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003953 return GL_NONE;
3954}
3955
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003956bool OutputHLSL::isVaryingOut(TQualifier qualifier)
3957{
3958 switch(qualifier)
3959 {
3960 case EvqVaryingOut:
3961 case EvqInvariantVaryingOut:
3962 case EvqSmoothOut:
3963 case EvqFlatOut:
3964 case EvqCentroidOut:
Jamie Madill19571812013-08-12 15:26:34 -07003965 case EvqVertexOut:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003966 return true;
3967 }
3968
3969 return false;
3970}
3971
3972bool OutputHLSL::isVaryingIn(TQualifier qualifier)
3973{
3974 switch(qualifier)
3975 {
3976 case EvqVaryingIn:
3977 case EvqInvariantVaryingIn:
3978 case EvqSmoothIn:
3979 case EvqFlatIn:
3980 case EvqCentroidIn:
Jamie Madill19571812013-08-12 15:26:34 -07003981 case EvqFragmentIn:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003982 return true;
3983 }
3984
3985 return false;
3986}
3987
3988bool OutputHLSL::isVarying(TQualifier qualifier)
3989{
3990 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
3991}
3992
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003993}