blob: 404ed392169908b287347984bb59cf00593660bf [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
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/OutputHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
alokp@chromium.org79fb1012012-04-26 21:07:39 +00009#include "common/angleutils.h"
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +000010#include "common/utilities.h"
Geoff Lang17732822013-08-29 13:46:49 -040011#include "compiler/translator/compilerdebug.h"
12#include "compiler/translator/InfoSink.h"
13#include "compiler/translator/DetectDiscontinuity.h"
14#include "compiler/translator/SearchSymbol.h"
15#include "compiler/translator/UnfoldShortCircuit.h"
16#include "compiler/translator/HLSLLayoutEncoder.h"
17#include "compiler/translator/FlagStd140Structs.h"
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
Geoff Lang036aa492013-10-09 16:23:30 -040026template <typename T>
27TString str(T i)
daniel@transgaming.com005c7392010-04-15 20:45:27 +000028{
Geoff Lang036aa492013-10-09 16:23:30 -040029 ASSERT(std::numeric_limits<T>::is_integer);
30 char buffer[(CHAR_BIT * sizeof(T) / 3) + 3];
31 const char *formatStr = std::numeric_limits<T>::is_signed ? "%d" : "%u";
32 snprintf(buffer, sizeof(buffer), formatStr, i);
daniel@transgaming.com005c7392010-04-15 20:45:27 +000033 return buffer;
34}
35
Nicolas Capense0ba27a2013-06-24 16:10:52 -040036TString OutputHLSL::TextureFunction::name() const
37{
38 TString name = "gl_texture";
39
Nicolas Capens6d232bb2013-07-08 15:56:38 -040040 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040041 {
42 name += "2D";
43 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040044 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040045 {
46 name += "3D";
47 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040048 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040049 {
50 name += "Cube";
51 }
52 else UNREACHABLE();
53
54 if (proj)
55 {
56 name += "Proj";
57 }
58
Nicolas Capens75fb4752013-07-10 15:14:47 -040059 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040060 {
61 case IMPLICIT: break;
62 case BIAS: break;
63 case LOD: name += "Lod"; break;
64 case LOD0: name += "Lod0"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -040065 case SIZE: name += "Size"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040066 default: UNREACHABLE();
67 }
68
69 return name + "(";
70}
71
Jamie Madillc2141fb2013-08-30 13:21:08 -040072const char *RegisterPrefix(const TType &type)
73{
74 if (IsSampler(type.getBasicType()))
75 {
76 return "s";
77 }
78 else
79 {
80 return "c";
81 }
82}
83
Nicolas Capense0ba27a2013-06-24 16:10:52 -040084bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
85{
86 if (sampler < rhs.sampler) return true;
87 if (coords < rhs.coords) return true;
88 if (!proj && rhs.proj) return true;
Nicolas Capens75fb4752013-07-10 15:14:47 -040089 if (method < rhs.method) return true;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040090
91 return false;
92}
93
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +000094OutputHLSL::OutputHLSL(TParseContext &context, const ShBuiltInResources& resources, ShShaderOutput outputType)
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +000095 : TIntermTraverser(true, true, true), mContext(context), mOutputType(outputType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000096{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000097 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +000098 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000099
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000100 mUsesFragColor = false;
101 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000102 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000103 mUsesFragCoord = false;
104 mUsesPointCoord = false;
105 mUsesFrontFacing = false;
106 mUsesPointSize = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400107 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000108 mUsesXor = false;
109 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000110 mUsesMod2v = false;
111 mUsesMod2f = false;
112 mUsesMod3v = false;
113 mUsesMod3f = false;
114 mUsesMod4v = false;
115 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000116 mUsesFaceforward1 = false;
117 mUsesFaceforward2 = false;
118 mUsesFaceforward3 = false;
119 mUsesFaceforward4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000120 mUsesAtan2_1 = false;
121 mUsesAtan2_2 = false;
122 mUsesAtan2_3 = false;
123 mUsesAtan2_4 = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000124
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000125 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
126
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000127 mScopeDepth = 0;
128
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000129 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000130
131 mContainsLoopDiscontinuity = false;
132 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000133 mInsideDiscontinuousLoop = false;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000134
135 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000136
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000137 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000138 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000139 if (mContext.shaderType == SH_FRAGMENT_SHADER)
140 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000141 mUniformRegister = 3; // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000142 }
143 else
144 {
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000145 mUniformRegister = 2; // Reserve registers for dx_DepthRange and dx_ViewAdjust
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000146 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000147 }
148 else
149 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000150 mUniformRegister = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000151 }
152
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000153 mSamplerRegister = 0;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000154 mInterfaceBlockRegister = 2; // Reserve registers for the default uniform block and driver constants
Jamie Madill574d9dd2013-06-20 11:55:56 -0400155 mPaddingCounter = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000156}
157
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000158OutputHLSL::~OutputHLSL()
159{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000160 delete mUnfoldShortCircuit;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000161}
162
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000163void OutputHLSL::output()
164{
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +0000165 mContainsLoopDiscontinuity = mContext.shaderType == SH_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400166 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
167 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000168
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000169 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 +0000170 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000171
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000172 mContext.infoSink().obj << mHeader.c_str();
173 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000174}
175
Jamie Madill570e04d2013-06-21 09:15:33 -0400176void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
177{
178 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
179 {
180 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
181
182 // This will mark the necessary block elements as referenced
183 flaggedNode->traverse(this);
184 TString structName(mBody.c_str());
185 mBody.erase();
186
187 mFlaggedStructOriginalNames[flaggedNode] = structName;
188
189 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
190 {
191 structName.erase(pos, 1);
192 }
193
194 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
195 }
196}
197
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000198TInfoSinkBase &OutputHLSL::getBodyStream()
199{
200 return mBody;
201}
202
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400203const std::vector<Uniform> &OutputHLSL::getUniforms()
daniel@transgaming.com043da132012-12-20 21:12:22 +0000204{
205 return mActiveUniforms;
206}
207
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000208const ActiveInterfaceBlocks &OutputHLSL::getInterfaceBlocks() const
209{
210 return mActiveInterfaceBlocks;
211}
212
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400213const std::vector<Attribute> &OutputHLSL::getOutputVariables() const
Jamie Madill46131a32013-06-20 11:55:50 -0400214{
215 return mActiveOutputVariables;
216}
217
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400218const std::vector<Attribute> &OutputHLSL::getAttributes() const
Jamie Madilldefb6742013-06-20 11:55:51 -0400219{
220 return mActiveAttributes;
221}
222
Jamie Madill47fdd132013-08-30 13:21:04 -0400223const std::vector<Varying> &OutputHLSL::getVaryings() const
224{
225 return mActiveVaryings;
226}
227
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000228int OutputHLSL::vectorSize(const TType &type) const
229{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000230 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000231 int arraySize = type.isArray() ? type.getArraySize() : 1;
232
233 return elementSize * arraySize;
234}
235
Jamie Madill98493dd2013-07-08 14:39:03 -0400236TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, const TField &field)
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000237{
Jamie Madill98493dd2013-07-08 14:39:03 -0400238 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000239 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400240 return interfaceBlock.name() + "." + field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000241 }
242 else
243 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400244 return field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000245 }
246}
247
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000248TString OutputHLSL::decoratePrivate(const TString &privateText)
249{
250 return "dx_" + privateText;
251}
252
Jamie Madill98493dd2013-07-08 14:39:03 -0400253TString OutputHLSL::interfaceBlockStructNameString(const TInterfaceBlock &interfaceBlock)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000254{
Jamie Madill98493dd2013-07-08 14:39:03 -0400255 return decoratePrivate(interfaceBlock.name()) + "_type";
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000256}
257
Jamie Madill98493dd2013-07-08 14:39:03 -0400258TString OutputHLSL::interfaceBlockInstanceString(const TInterfaceBlock& interfaceBlock, unsigned int arrayIndex)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000259{
Jamie Madill98493dd2013-07-08 14:39:03 -0400260 if (!interfaceBlock.hasInstanceName())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000261 {
262 return "";
263 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400264 else if (interfaceBlock.isArray())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000265 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400266 return decoratePrivate(interfaceBlock.instanceName()) + "_" + str(arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000267 }
268 else
269 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400270 return decorate(interfaceBlock.instanceName());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000271 }
272}
273
Jamie Madill98493dd2013-07-08 14:39:03 -0400274TString OutputHLSL::interfaceBlockFieldTypeString(const TField &field, TLayoutBlockStorage blockStorage)
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000275{
Jamie Madill98493dd2013-07-08 14:39:03 -0400276 const TType &fieldType = *field.type();
277 const TLayoutMatrixPacking matrixPacking = fieldType.getLayoutQualifier().matrixPacking;
Jamie Madill529077d2013-06-20 11:55:54 -0400278 ASSERT(matrixPacking != EmpUnspecified);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000279
Jamie Madill98493dd2013-07-08 14:39:03 -0400280 if (fieldType.isMatrix())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000281 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400282 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill529077d2013-06-20 11:55:54 -0400283 const TString &matrixPackString = (matrixPacking == EmpRowMajor ? "column_major" : "row_major");
Jamie Madill98493dd2013-07-08 14:39:03 -0400284 return matrixPackString + " " + typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000285 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400286 else if (fieldType.getStruct())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000287 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400288 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill98493dd2013-07-08 14:39:03 -0400289 return structureTypeName(*fieldType.getStruct(), matrixPacking == EmpColumnMajor, blockStorage == EbsStd140);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000290 }
291 else
292 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400293 return typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000294 }
295}
296
Jamie Madill98493dd2013-07-08 14:39:03 -0400297TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, TLayoutBlockStorage blockStorage)
298{
299 TString hlsl;
300
301 int elementIndex = 0;
302
303 for (unsigned int typeIndex = 0; typeIndex < interfaceBlock.fields().size(); typeIndex++)
304 {
305 const TField &field = *interfaceBlock.fields()[typeIndex];
306 const TType &fieldType = *field.type();
307
308 if (blockStorage == EbsStd140)
309 {
310 // 2 and 3 component vector types in some cases need pre-padding
311 hlsl += std140PrePaddingString(fieldType, &elementIndex);
312 }
313
314 hlsl += " " + interfaceBlockFieldTypeString(field, blockStorage) +
315 " " + decorate(field.name()) + arrayString(fieldType) + ";\n";
316
317 // must pad out after matrices and arrays, where HLSL usually allows itself room to pack stuff
318 if (blockStorage == EbsStd140)
319 {
320 const bool useHLSLRowMajorPacking = (fieldType.getLayoutQualifier().matrixPacking == EmpColumnMajor);
321 hlsl += std140PostPaddingString(fieldType, useHLSLRowMajorPacking);
322 }
323 }
324
325 return hlsl;
326}
327
328TString OutputHLSL::interfaceBlockStructString(const TInterfaceBlock &interfaceBlock)
329{
330 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
331
332 return "struct " + interfaceBlockStructNameString(interfaceBlock) + "\n"
333 "{\n" +
334 interfaceBlockFieldString(interfaceBlock, blockStorage) +
335 "};\n\n";
336}
337
338TString OutputHLSL::interfaceBlockString(const TInterfaceBlock &interfaceBlock, unsigned int registerIndex, unsigned int arrayIndex)
339{
340 const TString &arrayIndexString = (arrayIndex != GL_INVALID_INDEX ? decorate(str(arrayIndex)) : "");
341 const TString &blockName = interfaceBlock.name() + arrayIndexString;
342 TString hlsl;
343
344 hlsl += "cbuffer " + blockName + " : register(b" + str(registerIndex) + ")\n"
345 "{\n";
346
347 if (interfaceBlock.hasInstanceName())
348 {
349 hlsl += " " + interfaceBlockStructNameString(interfaceBlock) + " " + interfaceBlockInstanceString(interfaceBlock, arrayIndex) + ";\n";
350 }
351 else
352 {
353 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
354 hlsl += interfaceBlockFieldString(interfaceBlock, blockStorage);
355 }
356
357 hlsl += "};\n\n";
358
359 return hlsl;
360}
361
Jamie Madill574d9dd2013-06-20 11:55:56 -0400362TString OutputHLSL::std140PrePaddingString(const TType &type, int *elementIndex)
363{
364 if (type.getBasicType() == EbtStruct || type.isMatrix() || type.isArray())
365 {
366 // no padding needed, HLSL will align the field to a new register
367 *elementIndex = 0;
368 return "";
369 }
370
371 const GLenum glType = glVariableType(type);
372 const int numComponents = gl::UniformComponentCount(glType);
373
374 if (numComponents >= 4)
375 {
376 // no padding needed, HLSL will align the field to a new register
377 *elementIndex = 0;
378 return "";
379 }
380
381 if (*elementIndex + numComponents > 4)
382 {
383 // no padding needed, HLSL will align the field to a new register
384 *elementIndex = numComponents;
385 return "";
386 }
387
388 TString padding;
389
390 const int alignment = numComponents == 3 ? 4 : numComponents;
391 const int paddingOffset = (*elementIndex % alignment);
392
393 if (paddingOffset != 0)
394 {
395 // padding is neccessary
396 for (int paddingIndex = paddingOffset; paddingIndex < alignment; paddingIndex++)
397 {
398 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
399 }
400
401 *elementIndex += (alignment - paddingOffset);
402 }
403
404 *elementIndex += numComponents;
405 *elementIndex %= 4;
406
407 return padding;
408}
409
Jamie Madille4075c92013-06-21 09:15:32 -0400410TString OutputHLSL::std140PostPaddingString(const TType &type, bool useHLSLRowMajorPacking)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400411{
Jamie Madillc835df62013-06-21 09:15:32 -0400412 if (!type.isMatrix() && !type.isArray() && type.getBasicType() != EbtStruct)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400413 {
414 return "";
415 }
416
Jamie Madill574d9dd2013-06-20 11:55:56 -0400417 int numComponents = 0;
418
419 if (type.isMatrix())
420 {
Jamie Madille4075c92013-06-21 09:15:32 -0400421 // This method can also be called from structureString, which does not use layout qualifiers.
422 // Thus, use the method parameter for determining the matrix packing.
423 //
424 // Note HLSL row major packing corresponds to GL API column-major, and vice-versa, since we
425 // wish to always transpose GL matrices to play well with HLSL's matrix array indexing.
426 //
427 const bool isRowMajorMatrix = !useHLSLRowMajorPacking;
Jamie Madillc835df62013-06-21 09:15:32 -0400428 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400429 numComponents = gl::MatrixComponentCount(glType, isRowMajorMatrix);
430 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400431 else if (type.getStruct())
Jamie Madillc835df62013-06-21 09:15:32 -0400432 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400433 const TString &structName = structureTypeName(*type.getStruct(), useHLSLRowMajorPacking, true);
Jamie Madille4075c92013-06-21 09:15:32 -0400434 numComponents = mStd140StructElementIndexes[structName];
435
436 if (numComponents == 0)
437 {
438 return "";
439 }
Jamie Madillc835df62013-06-21 09:15:32 -0400440 }
Jamie Madill574d9dd2013-06-20 11:55:56 -0400441 else
442 {
Jamie Madillc835df62013-06-21 09:15:32 -0400443 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400444 numComponents = gl::UniformComponentCount(glType);
445 }
446
447 TString padding;
448 for (int paddingOffset = numComponents; paddingOffset < 4; paddingOffset++)
449 {
450 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
451 }
452 return padding;
453}
454
Jamie Madill440dc742013-06-20 11:55:55 -0400455// Use the same layout for packed and shared
456void setBlockLayout(InterfaceBlock *interfaceBlock, BlockLayoutType newLayout)
457{
458 interfaceBlock->layout = newLayout;
459 interfaceBlock->blockInfo.clear();
460
461 switch (newLayout)
462 {
463 case BLOCKLAYOUT_SHARED:
464 case BLOCKLAYOUT_PACKED:
465 {
466 HLSLBlockEncoder hlslEncoder(&interfaceBlock->blockInfo);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400467 hlslEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400468 interfaceBlock->dataSize = hlslEncoder.getBlockSize();
469 }
470 break;
471
472 case BLOCKLAYOUT_STANDARD:
473 {
474 Std140BlockEncoder stdEncoder(&interfaceBlock->blockInfo);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400475 stdEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400476 interfaceBlock->dataSize = stdEncoder.getBlockSize();
477 }
478 break;
479
480 default:
481 UNREACHABLE();
482 break;
483 }
484}
485
Jamie Madill574d9dd2013-06-20 11:55:56 -0400486BlockLayoutType convertBlockLayoutType(TLayoutBlockStorage blockStorage)
487{
488 switch (blockStorage)
489 {
490 case EbsPacked: return BLOCKLAYOUT_PACKED;
491 case EbsShared: return BLOCKLAYOUT_SHARED;
492 case EbsStd140: return BLOCKLAYOUT_STANDARD;
493 default: UNREACHABLE(); return BLOCKLAYOUT_SHARED;
494 }
495}
496
Jamie Madill98493dd2013-07-08 14:39:03 -0400497TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400498{
499 TString init;
500
501 TString preIndentString;
502 TString fullIndentString;
503
504 for (int spaces = 0; spaces < (indent * 4); spaces++)
505 {
506 preIndentString += ' ';
507 }
508
509 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
510 {
511 fullIndentString += ' ';
512 }
513
514 init += preIndentString + "{\n";
515
Jamie Madill98493dd2013-07-08 14:39:03 -0400516 const TFieldList &fields = structure.fields();
517 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400518 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400519 const TField &field = *fields[fieldIndex];
520 const TString &fieldName = rhsStructName + "." + decorate(field.name());
521 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400522
Jamie Madill98493dd2013-07-08 14:39:03 -0400523 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400524 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400525 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400526 }
527 else
528 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400529 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400530 }
531 }
532
533 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
534
535 return init;
536}
537
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000538void OutputHLSL::header()
539{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000540 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000541
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000542 TString uniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000543 TString interfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000544 TString varyings;
545 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400546 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000547
Jamie Madillc2141fb2013-08-30 13:21:08 -0400548 for (ReferencedSymbols::const_iterator uniformIt = mReferencedUniforms.begin(); uniformIt != mReferencedUniforms.end(); uniformIt++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000549 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400550 const TIntermSymbol &uniform = *uniformIt->second;
551 const TType &type = uniform.getType();
552 const TString &name = uniform.getSymbol();
553
554 int registerIndex = declareUniformAndAssignRegister(type, name);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000555
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000556 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture
557 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400558 uniforms += "uniform " + samplerString(type) + " sampler_" + decorateUniform(name, type) + arrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400559 " : register(s" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000560
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000561 uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400562 " : register(t" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000563 }
564 else
565 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400566 const TStructure *structure = type.getStruct();
567 const TString &typeName = (structure ? structureTypeName(*structure, false, false) : typeString(type));
568
569 const TString &registerString = TString("register(") + RegisterPrefix(type) + str(registerIndex) + ")";
570
571 uniforms += "uniform " + typeName + " " + decorateUniform(name, type) + arrayString(type) + " : " + registerString + ";\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000572 }
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000573 }
574
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000575 for (ReferencedSymbols::const_iterator interfaceBlockIt = mReferencedInterfaceBlocks.begin(); interfaceBlockIt != mReferencedInterfaceBlocks.end(); interfaceBlockIt++)
576 {
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000577 const TType &nodeType = interfaceBlockIt->second->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -0400578 const TInterfaceBlock &interfaceBlock = *nodeType.getInterfaceBlock();
579 const TFieldList &fieldList = interfaceBlock.fields();
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000580
Jamie Madill98493dd2013-07-08 14:39:03 -0400581 unsigned int arraySize = static_cast<unsigned int>(interfaceBlock.arraySize());
582 sh::InterfaceBlock activeBlock(interfaceBlock.name().c_str(), arraySize, mInterfaceBlockRegister);
583 for (unsigned int typeIndex = 0; typeIndex < fieldList.size(); typeIndex++)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000584 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400585 const TField &field = *fieldList[typeIndex];
586 const TString &fullUniformName = interfaceBlockFieldString(interfaceBlock, field);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400587 declareInterfaceBlockField(*field.type(), fullUniformName, activeBlock.fields);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000588 }
589
Jamie Madill98493dd2013-07-08 14:39:03 -0400590 mInterfaceBlockRegister += std::max(1u, arraySize);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000591
Jamie Madill98493dd2013-07-08 14:39:03 -0400592 BlockLayoutType blockLayoutType = convertBlockLayoutType(interfaceBlock.blockStorage());
593 setBlockLayout(&activeBlock, blockLayoutType);
Jamie Madill9060a4e2013-08-12 16:22:57 -0700594
595 if (interfaceBlock.matrixPacking() == EmpRowMajor)
596 {
597 activeBlock.isRowMajorLayout = true;
598 }
599
Jamie Madill98493dd2013-07-08 14:39:03 -0400600 mActiveInterfaceBlocks.push_back(activeBlock);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000601
Jamie Madill98493dd2013-07-08 14:39:03 -0400602 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000603 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400604 interfaceBlocks += interfaceBlockStructString(interfaceBlock);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000605 }
606
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000607 if (arraySize > 0)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000608 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000609 for (unsigned int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
610 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400611 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex + arrayIndex, arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000612 }
613 }
614 else
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000615 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400616 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000617 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000618 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000619
Jamie Madill570e04d2013-06-21 09:15:33 -0400620 for (auto flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
621 {
622 TIntermTyped *structNode = flaggedStructIt->first;
623 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400624 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400625 const TString &originalName = mFlaggedStructOriginalNames[structNode];
626
Jamie Madill98493dd2013-07-08 14:39:03 -0400627 flaggedStructs += "static " + decorate(structure.name()) + " " + mappedName + " =\n";
628 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400629 flaggedStructs += "\n";
630 }
631
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000632 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
633 {
634 const TType &type = varying->second->getType();
635 const TString &name = varying->second->getSymbol();
636
637 // Program linking depends on this exact format
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +0000638 varyings += "static " + interpolationString(type.getQualifier()) + " " + typeString(type) + " " +
639 decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madill47fdd132013-08-30 13:21:04 -0400640
Jamie Madill94599662013-08-30 13:21:10 -0400641 declareVaryingToList(type, type.getQualifier(), name, mActiveVaryings);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000642 }
643
644 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
645 {
646 const TType &type = attribute->second->getType();
647 const TString &name = attribute->second->getSymbol();
648
649 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400650
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400651 Attribute attributeVar(glVariableType(type), glVariablePrecision(type), name.c_str(),
652 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
653 mActiveAttributes.push_back(attributeVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000654 }
655
Jamie Madill529077d2013-06-20 11:55:54 -0400656 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
657 {
658 out << *structDeclaration;
659 }
660
661 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
662 {
663 out << *constructor;
664 }
665
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400666 if (mContext.shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000667 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000668 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000669 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000670
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000671 out << "// Varyings\n";
672 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400673 out << "\n";
674
675 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000676 {
Jamie Madill46131a32013-06-20 11:55:50 -0400677 for (auto outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000678 {
Jamie Madill46131a32013-06-20 11:55:50 -0400679 const TString &variableName = outputVariableIt->first;
680 const TType &variableType = outputVariableIt->second->getType();
681 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
682
683 out << "static " + typeString(variableType) + " out_" + variableName + arrayString(variableType) +
684 " = " + initializer(variableType) + ";\n";
685
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400686 Attribute outputVar(glVariableType(variableType), glVariablePrecision(variableType), variableName.c_str(),
687 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
Jamie Madill46131a32013-06-20 11:55:50 -0400688 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000689 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000690 }
Jamie Madill46131a32013-06-20 11:55:50 -0400691 else
692 {
693 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
694
695 out << "static float4 gl_Color[" << numColorValues << "] =\n"
696 "{\n";
697 for (unsigned int i = 0; i < numColorValues; i++)
698 {
699 out << " float4(0, 0, 0, 0)";
700 if (i + 1 != numColorValues)
701 {
702 out << ",";
703 }
704 out << "\n";
705 }
706
707 out << "};\n";
708 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000709
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400710 if (mUsesFragDepth)
711 {
712 out << "static float gl_Depth = 0.0;\n";
713 }
714
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000715 if (mUsesFragCoord)
716 {
717 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
718 }
719
720 if (mUsesPointCoord)
721 {
722 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
723 }
724
725 if (mUsesFrontFacing)
726 {
727 out << "static bool gl_FrontFacing = false;\n";
728 }
729
730 out << "\n";
731
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000732 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000733 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000734 out << "struct gl_DepthRangeParameters\n"
735 "{\n"
736 " float near;\n"
737 " float far;\n"
738 " float diff;\n"
739 "};\n"
740 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000741 }
742
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000743 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000744 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000745 out << "cbuffer DriverConstants : register(b1)\n"
746 "{\n";
747
748 if (mUsesDepthRange)
749 {
750 out << " float3 dx_DepthRange : packoffset(c0);\n";
751 }
752
753 if (mUsesFragCoord)
754 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000755 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000756 }
757
758 if (mUsesFragCoord || mUsesFrontFacing)
759 {
760 out << " float3 dx_DepthFront : packoffset(c2);\n";
761 }
762
763 out << "};\n";
764 }
765 else
766 {
767 if (mUsesDepthRange)
768 {
769 out << "uniform float3 dx_DepthRange : register(c0);";
770 }
771
772 if (mUsesFragCoord)
773 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000774 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000775 }
776
777 if (mUsesFragCoord || mUsesFrontFacing)
778 {
779 out << "uniform float3 dx_DepthFront : register(c2);\n";
780 }
781 }
782
783 out << "\n";
784
785 if (mUsesDepthRange)
786 {
787 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
788 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000789 }
790
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000791 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000792 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000793
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000794 if (!interfaceBlocks.empty())
795 {
796 out << interfaceBlocks;
797 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400798
799 if (!flaggedStructs.empty())
800 {
801 out << "// Std140 Structures accessed by value\n";
802 out << "\n";
803 out << flaggedStructs;
804 out << "\n";
805 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000806 }
807
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000808 if (usingMRTExtension && mNumRenderTargets > 1)
809 {
810 out << "#define GL_USES_MRT\n";
811 }
812
813 if (mUsesFragColor)
814 {
815 out << "#define GL_USES_FRAG_COLOR\n";
816 }
817
818 if (mUsesFragData)
819 {
820 out << "#define GL_USES_FRAG_DATA\n";
821 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000822 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000823 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000824 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000825 out << "// Attributes\n";
826 out << attributes;
827 out << "\n"
828 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
829
830 if (mUsesPointSize)
831 {
832 out << "static float gl_PointSize = float(1);\n";
833 }
834
835 out << "\n"
836 "// Varyings\n";
837 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000838 out << "\n";
839
840 if (mUsesDepthRange)
841 {
842 out << "struct gl_DepthRangeParameters\n"
843 "{\n"
844 " float near;\n"
845 " float far;\n"
846 " float diff;\n"
847 "};\n"
848 "\n";
849 }
850
851 if (mOutputType == SH_HLSL11_OUTPUT)
852 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000853 if (mUsesDepthRange)
854 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000855 out << "cbuffer DriverConstants : register(b1)\n"
856 "{\n"
857 " float3 dx_DepthRange : packoffset(c0);\n"
858 "};\n"
859 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000860 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000861 }
862 else
863 {
864 if (mUsesDepthRange)
865 {
866 out << "uniform float3 dx_DepthRange : register(c0);\n";
867 }
868
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000869 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000870 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000871 }
872
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000873 if (mUsesDepthRange)
874 {
875 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
876 "\n";
877 }
878
879 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000880 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000881
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000882 if (!interfaceBlocks.empty())
883 {
884 out << interfaceBlocks;
885 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400886
887 if (!flaggedStructs.empty())
888 {
889 out << "// Std140 Structures accessed by value\n";
890 out << "\n";
891 out << flaggedStructs;
892 out << "\n";
893 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000894 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400895 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000896
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400897 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
898 {
899 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400900 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000901 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400902 switch(textureFunction->sampler)
903 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400904 case EbtSampler2D: out << "int2 "; break;
905 case EbtSampler3D: out << "int3 "; break;
906 case EbtSamplerCube: out << "int2 "; break;
907 case EbtSampler2DArray: out << "int3 "; break;
908 case EbtISampler2D: out << "int2 "; break;
909 case EbtISampler3D: out << "int3 "; break;
910 case EbtISamplerCube: out << "int2 "; break;
911 case EbtISampler2DArray: out << "int3 "; break;
912 case EbtUSampler2D: out << "int2 "; break;
913 case EbtUSampler3D: out << "int3 "; break;
914 case EbtUSamplerCube: out << "int2 "; break;
915 case EbtUSampler2DArray: out << "int3 "; break;
916 case EbtSampler2DShadow: out << "int2 "; break;
917 case EbtSamplerCubeShadow: out << "int2 "; break;
918 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400919 default: UNREACHABLE();
920 }
921 }
922 else // Sampling function
923 {
924 switch(textureFunction->sampler)
925 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400926 case EbtSampler2D: out << "float4 "; break;
927 case EbtSampler3D: out << "float4 "; break;
928 case EbtSamplerCube: out << "float4 "; break;
929 case EbtSampler2DArray: out << "float4 "; break;
930 case EbtISampler2D: out << "int4 "; break;
931 case EbtISampler3D: out << "int4 "; break;
932 case EbtISamplerCube: out << "int4 "; break;
933 case EbtISampler2DArray: out << "int4 "; break;
934 case EbtUSampler2D: out << "uint4 "; break;
935 case EbtUSampler3D: out << "uint4 "; break;
936 case EbtUSamplerCube: out << "uint4 "; break;
937 case EbtUSampler2DArray: out << "uint4 "; break;
938 case EbtSampler2DShadow: out << "float "; break;
939 case EbtSamplerCubeShadow: out << "float "; break;
940 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400941 default: UNREACHABLE();
942 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000943 }
944
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400945 // Function name
946 out << textureFunction->name();
947
948 // Argument list
949 int hlslCoords = 4;
950
951 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000952 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400953 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000954 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400955 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
956 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
957 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000958 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400959
Nicolas Capens75fb4752013-07-10 15:14:47 -0400960 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000961 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400962 case TextureFunction::IMPLICIT: break;
963 case TextureFunction::BIAS: hlslCoords = 4; break;
964 case TextureFunction::LOD: hlslCoords = 4; break;
965 case TextureFunction::LOD0: hlslCoords = 4; break;
966 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000967 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400968 }
969 else if (mOutputType == SH_HLSL11_OUTPUT)
970 {
971 switch(textureFunction->sampler)
972 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400973 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
974 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
975 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
976 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
977 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
978 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
979 case EbtISamplerCube: out << "TextureCube<int4> x, SamplerState s"; hlslCoords = 3; break;
980 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
981 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
982 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
983 case EbtUSamplerCube: out << "TextureCube<uint4> x, SamplerState s"; hlslCoords = 3; break;
984 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
985 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
986 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
987 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400988 default: UNREACHABLE();
989 }
990 }
991 else UNREACHABLE();
992
993 switch(textureFunction->coords)
994 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400995 case 1: out << ", int lod"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400996 case 2: out << ", float2 t"; break;
997 case 3: out << ", float3 t"; break;
998 case 4: out << ", float4 t"; break;
999 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +00001000 }
1001
Nicolas Capens75fb4752013-07-10 15:14:47 -04001002 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +00001003 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001004 case TextureFunction::IMPLICIT: break;
1005 case TextureFunction::BIAS: out << ", float bias"; break;
1006 case TextureFunction::LOD: out << ", float lod"; break;
1007 case TextureFunction::LOD0: break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001008 case TextureFunction::SIZE: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001009 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +00001010 }
1011
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001012 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001013 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001014
Nicolas Capens75fb4752013-07-10 15:14:47 -04001015 if (textureFunction->method == TextureFunction::SIZE)
1016 {
1017 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
1018 {
1019 if (IsSamplerArray(textureFunction->sampler))
1020 {
1021 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
1022 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
1023 }
1024 else
1025 {
1026 out << " uint width; uint height; uint numberOfLevels;\n"
1027 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
1028 }
1029 }
1030 else if (IsSampler3D(textureFunction->sampler))
1031 {
1032 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
1033 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
1034 }
1035 else UNREACHABLE();
1036
1037 switch(textureFunction->sampler)
1038 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04001039 case EbtSampler2D: out << " return int2(width, height);"; break;
1040 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
1041 case EbtSamplerCube: out << " return int2(width, height);"; break;
1042 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
1043 case EbtISampler2D: out << " return int2(width, height);"; break;
1044 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
1045 case EbtISamplerCube: out << " return int2(width, height);"; break;
1046 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
1047 case EbtUSampler2D: out << " return int2(width, height);"; break;
1048 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
1049 case EbtUSamplerCube: out << " return int2(width, height);"; break;
1050 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
1051 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
1052 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
1053 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001054 default: UNREACHABLE();
1055 }
1056 }
1057 else if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
daniel@transgaming.com15795192011-05-11 15:36:20 +00001058 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001059 // Currently unsupported because TextureCube does not support Load
1060 // This will require emulation using a Texture2DArray with 6 faces
1061 if (textureFunction->sampler == EbtISamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001062 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001063 out << " return int4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001064 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001065 else if (textureFunction->sampler == EbtUSamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001066 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001067 out << " return uint4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001068 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001069 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001070 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001071 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001072 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001073 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001074 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001075 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001076 {
Nicolas Capens93e50de2013-07-09 13:46:28 -04001077 if (IsSamplerArray(textureFunction->sampler))
1078 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001079 out << " float width; float height; float layers; float levels;\n";
1080
1081 if (textureFunction->method == TextureFunction::LOD0)
1082 {
1083 out << " uint mip = 0;\n";
1084 }
1085 else
1086 {
1087 if (textureFunction->method == TextureFunction::IMPLICIT ||
1088 textureFunction->method == TextureFunction::BIAS)
1089 {
1090 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1091 " float2 tSized = float2(t.x * width, t.y * height);\n"
1092 " float dx = length(ddx(tSized));\n"
1093 " float dy = length(ddy(tSized));\n"
1094 " float lod = log2(max(sqrt(dx), sqrt(dy)));\n";
1095
1096 if (textureFunction->method == TextureFunction::BIAS)
1097 {
1098 out << " lod += bias;\n";
1099 }
1100 }
1101
1102 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1103 }
1104
1105 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001106 }
1107 else
1108 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001109 out << " float width; float height; float levels;\n";
1110
1111 if (textureFunction->method == TextureFunction::LOD0)
1112 {
1113 out << " uint mip = 0;\n";
1114 }
1115 else
1116 {
1117 if (textureFunction->method == TextureFunction::IMPLICIT ||
1118 textureFunction->method == TextureFunction::BIAS)
1119 {
1120 out << " x.GetDimensions(0, width, height, levels);\n"
1121 " float2 tSized = float2(t.x * width, t.y * height);\n"
1122 " float dx = length(ddx(tSized));\n"
1123 " float dy = length(ddy(tSized));\n"
1124 " float lod = log2(max(sqrt(dx), sqrt(dy)));\n";
1125
1126 if (textureFunction->method == TextureFunction::BIAS)
1127 {
1128 out << " lod += bias;\n";
1129 }
1130 }
1131
1132 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1133 }
1134
1135 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001136 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001137 }
1138 else if (IsSampler3D(textureFunction->sampler))
1139 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001140 out << " float width; float height; float depth; float levels;\n";
1141
1142 if (textureFunction->method == TextureFunction::LOD0)
1143 {
1144 out << " uint mip = 0;\n";
1145 }
1146 else
1147 {
1148 if (textureFunction->method == TextureFunction::IMPLICIT ||
1149 textureFunction->method == TextureFunction::BIAS)
1150 {
1151 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1152 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
1153 " float dx = length(ddx(tSized));\n"
1154 " float dy = length(ddy(tSized));\n"
1155 " float lod = log2(max(sqrt(dx), sqrt(dy)));\n";
1156
1157 if (textureFunction->method == TextureFunction::BIAS)
1158 {
1159 out << " lod += bias;\n";
1160 }
1161 }
1162
1163 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1164 }
1165
1166 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001167 }
1168 else UNREACHABLE();
1169 }
1170
1171 out << " return ";
1172
1173 // HLSL intrinsic
1174 if (mOutputType == SH_HLSL9_OUTPUT)
1175 {
1176 switch(textureFunction->sampler)
1177 {
1178 case EbtSampler2D: out << "tex2D"; break;
1179 case EbtSamplerCube: out << "texCUBE"; break;
1180 default: UNREACHABLE();
1181 }
1182
Nicolas Capens75fb4752013-07-10 15:14:47 -04001183 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001184 {
1185 case TextureFunction::IMPLICIT: out << "(s, "; break;
1186 case TextureFunction::BIAS: out << "bias(s, "; break;
1187 case TextureFunction::LOD: out << "lod(s, "; break;
1188 case TextureFunction::LOD0: out << "lod(s, "; break;
1189 default: UNREACHABLE();
1190 }
1191 }
1192 else if (mOutputType == SH_HLSL11_OUTPUT)
1193 {
1194 if (IsIntegerSampler(textureFunction->sampler))
1195 {
1196 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001197 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001198 else if (IsShadowSampler(textureFunction->sampler))
1199 {
1200 out << "x.SampleCmp(s, ";
1201 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001202 else
1203 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001204 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001205 {
1206 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1207 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1208 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1209 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
1210 default: UNREACHABLE();
1211 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001212 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001213 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001214 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001215
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001216 // Integer sampling requires integer addresses
1217 TString addressx = "";
1218 TString addressy = "";
1219 TString addressz = "";
1220 TString close = "";
1221
1222 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001223 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001224 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001225 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001226 case 2: out << "int3("; break;
1227 case 3: out << "int4("; break;
1228 default: UNREACHABLE();
1229 }
1230
Nicolas Capensc98406a2013-07-10 14:52:44 -04001231 addressx = "int(floor(width * frac((";
1232 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001233
1234 if (IsSamplerArray(textureFunction->sampler))
1235 {
1236 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1237 }
1238 else
1239 {
Nicolas Capensc98406a2013-07-10 14:52:44 -04001240 addressz = "int(floor(depth * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001241 }
1242
1243 close = "))))";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001244 }
1245 else
1246 {
1247 switch(hlslCoords)
1248 {
1249 case 2: out << "float2("; break;
1250 case 3: out << "float3("; break;
1251 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001252 default: UNREACHABLE();
1253 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001254 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001255
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001256 TString proj = ""; // Only used for projected textures
1257
1258 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001259 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001260 switch(textureFunction->coords)
1261 {
1262 case 3: proj = " / t.z"; break;
1263 case 4: proj = " / t.w"; break;
1264 default: UNREACHABLE();
1265 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001266 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001267
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001268 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001269
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001270 if (mOutputType == SH_HLSL9_OUTPUT)
1271 {
1272 if (hlslCoords >= 3)
1273 {
1274 if (textureFunction->coords < 3)
1275 {
1276 out << ", 0";
1277 }
1278 else
1279 {
1280 out << ", t.z" + proj;
1281 }
1282 }
1283
1284 if (hlslCoords == 4)
1285 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001286 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001287 {
1288 case TextureFunction::BIAS: out << ", bias"; break;
1289 case TextureFunction::LOD: out << ", lod"; break;
1290 case TextureFunction::LOD0: out << ", 0"; break;
1291 default: UNREACHABLE();
1292 }
1293 }
1294
1295 out << "));\n";
1296 }
1297 else if (mOutputType == SH_HLSL11_OUTPUT)
1298 {
1299 if (hlslCoords >= 3)
1300 {
1301 out << ", " + addressz + ("t.z" + proj) + close;
1302 }
1303
Nicolas Capenscb127d32013-07-15 17:26:18 -04001304 if (IsIntegerSampler(textureFunction->sampler))
1305 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001306 out << ", mip));";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001307 }
1308 else if (IsShadowSampler(textureFunction->sampler))
1309 {
1310 // Compare value
1311 switch(textureFunction->coords)
1312 {
1313 case 3: out << "), t.z);"; break;
1314 case 4: out << "), t.w);"; break;
1315 default: UNREACHABLE();
1316 }
1317 }
1318 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001319 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001320 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001321 {
1322 case TextureFunction::IMPLICIT: out << "));"; break;
1323 case TextureFunction::BIAS: out << "), bias);"; break;
1324 case TextureFunction::LOD: out << "), lod);"; break;
1325 case TextureFunction::LOD0: out << "), 0);"; break;
1326 default: UNREACHABLE();
1327 }
1328 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001329 }
1330 else UNREACHABLE();
1331 }
1332
1333 out << "\n"
1334 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001335 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001336 }
1337
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001338 if (mUsesFragCoord)
1339 {
1340 out << "#define GL_USES_FRAG_COORD\n";
1341 }
1342
1343 if (mUsesPointCoord)
1344 {
1345 out << "#define GL_USES_POINT_COORD\n";
1346 }
1347
1348 if (mUsesFrontFacing)
1349 {
1350 out << "#define GL_USES_FRONT_FACING\n";
1351 }
1352
1353 if (mUsesPointSize)
1354 {
1355 out << "#define GL_USES_POINT_SIZE\n";
1356 }
1357
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001358 if (mUsesFragDepth)
1359 {
1360 out << "#define GL_USES_FRAG_DEPTH\n";
1361 }
1362
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001363 if (mUsesDepthRange)
1364 {
1365 out << "#define GL_USES_DEPTH_RANGE\n";
1366 }
1367
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001368 if (mUsesXor)
1369 {
1370 out << "bool xor(bool p, bool q)\n"
1371 "{\n"
1372 " return (p || q) && !(p && q);\n"
1373 "}\n"
1374 "\n";
1375 }
1376
1377 if (mUsesMod1)
1378 {
1379 out << "float mod(float x, float y)\n"
1380 "{\n"
1381 " return x - y * floor(x / y);\n"
1382 "}\n"
1383 "\n";
1384 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001385
1386 if (mUsesMod2v)
1387 {
1388 out << "float2 mod(float2 x, float2 y)\n"
1389 "{\n"
1390 " return x - y * floor(x / y);\n"
1391 "}\n"
1392 "\n";
1393 }
1394
1395 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001396 {
1397 out << "float2 mod(float2 x, float y)\n"
1398 "{\n"
1399 " return x - y * floor(x / y);\n"
1400 "}\n"
1401 "\n";
1402 }
1403
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001404 if (mUsesMod3v)
1405 {
1406 out << "float3 mod(float3 x, float3 y)\n"
1407 "{\n"
1408 " return x - y * floor(x / y);\n"
1409 "}\n"
1410 "\n";
1411 }
1412
1413 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001414 {
1415 out << "float3 mod(float3 x, float y)\n"
1416 "{\n"
1417 " return x - y * floor(x / y);\n"
1418 "}\n"
1419 "\n";
1420 }
1421
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001422 if (mUsesMod4v)
1423 {
1424 out << "float4 mod(float4 x, float4 y)\n"
1425 "{\n"
1426 " return x - y * floor(x / y);\n"
1427 "}\n"
1428 "\n";
1429 }
1430
1431 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001432 {
1433 out << "float4 mod(float4 x, float y)\n"
1434 "{\n"
1435 " return x - y * floor(x / y);\n"
1436 "}\n"
1437 "\n";
1438 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001439
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001440 if (mUsesFaceforward1)
1441 {
1442 out << "float faceforward(float N, float I, float Nref)\n"
1443 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001444 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001445 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001446 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001447 " }\n"
1448 " else\n"
1449 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001450 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001451 " }\n"
1452 "}\n"
1453 "\n";
1454 }
1455
1456 if (mUsesFaceforward2)
1457 {
1458 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1459 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001460 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001461 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001462 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001463 " }\n"
1464 " else\n"
1465 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001466 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001467 " }\n"
1468 "}\n"
1469 "\n";
1470 }
1471
1472 if (mUsesFaceforward3)
1473 {
1474 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1475 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001476 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001477 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001478 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001479 " }\n"
1480 " else\n"
1481 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001482 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001483 " }\n"
1484 "}\n"
1485 "\n";
1486 }
1487
1488 if (mUsesFaceforward4)
1489 {
1490 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1491 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001492 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001493 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001494 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001495 " }\n"
1496 " else\n"
1497 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001498 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001499 " }\n"
1500 "}\n"
1501 "\n";
1502 }
1503
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001504 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001505 {
1506 out << "float atanyx(float y, float x)\n"
1507 "{\n"
1508 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1509 " return atan2(y, x);\n"
1510 "}\n";
1511 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001512
1513 if (mUsesAtan2_2)
1514 {
1515 out << "float2 atanyx(float2 y, float2 x)\n"
1516 "{\n"
1517 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1518 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1519 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1520 "}\n";
1521 }
1522
1523 if (mUsesAtan2_3)
1524 {
1525 out << "float3 atanyx(float3 y, float3 x)\n"
1526 "{\n"
1527 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1528 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1529 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1530 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1531 "}\n";
1532 }
1533
1534 if (mUsesAtan2_4)
1535 {
1536 out << "float4 atanyx(float4 y, float4 x)\n"
1537 "{\n"
1538 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1539 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1540 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1541 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1542 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1543 "}\n";
1544 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001545}
1546
1547void OutputHLSL::visitSymbol(TIntermSymbol *node)
1548{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001549 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001550
Jamie Madill570e04d2013-06-21 09:15:33 -04001551 // Handle accessing std140 structs by value
1552 if (mFlaggedStructMappedNames.count(node) > 0)
1553 {
1554 out << mFlaggedStructMappedNames[node];
1555 return;
1556 }
1557
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001558 TString name = node->getSymbol();
1559
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001560 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001561 {
1562 mUsesDepthRange = true;
1563 out << name;
1564 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001565 else
1566 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001567 TQualifier qualifier = node->getQualifier();
1568
1569 if (qualifier == EvqUniform)
1570 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001571 const TType& nodeType = node->getType();
1572 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1573
1574 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001575 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001576 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001577 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001578 else
1579 {
1580 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001581 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001582
1583 out << decorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001584 }
Jamie Madill19571812013-08-12 15:26:34 -07001585 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001586 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001587 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001588 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001589 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001590 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001591 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001592 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001593 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001594 }
Jamie Madill19571812013-08-12 15:26:34 -07001595 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001596 {
1597 mReferencedOutputVariables[name] = node;
1598 out << "out_" << name;
1599 }
1600 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001601 {
1602 out << "gl_Color[0]";
1603 mUsesFragColor = true;
1604 }
1605 else if (qualifier == EvqFragData)
1606 {
1607 out << "gl_Color";
1608 mUsesFragData = true;
1609 }
1610 else if (qualifier == EvqFragCoord)
1611 {
1612 mUsesFragCoord = true;
1613 out << name;
1614 }
1615 else if (qualifier == EvqPointCoord)
1616 {
1617 mUsesPointCoord = true;
1618 out << name;
1619 }
1620 else if (qualifier == EvqFrontFacing)
1621 {
1622 mUsesFrontFacing = true;
1623 out << name;
1624 }
1625 else if (qualifier == EvqPointSize)
1626 {
1627 mUsesPointSize = true;
1628 out << name;
1629 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001630 else if (name == "gl_FragDepthEXT")
1631 {
1632 mUsesFragDepth = true;
1633 out << "gl_Depth";
1634 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001635 else
1636 {
1637 out << decorate(name);
1638 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001639 }
1640}
1641
1642bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1643{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001644 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001645
Jamie Madill570e04d2013-06-21 09:15:33 -04001646 // Handle accessing std140 structs by value
1647 if (mFlaggedStructMappedNames.count(node) > 0)
1648 {
1649 out << mFlaggedStructMappedNames[node];
1650 return false;
1651 }
1652
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001653 switch (node->getOp())
1654 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001655 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001656 case EOpInitialize:
1657 if (visit == PreVisit)
1658 {
1659 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1660 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1661 // new variable is created before the assignment is evaluated), so we need to convert
1662 // this to "float t = x, x = t;".
1663
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001664 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1665 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001666
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001667 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1668 expression->traverse(&searchSymbol);
1669 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001670
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001671 if (sameSymbol)
1672 {
1673 // Type already printed
1674 out << "t" + str(mUniqueIndex) + " = ";
1675 expression->traverse(this);
1676 out << ", ";
1677 symbolNode->traverse(this);
1678 out << " = t" + str(mUniqueIndex);
1679
1680 mUniqueIndex++;
1681 return false;
1682 }
1683 }
1684 else if (visit == InVisit)
1685 {
1686 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001687 }
1688 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001689 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1690 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1691 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1692 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1693 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1694 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001695 if (visit == PreVisit)
1696 {
1697 out << "(";
1698 }
1699 else if (visit == InVisit)
1700 {
1701 out << " = mul(";
1702 node->getLeft()->traverse(this);
1703 out << ", transpose(";
1704 }
1705 else
1706 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001707 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001708 }
1709 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001710 case EOpMatrixTimesMatrixAssign:
1711 if (visit == PreVisit)
1712 {
1713 out << "(";
1714 }
1715 else if (visit == InVisit)
1716 {
1717 out << " = mul(";
1718 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001719 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001720 }
1721 else
1722 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001723 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001724 }
1725 break;
1726 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001727 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001728 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001729 const TType& leftType = node->getLeft()->getType();
1730 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001731 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001732 if (visit == PreVisit)
1733 {
1734 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1735 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1736
1737 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1738 out << interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1739
1740 return false;
1741 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001742 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001743 else
1744 {
1745 outputTriplet(visit, "", "[", "]");
1746 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001747 }
1748 break;
1749 case EOpIndexIndirect:
1750 // We do not currently support indirect references to interface blocks
1751 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1752 outputTriplet(visit, "", "[", "]");
1753 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001754 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001755 if (visit == InVisit)
1756 {
1757 const TStructure* structure = node->getLeft()->getType().getStruct();
1758 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1759 const TField* field = structure->fields()[index->getIConst(0)];
1760 out << "." + decorateField(field->name(), *structure);
1761
1762 return false;
1763 }
1764 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001765 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001766 if (visit == InVisit)
1767 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001768 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1769 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1770 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
1771 out << "." + decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001772
1773 return false;
1774 }
1775 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001776 case EOpVectorSwizzle:
1777 if (visit == InVisit)
1778 {
1779 out << ".";
1780
1781 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1782
1783 if (swizzle)
1784 {
1785 TIntermSequence &sequence = swizzle->getSequence();
1786
1787 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1788 {
1789 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1790
1791 if (element)
1792 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001793 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001794
1795 switch (i)
1796 {
1797 case 0: out << "x"; break;
1798 case 1: out << "y"; break;
1799 case 2: out << "z"; break;
1800 case 3: out << "w"; break;
1801 default: UNREACHABLE();
1802 }
1803 }
1804 else UNREACHABLE();
1805 }
1806 }
1807 else UNREACHABLE();
1808
1809 return false; // Fully processed
1810 }
1811 break;
1812 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1813 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1814 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1815 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001816 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001817 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001818 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001819 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001820 if (node->getOp() == EOpEqual)
1821 {
1822 outputTriplet(visit, "(", " == ", ")");
1823 }
1824 else
1825 {
1826 outputTriplet(visit, "(", " != ", ")");
1827 }
1828 }
1829 else if (node->getLeft()->getBasicType() == EbtStruct)
1830 {
1831 if (node->getOp() == EOpEqual)
1832 {
1833 out << "(";
1834 }
1835 else
1836 {
1837 out << "!(";
1838 }
1839
Jamie Madill98493dd2013-07-08 14:39:03 -04001840 const TStructure &structure = *node->getLeft()->getType().getStruct();
1841 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001842
Jamie Madill98493dd2013-07-08 14:39:03 -04001843 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001844 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001845 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001846
1847 node->getLeft()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001848 out << "." + decorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001849 node->getRight()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001850 out << "." + decorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001851
Jamie Madill98493dd2013-07-08 14:39:03 -04001852 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001853 {
1854 out << " && ";
1855 }
1856 }
1857
1858 out << ")";
1859
1860 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001861 }
1862 else
1863 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001864 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001865
1866 if (node->getOp() == EOpEqual)
1867 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001868 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001869 }
1870 else
1871 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001872 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001873 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001874 }
1875 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001876 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1877 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1878 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1879 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1880 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001881 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001882 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1883 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001884 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001885 case EOpLogicalOr:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001886 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001887 return false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001888 case EOpLogicalXor:
1889 mUsesXor = true;
1890 outputTriplet(visit, "xor(", ", ", ")");
1891 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001892 case EOpLogicalAnd:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001893 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001894 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001895 default: UNREACHABLE();
1896 }
1897
1898 return true;
1899}
1900
1901bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1902{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001903 switch (node->getOp())
1904 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001905 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1906 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1907 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1908 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1909 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1910 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1911 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001912 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04001913 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001914 case EOpConvFloatToBool:
1915 switch (node->getOperand()->getType().getNominalSize())
1916 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001917 case 1: outputTriplet(visit, "bool(", "", ")"); break;
1918 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
1919 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
1920 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001921 default: UNREACHABLE();
1922 }
1923 break;
1924 case EOpConvBoolToFloat:
1925 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04001926 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001927 switch (node->getOperand()->getType().getNominalSize())
1928 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001929 case 1: outputTriplet(visit, "float(", "", ")"); break;
1930 case 2: outputTriplet(visit, "float2(", "", ")"); break;
1931 case 3: outputTriplet(visit, "float3(", "", ")"); break;
1932 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001933 default: UNREACHABLE();
1934 }
1935 break;
1936 case EOpConvFloatToInt:
1937 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04001938 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001939 switch (node->getOperand()->getType().getNominalSize())
1940 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001941 case 1: outputTriplet(visit, "int(", "", ")"); break;
1942 case 2: outputTriplet(visit, "int2(", "", ")"); break;
1943 case 3: outputTriplet(visit, "int3(", "", ")"); break;
1944 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001945 default: UNREACHABLE();
1946 }
1947 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04001948 case EOpConvFloatToUInt:
1949 case EOpConvBoolToUInt:
1950 case EOpConvIntToUInt:
Jamie Madilla16f1672013-07-03 15:15:17 -04001951 switch (node->getOperand()->getType().getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001952 {
1953 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001954 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
1955 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
1956 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001957 default: UNREACHABLE();
1958 }
1959 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001960 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1961 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1962 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1963 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1964 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1965 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1966 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1967 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1968 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1969 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1970 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1971 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1972 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1973 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1974 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1975 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1976 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1977 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1978 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1979 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1980 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001981 case EOpDFdx:
1982 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1983 {
1984 outputTriplet(visit, "(", "", ", 0.0)");
1985 }
1986 else
1987 {
1988 outputTriplet(visit, "ddx(", "", ")");
1989 }
1990 break;
1991 case EOpDFdy:
1992 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1993 {
1994 outputTriplet(visit, "(", "", ", 0.0)");
1995 }
1996 else
1997 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001998 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001999 }
2000 break;
2001 case EOpFwidth:
2002 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2003 {
2004 outputTriplet(visit, "(", "", ", 0.0)");
2005 }
2006 else
2007 {
2008 outputTriplet(visit, "fwidth(", "", ")");
2009 }
2010 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002011 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
2012 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002013 default: UNREACHABLE();
2014 }
2015
2016 return true;
2017}
2018
2019bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2020{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002021 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002022
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002023 switch (node->getOp())
2024 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002025 case EOpSequence:
2026 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002027 if (mInsideFunction)
2028 {
Jamie Madill075edd82013-07-08 13:30:19 -04002029 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002030 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002031
2032 mScopeDepth++;
2033
2034 if (mScopeBracket.size() < mScopeDepth)
2035 {
2036 mScopeBracket.push_back(0); // New scope level
2037 }
2038 else
2039 {
2040 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
2041 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002042 }
2043
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002044 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
2045 {
Jamie Madill075edd82013-07-08 13:30:19 -04002046 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002047
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002048 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002049
2050 out << ";\n";
2051 }
2052
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002053 if (mInsideFunction)
2054 {
Jamie Madill075edd82013-07-08 13:30:19 -04002055 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002056 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002057
2058 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002059 }
2060
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002061 return false;
2062 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002063 case EOpDeclaration:
2064 if (visit == PreVisit)
2065 {
2066 TIntermSequence &sequence = node->getSequence();
2067 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002068
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00002069 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002070 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002071 if (variable->getType().getStruct())
2072 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002073 addConstructor(variable->getType(), scopedStruct(variable->getType().getStruct()->name()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00002074 }
2075
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002076 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002077 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00002078 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002079 {
2080 out << "static ";
2081 }
2082
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002083 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002084
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002085 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002086 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002087 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002088
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002089 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002090 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002091 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002092 out << arrayString(symbol->getType());
daniel@transgaming.com7127f202010-04-15 20:45:22 +00002093 out << " = " + initializer(variable->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002094 }
2095 else
2096 {
2097 (*sit)->traverse(this);
2098 }
2099
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002100 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002101 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002102 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002103 }
2104 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002105 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002106 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2107 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002108 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002109 }
2110 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002111 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002112 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002113 {
2114 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2115 {
2116 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2117
2118 if (symbol)
2119 {
2120 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2121 mReferencedVaryings[symbol->getSymbol()] = symbol;
2122 }
2123 else
2124 {
2125 (*sit)->traverse(this);
2126 }
2127 }
2128 }
2129
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002130 return false;
2131 }
2132 else if (visit == InVisit)
2133 {
2134 out << ", ";
2135 }
2136 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002137 case EOpPrototype:
2138 if (visit == PreVisit)
2139 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002140 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002141
2142 TIntermSequence &arguments = node->getSequence();
2143
2144 for (unsigned int i = 0; i < arguments.size(); i++)
2145 {
2146 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2147
2148 if (symbol)
2149 {
2150 out << argumentString(symbol);
2151
2152 if (i < arguments.size() - 1)
2153 {
2154 out << ", ";
2155 }
2156 }
2157 else UNREACHABLE();
2158 }
2159
2160 out << ");\n";
2161
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002162 // Also prototype the Lod0 variant if needed
2163 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2164 {
2165 mOutputLod0Function = true;
2166 node->traverse(this);
2167 mOutputLod0Function = false;
2168 }
2169
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002170 return false;
2171 }
2172 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002173 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002174 case EOpFunction:
2175 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002176 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002177
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002178 out << typeString(node->getType()) << " ";
2179
2180 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002181 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002182 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002183 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002184 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002185 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002186 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002187 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002188
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002189 TIntermSequence &sequence = node->getSequence();
2190 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2191
2192 for (unsigned int i = 0; i < arguments.size(); i++)
2193 {
2194 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2195
2196 if (symbol)
2197 {
2198 if (symbol->getType().getStruct())
2199 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002200 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getStruct()->name()), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002201 }
2202
2203 out << argumentString(symbol);
2204
2205 if (i < arguments.size() - 1)
2206 {
2207 out << ", ";
2208 }
2209 }
2210 else UNREACHABLE();
2211 }
2212
2213 out << ")\n"
2214 "{\n";
2215
2216 if (sequence.size() > 1)
2217 {
2218 mInsideFunction = true;
2219 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002220 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002221 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002222
2223 out << "}\n";
2224
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002225 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2226 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002227 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002228 {
2229 mOutputLod0Function = true;
2230 node->traverse(this);
2231 mOutputLod0Function = false;
2232 }
2233 }
2234
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002235 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002236 }
2237 break;
2238 case EOpFunctionCall:
2239 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002240 TString name = TFunction::unmangleName(node->getName());
2241 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002242 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002243
2244 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002245 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002246 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002247 }
2248 else
2249 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002250 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2251
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002252 TextureFunction textureFunction;
2253 textureFunction.sampler = samplerType;
2254 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002255 textureFunction.method = TextureFunction::IMPLICIT;
2256 textureFunction.proj = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002257
2258 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002259 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002260 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002261 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002262 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002263 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002264 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002265 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002266 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002267 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002268 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002269 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002270 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002271 else if (name == "texture2DProjLod" || name == "textureProjLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002272 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002273 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002274 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002275 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002276 else if (name == "textureSize")
2277 {
2278 textureFunction.method = TextureFunction::SIZE;
2279 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002280 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002281
Nicolas Capens75fb4752013-07-10 15:14:47 -04002282 if (textureFunction.method != TextureFunction::LOD &&
2283 textureFunction.method != TextureFunction::SIZE)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002284 {
2285 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2286 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002287 textureFunction.method = TextureFunction::LOD0;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002288 }
2289 else if (arguments.size() == 3)
2290 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002291 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002292 }
2293 }
2294
2295 mUsesTexture.insert(textureFunction);
2296
2297 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002298 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002299
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002300 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2301 {
2302 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2303 {
2304 out << "texture_";
2305 (*arg)->traverse(this);
2306 out << ", sampler_";
2307 }
2308
2309 (*arg)->traverse(this);
2310
2311 if (arg < arguments.end() - 1)
2312 {
2313 out << ", ";
2314 }
2315 }
2316
2317 out << ")";
2318
2319 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002320 }
2321 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002322 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002323 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002324 addConstructor(node->getType(), "vec1", &node->getSequence());
2325 outputTriplet(visit, "vec1(", "", ")");
2326 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002327 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002328 addConstructor(node->getType(), "vec2", &node->getSequence());
2329 outputTriplet(visit, "vec2(", ", ", ")");
2330 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002331 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002332 addConstructor(node->getType(), "vec3", &node->getSequence());
2333 outputTriplet(visit, "vec3(", ", ", ")");
2334 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002335 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002336 addConstructor(node->getType(), "vec4", &node->getSequence());
2337 outputTriplet(visit, "vec4(", ", ", ")");
2338 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002339 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002340 addConstructor(node->getType(), "bvec1", &node->getSequence());
2341 outputTriplet(visit, "bvec1(", "", ")");
2342 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002343 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002344 addConstructor(node->getType(), "bvec2", &node->getSequence());
2345 outputTriplet(visit, "bvec2(", ", ", ")");
2346 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002347 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002348 addConstructor(node->getType(), "bvec3", &node->getSequence());
2349 outputTriplet(visit, "bvec3(", ", ", ")");
2350 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002351 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002352 addConstructor(node->getType(), "bvec4", &node->getSequence());
2353 outputTriplet(visit, "bvec4(", ", ", ")");
2354 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002355 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002356 addConstructor(node->getType(), "ivec1", &node->getSequence());
2357 outputTriplet(visit, "ivec1(", "", ")");
2358 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002359 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002360 addConstructor(node->getType(), "ivec2", &node->getSequence());
2361 outputTriplet(visit, "ivec2(", ", ", ")");
2362 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002363 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002364 addConstructor(node->getType(), "ivec3", &node->getSequence());
2365 outputTriplet(visit, "ivec3(", ", ", ")");
2366 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002367 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002368 addConstructor(node->getType(), "ivec4", &node->getSequence());
2369 outputTriplet(visit, "ivec4(", ", ", ")");
2370 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002371 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002372 addConstructor(node->getType(), "uvec1", &node->getSequence());
2373 outputTriplet(visit, "uvec1(", "", ")");
2374 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002375 case EOpConstructUVec2:
2376 addConstructor(node->getType(), "uvec2", &node->getSequence());
2377 outputTriplet(visit, "uvec2(", ", ", ")");
2378 break;
2379 case EOpConstructUVec3:
2380 addConstructor(node->getType(), "uvec3", &node->getSequence());
2381 outputTriplet(visit, "uvec3(", ", ", ")");
2382 break;
2383 case EOpConstructUVec4:
2384 addConstructor(node->getType(), "uvec4", &node->getSequence());
2385 outputTriplet(visit, "uvec4(", ", ", ")");
2386 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002387 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002388 addConstructor(node->getType(), "mat2", &node->getSequence());
2389 outputTriplet(visit, "mat2(", ", ", ")");
2390 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002391 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002392 addConstructor(node->getType(), "mat3", &node->getSequence());
2393 outputTriplet(visit, "mat3(", ", ", ")");
2394 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002395 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002396 addConstructor(node->getType(), "mat4", &node->getSequence());
2397 outputTriplet(visit, "mat4(", ", ", ")");
2398 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002399 case EOpConstructStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04002400 addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence());
2401 outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002402 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002403 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2404 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2405 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2406 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2407 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2408 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002409 case EOpMod:
2410 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002411 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002412 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2413 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2414 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002415 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002416 case 11: mUsesMod1 = true; break;
2417 case 22: mUsesMod2v = true; break;
2418 case 21: mUsesMod2f = true; break;
2419 case 33: mUsesMod3v = true; break;
2420 case 31: mUsesMod3f = true; break;
2421 case 44: mUsesMod4v = true; break;
2422 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002423 default: UNREACHABLE();
2424 }
2425
2426 outputTriplet(visit, "mod(", ", ", ")");
2427 }
2428 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002429 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002430 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002431 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002432 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2433 {
2434 case 1: mUsesAtan2_1 = true; break;
2435 case 2: mUsesAtan2_2 = true; break;
2436 case 3: mUsesAtan2_3 = true; break;
2437 case 4: mUsesAtan2_4 = true; break;
2438 default: UNREACHABLE();
2439 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002440 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002441 break;
2442 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2443 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2444 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2445 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2446 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2447 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2448 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2449 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2450 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002451 case EOpFaceForward:
2452 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002453 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002454 {
2455 case 1: mUsesFaceforward1 = true; break;
2456 case 2: mUsesFaceforward2 = true; break;
2457 case 3: mUsesFaceforward3 = true; break;
2458 case 4: mUsesFaceforward4 = true; break;
2459 default: UNREACHABLE();
2460 }
2461
2462 outputTriplet(visit, "faceforward(", ", ", ")");
2463 }
2464 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002465 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2466 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2467 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002468 default: UNREACHABLE();
2469 }
2470
2471 return true;
2472}
2473
2474bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2475{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002476 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002477
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002478 if (node->usesTernaryOperator())
2479 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002480 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002481 }
2482 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002483 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002484 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002485
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002486 out << "if(";
2487
2488 node->getCondition()->traverse(this);
2489
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002490 out << ")\n";
2491
Jamie Madill075edd82013-07-08 13:30:19 -04002492 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002493 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002494
daniel@transgaming.combb885322010-04-15 20:45:24 +00002495 if (node->getTrueBlock())
2496 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002497 traverseStatements(node->getTrueBlock());
daniel@transgaming.combb885322010-04-15 20:45:24 +00002498 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002499
Jamie Madill075edd82013-07-08 13:30:19 -04002500 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002501 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002502
2503 if (node->getFalseBlock())
2504 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002505 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002506
Jamie Madill075edd82013-07-08 13:30:19 -04002507 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002508 out << "{\n";
2509
Jamie Madill075edd82013-07-08 13:30:19 -04002510 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002511 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002512
Jamie Madill075edd82013-07-08 13:30:19 -04002513 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002514 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002515 }
2516 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002517
2518 return false;
2519}
2520
2521void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2522{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002523 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002524}
2525
2526bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2527{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002528 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2529
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002530 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002531 {
2532 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2533 }
2534
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002535 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002536 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002537 if (handleExcessiveLoop(node))
2538 {
2539 return false;
2540 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002541 }
2542
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002543 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002544
alokp@chromium.org52813552010-11-16 18:36:09 +00002545 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002546 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002547 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002548
Jamie Madill075edd82013-07-08 13:30:19 -04002549 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002550 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002551 }
2552 else
2553 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002554 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002555
2556 if (node->getInit())
2557 {
2558 node->getInit()->traverse(this);
2559 }
2560
2561 out << "; ";
2562
alokp@chromium.org52813552010-11-16 18:36:09 +00002563 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002564 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002565 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002566 }
2567
2568 out << "; ";
2569
alokp@chromium.org52813552010-11-16 18:36:09 +00002570 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002571 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002572 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002573 }
2574
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002575 out << ")\n";
2576
Jamie Madill075edd82013-07-08 13:30:19 -04002577 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002578 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002579 }
2580
2581 if (node->getBody())
2582 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002583 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002584 }
2585
Jamie Madill075edd82013-07-08 13:30:19 -04002586 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002587 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002588
alokp@chromium.org52813552010-11-16 18:36:09 +00002589 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002590 {
Jamie Madill075edd82013-07-08 13:30:19 -04002591 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002592 out << "while(\n";
2593
alokp@chromium.org52813552010-11-16 18:36:09 +00002594 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002595
daniel@transgaming.com73536982012-03-21 20:45:49 +00002596 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002597 }
2598
daniel@transgaming.com73536982012-03-21 20:45:49 +00002599 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002600
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002601 mInsideDiscontinuousLoop = wasDiscontinuous;
2602
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002603 return false;
2604}
2605
2606bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2607{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002608 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002609
2610 switch (node->getFlowOp())
2611 {
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002612 case EOpKill: outputTriplet(visit, "discard;\n", "", ""); break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002613 case EOpBreak:
2614 if (visit == PreVisit)
2615 {
2616 if (mExcessiveLoopIndex)
2617 {
2618 out << "{Break";
2619 mExcessiveLoopIndex->traverse(this);
2620 out << " = true; break;}\n";
2621 }
2622 else
2623 {
2624 out << "break;\n";
2625 }
2626 }
2627 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002628 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002629 case EOpReturn:
2630 if (visit == PreVisit)
2631 {
2632 if (node->getExpression())
2633 {
2634 out << "return ";
2635 }
2636 else
2637 {
2638 out << "return;\n";
2639 }
2640 }
2641 else if (visit == PostVisit)
2642 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002643 if (node->getExpression())
2644 {
2645 out << ";\n";
2646 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002647 }
2648 break;
2649 default: UNREACHABLE();
2650 }
2651
2652 return true;
2653}
2654
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002655void OutputHLSL::traverseStatements(TIntermNode *node)
2656{
2657 if (isSingleStatement(node))
2658 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002659 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002660 }
2661
2662 node->traverse(this);
2663}
2664
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002665bool OutputHLSL::isSingleStatement(TIntermNode *node)
2666{
2667 TIntermAggregate *aggregate = node->getAsAggregate();
2668
2669 if (aggregate)
2670 {
2671 if (aggregate->getOp() == EOpSequence)
2672 {
2673 return false;
2674 }
2675 else
2676 {
2677 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2678 {
2679 if (!isSingleStatement(*sit))
2680 {
2681 return false;
2682 }
2683 }
2684
2685 return true;
2686 }
2687 }
2688
2689 return true;
2690}
2691
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002692// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2693// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002694bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2695{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002696 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002697 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002698
2699 // Parse loops of the form:
2700 // for(int index = initial; index [comparator] limit; index += increment)
2701 TIntermSymbol *index = NULL;
2702 TOperator comparator = EOpNull;
2703 int initial = 0;
2704 int limit = 0;
2705 int increment = 0;
2706
2707 // Parse index name and intial value
2708 if (node->getInit())
2709 {
2710 TIntermAggregate *init = node->getInit()->getAsAggregate();
2711
2712 if (init)
2713 {
2714 TIntermSequence &sequence = init->getSequence();
2715 TIntermTyped *variable = sequence[0]->getAsTyped();
2716
2717 if (variable && variable->getQualifier() == EvqTemporary)
2718 {
2719 TIntermBinary *assign = variable->getAsBinaryNode();
2720
2721 if (assign->getOp() == EOpInitialize)
2722 {
2723 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2724 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2725
2726 if (symbol && constant)
2727 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002728 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002729 {
2730 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002731 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002732 }
2733 }
2734 }
2735 }
2736 }
2737 }
2738
2739 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002740 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002741 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002742 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002743
2744 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2745 {
2746 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2747
2748 if (constant)
2749 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002750 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002751 {
2752 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002753 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002754 }
2755 }
2756 }
2757 }
2758
2759 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002760 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002761 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002762 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2763 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002764
2765 if (binaryTerminal)
2766 {
2767 TOperator op = binaryTerminal->getOp();
2768 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2769
2770 if (constant)
2771 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002772 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002773 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002774 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002775
2776 switch (op)
2777 {
2778 case EOpAddAssign: increment = value; break;
2779 case EOpSubAssign: increment = -value; break;
2780 default: UNIMPLEMENTED();
2781 }
2782 }
2783 }
2784 }
2785 else if (unaryTerminal)
2786 {
2787 TOperator op = unaryTerminal->getOp();
2788
2789 switch (op)
2790 {
2791 case EOpPostIncrement: increment = 1; break;
2792 case EOpPostDecrement: increment = -1; break;
2793 case EOpPreIncrement: increment = 1; break;
2794 case EOpPreDecrement: increment = -1; break;
2795 default: UNIMPLEMENTED();
2796 }
2797 }
2798 }
2799
2800 if (index != NULL && comparator != EOpNull && increment != 0)
2801 {
2802 if (comparator == EOpLessThanEqual)
2803 {
2804 comparator = EOpLessThan;
2805 limit += 1;
2806 }
2807
2808 if (comparator == EOpLessThan)
2809 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002810 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002811
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002812 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002813 {
2814 return false; // Not an excessive loop
2815 }
2816
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002817 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2818 mExcessiveLoopIndex = index;
2819
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002820 out << "{int ";
2821 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002822 out << ";\n"
2823 "bool Break";
2824 index->traverse(this);
2825 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002826
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002827 bool firstLoopFragment = true;
2828
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002829 while (iterations > 0)
2830 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002831 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002832
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002833 if (!firstLoopFragment)
2834 {
2835 out << "if(!Break";
2836 index->traverse(this);
2837 out << ") {\n";
2838 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002839
2840 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2841 {
2842 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2843 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002844
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002845 // for(int index = initial; index < clampedLimit; index += increment)
2846
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002847 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002848 index->traverse(this);
2849 out << " = ";
2850 out << initial;
2851
2852 out << "; ";
2853 index->traverse(this);
2854 out << " < ";
2855 out << clampedLimit;
2856
2857 out << "; ";
2858 index->traverse(this);
2859 out << " += ";
2860 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002861 out << ")\n";
2862
Jamie Madill075edd82013-07-08 13:30:19 -04002863 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002864 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002865
2866 if (node->getBody())
2867 {
2868 node->getBody()->traverse(this);
2869 }
2870
Jamie Madill075edd82013-07-08 13:30:19 -04002871 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002872 out << ";}\n";
2873
2874 if (!firstLoopFragment)
2875 {
2876 out << "}\n";
2877 }
2878
2879 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002880
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002881 initial += MAX_LOOP_ITERATIONS * increment;
2882 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002883 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002884
2885 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002886
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002887 mExcessiveLoopIndex = restoreIndex;
2888
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002889 return true;
2890 }
2891 else UNIMPLEMENTED();
2892 }
2893
2894 return false; // Not handled as an excessive loop
2895}
2896
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002897void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002898{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002899 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002900
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002901 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002902 {
2903 out << preString;
2904 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002905 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002906 {
2907 out << inString;
2908 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002909 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002910 {
2911 out << postString;
2912 }
2913}
2914
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002915void OutputHLSL::outputLineDirective(int line)
2916{
2917 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2918 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002919 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002920 mBody << "#line " << line;
2921
2922 if (mContext.sourcePath)
2923 {
2924 mBody << " \"" << mContext.sourcePath << "\"";
2925 }
2926
2927 mBody << "\n";
2928 }
2929}
2930
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002931TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2932{
2933 TQualifier qualifier = symbol->getQualifier();
2934 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002935 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002936
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002937 if (name.empty()) // HLSL demands named arguments, also for prototypes
2938 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002939 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002940 }
2941 else
2942 {
2943 name = decorate(name);
2944 }
2945
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002946 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2947 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04002948 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
2949 qualifierString(qualifier) + " " + samplerString(type) + " sampler_" + name + arrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002950 }
2951
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002952 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002953}
2954
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00002955TString OutputHLSL::interpolationString(TQualifier qualifier)
2956{
2957 switch(qualifier)
2958 {
2959 case EvqVaryingIn: return "";
Jamie Madill19571812013-08-12 15:26:34 -07002960 case EvqFragmentIn: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00002961 case EvqInvariantVaryingIn: return "";
2962 case EvqSmoothIn: return "linear";
2963 case EvqFlatIn: return "nointerpolation";
2964 case EvqCentroidIn: return "centroid";
2965 case EvqVaryingOut: return "";
Jamie Madill19571812013-08-12 15:26:34 -07002966 case EvqVertexOut: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00002967 case EvqInvariantVaryingOut: return "";
2968 case EvqSmoothOut: return "linear";
2969 case EvqFlatOut: return "nointerpolation";
2970 case EvqCentroidOut: return "centroid";
2971 default: UNREACHABLE();
2972 }
2973
2974 return "";
2975}
2976
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002977TString OutputHLSL::qualifierString(TQualifier qualifier)
2978{
2979 switch(qualifier)
2980 {
2981 case EvqIn: return "in";
2982 case EvqOut: return "out";
2983 case EvqInOut: return "inout";
2984 case EvqConstReadOnly: return "const";
2985 default: UNREACHABLE();
2986 }
2987
2988 return "";
2989}
2990
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002991TString OutputHLSL::typeString(const TType &type)
2992{
Jamie Madill98493dd2013-07-08 14:39:03 -04002993 const TStructure* structure = type.getStruct();
2994 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002995 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002996 const TString& typeName = structure->name();
2997 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002998 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002999 return structLookup(typeName);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003000 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003001 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003002 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003003 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003004 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003005 }
3006 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003007 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003008 int cols = type.getCols();
3009 int rows = type.getRows();
3010 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003011 }
3012 else
3013 {
3014 switch (type.getBasicType())
3015 {
3016 case EbtFloat:
3017 switch (type.getNominalSize())
3018 {
3019 case 1: return "float";
3020 case 2: return "float2";
3021 case 3: return "float3";
3022 case 4: return "float4";
3023 }
3024 case EbtInt:
3025 switch (type.getNominalSize())
3026 {
3027 case 1: return "int";
3028 case 2: return "int2";
3029 case 3: return "int3";
3030 case 4: return "int4";
3031 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003032 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04003033 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003034 {
3035 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003036 case 2: return "uint2";
3037 case 3: return "uint3";
3038 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003039 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003040 case EbtBool:
3041 switch (type.getNominalSize())
3042 {
3043 case 1: return "bool";
3044 case 2: return "bool2";
3045 case 3: return "bool3";
3046 case 4: return "bool4";
3047 }
3048 case EbtVoid:
3049 return "void";
3050 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003051 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04003052 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003053 case EbtSampler2DArray:
3054 case EbtISampler2DArray:
3055 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003056 return "sampler2D";
3057 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003058 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04003059 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003060 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00003061 case EbtSamplerExternalOES:
3062 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00003063 default:
3064 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003065 }
3066 }
3067
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003068 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003069 return "<unknown type>";
3070}
3071
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003072TString OutputHLSL::textureString(const TType &type)
3073{
3074 switch (type.getBasicType())
3075 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003076 case EbtSampler2D: return "Texture2D";
3077 case EbtSamplerCube: return "TextureCube";
3078 case EbtSamplerExternalOES: return "Texture2D";
3079 case EbtSampler2DArray: return "Texture2DArray";
3080 case EbtSampler3D: return "Texture3D";
3081 case EbtISampler2D: return "Texture2D<int4>";
3082 case EbtISampler3D: return "Texture3D<int4>";
3083 case EbtISamplerCube: return "TextureCube<int4>";
3084 case EbtISampler2DArray: return "Texture2DArray<int4>";
3085 case EbtUSampler2D: return "Texture2D<uint4>";
3086 case EbtUSampler3D: return "Texture3D<uint4>";
3087 case EbtUSamplerCube: return "TextureCube<uint4>";
3088 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
3089 case EbtSampler2DShadow: return "Texture2D";
3090 case EbtSamplerCubeShadow: return "TextureCube";
3091 case EbtSampler2DArrayShadow: return "Texture2DArray";
3092 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003093 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04003094
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003095 return "<unknown texture type>";
3096}
3097
Nicolas Capenscb127d32013-07-15 17:26:18 -04003098TString OutputHLSL::samplerString(const TType &type)
3099{
3100 if (IsShadowSampler(type.getBasicType()))
3101 {
3102 return "SamplerComparisonState";
3103 }
3104 else
3105 {
3106 return "SamplerState";
3107 }
3108}
3109
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003110TString OutputHLSL::arrayString(const TType &type)
3111{
3112 if (!type.isArray())
3113 {
3114 return "";
3115 }
3116
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003117 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003118}
3119
3120TString OutputHLSL::initializer(const TType &type)
3121{
3122 TString string;
3123
Jamie Madill94bf7f22013-07-08 13:31:15 -04003124 size_t size = type.getObjectSize();
3125 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003126 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003127 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003128
Jamie Madill94bf7f22013-07-08 13:31:15 -04003129 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003130 {
3131 string += ", ";
3132 }
3133 }
3134
daniel@transgaming.comead23042010-04-29 03:35:36 +00003135 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003136}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003137
Jamie Madill98493dd2013-07-08 14:39:03 -04003138TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003139{
Jamie Madill98493dd2013-07-08 14:39:03 -04003140 const TFieldList &fields = structure.fields();
3141 const bool isNameless = (structure.name() == "");
3142 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003143 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3144
Jamie Madill98493dd2013-07-08 14:39:03 -04003145 TString string;
3146 string += declareString + "\n"
3147 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003148
Jamie Madillc835df62013-06-21 09:15:32 -04003149 int elementIndex = 0;
3150
Jamie Madill9cf6c072013-06-20 11:55:53 -04003151 for (unsigned int i = 0; i < fields.size(); i++)
3152 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003153 const TField &field = *fields[i];
3154 const TType &fieldType = *field.type();
3155 const TStructure *fieldStruct = fieldType.getStruct();
3156 const TString &fieldTypeString = fieldStruct ? structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) : typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003157
Jamie Madillc835df62013-06-21 09:15:32 -04003158 if (useStd140Packing)
3159 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003160 string += std140PrePaddingString(*field.type(), &elementIndex);
Jamie Madillc835df62013-06-21 09:15:32 -04003161 }
3162
Jamie Madill98493dd2013-07-08 14:39:03 -04003163 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003164
3165 if (useStd140Packing)
3166 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003167 string += std140PostPaddingString(*field.type(), useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003168 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003169 }
3170
3171 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003172 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003173
Jamie Madille4075c92013-06-21 09:15:32 -04003174 // Add remaining element index to the global map, for use with nested structs in standard layouts
3175 if (useStd140Packing)
3176 {
3177 mStd140StructElementIndexes[structName] = elementIndex;
3178 }
3179
Jamie Madill98493dd2013-07-08 14:39:03 -04003180 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003181}
3182
Jamie Madill98493dd2013-07-08 14:39:03 -04003183TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003184{
Jamie Madill98493dd2013-07-08 14:39:03 -04003185 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003186 {
3187 return "";
3188 }
3189
3190 TString prefix = "";
3191
3192 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3193 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003194
3195 if (useStd140Packing)
3196 {
3197 prefix += "std";
3198 }
3199
Jamie Madill9cf6c072013-06-20 11:55:53 -04003200 if (useHLSLRowMajorPacking)
3201 {
Jamie Madillc835df62013-06-21 09:15:32 -04003202 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003203 prefix += "rm";
3204 }
3205
Jamie Madill98493dd2013-07-08 14:39:03 -04003206 return prefix + structLookup(structure.name());
Jamie Madill9cf6c072013-06-20 11:55:53 -04003207}
3208
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003209void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003210{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003211 if (name == "")
3212 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003213 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003214 }
3215
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003216 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
3217 {
3218 return; // Already added
3219 }
3220
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003221 TType ctorType = type;
3222 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003223 ctorType.setPrecision(EbpHigh);
3224 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003225
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003226 TString ctorName = type.getStruct() ? decorate(name) : name;
3227
3228 typedef std::vector<TType> ParameterArray;
3229 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003230
Jamie Madill98493dd2013-07-08 14:39:03 -04003231 const TStructure* structure = type.getStruct();
3232 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003233 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003234 mStructNames.insert(decorate(name));
3235
Jamie Madill98493dd2013-07-08 14:39:03 -04003236 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003237
Jamie Madill98493dd2013-07-08 14:39:03 -04003238 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003239 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003240 // Add row-major packed struct for interface blocks
3241 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003242 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003243 "#pragma pack_matrix(column_major)\n";
3244
Jamie Madillc835df62013-06-21 09:15:32 -04003245 const TString &std140Prefix = "std";
Jamie Madill98493dd2013-07-08 14:39:03 -04003246 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003247
3248 const TString &std140RowMajorPrefix = "std_rm";
3249 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003250 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003251 "#pragma pack_matrix(column_major)\n";
3252
Jamie Madill98493dd2013-07-08 14:39:03 -04003253 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003254 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003255 mStructDeclarations.push_back(std140String);
3256 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003257 }
3258
Jamie Madill98493dd2013-07-08 14:39:03 -04003259 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003260 for (unsigned int i = 0; i < fields.size(); i++)
3261 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003262 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003263 }
3264 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003265 else if (parameters)
3266 {
3267 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3268 {
3269 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3270 }
3271 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003272 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003273
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003274 TString constructor;
3275
3276 if (ctorType.getStruct())
3277 {
3278 constructor += ctorName + " " + ctorName + "_ctor(";
3279 }
3280 else // Built-in type
3281 {
3282 constructor += typeString(ctorType) + " " + ctorName + "(";
3283 }
3284
3285 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3286 {
3287 const TType &type = ctorParameters[parameter];
3288
3289 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3290
3291 if (parameter < ctorParameters.size() - 1)
3292 {
3293 constructor += ", ";
3294 }
3295 }
3296
3297 constructor += ")\n"
3298 "{\n";
3299
3300 if (ctorType.getStruct())
3301 {
3302 constructor += " " + ctorName + " structure = {";
3303 }
3304 else
3305 {
3306 constructor += " return " + typeString(ctorType) + "(";
3307 }
3308
3309 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3310 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003311 int rows = ctorType.getRows();
3312 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003313 const TType &parameter = ctorParameters[0];
3314
3315 if (parameter.isScalar())
3316 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003317 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003318 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003319 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003320 {
3321 constructor += TString((row == col) ? "x0" : "0.0");
3322
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003323 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003324 {
3325 constructor += ", ";
3326 }
3327 }
3328 }
3329 }
3330 else if (parameter.isMatrix())
3331 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003332 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003333 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003334 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003335 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003336 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003337 {
3338 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3339 }
3340 else
3341 {
3342 constructor += TString((row == col) ? "1.0" : "0.0");
3343 }
3344
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003345 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003346 {
3347 constructor += ", ";
3348 }
3349 }
3350 }
3351 }
3352 else UNREACHABLE();
3353 }
3354 else
3355 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003356 size_t remainingComponents = ctorType.getObjectSize();
3357 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003358
3359 while (remainingComponents > 0)
3360 {
3361 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003362 const size_t parameterSize = parameter.getObjectSize();
3363 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003364
3365 constructor += "x" + str(parameterIndex);
3366
3367 if (parameter.isScalar())
3368 {
3369 remainingComponents -= parameter.getObjectSize();
3370 }
3371 else if (parameter.isVector())
3372 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003373 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003374 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003375 ASSERT(parameterSize <= remainingComponents);
3376 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003377 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003378 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003379 {
3380 switch (remainingComponents)
3381 {
3382 case 1: constructor += ".x"; break;
3383 case 2: constructor += ".xy"; break;
3384 case 3: constructor += ".xyz"; break;
3385 case 4: constructor += ".xyzw"; break;
3386 default: UNREACHABLE();
3387 }
3388
3389 remainingComponents = 0;
3390 }
3391 else UNREACHABLE();
3392 }
3393 else if (parameter.isMatrix() || parameter.getStruct())
3394 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003395 ASSERT(remainingComponents == parameterSize || moreParameters);
3396 ASSERT(parameterSize <= remainingComponents);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003397
Jamie Madill94bf7f22013-07-08 13:31:15 -04003398 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003399 }
3400 else UNREACHABLE();
3401
3402 if (moreParameters)
3403 {
3404 parameterIndex++;
3405 }
3406
3407 if (remainingComponents)
3408 {
3409 constructor += ", ";
3410 }
3411 }
3412 }
3413
3414 if (ctorType.getStruct())
3415 {
3416 constructor += "};\n"
3417 " return structure;\n"
3418 "}\n";
3419 }
3420 else
3421 {
3422 constructor += ");\n"
3423 "}\n";
3424 }
3425
daniel@transgaming.com63691862010-04-29 03:32:42 +00003426 mConstructors.insert(constructor);
3427}
3428
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003429const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3430{
3431 TInfoSinkBase &out = mBody;
3432
Jamie Madill98493dd2013-07-08 14:39:03 -04003433 const TStructure* structure = type.getStruct();
3434 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003435 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003436 out << structLookup(structure->name()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003437
Jamie Madill98493dd2013-07-08 14:39:03 -04003438 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003439
Jamie Madill98493dd2013-07-08 14:39:03 -04003440 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003441 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003442 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003443
3444 constUnion = writeConstantUnion(*fieldType, constUnion);
3445
Jamie Madill98493dd2013-07-08 14:39:03 -04003446 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003447 {
3448 out << ", ";
3449 }
3450 }
3451
3452 out << ")";
3453 }
3454 else
3455 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003456 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003457 bool writeType = size > 1;
3458
3459 if (writeType)
3460 {
3461 out << typeString(type) << "(";
3462 }
3463
Jamie Madill94bf7f22013-07-08 13:31:15 -04003464 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003465 {
3466 switch (constUnion->getType())
3467 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003468 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003469 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003470 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003471 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003472 default: UNREACHABLE();
3473 }
3474
3475 if (i != size - 1)
3476 {
3477 out << ", ";
3478 }
3479 }
3480
3481 if (writeType)
3482 {
3483 out << ")";
3484 }
3485 }
3486
3487 return constUnion;
3488}
3489
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003490TString OutputHLSL::scopeString(unsigned int depthLimit)
3491{
3492 TString string;
3493
3494 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3495 {
3496 string += "_" + str(i);
3497 }
3498
3499 return string;
3500}
3501
3502TString OutputHLSL::scopedStruct(const TString &typeName)
3503{
3504 if (typeName == "")
3505 {
3506 return typeName;
3507 }
3508
3509 return typeName + scopeString(mScopeDepth);
3510}
3511
3512TString OutputHLSL::structLookup(const TString &typeName)
3513{
3514 for (int depth = mScopeDepth; depth >= 0; depth--)
3515 {
3516 TString scopedName = decorate(typeName + scopeString(depth));
3517
3518 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3519 {
3520 if (*structName == scopedName)
3521 {
3522 return scopedName;
3523 }
3524 }
3525 }
3526
3527 UNREACHABLE(); // Should have found a matching constructor
3528
3529 return typeName;
3530}
3531
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003532TString OutputHLSL::decorate(const TString &string)
3533{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003534 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003535 {
3536 return "_" + string;
3537 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003538
3539 return string;
3540}
3541
apatrick@chromium.org65756022012-01-17 21:45:38 +00003542TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003543{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003544 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003545 {
3546 return "ex_" + string;
3547 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003548
3549 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003550}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003551
Jamie Madill98493dd2013-07-08 14:39:03 -04003552TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003553{
Jamie Madill98493dd2013-07-08 14:39:03 -04003554 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003555 {
3556 return decorate(string);
3557 }
3558
3559 return string;
3560}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003561
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003562void OutputHLSL::declareInterfaceBlockField(const TType &type, const TString &name, std::vector<InterfaceBlockField>& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003563{
Jamie Madill98493dd2013-07-08 14:39:03 -04003564 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003565
3566 if (!structure)
3567 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003568 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003569 InterfaceBlockField field(glVariableType(type), glVariablePrecision(type), name.c_str(),
3570 (unsigned int)type.getArraySize(), isRowMajorMatrix);
3571 output.push_back(field);
3572 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003573 else
3574 {
Jamie Madill28167c62013-08-30 13:21:10 -04003575 InterfaceBlockField structField(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), false);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003576
3577 const TFieldList &fields = structure->fields();
3578
3579 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3580 {
3581 TField *field = fields[fieldIndex];
3582 TType *fieldType = field->type();
3583
3584 // make sure to copy matrix packing information
3585 fieldType->setLayoutQualifier(type.getLayoutQualifier());
3586
3587 declareInterfaceBlockField(*fieldType, field->name(), structField.fields);
3588 }
3589
3590 output.push_back(structField);
3591 }
3592}
3593
Jamie Madillc2141fb2013-08-30 13:21:08 -04003594Uniform OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, std::vector<Uniform>& output)
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003595{
3596 const TStructure *structure = type.getStruct();
3597
3598 if (!structure)
3599 {
3600 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
3601 Uniform uniform(glVariableType(type), glVariablePrecision(type), name.c_str(),
Jamie Madill56093782013-08-30 13:21:11 -04003602 (unsigned int)type.getArraySize(), (unsigned int)registerIndex, 0);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003603 output.push_back(uniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003604
3605 return uniform;
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003606 }
3607 else
3608 {
Jamie Madill56093782013-08-30 13:21:11 -04003609 Uniform structUniform(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(),
3610 (unsigned int)registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003611
Jamie Madill98493dd2013-07-08 14:39:03 -04003612 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003613
Jamie Madill98493dd2013-07-08 14:39:03 -04003614 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003615 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003616 TField *field = fields[fieldIndex];
3617 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003618
Jamie Madill56093782013-08-30 13:21:11 -04003619 declareUniformToList(*fieldType, field->name(), GL_INVALID_INDEX, structUniform.fields);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003620 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003621
Jamie Madill56093782013-08-30 13:21:11 -04003622 // assign register offset information -- this will override the information in any sub-structures.
3623 HLSLVariableGetRegisterInfo(registerIndex, &structUniform);
3624
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003625 output.push_back(structUniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003626
3627 return structUniform;
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003628 }
3629}
3630
Jamie Madill139b9092013-08-30 13:21:06 -04003631InterpolationType getInterpolationType(TQualifier qualifier)
3632{
3633 switch (qualifier)
3634 {
3635 case EvqFlatIn:
3636 case EvqFlatOut:
3637 return INTERPOLATION_FLAT;
3638
3639 case EvqSmoothIn:
3640 case EvqSmoothOut:
3641 case EvqVertexOut:
3642 case EvqFragmentIn:
Jamie Madill384b6042013-09-13 10:06:24 -04003643 case EvqVaryingIn:
3644 case EvqVaryingOut:
Jamie Madill139b9092013-08-30 13:21:06 -04003645 return INTERPOLATION_SMOOTH;
3646
3647 case EvqCentroidIn:
3648 case EvqCentroidOut:
3649 return INTERPOLATION_CENTROID;
3650
3651 default: UNREACHABLE();
3652 return INTERPOLATION_SMOOTH;
3653 }
3654}
3655
Jamie Madill94599662013-08-30 13:21:10 -04003656void OutputHLSL::declareVaryingToList(const TType &type, TQualifier baseTypeQualifier, const TString &name, std::vector<Varying>& fieldsOut)
Jamie Madill47fdd132013-08-30 13:21:04 -04003657{
3658 const TStructure *structure = type.getStruct();
3659
Jamie Madill94599662013-08-30 13:21:10 -04003660 InterpolationType interpolation = getInterpolationType(baseTypeQualifier);
Jamie Madill47fdd132013-08-30 13:21:04 -04003661 if (!structure)
3662 {
Jamie Madill139b9092013-08-30 13:21:06 -04003663 Varying varying(glVariableType(type), glVariablePrecision(type), name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003664 fieldsOut.push_back(varying);
3665 }
3666 else
3667 {
Jamie Madill28167c62013-08-30 13:21:10 -04003668 Varying structVarying(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003669 const TFieldList &fields = structure->fields();
3670
Jamie Madill28167c62013-08-30 13:21:10 -04003671 structVarying.structName = structure->name().c_str();
3672
Jamie Madill47fdd132013-08-30 13:21:04 -04003673 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3674 {
3675 const TField &field = *fields[fieldIndex];
Jamie Madill94599662013-08-30 13:21:10 -04003676 declareVaryingToList(*field.type(), baseTypeQualifier, field.name(), structVarying.fields);
Jamie Madill47fdd132013-08-30 13:21:04 -04003677 }
3678
3679 fieldsOut.push_back(structVarying);
3680 }
3681}
3682
Jamie Madillc2141fb2013-08-30 13:21:08 -04003683int OutputHLSL::declareUniformAndAssignRegister(const TType &type, const TString &name)
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003684{
Jamie Madillc2141fb2013-08-30 13:21:08 -04003685 int registerIndex = (IsSampler(type.getBasicType()) ? mSamplerRegister : mUniformRegister);
3686
3687 const Uniform &uniform = declareUniformToList(type, name, registerIndex, mActiveUniforms);
3688
3689 if (IsSampler(type.getBasicType()))
3690 {
3691 mSamplerRegister += HLSLVariableRegisterCount(uniform);
3692 }
3693 else
3694 {
3695 mUniformRegister += HLSLVariableRegisterCount(uniform);
3696 }
3697
3698 return registerIndex;
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003699}
3700
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003701GLenum OutputHLSL::glVariableType(const TType &type)
3702{
3703 if (type.getBasicType() == EbtFloat)
3704 {
3705 if (type.isScalar())
3706 {
3707 return GL_FLOAT;
3708 }
3709 else if (type.isVector())
3710 {
3711 switch(type.getNominalSize())
3712 {
3713 case 2: return GL_FLOAT_VEC2;
3714 case 3: return GL_FLOAT_VEC3;
3715 case 4: return GL_FLOAT_VEC4;
3716 default: UNREACHABLE();
3717 }
3718 }
3719 else if (type.isMatrix())
3720 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003721 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003722 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003723 case 2:
3724 switch(type.getRows())
3725 {
3726 case 2: return GL_FLOAT_MAT2;
3727 case 3: return GL_FLOAT_MAT2x3;
3728 case 4: return GL_FLOAT_MAT2x4;
3729 default: UNREACHABLE();
3730 }
3731
3732 case 3:
3733 switch(type.getRows())
3734 {
3735 case 2: return GL_FLOAT_MAT3x2;
3736 case 3: return GL_FLOAT_MAT3;
3737 case 4: return GL_FLOAT_MAT3x4;
3738 default: UNREACHABLE();
3739 }
3740
3741 case 4:
3742 switch(type.getRows())
3743 {
3744 case 2: return GL_FLOAT_MAT4x2;
3745 case 3: return GL_FLOAT_MAT4x3;
3746 case 4: return GL_FLOAT_MAT4;
3747 default: UNREACHABLE();
3748 }
3749
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003750 default: UNREACHABLE();
3751 }
3752 }
3753 else UNREACHABLE();
3754 }
3755 else if (type.getBasicType() == EbtInt)
3756 {
3757 if (type.isScalar())
3758 {
3759 return GL_INT;
3760 }
3761 else if (type.isVector())
3762 {
3763 switch(type.getNominalSize())
3764 {
3765 case 2: return GL_INT_VEC2;
3766 case 3: return GL_INT_VEC3;
3767 case 4: return GL_INT_VEC4;
3768 default: UNREACHABLE();
3769 }
3770 }
3771 else UNREACHABLE();
3772 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003773 else if (type.getBasicType() == EbtUInt)
3774 {
3775 if (type.isScalar())
3776 {
3777 return GL_UNSIGNED_INT;
3778 }
3779 else if (type.isVector())
3780 {
Jamie Madill22d63da2013-06-07 12:45:12 -04003781 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003782 {
3783 case 2: return GL_UNSIGNED_INT_VEC2;
3784 case 3: return GL_UNSIGNED_INT_VEC3;
3785 case 4: return GL_UNSIGNED_INT_VEC4;
3786 default: UNREACHABLE();
3787 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003788 }
3789 else UNREACHABLE();
3790 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003791 else if (type.getBasicType() == EbtBool)
3792 {
3793 if (type.isScalar())
3794 {
3795 return GL_BOOL;
3796 }
3797 else if (type.isVector())
3798 {
3799 switch(type.getNominalSize())
3800 {
3801 case 2: return GL_BOOL_VEC2;
3802 case 3: return GL_BOOL_VEC3;
3803 case 4: return GL_BOOL_VEC4;
3804 default: UNREACHABLE();
3805 }
3806 }
3807 else UNREACHABLE();
3808 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003809
3810 switch(type.getBasicType())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003811 {
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003812 case EbtSampler2D: return GL_SAMPLER_2D;
3813 case EbtSampler3D: return GL_SAMPLER_3D;
3814 case EbtSamplerCube: return GL_SAMPLER_CUBE;
3815 case EbtSampler2DArray: return GL_SAMPLER_2D_ARRAY;
3816 case EbtISampler2D: return GL_INT_SAMPLER_2D;
3817 case EbtISampler3D: return GL_INT_SAMPLER_3D;
3818 case EbtISamplerCube: return GL_INT_SAMPLER_CUBE;
3819 case EbtISampler2DArray: return GL_INT_SAMPLER_2D_ARRAY;
3820 case EbtUSampler2D: return GL_UNSIGNED_INT_SAMPLER_2D;
3821 case EbtUSampler3D: return GL_UNSIGNED_INT_SAMPLER_3D;
3822 case EbtUSamplerCube: return GL_UNSIGNED_INT_SAMPLER_CUBE;
3823 case EbtUSampler2DArray: return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3824 case EbtSampler2DShadow: return GL_SAMPLER_2D_SHADOW;
3825 case EbtSamplerCubeShadow: return GL_SAMPLER_CUBE_SHADOW;
3826 case EbtSampler2DArrayShadow: return GL_SAMPLER_2D_ARRAY_SHADOW;
3827 default: UNREACHABLE();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003828 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003829
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003830
3831 return GL_NONE;
3832}
3833
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003834GLenum OutputHLSL::glVariablePrecision(const TType &type)
3835{
3836 if (type.getBasicType() == EbtFloat)
3837 {
3838 switch (type.getPrecision())
3839 {
3840 case EbpHigh: return GL_HIGH_FLOAT;
3841 case EbpMedium: return GL_MEDIUM_FLOAT;
3842 case EbpLow: return GL_LOW_FLOAT;
3843 case EbpUndefined:
3844 // Should be defined as the default precision by the parser
3845 default: UNREACHABLE();
3846 }
3847 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003848 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003849 {
3850 switch (type.getPrecision())
3851 {
3852 case EbpHigh: return GL_HIGH_INT;
3853 case EbpMedium: return GL_MEDIUM_INT;
3854 case EbpLow: return GL_LOW_INT;
3855 case EbpUndefined:
3856 // Should be defined as the default precision by the parser
3857 default: UNREACHABLE();
3858 }
3859 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003860
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00003861 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003862 return GL_NONE;
3863}
3864
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003865bool OutputHLSL::isVaryingOut(TQualifier qualifier)
3866{
3867 switch(qualifier)
3868 {
3869 case EvqVaryingOut:
3870 case EvqInvariantVaryingOut:
3871 case EvqSmoothOut:
3872 case EvqFlatOut:
3873 case EvqCentroidOut:
Jamie Madill19571812013-08-12 15:26:34 -07003874 case EvqVertexOut:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003875 return true;
3876 }
3877
3878 return false;
3879}
3880
3881bool OutputHLSL::isVaryingIn(TQualifier qualifier)
3882{
3883 switch(qualifier)
3884 {
3885 case EvqVaryingIn:
3886 case EvqInvariantVaryingIn:
3887 case EvqSmoothIn:
3888 case EvqFlatIn:
3889 case EvqCentroidIn:
Jamie Madill19571812013-08-12 15:26:34 -07003890 case EvqFragmentIn:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003891 return true;
3892 }
3893
3894 return false;
3895}
3896
3897bool OutputHLSL::isVarying(TQualifier qualifier)
3898{
3899 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
3900}
3901
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003902}