blob: 28d981241cec153b26d2825860fc3732139cfc6e [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00007#include "compiler/OutputHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
alokp@chromium.org79fb1012012-04-26 21:07:39 +00009#include "common/angleutils.h"
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +000010#include "common/utilities.h"
alokp@chromium.org91b72322010-06-02 15:50:56 +000011#include "compiler/debug.h"
daniel@transgaming.com89431aa2012-05-31 01:20:29 +000012#include "compiler/DetectDiscontinuity.h"
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000013#include "compiler/InfoSink.h"
14#include "compiler/SearchSymbol.h"
15#include "compiler/UnfoldShortCircuit.h"
Jamie Madill440dc742013-06-20 11:55:55 -040016#include "compiler/HLSLLayoutEncoder.h"
Jamie Madill570e04d2013-06-21 09:15:33 -040017#include "compiler/FlagStd140Structs.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000018
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000019#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000020#include <cfloat>
21#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000022
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000023namespace sh
24{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000025// Integer to TString conversion
26TString str(int i)
27{
28 char buffer[20];
kbr@chromium.orgddb6e8e2012-04-25 00:48:13 +000029 snprintf(buffer, sizeof(buffer), "%d", i);
daniel@transgaming.com005c7392010-04-15 20:45:27 +000030 return buffer;
31}
32
Nicolas Capense0ba27a2013-06-24 16:10:52 -040033TString OutputHLSL::TextureFunction::name() const
34{
35 TString name = "gl_texture";
36
Nicolas Capens6d232bb2013-07-08 15:56:38 -040037 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040038 {
39 name += "2D";
40 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040041 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040042 {
43 name += "3D";
44 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040045 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040046 {
47 name += "Cube";
48 }
49 else UNREACHABLE();
50
51 if (proj)
52 {
53 name += "Proj";
54 }
55
Nicolas Capens75fb4752013-07-10 15:14:47 -040056 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040057 {
58 case IMPLICIT: break;
59 case BIAS: break;
60 case LOD: name += "Lod"; break;
61 case LOD0: name += "Lod0"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -040062 case SIZE: name += "Size"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040063 default: UNREACHABLE();
64 }
65
66 return name + "(";
67}
68
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;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000121
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000122 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
123
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000124 mScopeDepth = 0;
125
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000126 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000127
128 mContainsLoopDiscontinuity = false;
129 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000130 mInsideDiscontinuousLoop = false;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000131
132 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000133
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000134 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000135 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000136 if (mContext.shaderType == SH_FRAGMENT_SHADER)
137 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000138 mUniformRegister = 3; // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000139 }
140 else
141 {
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000142 mUniformRegister = 2; // Reserve registers for dx_DepthRange and dx_ViewAdjust
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000143 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000144 }
145 else
146 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000147 mUniformRegister = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000148 }
149
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000150 mSamplerRegister = 0;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000151 mInterfaceBlockRegister = 2; // Reserve registers for the default uniform block and driver constants
Jamie Madill574d9dd2013-06-20 11:55:56 -0400152 mPaddingCounter = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000153}
154
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000155OutputHLSL::~OutputHLSL()
156{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000157 delete mUnfoldShortCircuit;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000158}
159
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000160void OutputHLSL::output()
161{
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +0000162 mContainsLoopDiscontinuity = mContext.shaderType == SH_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400163 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
164 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000165
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000166 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 +0000167 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000168
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000169 mContext.infoSink().obj << mHeader.c_str();
170 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000171}
172
Jamie Madill570e04d2013-06-21 09:15:33 -0400173void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
174{
175 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
176 {
177 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
178
179 // This will mark the necessary block elements as referenced
180 flaggedNode->traverse(this);
181 TString structName(mBody.c_str());
182 mBody.erase();
183
184 mFlaggedStructOriginalNames[flaggedNode] = structName;
185
186 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
187 {
188 structName.erase(pos, 1);
189 }
190
191 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
192 }
193}
194
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000195TInfoSinkBase &OutputHLSL::getBodyStream()
196{
197 return mBody;
198}
199
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400200const std::vector<Uniform> &OutputHLSL::getUniforms()
daniel@transgaming.com043da132012-12-20 21:12:22 +0000201{
202 return mActiveUniforms;
203}
204
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000205const ActiveInterfaceBlocks &OutputHLSL::getInterfaceBlocks() const
206{
207 return mActiveInterfaceBlocks;
208}
209
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400210const std::vector<Attribute> &OutputHLSL::getOutputVariables() const
Jamie Madill46131a32013-06-20 11:55:50 -0400211{
212 return mActiveOutputVariables;
213}
214
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400215const std::vector<Attribute> &OutputHLSL::getAttributes() const
Jamie Madilldefb6742013-06-20 11:55:51 -0400216{
217 return mActiveAttributes;
218}
219
Jamie Madill47fdd132013-08-30 13:21:04 -0400220const std::vector<Varying> &OutputHLSL::getVaryings() const
221{
222 return mActiveVaryings;
223}
224
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000225int OutputHLSL::vectorSize(const TType &type) const
226{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000227 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000228 int arraySize = type.isArray() ? type.getArraySize() : 1;
229
230 return elementSize * arraySize;
231}
232
Jamie Madill98493dd2013-07-08 14:39:03 -0400233TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, const TField &field)
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000234{
Jamie Madill98493dd2013-07-08 14:39:03 -0400235 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000236 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400237 return interfaceBlock.name() + "." + field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000238 }
239 else
240 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400241 return field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000242 }
243}
244
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000245TString OutputHLSL::decoratePrivate(const TString &privateText)
246{
247 return "dx_" + privateText;
248}
249
Jamie Madill98493dd2013-07-08 14:39:03 -0400250TString OutputHLSL::interfaceBlockStructNameString(const TInterfaceBlock &interfaceBlock)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000251{
Jamie Madill98493dd2013-07-08 14:39:03 -0400252 return decoratePrivate(interfaceBlock.name()) + "_type";
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000253}
254
Jamie Madill98493dd2013-07-08 14:39:03 -0400255TString OutputHLSL::interfaceBlockInstanceString(const TInterfaceBlock& interfaceBlock, unsigned int arrayIndex)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000256{
Jamie Madill98493dd2013-07-08 14:39:03 -0400257 if (!interfaceBlock.hasInstanceName())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000258 {
259 return "";
260 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400261 else if (interfaceBlock.isArray())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000262 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400263 return decoratePrivate(interfaceBlock.instanceName()) + "_" + str(arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000264 }
265 else
266 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400267 return decorate(interfaceBlock.instanceName());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000268 }
269}
270
Jamie Madill98493dd2013-07-08 14:39:03 -0400271TString OutputHLSL::interfaceBlockFieldTypeString(const TField &field, TLayoutBlockStorage blockStorage)
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000272{
Jamie Madill98493dd2013-07-08 14:39:03 -0400273 const TType &fieldType = *field.type();
274 const TLayoutMatrixPacking matrixPacking = fieldType.getLayoutQualifier().matrixPacking;
Jamie Madill529077d2013-06-20 11:55:54 -0400275 ASSERT(matrixPacking != EmpUnspecified);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000276
Jamie Madill98493dd2013-07-08 14:39:03 -0400277 if (fieldType.isMatrix())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000278 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400279 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill529077d2013-06-20 11:55:54 -0400280 const TString &matrixPackString = (matrixPacking == EmpRowMajor ? "column_major" : "row_major");
Jamie Madill98493dd2013-07-08 14:39:03 -0400281 return matrixPackString + " " + typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000282 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400283 else if (fieldType.getStruct())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000284 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400285 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill98493dd2013-07-08 14:39:03 -0400286 return structureTypeName(*fieldType.getStruct(), matrixPacking == EmpColumnMajor, blockStorage == EbsStd140);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000287 }
288 else
289 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400290 return typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000291 }
292}
293
Jamie Madill98493dd2013-07-08 14:39:03 -0400294TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, TLayoutBlockStorage blockStorage)
295{
296 TString hlsl;
297
298 int elementIndex = 0;
299
300 for (unsigned int typeIndex = 0; typeIndex < interfaceBlock.fields().size(); typeIndex++)
301 {
302 const TField &field = *interfaceBlock.fields()[typeIndex];
303 const TType &fieldType = *field.type();
304
305 if (blockStorage == EbsStd140)
306 {
307 // 2 and 3 component vector types in some cases need pre-padding
308 hlsl += std140PrePaddingString(fieldType, &elementIndex);
309 }
310
311 hlsl += " " + interfaceBlockFieldTypeString(field, blockStorage) +
312 " " + decorate(field.name()) + arrayString(fieldType) + ";\n";
313
314 // must pad out after matrices and arrays, where HLSL usually allows itself room to pack stuff
315 if (blockStorage == EbsStd140)
316 {
317 const bool useHLSLRowMajorPacking = (fieldType.getLayoutQualifier().matrixPacking == EmpColumnMajor);
318 hlsl += std140PostPaddingString(fieldType, useHLSLRowMajorPacking);
319 }
320 }
321
322 return hlsl;
323}
324
325TString OutputHLSL::interfaceBlockStructString(const TInterfaceBlock &interfaceBlock)
326{
327 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
328
329 return "struct " + interfaceBlockStructNameString(interfaceBlock) + "\n"
330 "{\n" +
331 interfaceBlockFieldString(interfaceBlock, blockStorage) +
332 "};\n\n";
333}
334
335TString OutputHLSL::interfaceBlockString(const TInterfaceBlock &interfaceBlock, unsigned int registerIndex, unsigned int arrayIndex)
336{
337 const TString &arrayIndexString = (arrayIndex != GL_INVALID_INDEX ? decorate(str(arrayIndex)) : "");
338 const TString &blockName = interfaceBlock.name() + arrayIndexString;
339 TString hlsl;
340
341 hlsl += "cbuffer " + blockName + " : register(b" + str(registerIndex) + ")\n"
342 "{\n";
343
344 if (interfaceBlock.hasInstanceName())
345 {
346 hlsl += " " + interfaceBlockStructNameString(interfaceBlock) + " " + interfaceBlockInstanceString(interfaceBlock, arrayIndex) + ";\n";
347 }
348 else
349 {
350 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
351 hlsl += interfaceBlockFieldString(interfaceBlock, blockStorage);
352 }
353
354 hlsl += "};\n\n";
355
356 return hlsl;
357}
358
Jamie Madill574d9dd2013-06-20 11:55:56 -0400359TString OutputHLSL::std140PrePaddingString(const TType &type, int *elementIndex)
360{
361 if (type.getBasicType() == EbtStruct || type.isMatrix() || type.isArray())
362 {
363 // no padding needed, HLSL will align the field to a new register
364 *elementIndex = 0;
365 return "";
366 }
367
368 const GLenum glType = glVariableType(type);
369 const int numComponents = gl::UniformComponentCount(glType);
370
371 if (numComponents >= 4)
372 {
373 // no padding needed, HLSL will align the field to a new register
374 *elementIndex = 0;
375 return "";
376 }
377
378 if (*elementIndex + numComponents > 4)
379 {
380 // no padding needed, HLSL will align the field to a new register
381 *elementIndex = numComponents;
382 return "";
383 }
384
385 TString padding;
386
387 const int alignment = numComponents == 3 ? 4 : numComponents;
388 const int paddingOffset = (*elementIndex % alignment);
389
390 if (paddingOffset != 0)
391 {
392 // padding is neccessary
393 for (int paddingIndex = paddingOffset; paddingIndex < alignment; paddingIndex++)
394 {
395 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
396 }
397
398 *elementIndex += (alignment - paddingOffset);
399 }
400
401 *elementIndex += numComponents;
402 *elementIndex %= 4;
403
404 return padding;
405}
406
Jamie Madille4075c92013-06-21 09:15:32 -0400407TString OutputHLSL::std140PostPaddingString(const TType &type, bool useHLSLRowMajorPacking)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400408{
Jamie Madillc835df62013-06-21 09:15:32 -0400409 if (!type.isMatrix() && !type.isArray() && type.getBasicType() != EbtStruct)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400410 {
411 return "";
412 }
413
Jamie Madill574d9dd2013-06-20 11:55:56 -0400414 int numComponents = 0;
415
416 if (type.isMatrix())
417 {
Jamie Madille4075c92013-06-21 09:15:32 -0400418 // This method can also be called from structureString, which does not use layout qualifiers.
419 // Thus, use the method parameter for determining the matrix packing.
420 //
421 // Note HLSL row major packing corresponds to GL API column-major, and vice-versa, since we
422 // wish to always transpose GL matrices to play well with HLSL's matrix array indexing.
423 //
424 const bool isRowMajorMatrix = !useHLSLRowMajorPacking;
Jamie Madillc835df62013-06-21 09:15:32 -0400425 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400426 numComponents = gl::MatrixComponentCount(glType, isRowMajorMatrix);
427 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400428 else if (type.getStruct())
Jamie Madillc835df62013-06-21 09:15:32 -0400429 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400430 const TString &structName = structureTypeName(*type.getStruct(), useHLSLRowMajorPacking, true);
Jamie Madille4075c92013-06-21 09:15:32 -0400431 numComponents = mStd140StructElementIndexes[structName];
432
433 if (numComponents == 0)
434 {
435 return "";
436 }
Jamie Madillc835df62013-06-21 09:15:32 -0400437 }
Jamie Madill574d9dd2013-06-20 11:55:56 -0400438 else
439 {
Jamie Madillc835df62013-06-21 09:15:32 -0400440 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400441 numComponents = gl::UniformComponentCount(glType);
442 }
443
444 TString padding;
445 for (int paddingOffset = numComponents; paddingOffset < 4; paddingOffset++)
446 {
447 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
448 }
449 return padding;
450}
451
Jamie Madill440dc742013-06-20 11:55:55 -0400452// Use the same layout for packed and shared
453void setBlockLayout(InterfaceBlock *interfaceBlock, BlockLayoutType newLayout)
454{
455 interfaceBlock->layout = newLayout;
456 interfaceBlock->blockInfo.clear();
457
458 switch (newLayout)
459 {
460 case BLOCKLAYOUT_SHARED:
461 case BLOCKLAYOUT_PACKED:
462 {
463 HLSLBlockEncoder hlslEncoder(&interfaceBlock->blockInfo);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400464 hlslEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400465 interfaceBlock->dataSize = hlslEncoder.getBlockSize();
466 }
467 break;
468
469 case BLOCKLAYOUT_STANDARD:
470 {
471 Std140BlockEncoder stdEncoder(&interfaceBlock->blockInfo);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400472 stdEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400473 interfaceBlock->dataSize = stdEncoder.getBlockSize();
474 }
475 break;
476
477 default:
478 UNREACHABLE();
479 break;
480 }
481}
482
Jamie Madill574d9dd2013-06-20 11:55:56 -0400483BlockLayoutType convertBlockLayoutType(TLayoutBlockStorage blockStorage)
484{
485 switch (blockStorage)
486 {
487 case EbsPacked: return BLOCKLAYOUT_PACKED;
488 case EbsShared: return BLOCKLAYOUT_SHARED;
489 case EbsStd140: return BLOCKLAYOUT_STANDARD;
490 default: UNREACHABLE(); return BLOCKLAYOUT_SHARED;
491 }
492}
493
Jamie Madill98493dd2013-07-08 14:39:03 -0400494TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400495{
496 TString init;
497
498 TString preIndentString;
499 TString fullIndentString;
500
501 for (int spaces = 0; spaces < (indent * 4); spaces++)
502 {
503 preIndentString += ' ';
504 }
505
506 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
507 {
508 fullIndentString += ' ';
509 }
510
511 init += preIndentString + "{\n";
512
Jamie Madill98493dd2013-07-08 14:39:03 -0400513 const TFieldList &fields = structure.fields();
514 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400515 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400516 const TField &field = *fields[fieldIndex];
517 const TString &fieldName = rhsStructName + "." + decorate(field.name());
518 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400519
Jamie Madill98493dd2013-07-08 14:39:03 -0400520 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400521 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400522 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400523 }
524 else
525 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400526 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400527 }
528 }
529
530 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
531
532 return init;
533}
534
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000535void OutputHLSL::header()
536{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000537 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000538
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000539 TString uniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000540 TString interfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000541 TString varyings;
542 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400543 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000544
Jamie Madillc2141fb2013-08-30 13:21:08 -0400545 for (ReferencedSymbols::const_iterator uniformIt = mReferencedUniforms.begin(); uniformIt != mReferencedUniforms.end(); uniformIt++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000546 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400547 const TIntermSymbol &uniform = *uniformIt->second;
548 const TType &type = uniform.getType();
549 const TString &name = uniform.getSymbol();
550
551 int registerIndex = declareUniformAndAssignRegister(type, name);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000552
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000553 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture
554 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400555 uniforms += "uniform " + samplerString(type) + " sampler_" + decorateUniform(name, type) + arrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400556 " : register(s" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000557
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000558 uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400559 " : register(t" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000560 }
561 else
562 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400563 const TStructure *structure = type.getStruct();
564 const TString &typeName = (structure ? structureTypeName(*structure, false, false) : typeString(type));
565
566 const TString &registerString = TString("register(") + RegisterPrefix(type) + str(registerIndex) + ")";
567
568 uniforms += "uniform " + typeName + " " + decorateUniform(name, type) + arrayString(type) + " : " + registerString + ";\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000569 }
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000570 }
571
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000572 for (ReferencedSymbols::const_iterator interfaceBlockIt = mReferencedInterfaceBlocks.begin(); interfaceBlockIt != mReferencedInterfaceBlocks.end(); interfaceBlockIt++)
573 {
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000574 const TType &nodeType = interfaceBlockIt->second->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -0400575 const TInterfaceBlock &interfaceBlock = *nodeType.getInterfaceBlock();
576 const TFieldList &fieldList = interfaceBlock.fields();
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000577
Jamie Madill98493dd2013-07-08 14:39:03 -0400578 unsigned int arraySize = static_cast<unsigned int>(interfaceBlock.arraySize());
579 sh::InterfaceBlock activeBlock(interfaceBlock.name().c_str(), arraySize, mInterfaceBlockRegister);
580 for (unsigned int typeIndex = 0; typeIndex < fieldList.size(); typeIndex++)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000581 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400582 const TField &field = *fieldList[typeIndex];
583 const TString &fullUniformName = interfaceBlockFieldString(interfaceBlock, field);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400584 declareInterfaceBlockField(*field.type(), fullUniformName, activeBlock.fields);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000585 }
586
Jamie Madill98493dd2013-07-08 14:39:03 -0400587 mInterfaceBlockRegister += std::max(1u, arraySize);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000588
Jamie Madill98493dd2013-07-08 14:39:03 -0400589 BlockLayoutType blockLayoutType = convertBlockLayoutType(interfaceBlock.blockStorage());
590 setBlockLayout(&activeBlock, blockLayoutType);
Jamie Madill9060a4e2013-08-12 16:22:57 -0700591
592 if (interfaceBlock.matrixPacking() == EmpRowMajor)
593 {
594 activeBlock.isRowMajorLayout = true;
595 }
596
Jamie Madill98493dd2013-07-08 14:39:03 -0400597 mActiveInterfaceBlocks.push_back(activeBlock);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000598
Jamie Madill98493dd2013-07-08 14:39:03 -0400599 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000600 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400601 interfaceBlocks += interfaceBlockStructString(interfaceBlock);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000602 }
603
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000604 if (arraySize > 0)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000605 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000606 for (unsigned int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
607 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400608 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex + arrayIndex, arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000609 }
610 }
611 else
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000612 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400613 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000614 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000615 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000616
Jamie Madill570e04d2013-06-21 09:15:33 -0400617 for (auto flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
618 {
619 TIntermTyped *structNode = flaggedStructIt->first;
620 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400621 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400622 const TString &originalName = mFlaggedStructOriginalNames[structNode];
623
Jamie Madill98493dd2013-07-08 14:39:03 -0400624 flaggedStructs += "static " + decorate(structure.name()) + " " + mappedName + " =\n";
625 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400626 flaggedStructs += "\n";
627 }
628
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000629 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
630 {
631 const TType &type = varying->second->getType();
632 const TString &name = varying->second->getSymbol();
633
634 // Program linking depends on this exact format
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +0000635 varyings += "static " + interpolationString(type.getQualifier()) + " " + typeString(type) + " " +
636 decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madill47fdd132013-08-30 13:21:04 -0400637
Jamie Madill94599662013-08-30 13:21:10 -0400638 declareVaryingToList(type, type.getQualifier(), name, mActiveVaryings);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000639 }
640
641 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
642 {
643 const TType &type = attribute->second->getType();
644 const TString &name = attribute->second->getSymbol();
645
646 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400647
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400648 Attribute attributeVar(glVariableType(type), glVariablePrecision(type), name.c_str(),
649 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
650 mActiveAttributes.push_back(attributeVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000651 }
652
Jamie Madill529077d2013-06-20 11:55:54 -0400653 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
654 {
655 out << *structDeclaration;
656 }
657
658 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
659 {
660 out << *constructor;
661 }
662
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400663 if (mContext.shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000664 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000665 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000666 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000667
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000668 out << "// Varyings\n";
669 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400670 out << "\n";
671
672 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000673 {
Jamie Madill46131a32013-06-20 11:55:50 -0400674 for (auto outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000675 {
Jamie Madill46131a32013-06-20 11:55:50 -0400676 const TString &variableName = outputVariableIt->first;
677 const TType &variableType = outputVariableIt->second->getType();
678 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
679
680 out << "static " + typeString(variableType) + " out_" + variableName + arrayString(variableType) +
681 " = " + initializer(variableType) + ";\n";
682
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400683 Attribute outputVar(glVariableType(variableType), glVariablePrecision(variableType), variableName.c_str(),
684 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
Jamie Madill46131a32013-06-20 11:55:50 -0400685 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000686 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000687 }
Jamie Madill46131a32013-06-20 11:55:50 -0400688 else
689 {
690 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
691
692 out << "static float4 gl_Color[" << numColorValues << "] =\n"
693 "{\n";
694 for (unsigned int i = 0; i < numColorValues; i++)
695 {
696 out << " float4(0, 0, 0, 0)";
697 if (i + 1 != numColorValues)
698 {
699 out << ",";
700 }
701 out << "\n";
702 }
703
704 out << "};\n";
705 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000706
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400707 if (mUsesFragDepth)
708 {
709 out << "static float gl_Depth = 0.0;\n";
710 }
711
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000712 if (mUsesFragCoord)
713 {
714 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
715 }
716
717 if (mUsesPointCoord)
718 {
719 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
720 }
721
722 if (mUsesFrontFacing)
723 {
724 out << "static bool gl_FrontFacing = false;\n";
725 }
726
727 out << "\n";
728
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000729 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000730 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000731 out << "struct gl_DepthRangeParameters\n"
732 "{\n"
733 " float near;\n"
734 " float far;\n"
735 " float diff;\n"
736 "};\n"
737 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000738 }
739
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000740 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000741 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000742 out << "cbuffer DriverConstants : register(b1)\n"
743 "{\n";
744
745 if (mUsesDepthRange)
746 {
747 out << " float3 dx_DepthRange : packoffset(c0);\n";
748 }
749
750 if (mUsesFragCoord)
751 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000752 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000753 }
754
755 if (mUsesFragCoord || mUsesFrontFacing)
756 {
757 out << " float3 dx_DepthFront : packoffset(c2);\n";
758 }
759
760 out << "};\n";
761 }
762 else
763 {
764 if (mUsesDepthRange)
765 {
766 out << "uniform float3 dx_DepthRange : register(c0);";
767 }
768
769 if (mUsesFragCoord)
770 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000771 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000772 }
773
774 if (mUsesFragCoord || mUsesFrontFacing)
775 {
776 out << "uniform float3 dx_DepthFront : register(c2);\n";
777 }
778 }
779
780 out << "\n";
781
782 if (mUsesDepthRange)
783 {
784 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
785 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000786 }
787
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000788 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000789 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000790
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000791 if (!interfaceBlocks.empty())
792 {
793 out << interfaceBlocks;
794 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400795
796 if (!flaggedStructs.empty())
797 {
798 out << "// Std140 Structures accessed by value\n";
799 out << "\n";
800 out << flaggedStructs;
801 out << "\n";
802 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000803 }
804
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000805 if (usingMRTExtension && mNumRenderTargets > 1)
806 {
807 out << "#define GL_USES_MRT\n";
808 }
809
810 if (mUsesFragColor)
811 {
812 out << "#define GL_USES_FRAG_COLOR\n";
813 }
814
815 if (mUsesFragData)
816 {
817 out << "#define GL_USES_FRAG_DATA\n";
818 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000819 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000820 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000821 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000822 out << "// Attributes\n";
823 out << attributes;
824 out << "\n"
825 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
826
827 if (mUsesPointSize)
828 {
829 out << "static float gl_PointSize = float(1);\n";
830 }
831
832 out << "\n"
833 "// Varyings\n";
834 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000835 out << "\n";
836
837 if (mUsesDepthRange)
838 {
839 out << "struct gl_DepthRangeParameters\n"
840 "{\n"
841 " float near;\n"
842 " float far;\n"
843 " float diff;\n"
844 "};\n"
845 "\n";
846 }
847
848 if (mOutputType == SH_HLSL11_OUTPUT)
849 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000850 if (mUsesDepthRange)
851 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000852 out << "cbuffer DriverConstants : register(b1)\n"
853 "{\n"
854 " float3 dx_DepthRange : packoffset(c0);\n"
855 "};\n"
856 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000857 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000858 }
859 else
860 {
861 if (mUsesDepthRange)
862 {
863 out << "uniform float3 dx_DepthRange : register(c0);\n";
864 }
865
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000866 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000867 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000868 }
869
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000870 if (mUsesDepthRange)
871 {
872 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
873 "\n";
874 }
875
876 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000878
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000879 if (!interfaceBlocks.empty())
880 {
881 out << interfaceBlocks;
882 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400883
884 if (!flaggedStructs.empty())
885 {
886 out << "// Std140 Structures accessed by value\n";
887 out << "\n";
888 out << flaggedStructs;
889 out << "\n";
890 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000891 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400892 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000893
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400894 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
895 {
896 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400897 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000898 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400899 switch(textureFunction->sampler)
900 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400901 case EbtSampler2D: out << "int2 "; break;
902 case EbtSampler3D: out << "int3 "; break;
903 case EbtSamplerCube: out << "int2 "; break;
904 case EbtSampler2DArray: out << "int3 "; break;
905 case EbtISampler2D: out << "int2 "; break;
906 case EbtISampler3D: out << "int3 "; break;
907 case EbtISamplerCube: out << "int2 "; break;
908 case EbtISampler2DArray: out << "int3 "; break;
909 case EbtUSampler2D: out << "int2 "; break;
910 case EbtUSampler3D: out << "int3 "; break;
911 case EbtUSamplerCube: out << "int2 "; break;
912 case EbtUSampler2DArray: out << "int3 "; break;
913 case EbtSampler2DShadow: out << "int2 "; break;
914 case EbtSamplerCubeShadow: out << "int2 "; break;
915 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400916 default: UNREACHABLE();
917 }
918 }
919 else // Sampling function
920 {
921 switch(textureFunction->sampler)
922 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400923 case EbtSampler2D: out << "float4 "; break;
924 case EbtSampler3D: out << "float4 "; break;
925 case EbtSamplerCube: out << "float4 "; break;
926 case EbtSampler2DArray: out << "float4 "; break;
927 case EbtISampler2D: out << "int4 "; break;
928 case EbtISampler3D: out << "int4 "; break;
929 case EbtISamplerCube: out << "int4 "; break;
930 case EbtISampler2DArray: out << "int4 "; break;
931 case EbtUSampler2D: out << "uint4 "; break;
932 case EbtUSampler3D: out << "uint4 "; break;
933 case EbtUSamplerCube: out << "uint4 "; break;
934 case EbtUSampler2DArray: out << "uint4 "; break;
935 case EbtSampler2DShadow: out << "float "; break;
936 case EbtSamplerCubeShadow: out << "float "; break;
937 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400938 default: UNREACHABLE();
939 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000940 }
941
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400942 // Function name
943 out << textureFunction->name();
944
945 // Argument list
946 int hlslCoords = 4;
947
948 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000949 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400950 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000951 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400952 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
953 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
954 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000955 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400956
Nicolas Capens75fb4752013-07-10 15:14:47 -0400957 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000958 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400959 case TextureFunction::IMPLICIT: break;
960 case TextureFunction::BIAS: hlslCoords = 4; break;
961 case TextureFunction::LOD: hlslCoords = 4; break;
962 case TextureFunction::LOD0: hlslCoords = 4; break;
963 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000964 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400965 }
966 else if (mOutputType == SH_HLSL11_OUTPUT)
967 {
968 switch(textureFunction->sampler)
969 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400970 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
971 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
972 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
973 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
974 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
975 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
976 case EbtISamplerCube: out << "TextureCube<int4> x, SamplerState s"; hlslCoords = 3; break;
977 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
978 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
979 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
980 case EbtUSamplerCube: out << "TextureCube<uint4> x, SamplerState s"; hlslCoords = 3; break;
981 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
982 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
983 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
984 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400985 default: UNREACHABLE();
986 }
987 }
988 else UNREACHABLE();
989
990 switch(textureFunction->coords)
991 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400992 case 1: out << ", int lod"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400993 case 2: out << ", float2 t"; break;
994 case 3: out << ", float3 t"; break;
995 case 4: out << ", float4 t"; break;
996 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000997 }
998
Nicolas Capens75fb4752013-07-10 15:14:47 -0400999 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +00001000 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001001 case TextureFunction::IMPLICIT: break;
1002 case TextureFunction::BIAS: out << ", float bias"; break;
1003 case TextureFunction::LOD: out << ", float lod"; break;
1004 case TextureFunction::LOD0: break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001005 case TextureFunction::SIZE: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001006 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +00001007 }
1008
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001009 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001010 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001011
Nicolas Capens75fb4752013-07-10 15:14:47 -04001012 if (textureFunction->method == TextureFunction::SIZE)
1013 {
1014 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
1015 {
1016 if (IsSamplerArray(textureFunction->sampler))
1017 {
1018 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
1019 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
1020 }
1021 else
1022 {
1023 out << " uint width; uint height; uint numberOfLevels;\n"
1024 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
1025 }
1026 }
1027 else if (IsSampler3D(textureFunction->sampler))
1028 {
1029 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
1030 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
1031 }
1032 else UNREACHABLE();
1033
1034 switch(textureFunction->sampler)
1035 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04001036 case EbtSampler2D: out << " return int2(width, height);"; break;
1037 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
1038 case EbtSamplerCube: out << " return int2(width, height);"; break;
1039 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
1040 case EbtISampler2D: out << " return int2(width, height);"; break;
1041 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
1042 case EbtISamplerCube: out << " return int2(width, height);"; break;
1043 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
1044 case EbtUSampler2D: out << " return int2(width, height);"; break;
1045 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
1046 case EbtUSamplerCube: out << " return int2(width, height);"; break;
1047 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
1048 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
1049 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
1050 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001051 default: UNREACHABLE();
1052 }
1053 }
1054 else if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
daniel@transgaming.com15795192011-05-11 15:36:20 +00001055 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001056 // Currently unsupported because TextureCube does not support Load
1057 // This will require emulation using a Texture2DArray with 6 faces
1058 if (textureFunction->sampler == EbtISamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001059 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001060 out << " return int4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001061 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001062 else if (textureFunction->sampler == EbtUSamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001063 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001064 out << " return uint4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001065 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001066 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001067 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001068 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001069 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001070 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001071 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001072 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001073 {
Nicolas Capens93e50de2013-07-09 13:46:28 -04001074 if (IsSamplerArray(textureFunction->sampler))
1075 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001076 out << " float width; float height; float layers; float levels;\n";
1077
1078 if (textureFunction->method == TextureFunction::LOD0)
1079 {
1080 out << " uint mip = 0;\n";
1081 }
1082 else
1083 {
1084 if (textureFunction->method == TextureFunction::IMPLICIT ||
1085 textureFunction->method == TextureFunction::BIAS)
1086 {
1087 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1088 " float2 tSized = float2(t.x * width, t.y * height);\n"
1089 " float dx = length(ddx(tSized));\n"
1090 " float dy = length(ddy(tSized));\n"
1091 " float lod = log2(max(sqrt(dx), sqrt(dy)));\n";
1092
1093 if (textureFunction->method == TextureFunction::BIAS)
1094 {
1095 out << " lod += bias;\n";
1096 }
1097 }
1098
1099 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1100 }
1101
1102 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001103 }
1104 else
1105 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001106 out << " float width; float height; float levels;\n";
1107
1108 if (textureFunction->method == TextureFunction::LOD0)
1109 {
1110 out << " uint mip = 0;\n";
1111 }
1112 else
1113 {
1114 if (textureFunction->method == TextureFunction::IMPLICIT ||
1115 textureFunction->method == TextureFunction::BIAS)
1116 {
1117 out << " x.GetDimensions(0, width, height, levels);\n"
1118 " float2 tSized = float2(t.x * width, t.y * height);\n"
1119 " float dx = length(ddx(tSized));\n"
1120 " float dy = length(ddy(tSized));\n"
1121 " float lod = log2(max(sqrt(dx), sqrt(dy)));\n";
1122
1123 if (textureFunction->method == TextureFunction::BIAS)
1124 {
1125 out << " lod += bias;\n";
1126 }
1127 }
1128
1129 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1130 }
1131
1132 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001133 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001134 }
1135 else if (IsSampler3D(textureFunction->sampler))
1136 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001137 out << " float width; float height; float depth; float levels;\n";
1138
1139 if (textureFunction->method == TextureFunction::LOD0)
1140 {
1141 out << " uint mip = 0;\n";
1142 }
1143 else
1144 {
1145 if (textureFunction->method == TextureFunction::IMPLICIT ||
1146 textureFunction->method == TextureFunction::BIAS)
1147 {
1148 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1149 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
1150 " float dx = length(ddx(tSized));\n"
1151 " float dy = length(ddy(tSized));\n"
1152 " float lod = log2(max(sqrt(dx), sqrt(dy)));\n";
1153
1154 if (textureFunction->method == TextureFunction::BIAS)
1155 {
1156 out << " lod += bias;\n";
1157 }
1158 }
1159
1160 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1161 }
1162
1163 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001164 }
1165 else UNREACHABLE();
1166 }
1167
1168 out << " return ";
1169
1170 // HLSL intrinsic
1171 if (mOutputType == SH_HLSL9_OUTPUT)
1172 {
1173 switch(textureFunction->sampler)
1174 {
1175 case EbtSampler2D: out << "tex2D"; break;
1176 case EbtSamplerCube: out << "texCUBE"; break;
1177 default: UNREACHABLE();
1178 }
1179
Nicolas Capens75fb4752013-07-10 15:14:47 -04001180 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001181 {
1182 case TextureFunction::IMPLICIT: out << "(s, "; break;
1183 case TextureFunction::BIAS: out << "bias(s, "; break;
1184 case TextureFunction::LOD: out << "lod(s, "; break;
1185 case TextureFunction::LOD0: out << "lod(s, "; break;
1186 default: UNREACHABLE();
1187 }
1188 }
1189 else if (mOutputType == SH_HLSL11_OUTPUT)
1190 {
1191 if (IsIntegerSampler(textureFunction->sampler))
1192 {
1193 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001194 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001195 else if (IsShadowSampler(textureFunction->sampler))
1196 {
1197 out << "x.SampleCmp(s, ";
1198 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001199 else
1200 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001201 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001202 {
1203 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1204 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1205 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1206 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
1207 default: UNREACHABLE();
1208 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001209 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001210 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001211 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001212
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001213 // Integer sampling requires integer addresses
1214 TString addressx = "";
1215 TString addressy = "";
1216 TString addressz = "";
1217 TString close = "";
1218
1219 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001220 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001221 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001222 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001223 case 2: out << "int3("; break;
1224 case 3: out << "int4("; break;
1225 default: UNREACHABLE();
1226 }
1227
Nicolas Capensc98406a2013-07-10 14:52:44 -04001228 addressx = "int(floor(width * frac((";
1229 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001230
1231 if (IsSamplerArray(textureFunction->sampler))
1232 {
1233 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1234 }
1235 else
1236 {
Nicolas Capensc98406a2013-07-10 14:52:44 -04001237 addressz = "int(floor(depth * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001238 }
1239
1240 close = "))))";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001241 }
1242 else
1243 {
1244 switch(hlslCoords)
1245 {
1246 case 2: out << "float2("; break;
1247 case 3: out << "float3("; break;
1248 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001249 default: UNREACHABLE();
1250 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001251 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001252
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001253 TString proj = ""; // Only used for projected textures
1254
1255 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001256 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001257 switch(textureFunction->coords)
1258 {
1259 case 3: proj = " / t.z"; break;
1260 case 4: proj = " / t.w"; break;
1261 default: UNREACHABLE();
1262 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001263 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001264
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001265 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001266
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001267 if (mOutputType == SH_HLSL9_OUTPUT)
1268 {
1269 if (hlslCoords >= 3)
1270 {
1271 if (textureFunction->coords < 3)
1272 {
1273 out << ", 0";
1274 }
1275 else
1276 {
1277 out << ", t.z" + proj;
1278 }
1279 }
1280
1281 if (hlslCoords == 4)
1282 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001283 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001284 {
1285 case TextureFunction::BIAS: out << ", bias"; break;
1286 case TextureFunction::LOD: out << ", lod"; break;
1287 case TextureFunction::LOD0: out << ", 0"; break;
1288 default: UNREACHABLE();
1289 }
1290 }
1291
1292 out << "));\n";
1293 }
1294 else if (mOutputType == SH_HLSL11_OUTPUT)
1295 {
1296 if (hlslCoords >= 3)
1297 {
1298 out << ", " + addressz + ("t.z" + proj) + close;
1299 }
1300
Nicolas Capenscb127d32013-07-15 17:26:18 -04001301 if (IsIntegerSampler(textureFunction->sampler))
1302 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001303 out << ", mip));";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001304 }
1305 else if (IsShadowSampler(textureFunction->sampler))
1306 {
1307 // Compare value
1308 switch(textureFunction->coords)
1309 {
1310 case 3: out << "), t.z);"; break;
1311 case 4: out << "), t.w);"; break;
1312 default: UNREACHABLE();
1313 }
1314 }
1315 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001316 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001317 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001318 {
1319 case TextureFunction::IMPLICIT: out << "));"; break;
1320 case TextureFunction::BIAS: out << "), bias);"; break;
1321 case TextureFunction::LOD: out << "), lod);"; break;
1322 case TextureFunction::LOD0: out << "), 0);"; break;
1323 default: UNREACHABLE();
1324 }
1325 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001326 }
1327 else UNREACHABLE();
1328 }
1329
1330 out << "\n"
1331 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001332 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001333 }
1334
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001335 if (mUsesFragCoord)
1336 {
1337 out << "#define GL_USES_FRAG_COORD\n";
1338 }
1339
1340 if (mUsesPointCoord)
1341 {
1342 out << "#define GL_USES_POINT_COORD\n";
1343 }
1344
1345 if (mUsesFrontFacing)
1346 {
1347 out << "#define GL_USES_FRONT_FACING\n";
1348 }
1349
1350 if (mUsesPointSize)
1351 {
1352 out << "#define GL_USES_POINT_SIZE\n";
1353 }
1354
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001355 if (mUsesFragDepth)
1356 {
1357 out << "#define GL_USES_FRAG_DEPTH\n";
1358 }
1359
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001360 if (mUsesDepthRange)
1361 {
1362 out << "#define GL_USES_DEPTH_RANGE\n";
1363 }
1364
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001365 if (mUsesXor)
1366 {
1367 out << "bool xor(bool p, bool q)\n"
1368 "{\n"
1369 " return (p || q) && !(p && q);\n"
1370 "}\n"
1371 "\n";
1372 }
1373
1374 if (mUsesMod1)
1375 {
1376 out << "float mod(float x, float y)\n"
1377 "{\n"
1378 " return x - y * floor(x / y);\n"
1379 "}\n"
1380 "\n";
1381 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001382
1383 if (mUsesMod2v)
1384 {
1385 out << "float2 mod(float2 x, float2 y)\n"
1386 "{\n"
1387 " return x - y * floor(x / y);\n"
1388 "}\n"
1389 "\n";
1390 }
1391
1392 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001393 {
1394 out << "float2 mod(float2 x, float y)\n"
1395 "{\n"
1396 " return x - y * floor(x / y);\n"
1397 "}\n"
1398 "\n";
1399 }
1400
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001401 if (mUsesMod3v)
1402 {
1403 out << "float3 mod(float3 x, float3 y)\n"
1404 "{\n"
1405 " return x - y * floor(x / y);\n"
1406 "}\n"
1407 "\n";
1408 }
1409
1410 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001411 {
1412 out << "float3 mod(float3 x, float y)\n"
1413 "{\n"
1414 " return x - y * floor(x / y);\n"
1415 "}\n"
1416 "\n";
1417 }
1418
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001419 if (mUsesMod4v)
1420 {
1421 out << "float4 mod(float4 x, float4 y)\n"
1422 "{\n"
1423 " return x - y * floor(x / y);\n"
1424 "}\n"
1425 "\n";
1426 }
1427
1428 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001429 {
1430 out << "float4 mod(float4 x, float y)\n"
1431 "{\n"
1432 " return x - y * floor(x / y);\n"
1433 "}\n"
1434 "\n";
1435 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001436
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001437 if (mUsesFaceforward1)
1438 {
1439 out << "float faceforward(float N, float I, float Nref)\n"
1440 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001441 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001442 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001443 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001444 " }\n"
1445 " else\n"
1446 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001447 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001448 " }\n"
1449 "}\n"
1450 "\n";
1451 }
1452
1453 if (mUsesFaceforward2)
1454 {
1455 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1456 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001457 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001458 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001459 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001460 " }\n"
1461 " else\n"
1462 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001463 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001464 " }\n"
1465 "}\n"
1466 "\n";
1467 }
1468
1469 if (mUsesFaceforward3)
1470 {
1471 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1472 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001473 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001474 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001475 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001476 " }\n"
1477 " else\n"
1478 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001479 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001480 " }\n"
1481 "}\n"
1482 "\n";
1483 }
1484
1485 if (mUsesFaceforward4)
1486 {
1487 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1488 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001489 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001490 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001491 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001492 " }\n"
1493 " else\n"
1494 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001495 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001496 " }\n"
1497 "}\n"
1498 "\n";
1499 }
1500
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001501 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001502 {
1503 out << "float atanyx(float y, float x)\n"
1504 "{\n"
1505 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1506 " return atan2(y, x);\n"
1507 "}\n";
1508 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001509
1510 if (mUsesAtan2_2)
1511 {
1512 out << "float2 atanyx(float2 y, float2 x)\n"
1513 "{\n"
1514 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1515 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1516 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1517 "}\n";
1518 }
1519
1520 if (mUsesAtan2_3)
1521 {
1522 out << "float3 atanyx(float3 y, float3 x)\n"
1523 "{\n"
1524 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1525 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1526 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1527 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1528 "}\n";
1529 }
1530
1531 if (mUsesAtan2_4)
1532 {
1533 out << "float4 atanyx(float4 y, float4 x)\n"
1534 "{\n"
1535 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1536 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1537 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1538 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1539 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1540 "}\n";
1541 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001542}
1543
1544void OutputHLSL::visitSymbol(TIntermSymbol *node)
1545{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001546 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001547
Jamie Madill570e04d2013-06-21 09:15:33 -04001548 // Handle accessing std140 structs by value
1549 if (mFlaggedStructMappedNames.count(node) > 0)
1550 {
1551 out << mFlaggedStructMappedNames[node];
1552 return;
1553 }
1554
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001555 TString name = node->getSymbol();
1556
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001557 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001558 {
1559 mUsesDepthRange = true;
1560 out << name;
1561 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001562 else
1563 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001564 TQualifier qualifier = node->getQualifier();
1565
1566 if (qualifier == EvqUniform)
1567 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001568 const TType& nodeType = node->getType();
1569 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1570
1571 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001572 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001573 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001574 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001575 else
1576 {
1577 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001578 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001579
1580 out << decorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001581 }
Jamie Madill19571812013-08-12 15:26:34 -07001582 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001583 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001584 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001585 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001586 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001587 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001588 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001589 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001590 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001591 }
Jamie Madill19571812013-08-12 15:26:34 -07001592 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001593 {
1594 mReferencedOutputVariables[name] = node;
1595 out << "out_" << name;
1596 }
1597 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001598 {
1599 out << "gl_Color[0]";
1600 mUsesFragColor = true;
1601 }
1602 else if (qualifier == EvqFragData)
1603 {
1604 out << "gl_Color";
1605 mUsesFragData = true;
1606 }
1607 else if (qualifier == EvqFragCoord)
1608 {
1609 mUsesFragCoord = true;
1610 out << name;
1611 }
1612 else if (qualifier == EvqPointCoord)
1613 {
1614 mUsesPointCoord = true;
1615 out << name;
1616 }
1617 else if (qualifier == EvqFrontFacing)
1618 {
1619 mUsesFrontFacing = true;
1620 out << name;
1621 }
1622 else if (qualifier == EvqPointSize)
1623 {
1624 mUsesPointSize = true;
1625 out << name;
1626 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001627 else if (name == "gl_FragDepthEXT")
1628 {
1629 mUsesFragDepth = true;
1630 out << "gl_Depth";
1631 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001632 else
1633 {
1634 out << decorate(name);
1635 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001636 }
1637}
1638
1639bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1640{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001641 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001642
Jamie Madill570e04d2013-06-21 09:15:33 -04001643 // Handle accessing std140 structs by value
1644 if (mFlaggedStructMappedNames.count(node) > 0)
1645 {
1646 out << mFlaggedStructMappedNames[node];
1647 return false;
1648 }
1649
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001650 switch (node->getOp())
1651 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001652 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001653 case EOpInitialize:
1654 if (visit == PreVisit)
1655 {
1656 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1657 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1658 // new variable is created before the assignment is evaluated), so we need to convert
1659 // this to "float t = x, x = t;".
1660
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001661 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1662 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001663
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001664 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1665 expression->traverse(&searchSymbol);
1666 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001667
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001668 if (sameSymbol)
1669 {
1670 // Type already printed
1671 out << "t" + str(mUniqueIndex) + " = ";
1672 expression->traverse(this);
1673 out << ", ";
1674 symbolNode->traverse(this);
1675 out << " = t" + str(mUniqueIndex);
1676
1677 mUniqueIndex++;
1678 return false;
1679 }
1680 }
1681 else if (visit == InVisit)
1682 {
1683 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001684 }
1685 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001686 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1687 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1688 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1689 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1690 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1691 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001692 if (visit == PreVisit)
1693 {
1694 out << "(";
1695 }
1696 else if (visit == InVisit)
1697 {
1698 out << " = mul(";
1699 node->getLeft()->traverse(this);
1700 out << ", transpose(";
1701 }
1702 else
1703 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001704 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001705 }
1706 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001707 case EOpMatrixTimesMatrixAssign:
1708 if (visit == PreVisit)
1709 {
1710 out << "(";
1711 }
1712 else if (visit == InVisit)
1713 {
1714 out << " = mul(";
1715 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001716 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001717 }
1718 else
1719 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001720 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001721 }
1722 break;
1723 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001724 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001725 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001726 const TType& leftType = node->getLeft()->getType();
1727 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001728 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001729 if (visit == PreVisit)
1730 {
1731 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1732 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1733
1734 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1735 out << interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1736
1737 return false;
1738 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001739 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001740 else
1741 {
1742 outputTriplet(visit, "", "[", "]");
1743 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001744 }
1745 break;
1746 case EOpIndexIndirect:
1747 // We do not currently support indirect references to interface blocks
1748 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1749 outputTriplet(visit, "", "[", "]");
1750 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001751 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001752 if (visit == InVisit)
1753 {
1754 const TStructure* structure = node->getLeft()->getType().getStruct();
1755 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1756 const TField* field = structure->fields()[index->getIConst(0)];
1757 out << "." + decorateField(field->name(), *structure);
1758
1759 return false;
1760 }
1761 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001762 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001763 if (visit == InVisit)
1764 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001765 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1766 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1767 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
1768 out << "." + decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001769
1770 return false;
1771 }
1772 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001773 case EOpVectorSwizzle:
1774 if (visit == InVisit)
1775 {
1776 out << ".";
1777
1778 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1779
1780 if (swizzle)
1781 {
1782 TIntermSequence &sequence = swizzle->getSequence();
1783
1784 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1785 {
1786 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1787
1788 if (element)
1789 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001790 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001791
1792 switch (i)
1793 {
1794 case 0: out << "x"; break;
1795 case 1: out << "y"; break;
1796 case 2: out << "z"; break;
1797 case 3: out << "w"; break;
1798 default: UNREACHABLE();
1799 }
1800 }
1801 else UNREACHABLE();
1802 }
1803 }
1804 else UNREACHABLE();
1805
1806 return false; // Fully processed
1807 }
1808 break;
1809 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1810 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1811 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1812 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001813 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001814 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001815 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001816 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001817 if (node->getOp() == EOpEqual)
1818 {
1819 outputTriplet(visit, "(", " == ", ")");
1820 }
1821 else
1822 {
1823 outputTriplet(visit, "(", " != ", ")");
1824 }
1825 }
1826 else if (node->getLeft()->getBasicType() == EbtStruct)
1827 {
1828 if (node->getOp() == EOpEqual)
1829 {
1830 out << "(";
1831 }
1832 else
1833 {
1834 out << "!(";
1835 }
1836
Jamie Madill98493dd2013-07-08 14:39:03 -04001837 const TStructure &structure = *node->getLeft()->getType().getStruct();
1838 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001839
Jamie Madill98493dd2013-07-08 14:39:03 -04001840 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001841 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001842 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001843
1844 node->getLeft()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001845 out << "." + decorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001846 node->getRight()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001847 out << "." + decorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001848
Jamie Madill98493dd2013-07-08 14:39:03 -04001849 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001850 {
1851 out << " && ";
1852 }
1853 }
1854
1855 out << ")";
1856
1857 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001858 }
1859 else
1860 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001861 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001862
1863 if (node->getOp() == EOpEqual)
1864 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001865 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001866 }
1867 else
1868 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001869 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001870 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001871 }
1872 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001873 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1874 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1875 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1876 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1877 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001878 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001879 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1880 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001881 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001882 case EOpLogicalOr:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001883 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001884 return false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001885 case EOpLogicalXor:
1886 mUsesXor = true;
1887 outputTriplet(visit, "xor(", ", ", ")");
1888 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001889 case EOpLogicalAnd:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001890 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001891 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001892 default: UNREACHABLE();
1893 }
1894
1895 return true;
1896}
1897
1898bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1899{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001900 switch (node->getOp())
1901 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001902 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1903 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1904 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1905 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1906 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1907 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1908 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001909 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04001910 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001911 case EOpConvFloatToBool:
1912 switch (node->getOperand()->getType().getNominalSize())
1913 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001914 case 1: outputTriplet(visit, "bool(", "", ")"); break;
1915 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
1916 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
1917 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001918 default: UNREACHABLE();
1919 }
1920 break;
1921 case EOpConvBoolToFloat:
1922 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04001923 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001924 switch (node->getOperand()->getType().getNominalSize())
1925 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001926 case 1: outputTriplet(visit, "float(", "", ")"); break;
1927 case 2: outputTriplet(visit, "float2(", "", ")"); break;
1928 case 3: outputTriplet(visit, "float3(", "", ")"); break;
1929 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001930 default: UNREACHABLE();
1931 }
1932 break;
1933 case EOpConvFloatToInt:
1934 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04001935 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001936 switch (node->getOperand()->getType().getNominalSize())
1937 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001938 case 1: outputTriplet(visit, "int(", "", ")"); break;
1939 case 2: outputTriplet(visit, "int2(", "", ")"); break;
1940 case 3: outputTriplet(visit, "int3(", "", ")"); break;
1941 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001942 default: UNREACHABLE();
1943 }
1944 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04001945 case EOpConvFloatToUInt:
1946 case EOpConvBoolToUInt:
1947 case EOpConvIntToUInt:
Jamie Madilla16f1672013-07-03 15:15:17 -04001948 switch (node->getOperand()->getType().getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001949 {
1950 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001951 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
1952 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
1953 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001954 default: UNREACHABLE();
1955 }
1956 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001957 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1958 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1959 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1960 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1961 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1962 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1963 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1964 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1965 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1966 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1967 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1968 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1969 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1970 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1971 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1972 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1973 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1974 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1975 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1976 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1977 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001978 case EOpDFdx:
1979 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1980 {
1981 outputTriplet(visit, "(", "", ", 0.0)");
1982 }
1983 else
1984 {
1985 outputTriplet(visit, "ddx(", "", ")");
1986 }
1987 break;
1988 case EOpDFdy:
1989 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1990 {
1991 outputTriplet(visit, "(", "", ", 0.0)");
1992 }
1993 else
1994 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001995 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001996 }
1997 break;
1998 case EOpFwidth:
1999 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2000 {
2001 outputTriplet(visit, "(", "", ", 0.0)");
2002 }
2003 else
2004 {
2005 outputTriplet(visit, "fwidth(", "", ")");
2006 }
2007 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002008 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
2009 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002010 default: UNREACHABLE();
2011 }
2012
2013 return true;
2014}
2015
2016bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2017{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002018 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002019
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002020 switch (node->getOp())
2021 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002022 case EOpSequence:
2023 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002024 if (mInsideFunction)
2025 {
Jamie Madill075edd82013-07-08 13:30:19 -04002026 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002027 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002028
2029 mScopeDepth++;
2030
2031 if (mScopeBracket.size() < mScopeDepth)
2032 {
2033 mScopeBracket.push_back(0); // New scope level
2034 }
2035 else
2036 {
2037 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
2038 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002039 }
2040
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002041 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
2042 {
Jamie Madill075edd82013-07-08 13:30:19 -04002043 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002044
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002045 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002046
2047 out << ";\n";
2048 }
2049
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002050 if (mInsideFunction)
2051 {
Jamie Madill075edd82013-07-08 13:30:19 -04002052 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002053 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002054
2055 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002056 }
2057
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002058 return false;
2059 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002060 case EOpDeclaration:
2061 if (visit == PreVisit)
2062 {
2063 TIntermSequence &sequence = node->getSequence();
2064 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002065
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00002066 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002067 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002068 if (variable->getType().getStruct())
2069 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002070 addConstructor(variable->getType(), scopedStruct(variable->getType().getStruct()->name()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00002071 }
2072
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002073 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002074 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00002075 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002076 {
2077 out << "static ";
2078 }
2079
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002080 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002081
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002082 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002083 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002084 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002085
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002086 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002087 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002088 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002089 out << arrayString(symbol->getType());
daniel@transgaming.com7127f202010-04-15 20:45:22 +00002090 out << " = " + initializer(variable->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002091 }
2092 else
2093 {
2094 (*sit)->traverse(this);
2095 }
2096
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002097 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002098 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002099 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002100 }
2101 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002102 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002103 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2104 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002105 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002106 }
2107 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002108 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002109 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002110 {
2111 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2112 {
2113 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2114
2115 if (symbol)
2116 {
2117 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2118 mReferencedVaryings[symbol->getSymbol()] = symbol;
2119 }
2120 else
2121 {
2122 (*sit)->traverse(this);
2123 }
2124 }
2125 }
2126
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002127 return false;
2128 }
2129 else if (visit == InVisit)
2130 {
2131 out << ", ";
2132 }
2133 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002134 case EOpPrototype:
2135 if (visit == PreVisit)
2136 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002137 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002138
2139 TIntermSequence &arguments = node->getSequence();
2140
2141 for (unsigned int i = 0; i < arguments.size(); i++)
2142 {
2143 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2144
2145 if (symbol)
2146 {
2147 out << argumentString(symbol);
2148
2149 if (i < arguments.size() - 1)
2150 {
2151 out << ", ";
2152 }
2153 }
2154 else UNREACHABLE();
2155 }
2156
2157 out << ");\n";
2158
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002159 // Also prototype the Lod0 variant if needed
2160 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2161 {
2162 mOutputLod0Function = true;
2163 node->traverse(this);
2164 mOutputLod0Function = false;
2165 }
2166
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002167 return false;
2168 }
2169 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002170 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002171 case EOpFunction:
2172 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002173 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002174
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002175 out << typeString(node->getType()) << " ";
2176
2177 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002178 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002179 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002180 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002181 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002182 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002183 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002184 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002185
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002186 TIntermSequence &sequence = node->getSequence();
2187 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2188
2189 for (unsigned int i = 0; i < arguments.size(); i++)
2190 {
2191 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2192
2193 if (symbol)
2194 {
2195 if (symbol->getType().getStruct())
2196 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002197 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getStruct()->name()), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002198 }
2199
2200 out << argumentString(symbol);
2201
2202 if (i < arguments.size() - 1)
2203 {
2204 out << ", ";
2205 }
2206 }
2207 else UNREACHABLE();
2208 }
2209
2210 out << ")\n"
2211 "{\n";
2212
2213 if (sequence.size() > 1)
2214 {
2215 mInsideFunction = true;
2216 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002217 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002218 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002219
2220 out << "}\n";
2221
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002222 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2223 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002224 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002225 {
2226 mOutputLod0Function = true;
2227 node->traverse(this);
2228 mOutputLod0Function = false;
2229 }
2230 }
2231
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002232 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002233 }
2234 break;
2235 case EOpFunctionCall:
2236 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002237 TString name = TFunction::unmangleName(node->getName());
2238 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002239 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002240
2241 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002242 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002243 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002244 }
2245 else
2246 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002247 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2248
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002249 TextureFunction textureFunction;
2250 textureFunction.sampler = samplerType;
2251 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002252 textureFunction.method = TextureFunction::IMPLICIT;
2253 textureFunction.proj = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002254
2255 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002256 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002257 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002258 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002259 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002260 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002261 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002262 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002263 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002264 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002265 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002266 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002267 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002268 else if (name == "texture2DProjLod" || name == "textureProjLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002269 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002270 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002271 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002272 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002273 else if (name == "textureSize")
2274 {
2275 textureFunction.method = TextureFunction::SIZE;
2276 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002277 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002278
Nicolas Capens75fb4752013-07-10 15:14:47 -04002279 if (textureFunction.method != TextureFunction::LOD &&
2280 textureFunction.method != TextureFunction::SIZE)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002281 {
2282 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2283 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002284 textureFunction.method = TextureFunction::LOD0;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002285 }
2286 else if (arguments.size() == 3)
2287 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002288 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002289 }
2290 }
2291
2292 mUsesTexture.insert(textureFunction);
2293
2294 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002295 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002296
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002297 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2298 {
2299 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2300 {
2301 out << "texture_";
2302 (*arg)->traverse(this);
2303 out << ", sampler_";
2304 }
2305
2306 (*arg)->traverse(this);
2307
2308 if (arg < arguments.end() - 1)
2309 {
2310 out << ", ";
2311 }
2312 }
2313
2314 out << ")";
2315
2316 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002317 }
2318 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002319 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002320 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002321 addConstructor(node->getType(), "vec1", &node->getSequence());
2322 outputTriplet(visit, "vec1(", "", ")");
2323 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002324 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002325 addConstructor(node->getType(), "vec2", &node->getSequence());
2326 outputTriplet(visit, "vec2(", ", ", ")");
2327 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002328 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002329 addConstructor(node->getType(), "vec3", &node->getSequence());
2330 outputTriplet(visit, "vec3(", ", ", ")");
2331 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002332 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002333 addConstructor(node->getType(), "vec4", &node->getSequence());
2334 outputTriplet(visit, "vec4(", ", ", ")");
2335 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002336 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002337 addConstructor(node->getType(), "bvec1", &node->getSequence());
2338 outputTriplet(visit, "bvec1(", "", ")");
2339 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002340 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002341 addConstructor(node->getType(), "bvec2", &node->getSequence());
2342 outputTriplet(visit, "bvec2(", ", ", ")");
2343 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002344 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002345 addConstructor(node->getType(), "bvec3", &node->getSequence());
2346 outputTriplet(visit, "bvec3(", ", ", ")");
2347 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002348 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002349 addConstructor(node->getType(), "bvec4", &node->getSequence());
2350 outputTriplet(visit, "bvec4(", ", ", ")");
2351 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002352 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002353 addConstructor(node->getType(), "ivec1", &node->getSequence());
2354 outputTriplet(visit, "ivec1(", "", ")");
2355 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002356 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002357 addConstructor(node->getType(), "ivec2", &node->getSequence());
2358 outputTriplet(visit, "ivec2(", ", ", ")");
2359 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002360 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002361 addConstructor(node->getType(), "ivec3", &node->getSequence());
2362 outputTriplet(visit, "ivec3(", ", ", ")");
2363 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002364 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002365 addConstructor(node->getType(), "ivec4", &node->getSequence());
2366 outputTriplet(visit, "ivec4(", ", ", ")");
2367 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002368 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002369 addConstructor(node->getType(), "uvec1", &node->getSequence());
2370 outputTriplet(visit, "uvec1(", "", ")");
2371 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002372 case EOpConstructUVec2:
2373 addConstructor(node->getType(), "uvec2", &node->getSequence());
2374 outputTriplet(visit, "uvec2(", ", ", ")");
2375 break;
2376 case EOpConstructUVec3:
2377 addConstructor(node->getType(), "uvec3", &node->getSequence());
2378 outputTriplet(visit, "uvec3(", ", ", ")");
2379 break;
2380 case EOpConstructUVec4:
2381 addConstructor(node->getType(), "uvec4", &node->getSequence());
2382 outputTriplet(visit, "uvec4(", ", ", ")");
2383 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002384 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002385 addConstructor(node->getType(), "mat2", &node->getSequence());
2386 outputTriplet(visit, "mat2(", ", ", ")");
2387 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002388 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002389 addConstructor(node->getType(), "mat3", &node->getSequence());
2390 outputTriplet(visit, "mat3(", ", ", ")");
2391 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002392 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002393 addConstructor(node->getType(), "mat4", &node->getSequence());
2394 outputTriplet(visit, "mat4(", ", ", ")");
2395 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002396 case EOpConstructStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04002397 addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence());
2398 outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002399 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002400 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2401 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2402 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2403 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2404 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2405 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002406 case EOpMod:
2407 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002408 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002409 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2410 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2411 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002412 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002413 case 11: mUsesMod1 = true; break;
2414 case 22: mUsesMod2v = true; break;
2415 case 21: mUsesMod2f = true; break;
2416 case 33: mUsesMod3v = true; break;
2417 case 31: mUsesMod3f = true; break;
2418 case 44: mUsesMod4v = true; break;
2419 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002420 default: UNREACHABLE();
2421 }
2422
2423 outputTriplet(visit, "mod(", ", ", ")");
2424 }
2425 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002426 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002427 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002428 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002429 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2430 {
2431 case 1: mUsesAtan2_1 = true; break;
2432 case 2: mUsesAtan2_2 = true; break;
2433 case 3: mUsesAtan2_3 = true; break;
2434 case 4: mUsesAtan2_4 = true; break;
2435 default: UNREACHABLE();
2436 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002437 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002438 break;
2439 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2440 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2441 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2442 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2443 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2444 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2445 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2446 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2447 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002448 case EOpFaceForward:
2449 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002450 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002451 {
2452 case 1: mUsesFaceforward1 = true; break;
2453 case 2: mUsesFaceforward2 = true; break;
2454 case 3: mUsesFaceforward3 = true; break;
2455 case 4: mUsesFaceforward4 = true; break;
2456 default: UNREACHABLE();
2457 }
2458
2459 outputTriplet(visit, "faceforward(", ", ", ")");
2460 }
2461 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002462 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2463 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2464 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002465 default: UNREACHABLE();
2466 }
2467
2468 return true;
2469}
2470
2471bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2472{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002473 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002474
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002475 if (node->usesTernaryOperator())
2476 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002477 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002478 }
2479 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002480 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002481 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002482
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002483 out << "if(";
2484
2485 node->getCondition()->traverse(this);
2486
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002487 out << ")\n";
2488
Jamie Madill075edd82013-07-08 13:30:19 -04002489 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002490 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002491
daniel@transgaming.combb885322010-04-15 20:45:24 +00002492 if (node->getTrueBlock())
2493 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002494 traverseStatements(node->getTrueBlock());
daniel@transgaming.combb885322010-04-15 20:45:24 +00002495 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002496
Jamie Madill075edd82013-07-08 13:30:19 -04002497 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002498 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002499
2500 if (node->getFalseBlock())
2501 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002502 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002503
Jamie Madill075edd82013-07-08 13:30:19 -04002504 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002505 out << "{\n";
2506
Jamie Madill075edd82013-07-08 13:30:19 -04002507 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002508 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002509
Jamie Madill075edd82013-07-08 13:30:19 -04002510 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002511 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002512 }
2513 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002514
2515 return false;
2516}
2517
2518void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2519{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002520 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002521}
2522
2523bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2524{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002525 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2526
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002527 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002528 {
2529 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2530 }
2531
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002532 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002533 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002534 if (handleExcessiveLoop(node))
2535 {
2536 return false;
2537 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002538 }
2539
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002540 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002541
alokp@chromium.org52813552010-11-16 18:36:09 +00002542 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002543 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002544 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002545
Jamie Madill075edd82013-07-08 13:30:19 -04002546 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002547 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002548 }
2549 else
2550 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002551 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002552
2553 if (node->getInit())
2554 {
2555 node->getInit()->traverse(this);
2556 }
2557
2558 out << "; ";
2559
alokp@chromium.org52813552010-11-16 18:36:09 +00002560 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002561 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002562 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002563 }
2564
2565 out << "; ";
2566
alokp@chromium.org52813552010-11-16 18:36:09 +00002567 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002568 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002569 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002570 }
2571
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002572 out << ")\n";
2573
Jamie Madill075edd82013-07-08 13:30:19 -04002574 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002575 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002576 }
2577
2578 if (node->getBody())
2579 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002580 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002581 }
2582
Jamie Madill075edd82013-07-08 13:30:19 -04002583 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002584 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002585
alokp@chromium.org52813552010-11-16 18:36:09 +00002586 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002587 {
Jamie Madill075edd82013-07-08 13:30:19 -04002588 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002589 out << "while(\n";
2590
alokp@chromium.org52813552010-11-16 18:36:09 +00002591 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002592
daniel@transgaming.com73536982012-03-21 20:45:49 +00002593 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002594 }
2595
daniel@transgaming.com73536982012-03-21 20:45:49 +00002596 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002597
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002598 mInsideDiscontinuousLoop = wasDiscontinuous;
2599
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002600 return false;
2601}
2602
2603bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2604{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002605 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002606
2607 switch (node->getFlowOp())
2608 {
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002609 case EOpKill: outputTriplet(visit, "discard;\n", "", ""); break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002610 case EOpBreak:
2611 if (visit == PreVisit)
2612 {
2613 if (mExcessiveLoopIndex)
2614 {
2615 out << "{Break";
2616 mExcessiveLoopIndex->traverse(this);
2617 out << " = true; break;}\n";
2618 }
2619 else
2620 {
2621 out << "break;\n";
2622 }
2623 }
2624 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002625 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002626 case EOpReturn:
2627 if (visit == PreVisit)
2628 {
2629 if (node->getExpression())
2630 {
2631 out << "return ";
2632 }
2633 else
2634 {
2635 out << "return;\n";
2636 }
2637 }
2638 else if (visit == PostVisit)
2639 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002640 if (node->getExpression())
2641 {
2642 out << ";\n";
2643 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002644 }
2645 break;
2646 default: UNREACHABLE();
2647 }
2648
2649 return true;
2650}
2651
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002652void OutputHLSL::traverseStatements(TIntermNode *node)
2653{
2654 if (isSingleStatement(node))
2655 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002656 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002657 }
2658
2659 node->traverse(this);
2660}
2661
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002662bool OutputHLSL::isSingleStatement(TIntermNode *node)
2663{
2664 TIntermAggregate *aggregate = node->getAsAggregate();
2665
2666 if (aggregate)
2667 {
2668 if (aggregate->getOp() == EOpSequence)
2669 {
2670 return false;
2671 }
2672 else
2673 {
2674 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2675 {
2676 if (!isSingleStatement(*sit))
2677 {
2678 return false;
2679 }
2680 }
2681
2682 return true;
2683 }
2684 }
2685
2686 return true;
2687}
2688
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002689// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2690// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002691bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2692{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002693 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002694 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002695
2696 // Parse loops of the form:
2697 // for(int index = initial; index [comparator] limit; index += increment)
2698 TIntermSymbol *index = NULL;
2699 TOperator comparator = EOpNull;
2700 int initial = 0;
2701 int limit = 0;
2702 int increment = 0;
2703
2704 // Parse index name and intial value
2705 if (node->getInit())
2706 {
2707 TIntermAggregate *init = node->getInit()->getAsAggregate();
2708
2709 if (init)
2710 {
2711 TIntermSequence &sequence = init->getSequence();
2712 TIntermTyped *variable = sequence[0]->getAsTyped();
2713
2714 if (variable && variable->getQualifier() == EvqTemporary)
2715 {
2716 TIntermBinary *assign = variable->getAsBinaryNode();
2717
2718 if (assign->getOp() == EOpInitialize)
2719 {
2720 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2721 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2722
2723 if (symbol && constant)
2724 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002725 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002726 {
2727 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002728 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002729 }
2730 }
2731 }
2732 }
2733 }
2734 }
2735
2736 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002737 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002738 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002739 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002740
2741 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2742 {
2743 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2744
2745 if (constant)
2746 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002747 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002748 {
2749 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002750 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002751 }
2752 }
2753 }
2754 }
2755
2756 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002757 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002758 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002759 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2760 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002761
2762 if (binaryTerminal)
2763 {
2764 TOperator op = binaryTerminal->getOp();
2765 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2766
2767 if (constant)
2768 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002769 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002770 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002771 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002772
2773 switch (op)
2774 {
2775 case EOpAddAssign: increment = value; break;
2776 case EOpSubAssign: increment = -value; break;
2777 default: UNIMPLEMENTED();
2778 }
2779 }
2780 }
2781 }
2782 else if (unaryTerminal)
2783 {
2784 TOperator op = unaryTerminal->getOp();
2785
2786 switch (op)
2787 {
2788 case EOpPostIncrement: increment = 1; break;
2789 case EOpPostDecrement: increment = -1; break;
2790 case EOpPreIncrement: increment = 1; break;
2791 case EOpPreDecrement: increment = -1; break;
2792 default: UNIMPLEMENTED();
2793 }
2794 }
2795 }
2796
2797 if (index != NULL && comparator != EOpNull && increment != 0)
2798 {
2799 if (comparator == EOpLessThanEqual)
2800 {
2801 comparator = EOpLessThan;
2802 limit += 1;
2803 }
2804
2805 if (comparator == EOpLessThan)
2806 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002807 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002808
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002809 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002810 {
2811 return false; // Not an excessive loop
2812 }
2813
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002814 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2815 mExcessiveLoopIndex = index;
2816
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002817 out << "{int ";
2818 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002819 out << ";\n"
2820 "bool Break";
2821 index->traverse(this);
2822 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002823
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002824 bool firstLoopFragment = true;
2825
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002826 while (iterations > 0)
2827 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002828 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002829
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002830 if (!firstLoopFragment)
2831 {
2832 out << "if(!Break";
2833 index->traverse(this);
2834 out << ") {\n";
2835 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002836
2837 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2838 {
2839 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2840 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002841
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002842 // for(int index = initial; index < clampedLimit; index += increment)
2843
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002844 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002845 index->traverse(this);
2846 out << " = ";
2847 out << initial;
2848
2849 out << "; ";
2850 index->traverse(this);
2851 out << " < ";
2852 out << clampedLimit;
2853
2854 out << "; ";
2855 index->traverse(this);
2856 out << " += ";
2857 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002858 out << ")\n";
2859
Jamie Madill075edd82013-07-08 13:30:19 -04002860 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002861 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002862
2863 if (node->getBody())
2864 {
2865 node->getBody()->traverse(this);
2866 }
2867
Jamie Madill075edd82013-07-08 13:30:19 -04002868 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002869 out << ";}\n";
2870
2871 if (!firstLoopFragment)
2872 {
2873 out << "}\n";
2874 }
2875
2876 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002877
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002878 initial += MAX_LOOP_ITERATIONS * increment;
2879 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002880 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002881
2882 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002883
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002884 mExcessiveLoopIndex = restoreIndex;
2885
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002886 return true;
2887 }
2888 else UNIMPLEMENTED();
2889 }
2890
2891 return false; // Not handled as an excessive loop
2892}
2893
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002894void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002895{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002896 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002897
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002898 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002899 {
2900 out << preString;
2901 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002902 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002903 {
2904 out << inString;
2905 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002906 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002907 {
2908 out << postString;
2909 }
2910}
2911
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002912void OutputHLSL::outputLineDirective(int line)
2913{
2914 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2915 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002916 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002917 mBody << "#line " << line;
2918
2919 if (mContext.sourcePath)
2920 {
2921 mBody << " \"" << mContext.sourcePath << "\"";
2922 }
2923
2924 mBody << "\n";
2925 }
2926}
2927
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002928TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2929{
2930 TQualifier qualifier = symbol->getQualifier();
2931 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002932 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002933
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002934 if (name.empty()) // HLSL demands named arguments, also for prototypes
2935 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002936 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002937 }
2938 else
2939 {
2940 name = decorate(name);
2941 }
2942
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002943 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2944 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04002945 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
2946 qualifierString(qualifier) + " " + samplerString(type) + " sampler_" + name + arrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002947 }
2948
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002949 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002950}
2951
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00002952TString OutputHLSL::interpolationString(TQualifier qualifier)
2953{
2954 switch(qualifier)
2955 {
2956 case EvqVaryingIn: return "";
Jamie Madill19571812013-08-12 15:26:34 -07002957 case EvqFragmentIn: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00002958 case EvqInvariantVaryingIn: return "";
2959 case EvqSmoothIn: return "linear";
2960 case EvqFlatIn: return "nointerpolation";
2961 case EvqCentroidIn: return "centroid";
2962 case EvqVaryingOut: return "";
Jamie Madill19571812013-08-12 15:26:34 -07002963 case EvqVertexOut: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00002964 case EvqInvariantVaryingOut: return "";
2965 case EvqSmoothOut: return "linear";
2966 case EvqFlatOut: return "nointerpolation";
2967 case EvqCentroidOut: return "centroid";
2968 default: UNREACHABLE();
2969 }
2970
2971 return "";
2972}
2973
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002974TString OutputHLSL::qualifierString(TQualifier qualifier)
2975{
2976 switch(qualifier)
2977 {
2978 case EvqIn: return "in";
2979 case EvqOut: return "out";
2980 case EvqInOut: return "inout";
2981 case EvqConstReadOnly: return "const";
2982 default: UNREACHABLE();
2983 }
2984
2985 return "";
2986}
2987
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002988TString OutputHLSL::typeString(const TType &type)
2989{
Jamie Madill98493dd2013-07-08 14:39:03 -04002990 const TStructure* structure = type.getStruct();
2991 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002992 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002993 const TString& typeName = structure->name();
2994 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002995 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002996 return structLookup(typeName);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002997 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00002998 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002999 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003000 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003001 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003002 }
3003 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003004 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003005 int cols = type.getCols();
3006 int rows = type.getRows();
3007 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003008 }
3009 else
3010 {
3011 switch (type.getBasicType())
3012 {
3013 case EbtFloat:
3014 switch (type.getNominalSize())
3015 {
3016 case 1: return "float";
3017 case 2: return "float2";
3018 case 3: return "float3";
3019 case 4: return "float4";
3020 }
3021 case EbtInt:
3022 switch (type.getNominalSize())
3023 {
3024 case 1: return "int";
3025 case 2: return "int2";
3026 case 3: return "int3";
3027 case 4: return "int4";
3028 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003029 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04003030 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003031 {
3032 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003033 case 2: return "uint2";
3034 case 3: return "uint3";
3035 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003036 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003037 case EbtBool:
3038 switch (type.getNominalSize())
3039 {
3040 case 1: return "bool";
3041 case 2: return "bool2";
3042 case 3: return "bool3";
3043 case 4: return "bool4";
3044 }
3045 case EbtVoid:
3046 return "void";
3047 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003048 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04003049 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003050 case EbtSampler2DArray:
3051 case EbtISampler2DArray:
3052 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003053 return "sampler2D";
3054 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003055 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04003056 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003057 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00003058 case EbtSamplerExternalOES:
3059 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00003060 default:
3061 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003062 }
3063 }
3064
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003065 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003066 return "<unknown type>";
3067}
3068
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003069TString OutputHLSL::textureString(const TType &type)
3070{
3071 switch (type.getBasicType())
3072 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003073 case EbtSampler2D: return "Texture2D";
3074 case EbtSamplerCube: return "TextureCube";
3075 case EbtSamplerExternalOES: return "Texture2D";
3076 case EbtSampler2DArray: return "Texture2DArray";
3077 case EbtSampler3D: return "Texture3D";
3078 case EbtISampler2D: return "Texture2D<int4>";
3079 case EbtISampler3D: return "Texture3D<int4>";
3080 case EbtISamplerCube: return "TextureCube<int4>";
3081 case EbtISampler2DArray: return "Texture2DArray<int4>";
3082 case EbtUSampler2D: return "Texture2D<uint4>";
3083 case EbtUSampler3D: return "Texture3D<uint4>";
3084 case EbtUSamplerCube: return "TextureCube<uint4>";
3085 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
3086 case EbtSampler2DShadow: return "Texture2D";
3087 case EbtSamplerCubeShadow: return "TextureCube";
3088 case EbtSampler2DArrayShadow: return "Texture2DArray";
3089 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003090 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04003091
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003092 return "<unknown texture type>";
3093}
3094
Nicolas Capenscb127d32013-07-15 17:26:18 -04003095TString OutputHLSL::samplerString(const TType &type)
3096{
3097 if (IsShadowSampler(type.getBasicType()))
3098 {
3099 return "SamplerComparisonState";
3100 }
3101 else
3102 {
3103 return "SamplerState";
3104 }
3105}
3106
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003107TString OutputHLSL::arrayString(const TType &type)
3108{
3109 if (!type.isArray())
3110 {
3111 return "";
3112 }
3113
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003114 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003115}
3116
3117TString OutputHLSL::initializer(const TType &type)
3118{
3119 TString string;
3120
Jamie Madill94bf7f22013-07-08 13:31:15 -04003121 size_t size = type.getObjectSize();
3122 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003123 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003124 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003125
Jamie Madill94bf7f22013-07-08 13:31:15 -04003126 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003127 {
3128 string += ", ";
3129 }
3130 }
3131
daniel@transgaming.comead23042010-04-29 03:35:36 +00003132 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003133}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003134
Jamie Madill98493dd2013-07-08 14:39:03 -04003135TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003136{
Jamie Madill98493dd2013-07-08 14:39:03 -04003137 const TFieldList &fields = structure.fields();
3138 const bool isNameless = (structure.name() == "");
3139 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003140 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3141
Jamie Madill98493dd2013-07-08 14:39:03 -04003142 TString string;
3143 string += declareString + "\n"
3144 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003145
Jamie Madillc835df62013-06-21 09:15:32 -04003146 int elementIndex = 0;
3147
Jamie Madill9cf6c072013-06-20 11:55:53 -04003148 for (unsigned int i = 0; i < fields.size(); i++)
3149 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003150 const TField &field = *fields[i];
3151 const TType &fieldType = *field.type();
3152 const TStructure *fieldStruct = fieldType.getStruct();
3153 const TString &fieldTypeString = fieldStruct ? structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) : typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003154
Jamie Madillc835df62013-06-21 09:15:32 -04003155 if (useStd140Packing)
3156 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003157 string += std140PrePaddingString(*field.type(), &elementIndex);
Jamie Madillc835df62013-06-21 09:15:32 -04003158 }
3159
Jamie Madill98493dd2013-07-08 14:39:03 -04003160 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003161
3162 if (useStd140Packing)
3163 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003164 string += std140PostPaddingString(*field.type(), useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003165 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003166 }
3167
3168 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003169 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003170
Jamie Madille4075c92013-06-21 09:15:32 -04003171 // Add remaining element index to the global map, for use with nested structs in standard layouts
3172 if (useStd140Packing)
3173 {
3174 mStd140StructElementIndexes[structName] = elementIndex;
3175 }
3176
Jamie Madill98493dd2013-07-08 14:39:03 -04003177 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003178}
3179
Jamie Madill98493dd2013-07-08 14:39:03 -04003180TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003181{
Jamie Madill98493dd2013-07-08 14:39:03 -04003182 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003183 {
3184 return "";
3185 }
3186
3187 TString prefix = "";
3188
3189 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3190 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003191
3192 if (useStd140Packing)
3193 {
3194 prefix += "std";
3195 }
3196
Jamie Madill9cf6c072013-06-20 11:55:53 -04003197 if (useHLSLRowMajorPacking)
3198 {
Jamie Madillc835df62013-06-21 09:15:32 -04003199 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003200 prefix += "rm";
3201 }
3202
Jamie Madill98493dd2013-07-08 14:39:03 -04003203 return prefix + structLookup(structure.name());
Jamie Madill9cf6c072013-06-20 11:55:53 -04003204}
3205
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003206void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003207{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003208 if (name == "")
3209 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003210 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003211 }
3212
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003213 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
3214 {
3215 return; // Already added
3216 }
3217
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003218 TType ctorType = type;
3219 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003220 ctorType.setPrecision(EbpHigh);
3221 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003222
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003223 TString ctorName = type.getStruct() ? decorate(name) : name;
3224
3225 typedef std::vector<TType> ParameterArray;
3226 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003227
Jamie Madill98493dd2013-07-08 14:39:03 -04003228 const TStructure* structure = type.getStruct();
3229 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003230 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003231 mStructNames.insert(decorate(name));
3232
Jamie Madill98493dd2013-07-08 14:39:03 -04003233 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003234
Jamie Madill98493dd2013-07-08 14:39:03 -04003235 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003236 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003237 // Add row-major packed struct for interface blocks
3238 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003239 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003240 "#pragma pack_matrix(column_major)\n";
3241
Jamie Madillc835df62013-06-21 09:15:32 -04003242 const TString &std140Prefix = "std";
Jamie Madill98493dd2013-07-08 14:39:03 -04003243 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003244
3245 const TString &std140RowMajorPrefix = "std_rm";
3246 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003247 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003248 "#pragma pack_matrix(column_major)\n";
3249
Jamie Madill98493dd2013-07-08 14:39:03 -04003250 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003251 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003252 mStructDeclarations.push_back(std140String);
3253 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003254 }
3255
Jamie Madill98493dd2013-07-08 14:39:03 -04003256 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003257 for (unsigned int i = 0; i < fields.size(); i++)
3258 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003259 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003260 }
3261 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003262 else if (parameters)
3263 {
3264 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3265 {
3266 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3267 }
3268 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003269 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003270
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003271 TString constructor;
3272
3273 if (ctorType.getStruct())
3274 {
3275 constructor += ctorName + " " + ctorName + "_ctor(";
3276 }
3277 else // Built-in type
3278 {
3279 constructor += typeString(ctorType) + " " + ctorName + "(";
3280 }
3281
3282 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3283 {
3284 const TType &type = ctorParameters[parameter];
3285
3286 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3287
3288 if (parameter < ctorParameters.size() - 1)
3289 {
3290 constructor += ", ";
3291 }
3292 }
3293
3294 constructor += ")\n"
3295 "{\n";
3296
3297 if (ctorType.getStruct())
3298 {
3299 constructor += " " + ctorName + " structure = {";
3300 }
3301 else
3302 {
3303 constructor += " return " + typeString(ctorType) + "(";
3304 }
3305
3306 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3307 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003308 int rows = ctorType.getRows();
3309 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003310 const TType &parameter = ctorParameters[0];
3311
3312 if (parameter.isScalar())
3313 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003314 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003315 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003316 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003317 {
3318 constructor += TString((row == col) ? "x0" : "0.0");
3319
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003320 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003321 {
3322 constructor += ", ";
3323 }
3324 }
3325 }
3326 }
3327 else if (parameter.isMatrix())
3328 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003329 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003330 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003331 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003332 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003333 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003334 {
3335 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3336 }
3337 else
3338 {
3339 constructor += TString((row == col) ? "1.0" : "0.0");
3340 }
3341
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003342 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003343 {
3344 constructor += ", ";
3345 }
3346 }
3347 }
3348 }
3349 else UNREACHABLE();
3350 }
3351 else
3352 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003353 size_t remainingComponents = ctorType.getObjectSize();
3354 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003355
3356 while (remainingComponents > 0)
3357 {
3358 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003359 const size_t parameterSize = parameter.getObjectSize();
3360 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003361
3362 constructor += "x" + str(parameterIndex);
3363
3364 if (parameter.isScalar())
3365 {
3366 remainingComponents -= parameter.getObjectSize();
3367 }
3368 else if (parameter.isVector())
3369 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003370 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003371 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003372 ASSERT(parameterSize <= remainingComponents);
3373 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003374 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003375 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003376 {
3377 switch (remainingComponents)
3378 {
3379 case 1: constructor += ".x"; break;
3380 case 2: constructor += ".xy"; break;
3381 case 3: constructor += ".xyz"; break;
3382 case 4: constructor += ".xyzw"; break;
3383 default: UNREACHABLE();
3384 }
3385
3386 remainingComponents = 0;
3387 }
3388 else UNREACHABLE();
3389 }
3390 else if (parameter.isMatrix() || parameter.getStruct())
3391 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003392 ASSERT(remainingComponents == parameterSize || moreParameters);
3393 ASSERT(parameterSize <= remainingComponents);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003394
Jamie Madill94bf7f22013-07-08 13:31:15 -04003395 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003396 }
3397 else UNREACHABLE();
3398
3399 if (moreParameters)
3400 {
3401 parameterIndex++;
3402 }
3403
3404 if (remainingComponents)
3405 {
3406 constructor += ", ";
3407 }
3408 }
3409 }
3410
3411 if (ctorType.getStruct())
3412 {
3413 constructor += "};\n"
3414 " return structure;\n"
3415 "}\n";
3416 }
3417 else
3418 {
3419 constructor += ");\n"
3420 "}\n";
3421 }
3422
daniel@transgaming.com63691862010-04-29 03:32:42 +00003423 mConstructors.insert(constructor);
3424}
3425
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003426const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3427{
3428 TInfoSinkBase &out = mBody;
3429
Jamie Madill98493dd2013-07-08 14:39:03 -04003430 const TStructure* structure = type.getStruct();
3431 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003432 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003433 out << structLookup(structure->name()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003434
Jamie Madill98493dd2013-07-08 14:39:03 -04003435 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003436
Jamie Madill98493dd2013-07-08 14:39:03 -04003437 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003438 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003439 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003440
3441 constUnion = writeConstantUnion(*fieldType, constUnion);
3442
Jamie Madill98493dd2013-07-08 14:39:03 -04003443 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003444 {
3445 out << ", ";
3446 }
3447 }
3448
3449 out << ")";
3450 }
3451 else
3452 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003453 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003454 bool writeType = size > 1;
3455
3456 if (writeType)
3457 {
3458 out << typeString(type) << "(";
3459 }
3460
Jamie Madill94bf7f22013-07-08 13:31:15 -04003461 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003462 {
3463 switch (constUnion->getType())
3464 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003465 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003466 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003467 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003468 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003469 default: UNREACHABLE();
3470 }
3471
3472 if (i != size - 1)
3473 {
3474 out << ", ";
3475 }
3476 }
3477
3478 if (writeType)
3479 {
3480 out << ")";
3481 }
3482 }
3483
3484 return constUnion;
3485}
3486
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003487TString OutputHLSL::scopeString(unsigned int depthLimit)
3488{
3489 TString string;
3490
3491 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3492 {
3493 string += "_" + str(i);
3494 }
3495
3496 return string;
3497}
3498
3499TString OutputHLSL::scopedStruct(const TString &typeName)
3500{
3501 if (typeName == "")
3502 {
3503 return typeName;
3504 }
3505
3506 return typeName + scopeString(mScopeDepth);
3507}
3508
3509TString OutputHLSL::structLookup(const TString &typeName)
3510{
3511 for (int depth = mScopeDepth; depth >= 0; depth--)
3512 {
3513 TString scopedName = decorate(typeName + scopeString(depth));
3514
3515 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3516 {
3517 if (*structName == scopedName)
3518 {
3519 return scopedName;
3520 }
3521 }
3522 }
3523
3524 UNREACHABLE(); // Should have found a matching constructor
3525
3526 return typeName;
3527}
3528
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003529TString OutputHLSL::decorate(const TString &string)
3530{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003531 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003532 {
3533 return "_" + string;
3534 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003535
3536 return string;
3537}
3538
apatrick@chromium.org65756022012-01-17 21:45:38 +00003539TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003540{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003541 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003542 {
3543 return "ex_" + string;
3544 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003545
3546 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003547}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003548
Jamie Madill98493dd2013-07-08 14:39:03 -04003549TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003550{
Jamie Madill98493dd2013-07-08 14:39:03 -04003551 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003552 {
3553 return decorate(string);
3554 }
3555
3556 return string;
3557}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003558
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003559void OutputHLSL::declareInterfaceBlockField(const TType &type, const TString &name, std::vector<InterfaceBlockField>& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003560{
Jamie Madill98493dd2013-07-08 14:39:03 -04003561 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003562
3563 if (!structure)
3564 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003565 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003566 InterfaceBlockField field(glVariableType(type), glVariablePrecision(type), name.c_str(),
3567 (unsigned int)type.getArraySize(), isRowMajorMatrix);
3568 output.push_back(field);
3569 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003570 else
3571 {
Jamie Madill28167c62013-08-30 13:21:10 -04003572 InterfaceBlockField structField(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), false);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003573
3574 const TFieldList &fields = structure->fields();
3575
3576 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3577 {
3578 TField *field = fields[fieldIndex];
3579 TType *fieldType = field->type();
3580
3581 // make sure to copy matrix packing information
3582 fieldType->setLayoutQualifier(type.getLayoutQualifier());
3583
3584 declareInterfaceBlockField(*fieldType, field->name(), structField.fields);
3585 }
3586
3587 output.push_back(structField);
3588 }
3589}
3590
Jamie Madillc2141fb2013-08-30 13:21:08 -04003591Uniform OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, std::vector<Uniform>& output)
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003592{
3593 const TStructure *structure = type.getStruct();
3594
3595 if (!structure)
3596 {
3597 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
3598 Uniform uniform(glVariableType(type), glVariablePrecision(type), name.c_str(),
3599 (unsigned int)type.getArraySize(), (unsigned int)registerIndex);
3600 output.push_back(uniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003601
3602 return uniform;
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003603 }
3604 else
3605 {
Jamie Madill28167c62013-08-30 13:21:10 -04003606 Uniform structUniform(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), (unsigned int)registerIndex);
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003607
Jamie Madill98493dd2013-07-08 14:39:03 -04003608 int fieldRegister = registerIndex;
3609 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003610
Jamie Madill98493dd2013-07-08 14:39:03 -04003611 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003612 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003613 TField *field = fields[fieldIndex];
3614 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003615
Jamie Madillc2141fb2013-08-30 13:21:08 -04003616 const Uniform &fieldVariable = declareUniformToList(*fieldType, field->name(), fieldRegister, structUniform.fields);
3617 fieldRegister += HLSLVariableRegisterCount(fieldVariable);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003618 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003619
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003620 output.push_back(structUniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003621
3622 return structUniform;
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003623 }
3624}
3625
Jamie Madill139b9092013-08-30 13:21:06 -04003626InterpolationType getInterpolationType(TQualifier qualifier)
3627{
3628 switch (qualifier)
3629 {
3630 case EvqFlatIn:
3631 case EvqFlatOut:
3632 return INTERPOLATION_FLAT;
3633
3634 case EvqSmoothIn:
3635 case EvqSmoothOut:
3636 case EvqVertexOut:
3637 case EvqFragmentIn:
3638 return INTERPOLATION_SMOOTH;
3639
3640 case EvqCentroidIn:
3641 case EvqCentroidOut:
3642 return INTERPOLATION_CENTROID;
3643
3644 default: UNREACHABLE();
3645 return INTERPOLATION_SMOOTH;
3646 }
3647}
3648
Jamie Madill94599662013-08-30 13:21:10 -04003649void OutputHLSL::declareVaryingToList(const TType &type, TQualifier baseTypeQualifier, const TString &name, std::vector<Varying>& fieldsOut)
Jamie Madill47fdd132013-08-30 13:21:04 -04003650{
3651 const TStructure *structure = type.getStruct();
3652
Jamie Madill94599662013-08-30 13:21:10 -04003653 InterpolationType interpolation = getInterpolationType(baseTypeQualifier);
Jamie Madill47fdd132013-08-30 13:21:04 -04003654 if (!structure)
3655 {
Jamie Madill139b9092013-08-30 13:21:06 -04003656 Varying varying(glVariableType(type), glVariablePrecision(type), name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003657 fieldsOut.push_back(varying);
3658 }
3659 else
3660 {
Jamie Madill28167c62013-08-30 13:21:10 -04003661 Varying structVarying(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003662 const TFieldList &fields = structure->fields();
3663
Jamie Madill28167c62013-08-30 13:21:10 -04003664 structVarying.structName = structure->name().c_str();
3665
Jamie Madill47fdd132013-08-30 13:21:04 -04003666 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3667 {
3668 const TField &field = *fields[fieldIndex];
Jamie Madill94599662013-08-30 13:21:10 -04003669 declareVaryingToList(*field.type(), baseTypeQualifier, field.name(), structVarying.fields);
Jamie Madill47fdd132013-08-30 13:21:04 -04003670 }
3671
3672 fieldsOut.push_back(structVarying);
3673 }
3674}
3675
Jamie Madillc2141fb2013-08-30 13:21:08 -04003676int OutputHLSL::declareUniformAndAssignRegister(const TType &type, const TString &name)
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003677{
Jamie Madillc2141fb2013-08-30 13:21:08 -04003678 int registerIndex = (IsSampler(type.getBasicType()) ? mSamplerRegister : mUniformRegister);
3679
3680 const Uniform &uniform = declareUniformToList(type, name, registerIndex, mActiveUniforms);
3681
3682 if (IsSampler(type.getBasicType()))
3683 {
3684 mSamplerRegister += HLSLVariableRegisterCount(uniform);
3685 }
3686 else
3687 {
3688 mUniformRegister += HLSLVariableRegisterCount(uniform);
3689 }
3690
3691 return registerIndex;
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003692}
3693
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003694GLenum OutputHLSL::glVariableType(const TType &type)
3695{
3696 if (type.getBasicType() == EbtFloat)
3697 {
3698 if (type.isScalar())
3699 {
3700 return GL_FLOAT;
3701 }
3702 else if (type.isVector())
3703 {
3704 switch(type.getNominalSize())
3705 {
3706 case 2: return GL_FLOAT_VEC2;
3707 case 3: return GL_FLOAT_VEC3;
3708 case 4: return GL_FLOAT_VEC4;
3709 default: UNREACHABLE();
3710 }
3711 }
3712 else if (type.isMatrix())
3713 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003714 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003715 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003716 case 2:
3717 switch(type.getRows())
3718 {
3719 case 2: return GL_FLOAT_MAT2;
3720 case 3: return GL_FLOAT_MAT2x3;
3721 case 4: return GL_FLOAT_MAT2x4;
3722 default: UNREACHABLE();
3723 }
3724
3725 case 3:
3726 switch(type.getRows())
3727 {
3728 case 2: return GL_FLOAT_MAT3x2;
3729 case 3: return GL_FLOAT_MAT3;
3730 case 4: return GL_FLOAT_MAT3x4;
3731 default: UNREACHABLE();
3732 }
3733
3734 case 4:
3735 switch(type.getRows())
3736 {
3737 case 2: return GL_FLOAT_MAT4x2;
3738 case 3: return GL_FLOAT_MAT4x3;
3739 case 4: return GL_FLOAT_MAT4;
3740 default: UNREACHABLE();
3741 }
3742
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003743 default: UNREACHABLE();
3744 }
3745 }
3746 else UNREACHABLE();
3747 }
3748 else if (type.getBasicType() == EbtInt)
3749 {
3750 if (type.isScalar())
3751 {
3752 return GL_INT;
3753 }
3754 else if (type.isVector())
3755 {
3756 switch(type.getNominalSize())
3757 {
3758 case 2: return GL_INT_VEC2;
3759 case 3: return GL_INT_VEC3;
3760 case 4: return GL_INT_VEC4;
3761 default: UNREACHABLE();
3762 }
3763 }
3764 else UNREACHABLE();
3765 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003766 else if (type.getBasicType() == EbtUInt)
3767 {
3768 if (type.isScalar())
3769 {
3770 return GL_UNSIGNED_INT;
3771 }
3772 else if (type.isVector())
3773 {
Jamie Madill22d63da2013-06-07 12:45:12 -04003774 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003775 {
3776 case 2: return GL_UNSIGNED_INT_VEC2;
3777 case 3: return GL_UNSIGNED_INT_VEC3;
3778 case 4: return GL_UNSIGNED_INT_VEC4;
3779 default: UNREACHABLE();
3780 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003781 }
3782 else UNREACHABLE();
3783 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003784 else if (type.getBasicType() == EbtBool)
3785 {
3786 if (type.isScalar())
3787 {
3788 return GL_BOOL;
3789 }
3790 else if (type.isVector())
3791 {
3792 switch(type.getNominalSize())
3793 {
3794 case 2: return GL_BOOL_VEC2;
3795 case 3: return GL_BOOL_VEC3;
3796 case 4: return GL_BOOL_VEC4;
3797 default: UNREACHABLE();
3798 }
3799 }
3800 else UNREACHABLE();
3801 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003802
3803 switch(type.getBasicType())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003804 {
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003805 case EbtSampler2D: return GL_SAMPLER_2D;
3806 case EbtSampler3D: return GL_SAMPLER_3D;
3807 case EbtSamplerCube: return GL_SAMPLER_CUBE;
3808 case EbtSampler2DArray: return GL_SAMPLER_2D_ARRAY;
3809 case EbtISampler2D: return GL_INT_SAMPLER_2D;
3810 case EbtISampler3D: return GL_INT_SAMPLER_3D;
3811 case EbtISamplerCube: return GL_INT_SAMPLER_CUBE;
3812 case EbtISampler2DArray: return GL_INT_SAMPLER_2D_ARRAY;
3813 case EbtUSampler2D: return GL_UNSIGNED_INT_SAMPLER_2D;
3814 case EbtUSampler3D: return GL_UNSIGNED_INT_SAMPLER_3D;
3815 case EbtUSamplerCube: return GL_UNSIGNED_INT_SAMPLER_CUBE;
3816 case EbtUSampler2DArray: return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3817 case EbtSampler2DShadow: return GL_SAMPLER_2D_SHADOW;
3818 case EbtSamplerCubeShadow: return GL_SAMPLER_CUBE_SHADOW;
3819 case EbtSampler2DArrayShadow: return GL_SAMPLER_2D_ARRAY_SHADOW;
3820 default: UNREACHABLE();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003821 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003822
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003823
3824 return GL_NONE;
3825}
3826
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003827GLenum OutputHLSL::glVariablePrecision(const TType &type)
3828{
3829 if (type.getBasicType() == EbtFloat)
3830 {
3831 switch (type.getPrecision())
3832 {
3833 case EbpHigh: return GL_HIGH_FLOAT;
3834 case EbpMedium: return GL_MEDIUM_FLOAT;
3835 case EbpLow: return GL_LOW_FLOAT;
3836 case EbpUndefined:
3837 // Should be defined as the default precision by the parser
3838 default: UNREACHABLE();
3839 }
3840 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003841 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003842 {
3843 switch (type.getPrecision())
3844 {
3845 case EbpHigh: return GL_HIGH_INT;
3846 case EbpMedium: return GL_MEDIUM_INT;
3847 case EbpLow: return GL_LOW_INT;
3848 case EbpUndefined:
3849 // Should be defined as the default precision by the parser
3850 default: UNREACHABLE();
3851 }
3852 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003853
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00003854 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003855 return GL_NONE;
3856}
3857
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003858bool OutputHLSL::isVaryingOut(TQualifier qualifier)
3859{
3860 switch(qualifier)
3861 {
3862 case EvqVaryingOut:
3863 case EvqInvariantVaryingOut:
3864 case EvqSmoothOut:
3865 case EvqFlatOut:
3866 case EvqCentroidOut:
Jamie Madill19571812013-08-12 15:26:34 -07003867 case EvqVertexOut:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003868 return true;
3869 }
3870
3871 return false;
3872}
3873
3874bool OutputHLSL::isVaryingIn(TQualifier qualifier)
3875{
3876 switch(qualifier)
3877 {
3878 case EvqVaryingIn:
3879 case EvqInvariantVaryingIn:
3880 case EvqSmoothIn:
3881 case EvqFlatIn:
3882 case EvqCentroidIn:
Jamie Madill19571812013-08-12 15:26:34 -07003883 case EvqFragmentIn:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003884 return true;
3885 }
3886
3887 return false;
3888}
3889
3890bool OutputHLSL::isVarying(TQualifier qualifier)
3891{
3892 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
3893}
3894
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003895}