blob: 860e29a6ca5e7223304542aa8a0be79f1649ff4b [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/OutputHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
alokp@chromium.org79fb1012012-04-26 21:07:39 +00009#include "common/angleutils.h"
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +000010#include "common/utilities.h"
Geoff Lang17732822013-08-29 13:46:49 -040011#include "compiler/translator/compilerdebug.h"
12#include "compiler/translator/InfoSink.h"
13#include "compiler/translator/DetectDiscontinuity.h"
14#include "compiler/translator/SearchSymbol.h"
15#include "compiler/translator/UnfoldShortCircuit.h"
16#include "compiler/translator/HLSLLayoutEncoder.h"
17#include "compiler/translator/FlagStd140Structs.h"
Jamie Madill3c9eeb92013-11-04 11:09:26 -050018#include "compiler/translator/NodeSearch.h"
Jamie Madille53c98b2014-02-03 11:57:13 -050019#include "compiler/translator/RewriteElseBlocks.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000020
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000021#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000022#include <cfloat>
23#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000024
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000025namespace sh
26{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000027
Nicolas Capense0ba27a2013-06-24 16:10:52 -040028TString OutputHLSL::TextureFunction::name() const
29{
30 TString name = "gl_texture";
31
Nicolas Capens6d232bb2013-07-08 15:56:38 -040032 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040033 {
34 name += "2D";
35 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040036 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040037 {
38 name += "3D";
39 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040040 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040041 {
42 name += "Cube";
43 }
44 else UNREACHABLE();
45
46 if (proj)
47 {
48 name += "Proj";
49 }
50
Nicolas Capensb1f45b72013-12-19 17:37:19 -050051 if (offset)
52 {
53 name += "Offset";
54 }
55
Nicolas Capens75fb4752013-07-10 15:14:47 -040056 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040057 {
Nicolas Capensfc014542014-02-18 14:47:13 -050058 case IMPLICIT: break;
59 case BIAS: break;
60 case LOD: name += "Lod"; break;
61 case LOD0: name += "Lod0"; break;
62 case SIZE: name += "Size"; break;
63 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -050064 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040065 default: UNREACHABLE();
66 }
67
68 return name + "(";
69}
70
Jamie Madillc2141fb2013-08-30 13:21:08 -040071const char *RegisterPrefix(const TType &type)
72{
73 if (IsSampler(type.getBasicType()))
74 {
75 return "s";
76 }
77 else
78 {
79 return "c";
80 }
81}
82
Nicolas Capense0ba27a2013-06-24 16:10:52 -040083bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
84{
85 if (sampler < rhs.sampler) return true;
86 if (coords < rhs.coords) return true;
87 if (!proj && rhs.proj) return true;
Nicolas Capens75fb4752013-07-10 15:14:47 -040088 if (method < rhs.method) return true;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040089
90 return false;
91}
92
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +000093OutputHLSL::OutputHLSL(TParseContext &context, const ShBuiltInResources& resources, ShShaderOutput outputType)
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +000094 : TIntermTraverser(true, true, true), mContext(context), mOutputType(outputType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000096 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +000097 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000098
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +000099 mUsesFragColor = false;
100 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000101 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000102 mUsesFragCoord = false;
103 mUsesPointCoord = false;
104 mUsesFrontFacing = false;
105 mUsesPointSize = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400106 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000107 mUsesXor = false;
108 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000109 mUsesMod2v = false;
110 mUsesMod2f = false;
111 mUsesMod3v = false;
112 mUsesMod3f = false;
113 mUsesMod4v = false;
114 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000115 mUsesFaceforward1 = false;
116 mUsesFaceforward2 = false;
117 mUsesFaceforward3 = false;
118 mUsesFaceforward4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000119 mUsesAtan2_1 = false;
120 mUsesAtan2_2 = false;
121 mUsesAtan2_3 = false;
122 mUsesAtan2_4 = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500123 mUsesDiscardRewriting = 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
Jamie Madille53c98b2014-02-03 11:57:13 -0500169 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
170 // use a vertex attribute as a condition, and some related computation in the else block.
171 if (mOutputType == SH_HLSL9_OUTPUT && mContext.shaderType == SH_VERTEX_SHADER)
172 {
173 RewriteElseBlocks(mContext.treeRoot);
174 }
175
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000176 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 +0000177 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000178
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000179 mContext.infoSink().obj << mHeader.c_str();
180 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000181}
182
Jamie Madill570e04d2013-06-21 09:15:33 -0400183void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
184{
185 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
186 {
187 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
188
189 // This will mark the necessary block elements as referenced
190 flaggedNode->traverse(this);
191 TString structName(mBody.c_str());
192 mBody.erase();
193
194 mFlaggedStructOriginalNames[flaggedNode] = structName;
195
196 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
197 {
198 structName.erase(pos, 1);
199 }
200
201 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
202 }
203}
204
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000205TInfoSinkBase &OutputHLSL::getBodyStream()
206{
207 return mBody;
208}
209
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400210const std::vector<Uniform> &OutputHLSL::getUniforms()
daniel@transgaming.com043da132012-12-20 21:12:22 +0000211{
212 return mActiveUniforms;
213}
214
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000215const ActiveInterfaceBlocks &OutputHLSL::getInterfaceBlocks() const
216{
217 return mActiveInterfaceBlocks;
218}
219
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400220const std::vector<Attribute> &OutputHLSL::getOutputVariables() const
Jamie Madill46131a32013-06-20 11:55:50 -0400221{
222 return mActiveOutputVariables;
223}
224
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400225const std::vector<Attribute> &OutputHLSL::getAttributes() const
Jamie Madilldefb6742013-06-20 11:55:51 -0400226{
227 return mActiveAttributes;
228}
229
Jamie Madill47fdd132013-08-30 13:21:04 -0400230const std::vector<Varying> &OutputHLSL::getVaryings() const
231{
232 return mActiveVaryings;
233}
234
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000235int OutputHLSL::vectorSize(const TType &type) const
236{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000237 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000238 int arraySize = type.isArray() ? type.getArraySize() : 1;
239
240 return elementSize * arraySize;
241}
242
Jamie Madill98493dd2013-07-08 14:39:03 -0400243TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, const TField &field)
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000244{
Jamie Madill98493dd2013-07-08 14:39:03 -0400245 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000246 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400247 return interfaceBlock.name() + "." + field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000248 }
249 else
250 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400251 return field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000252 }
253}
254
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000255TString OutputHLSL::decoratePrivate(const TString &privateText)
256{
257 return "dx_" + privateText;
258}
259
Jamie Madill98493dd2013-07-08 14:39:03 -0400260TString OutputHLSL::interfaceBlockStructNameString(const TInterfaceBlock &interfaceBlock)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000261{
Jamie Madill98493dd2013-07-08 14:39:03 -0400262 return decoratePrivate(interfaceBlock.name()) + "_type";
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000263}
264
Jamie Madill98493dd2013-07-08 14:39:03 -0400265TString OutputHLSL::interfaceBlockInstanceString(const TInterfaceBlock& interfaceBlock, unsigned int arrayIndex)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000266{
Jamie Madill98493dd2013-07-08 14:39:03 -0400267 if (!interfaceBlock.hasInstanceName())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000268 {
269 return "";
270 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400271 else if (interfaceBlock.isArray())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000272 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400273 return decoratePrivate(interfaceBlock.instanceName()) + "_" + str(arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000274 }
275 else
276 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400277 return decorate(interfaceBlock.instanceName());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000278 }
279}
280
Jamie Madill98493dd2013-07-08 14:39:03 -0400281TString OutputHLSL::interfaceBlockFieldTypeString(const TField &field, TLayoutBlockStorage blockStorage)
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000282{
Jamie Madill98493dd2013-07-08 14:39:03 -0400283 const TType &fieldType = *field.type();
284 const TLayoutMatrixPacking matrixPacking = fieldType.getLayoutQualifier().matrixPacking;
Jamie Madill529077d2013-06-20 11:55:54 -0400285 ASSERT(matrixPacking != EmpUnspecified);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000286
Jamie Madill98493dd2013-07-08 14:39:03 -0400287 if (fieldType.isMatrix())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000288 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400289 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill529077d2013-06-20 11:55:54 -0400290 const TString &matrixPackString = (matrixPacking == EmpRowMajor ? "column_major" : "row_major");
Jamie Madill98493dd2013-07-08 14:39:03 -0400291 return matrixPackString + " " + typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000292 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400293 else if (fieldType.getStruct())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000294 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400295 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill98493dd2013-07-08 14:39:03 -0400296 return structureTypeName(*fieldType.getStruct(), matrixPacking == EmpColumnMajor, blockStorage == EbsStd140);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000297 }
298 else
299 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400300 return typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000301 }
302}
303
Jamie Madill98493dd2013-07-08 14:39:03 -0400304TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, TLayoutBlockStorage blockStorage)
305{
306 TString hlsl;
307
308 int elementIndex = 0;
309
310 for (unsigned int typeIndex = 0; typeIndex < interfaceBlock.fields().size(); typeIndex++)
311 {
312 const TField &field = *interfaceBlock.fields()[typeIndex];
313 const TType &fieldType = *field.type();
314
315 if (blockStorage == EbsStd140)
316 {
317 // 2 and 3 component vector types in some cases need pre-padding
318 hlsl += std140PrePaddingString(fieldType, &elementIndex);
319 }
320
321 hlsl += " " + interfaceBlockFieldTypeString(field, blockStorage) +
322 " " + decorate(field.name()) + arrayString(fieldType) + ";\n";
323
324 // must pad out after matrices and arrays, where HLSL usually allows itself room to pack stuff
325 if (blockStorage == EbsStd140)
326 {
327 const bool useHLSLRowMajorPacking = (fieldType.getLayoutQualifier().matrixPacking == EmpColumnMajor);
328 hlsl += std140PostPaddingString(fieldType, useHLSLRowMajorPacking);
329 }
330 }
331
332 return hlsl;
333}
334
335TString OutputHLSL::interfaceBlockStructString(const TInterfaceBlock &interfaceBlock)
336{
337 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
338
339 return "struct " + interfaceBlockStructNameString(interfaceBlock) + "\n"
340 "{\n" +
341 interfaceBlockFieldString(interfaceBlock, blockStorage) +
342 "};\n\n";
343}
344
345TString OutputHLSL::interfaceBlockString(const TInterfaceBlock &interfaceBlock, unsigned int registerIndex, unsigned int arrayIndex)
346{
347 const TString &arrayIndexString = (arrayIndex != GL_INVALID_INDEX ? decorate(str(arrayIndex)) : "");
348 const TString &blockName = interfaceBlock.name() + arrayIndexString;
349 TString hlsl;
350
351 hlsl += "cbuffer " + blockName + " : register(b" + str(registerIndex) + ")\n"
352 "{\n";
353
354 if (interfaceBlock.hasInstanceName())
355 {
356 hlsl += " " + interfaceBlockStructNameString(interfaceBlock) + " " + interfaceBlockInstanceString(interfaceBlock, arrayIndex) + ";\n";
357 }
358 else
359 {
360 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
361 hlsl += interfaceBlockFieldString(interfaceBlock, blockStorage);
362 }
363
364 hlsl += "};\n\n";
365
366 return hlsl;
367}
368
Jamie Madill574d9dd2013-06-20 11:55:56 -0400369TString OutputHLSL::std140PrePaddingString(const TType &type, int *elementIndex)
370{
371 if (type.getBasicType() == EbtStruct || type.isMatrix() || type.isArray())
372 {
373 // no padding needed, HLSL will align the field to a new register
374 *elementIndex = 0;
375 return "";
376 }
377
378 const GLenum glType = glVariableType(type);
379 const int numComponents = gl::UniformComponentCount(glType);
380
381 if (numComponents >= 4)
382 {
383 // no padding needed, HLSL will align the field to a new register
384 *elementIndex = 0;
385 return "";
386 }
387
388 if (*elementIndex + numComponents > 4)
389 {
390 // no padding needed, HLSL will align the field to a new register
391 *elementIndex = numComponents;
392 return "";
393 }
394
395 TString padding;
396
397 const int alignment = numComponents == 3 ? 4 : numComponents;
398 const int paddingOffset = (*elementIndex % alignment);
399
400 if (paddingOffset != 0)
401 {
402 // padding is neccessary
403 for (int paddingIndex = paddingOffset; paddingIndex < alignment; paddingIndex++)
404 {
405 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
406 }
407
408 *elementIndex += (alignment - paddingOffset);
409 }
410
411 *elementIndex += numComponents;
412 *elementIndex %= 4;
413
414 return padding;
415}
416
Jamie Madille4075c92013-06-21 09:15:32 -0400417TString OutputHLSL::std140PostPaddingString(const TType &type, bool useHLSLRowMajorPacking)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400418{
Jamie Madillc835df62013-06-21 09:15:32 -0400419 if (!type.isMatrix() && !type.isArray() && type.getBasicType() != EbtStruct)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400420 {
421 return "";
422 }
423
Jamie Madill574d9dd2013-06-20 11:55:56 -0400424 int numComponents = 0;
425
426 if (type.isMatrix())
427 {
Jamie Madille4075c92013-06-21 09:15:32 -0400428 // This method can also be called from structureString, which does not use layout qualifiers.
429 // Thus, use the method parameter for determining the matrix packing.
430 //
431 // Note HLSL row major packing corresponds to GL API column-major, and vice-versa, since we
432 // wish to always transpose GL matrices to play well with HLSL's matrix array indexing.
433 //
434 const bool isRowMajorMatrix = !useHLSLRowMajorPacking;
Jamie Madillc835df62013-06-21 09:15:32 -0400435 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400436 numComponents = gl::MatrixComponentCount(glType, isRowMajorMatrix);
437 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400438 else if (type.getStruct())
Jamie Madillc835df62013-06-21 09:15:32 -0400439 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400440 const TString &structName = structureTypeName(*type.getStruct(), useHLSLRowMajorPacking, true);
Jamie Madille4075c92013-06-21 09:15:32 -0400441 numComponents = mStd140StructElementIndexes[structName];
442
443 if (numComponents == 0)
444 {
445 return "";
446 }
Jamie Madillc835df62013-06-21 09:15:32 -0400447 }
Jamie Madill574d9dd2013-06-20 11:55:56 -0400448 else
449 {
Jamie Madillc835df62013-06-21 09:15:32 -0400450 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400451 numComponents = gl::UniformComponentCount(glType);
452 }
453
454 TString padding;
455 for (int paddingOffset = numComponents; paddingOffset < 4; paddingOffset++)
456 {
457 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
458 }
459 return padding;
460}
461
Jamie Madill440dc742013-06-20 11:55:55 -0400462// Use the same layout for packed and shared
463void setBlockLayout(InterfaceBlock *interfaceBlock, BlockLayoutType newLayout)
464{
465 interfaceBlock->layout = newLayout;
466 interfaceBlock->blockInfo.clear();
467
468 switch (newLayout)
469 {
470 case BLOCKLAYOUT_SHARED:
471 case BLOCKLAYOUT_PACKED:
472 {
473 HLSLBlockEncoder hlslEncoder(&interfaceBlock->blockInfo);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400474 hlslEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400475 interfaceBlock->dataSize = hlslEncoder.getBlockSize();
476 }
477 break;
478
479 case BLOCKLAYOUT_STANDARD:
480 {
481 Std140BlockEncoder stdEncoder(&interfaceBlock->blockInfo);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400482 stdEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400483 interfaceBlock->dataSize = stdEncoder.getBlockSize();
484 }
485 break;
486
487 default:
488 UNREACHABLE();
489 break;
490 }
491}
492
Jamie Madill574d9dd2013-06-20 11:55:56 -0400493BlockLayoutType convertBlockLayoutType(TLayoutBlockStorage blockStorage)
494{
495 switch (blockStorage)
496 {
497 case EbsPacked: return BLOCKLAYOUT_PACKED;
498 case EbsShared: return BLOCKLAYOUT_SHARED;
499 case EbsStd140: return BLOCKLAYOUT_STANDARD;
500 default: UNREACHABLE(); return BLOCKLAYOUT_SHARED;
501 }
502}
503
Jamie Madill98493dd2013-07-08 14:39:03 -0400504TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400505{
506 TString init;
507
508 TString preIndentString;
509 TString fullIndentString;
510
511 for (int spaces = 0; spaces < (indent * 4); spaces++)
512 {
513 preIndentString += ' ';
514 }
515
516 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
517 {
518 fullIndentString += ' ';
519 }
520
521 init += preIndentString + "{\n";
522
Jamie Madill98493dd2013-07-08 14:39:03 -0400523 const TFieldList &fields = structure.fields();
524 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400525 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400526 const TField &field = *fields[fieldIndex];
527 const TString &fieldName = rhsStructName + "." + decorate(field.name());
528 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400529
Jamie Madill98493dd2013-07-08 14:39:03 -0400530 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400531 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400532 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400533 }
534 else
535 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400536 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400537 }
538 }
539
540 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
541
542 return init;
543}
544
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000545void OutputHLSL::header()
546{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000547 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000548
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000549 TString uniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000550 TString interfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000551 TString varyings;
552 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400553 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000554
Jamie Madillc2141fb2013-08-30 13:21:08 -0400555 for (ReferencedSymbols::const_iterator uniformIt = mReferencedUniforms.begin(); uniformIt != mReferencedUniforms.end(); uniformIt++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000556 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400557 const TIntermSymbol &uniform = *uniformIt->second;
558 const TType &type = uniform.getType();
559 const TString &name = uniform.getSymbol();
560
561 int registerIndex = declareUniformAndAssignRegister(type, name);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000562
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000563 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture
564 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400565 uniforms += "uniform " + samplerString(type) + " sampler_" + decorateUniform(name, type) + arrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400566 " : register(s" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000567
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000568 uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400569 " : register(t" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000570 }
571 else
572 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400573 const TStructure *structure = type.getStruct();
574 const TString &typeName = (structure ? structureTypeName(*structure, false, false) : typeString(type));
575
576 const TString &registerString = TString("register(") + RegisterPrefix(type) + str(registerIndex) + ")";
577
578 uniforms += "uniform " + typeName + " " + decorateUniform(name, type) + arrayString(type) + " : " + registerString + ";\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000579 }
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000580 }
581
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000582 for (ReferencedSymbols::const_iterator interfaceBlockIt = mReferencedInterfaceBlocks.begin(); interfaceBlockIt != mReferencedInterfaceBlocks.end(); interfaceBlockIt++)
583 {
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000584 const TType &nodeType = interfaceBlockIt->second->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -0400585 const TInterfaceBlock &interfaceBlock = *nodeType.getInterfaceBlock();
586 const TFieldList &fieldList = interfaceBlock.fields();
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000587
Jamie Madill98493dd2013-07-08 14:39:03 -0400588 unsigned int arraySize = static_cast<unsigned int>(interfaceBlock.arraySize());
589 sh::InterfaceBlock activeBlock(interfaceBlock.name().c_str(), arraySize, mInterfaceBlockRegister);
590 for (unsigned int typeIndex = 0; typeIndex < fieldList.size(); typeIndex++)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000591 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400592 const TField &field = *fieldList[typeIndex];
593 const TString &fullUniformName = interfaceBlockFieldString(interfaceBlock, field);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400594 declareInterfaceBlockField(*field.type(), fullUniformName, activeBlock.fields);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000595 }
596
Jamie Madill98493dd2013-07-08 14:39:03 -0400597 mInterfaceBlockRegister += std::max(1u, arraySize);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000598
Jamie Madill98493dd2013-07-08 14:39:03 -0400599 BlockLayoutType blockLayoutType = convertBlockLayoutType(interfaceBlock.blockStorage());
600 setBlockLayout(&activeBlock, blockLayoutType);
Jamie Madill9060a4e2013-08-12 16:22:57 -0700601
602 if (interfaceBlock.matrixPacking() == EmpRowMajor)
603 {
604 activeBlock.isRowMajorLayout = true;
605 }
606
Jamie Madill98493dd2013-07-08 14:39:03 -0400607 mActiveInterfaceBlocks.push_back(activeBlock);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000608
Jamie Madill98493dd2013-07-08 14:39:03 -0400609 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000610 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400611 interfaceBlocks += interfaceBlockStructString(interfaceBlock);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000612 }
613
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000614 if (arraySize > 0)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000615 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000616 for (unsigned int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
617 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400618 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex + arrayIndex, arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000619 }
620 }
621 else
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000622 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400623 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000624 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000625 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000626
Jamie Madill829f59e2013-11-13 19:40:54 -0500627 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400628 {
629 TIntermTyped *structNode = flaggedStructIt->first;
630 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400631 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400632 const TString &originalName = mFlaggedStructOriginalNames[structNode];
633
Jamie Madill98493dd2013-07-08 14:39:03 -0400634 flaggedStructs += "static " + decorate(structure.name()) + " " + mappedName + " =\n";
635 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400636 flaggedStructs += "\n";
637 }
638
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000639 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
640 {
641 const TType &type = varying->second->getType();
642 const TString &name = varying->second->getSymbol();
643
644 // Program linking depends on this exact format
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +0000645 varyings += "static " + interpolationString(type.getQualifier()) + " " + typeString(type) + " " +
646 decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madill47fdd132013-08-30 13:21:04 -0400647
Jamie Madill94599662013-08-30 13:21:10 -0400648 declareVaryingToList(type, type.getQualifier(), name, mActiveVaryings);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000649 }
650
651 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
652 {
653 const TType &type = attribute->second->getType();
654 const TString &name = attribute->second->getSymbol();
655
656 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400657
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400658 Attribute attributeVar(glVariableType(type), glVariablePrecision(type), name.c_str(),
659 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
660 mActiveAttributes.push_back(attributeVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000661 }
662
Jamie Madill529077d2013-06-20 11:55:54 -0400663 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
664 {
665 out << *structDeclaration;
666 }
667
668 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
669 {
670 out << *constructor;
671 }
672
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500673 if (mUsesDiscardRewriting)
674 {
675 out << "#define ANGLE_USES_DISCARD_REWRITING" << "\n";
676 }
677
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400678 if (mContext.shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000679 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000680 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000681 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000682
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000683 out << "// Varyings\n";
684 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400685 out << "\n";
686
687 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000688 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500689 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000690 {
Jamie Madill46131a32013-06-20 11:55:50 -0400691 const TString &variableName = outputVariableIt->first;
692 const TType &variableType = outputVariableIt->second->getType();
693 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
694
695 out << "static " + typeString(variableType) + " out_" + variableName + arrayString(variableType) +
696 " = " + initializer(variableType) + ";\n";
697
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400698 Attribute outputVar(glVariableType(variableType), glVariablePrecision(variableType), variableName.c_str(),
699 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
Jamie Madill46131a32013-06-20 11:55:50 -0400700 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000701 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000702 }
Jamie Madill46131a32013-06-20 11:55:50 -0400703 else
704 {
705 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
706
707 out << "static float4 gl_Color[" << numColorValues << "] =\n"
708 "{\n";
709 for (unsigned int i = 0; i < numColorValues; i++)
710 {
711 out << " float4(0, 0, 0, 0)";
712 if (i + 1 != numColorValues)
713 {
714 out << ",";
715 }
716 out << "\n";
717 }
718
719 out << "};\n";
720 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000721
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400722 if (mUsesFragDepth)
723 {
724 out << "static float gl_Depth = 0.0;\n";
725 }
726
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000727 if (mUsesFragCoord)
728 {
729 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
730 }
731
732 if (mUsesPointCoord)
733 {
734 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
735 }
736
737 if (mUsesFrontFacing)
738 {
739 out << "static bool gl_FrontFacing = false;\n";
740 }
741
742 out << "\n";
743
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000744 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000745 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000746 out << "struct gl_DepthRangeParameters\n"
747 "{\n"
748 " float near;\n"
749 " float far;\n"
750 " float diff;\n"
751 "};\n"
752 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000753 }
754
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000755 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000756 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000757 out << "cbuffer DriverConstants : register(b1)\n"
758 "{\n";
759
760 if (mUsesDepthRange)
761 {
762 out << " float3 dx_DepthRange : packoffset(c0);\n";
763 }
764
765 if (mUsesFragCoord)
766 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000767 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000768 }
769
770 if (mUsesFragCoord || mUsesFrontFacing)
771 {
772 out << " float3 dx_DepthFront : packoffset(c2);\n";
773 }
774
775 out << "};\n";
776 }
777 else
778 {
779 if (mUsesDepthRange)
780 {
781 out << "uniform float3 dx_DepthRange : register(c0);";
782 }
783
784 if (mUsesFragCoord)
785 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000786 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000787 }
788
789 if (mUsesFragCoord || mUsesFrontFacing)
790 {
791 out << "uniform float3 dx_DepthFront : register(c2);\n";
792 }
793 }
794
795 out << "\n";
796
797 if (mUsesDepthRange)
798 {
799 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
800 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000801 }
802
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000803 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000804 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000805
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000806 if (!interfaceBlocks.empty())
807 {
808 out << interfaceBlocks;
809 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400810
811 if (!flaggedStructs.empty())
812 {
813 out << "// Std140 Structures accessed by value\n";
814 out << "\n";
815 out << flaggedStructs;
816 out << "\n";
817 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000818 }
819
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000820 if (usingMRTExtension && mNumRenderTargets > 1)
821 {
822 out << "#define GL_USES_MRT\n";
823 }
824
825 if (mUsesFragColor)
826 {
827 out << "#define GL_USES_FRAG_COLOR\n";
828 }
829
830 if (mUsesFragData)
831 {
832 out << "#define GL_USES_FRAG_DATA\n";
833 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000834 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000835 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000836 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000837 out << "// Attributes\n";
838 out << attributes;
839 out << "\n"
840 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
841
842 if (mUsesPointSize)
843 {
844 out << "static float gl_PointSize = float(1);\n";
845 }
846
847 out << "\n"
848 "// Varyings\n";
849 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000850 out << "\n";
851
852 if (mUsesDepthRange)
853 {
854 out << "struct gl_DepthRangeParameters\n"
855 "{\n"
856 " float near;\n"
857 " float far;\n"
858 " float diff;\n"
859 "};\n"
860 "\n";
861 }
862
863 if (mOutputType == SH_HLSL11_OUTPUT)
864 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000865 if (mUsesDepthRange)
866 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000867 out << "cbuffer DriverConstants : register(b1)\n"
868 "{\n"
869 " float3 dx_DepthRange : packoffset(c0);\n"
870 "};\n"
871 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000872 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000873 }
874 else
875 {
876 if (mUsesDepthRange)
877 {
878 out << "uniform float3 dx_DepthRange : register(c0);\n";
879 }
880
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000881 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000882 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000883 }
884
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000885 if (mUsesDepthRange)
886 {
887 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
888 "\n";
889 }
890
891 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000893
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000894 if (!interfaceBlocks.empty())
895 {
896 out << interfaceBlocks;
897 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400898
899 if (!flaggedStructs.empty())
900 {
901 out << "// Std140 Structures accessed by value\n";
902 out << "\n";
903 out << flaggedStructs;
904 out << "\n";
905 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000906 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400907 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000908
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400909 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
910 {
911 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400912 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000913 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400914 switch(textureFunction->sampler)
915 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400916 case EbtSampler2D: out << "int2 "; break;
917 case EbtSampler3D: out << "int3 "; break;
918 case EbtSamplerCube: out << "int2 "; break;
919 case EbtSampler2DArray: out << "int3 "; break;
920 case EbtISampler2D: out << "int2 "; break;
921 case EbtISampler3D: out << "int3 "; break;
922 case EbtISamplerCube: out << "int2 "; break;
923 case EbtISampler2DArray: out << "int3 "; break;
924 case EbtUSampler2D: out << "int2 "; break;
925 case EbtUSampler3D: out << "int3 "; break;
926 case EbtUSamplerCube: out << "int2 "; break;
927 case EbtUSampler2DArray: out << "int3 "; break;
928 case EbtSampler2DShadow: out << "int2 "; break;
929 case EbtSamplerCubeShadow: out << "int2 "; break;
930 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400931 default: UNREACHABLE();
932 }
933 }
934 else // Sampling function
935 {
936 switch(textureFunction->sampler)
937 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400938 case EbtSampler2D: out << "float4 "; break;
939 case EbtSampler3D: out << "float4 "; break;
940 case EbtSamplerCube: out << "float4 "; break;
941 case EbtSampler2DArray: out << "float4 "; break;
942 case EbtISampler2D: out << "int4 "; break;
943 case EbtISampler3D: out << "int4 "; break;
944 case EbtISamplerCube: out << "int4 "; break;
945 case EbtISampler2DArray: out << "int4 "; break;
946 case EbtUSampler2D: out << "uint4 "; break;
947 case EbtUSampler3D: out << "uint4 "; break;
948 case EbtUSamplerCube: out << "uint4 "; break;
949 case EbtUSampler2DArray: out << "uint4 "; break;
950 case EbtSampler2DShadow: out << "float "; break;
951 case EbtSamplerCubeShadow: out << "float "; break;
952 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400953 default: UNREACHABLE();
954 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000955 }
956
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400957 // Function name
958 out << textureFunction->name();
959
960 // Argument list
961 int hlslCoords = 4;
962
963 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000964 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400965 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000966 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400967 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
968 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
969 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000970 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400971
Nicolas Capens75fb4752013-07-10 15:14:47 -0400972 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000973 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400974 case TextureFunction::IMPLICIT: break;
975 case TextureFunction::BIAS: hlslCoords = 4; break;
976 case TextureFunction::LOD: hlslCoords = 4; break;
977 case TextureFunction::LOD0: hlslCoords = 4; break;
978 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000979 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400980 }
981 else if (mOutputType == SH_HLSL11_OUTPUT)
982 {
983 switch(textureFunction->sampler)
984 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400985 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
986 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
987 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
988 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
989 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
990 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
991 case EbtISamplerCube: out << "TextureCube<int4> x, SamplerState s"; hlslCoords = 3; break;
992 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
993 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
994 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
995 case EbtUSamplerCube: out << "TextureCube<uint4> x, SamplerState s"; hlslCoords = 3; break;
996 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
997 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
998 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
999 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001000 default: UNREACHABLE();
1001 }
1002 }
1003 else UNREACHABLE();
1004
Nicolas Capensfc014542014-02-18 14:47:13 -05001005 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001006 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001007 switch(textureFunction->coords)
1008 {
1009 case 2: out << ", int2 t"; break;
1010 case 3: out << ", int3 t"; break;
1011 default: UNREACHABLE();
1012 }
1013 }
1014 else // Floating-point coordinates (except textureSize)
1015 {
1016 switch(textureFunction->coords)
1017 {
1018 case 1: out << ", int lod"; break; // textureSize()
1019 case 2: out << ", float2 t"; break;
1020 case 3: out << ", float3 t"; break;
1021 case 4: out << ", float4 t"; break;
1022 default: UNREACHABLE();
1023 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001024 }
1025
Nicolas Capensd11d5492014-02-19 17:06:10 -05001026 if (textureFunction->method == TextureFunction::GRAD)
1027 {
1028 switch(textureFunction->sampler)
1029 {
1030 case EbtSampler2D:
1031 case EbtISampler2D:
1032 case EbtUSampler2D:
1033 case EbtSampler2DArray:
1034 case EbtISampler2DArray:
1035 case EbtUSampler2DArray:
1036 case EbtSampler2DShadow:
1037 case EbtSampler2DArrayShadow:
1038 out << ", float2 ddx, float2 ddy";
1039 break;
1040 case EbtSampler3D:
1041 case EbtISampler3D:
1042 case EbtUSampler3D:
1043 case EbtSamplerCube:
1044 case EbtISamplerCube:
1045 case EbtUSamplerCube:
1046 case EbtSamplerCubeShadow:
1047 out << ", float3 ddx, float3 ddy";
1048 break;
1049 default: UNREACHABLE();
1050 }
1051 }
1052
Nicolas Capens75fb4752013-07-10 15:14:47 -04001053 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +00001054 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001055 case TextureFunction::IMPLICIT: break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001056 case TextureFunction::BIAS: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001057 case TextureFunction::LOD: out << ", float lod"; break;
1058 case TextureFunction::LOD0: break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001059 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -05001060 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -05001061 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001062 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +00001063 }
1064
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001065 if (textureFunction->offset)
1066 {
1067 switch(textureFunction->sampler)
1068 {
1069 case EbtSampler2D: out << ", int2 offset"; break;
1070 case EbtSampler3D: out << ", int3 offset"; break;
1071 case EbtSampler2DArray: out << ", int2 offset"; break;
1072 case EbtISampler2D: out << ", int2 offset"; break;
1073 case EbtISampler3D: out << ", int3 offset"; break;
1074 case EbtISampler2DArray: out << ", int2 offset"; break;
1075 case EbtUSampler2D: out << ", int2 offset"; break;
1076 case EbtUSampler3D: out << ", int3 offset"; break;
1077 case EbtUSampler2DArray: out << ", int2 offset"; break;
1078 case EbtSampler2DShadow: out << ", int2 offset"; break;
1079 default: UNREACHABLE();
1080 }
1081 }
1082
1083 if (textureFunction->method == TextureFunction::BIAS)
1084 {
1085 out << ", float bias";
1086 }
1087
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001088 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001089 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001090
Nicolas Capens75fb4752013-07-10 15:14:47 -04001091 if (textureFunction->method == TextureFunction::SIZE)
1092 {
1093 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
1094 {
1095 if (IsSamplerArray(textureFunction->sampler))
1096 {
1097 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
1098 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
1099 }
1100 else
1101 {
1102 out << " uint width; uint height; uint numberOfLevels;\n"
1103 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
1104 }
1105 }
1106 else if (IsSampler3D(textureFunction->sampler))
1107 {
1108 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
1109 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
1110 }
1111 else UNREACHABLE();
1112
1113 switch(textureFunction->sampler)
1114 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04001115 case EbtSampler2D: out << " return int2(width, height);"; break;
1116 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
1117 case EbtSamplerCube: out << " return int2(width, height);"; break;
1118 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
1119 case EbtISampler2D: out << " return int2(width, height);"; break;
1120 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
1121 case EbtISamplerCube: out << " return int2(width, height);"; break;
1122 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
1123 case EbtUSampler2D: out << " return int2(width, height);"; break;
1124 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
1125 case EbtUSamplerCube: out << " return int2(width, height);"; break;
1126 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
1127 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
1128 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
1129 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001130 default: UNREACHABLE();
1131 }
1132 }
1133 else if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
daniel@transgaming.com15795192011-05-11 15:36:20 +00001134 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001135 // Currently unsupported because TextureCube does not support Load
1136 // This will require emulation using a Texture2DArray with 6 faces
1137 if (textureFunction->sampler == EbtISamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001138 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001139 out << " return int4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001140 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001141 else if (textureFunction->sampler == EbtUSamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001142 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001143 out << " return uint4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001144 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001145 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001146 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001147 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001148 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001149 if (IsIntegerSampler(textureFunction->sampler) &&
1150 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001151 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001152 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001153 {
Nicolas Capens93e50de2013-07-09 13:46:28 -04001154 if (IsSamplerArray(textureFunction->sampler))
1155 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001156 out << " float width; float height; float layers; float levels;\n";
1157
1158 if (textureFunction->method == TextureFunction::LOD0)
1159 {
1160 out << " uint mip = 0;\n";
1161 }
1162 else
1163 {
1164 if (textureFunction->method == TextureFunction::IMPLICIT ||
1165 textureFunction->method == TextureFunction::BIAS)
1166 {
1167 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1168 " float2 tSized = float2(t.x * width, t.y * height);\n"
1169 " float dx = length(ddx(tSized));\n"
1170 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001171 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001172
1173 if (textureFunction->method == TextureFunction::BIAS)
1174 {
1175 out << " lod += bias;\n";
1176 }
1177 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001178 else if (textureFunction->method == TextureFunction::GRAD)
1179 {
1180 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1181 " float lod = log2(max(length(ddx), length(ddy)));\n";
1182 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001183
1184 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1185 }
1186
1187 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001188 }
1189 else
1190 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001191 out << " float width; float height; float levels;\n";
1192
1193 if (textureFunction->method == TextureFunction::LOD0)
1194 {
1195 out << " uint mip = 0;\n";
1196 }
1197 else
1198 {
1199 if (textureFunction->method == TextureFunction::IMPLICIT ||
1200 textureFunction->method == TextureFunction::BIAS)
1201 {
1202 out << " x.GetDimensions(0, width, height, levels);\n"
1203 " float2 tSized = float2(t.x * width, t.y * height);\n"
1204 " float dx = length(ddx(tSized));\n"
1205 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001206 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001207
1208 if (textureFunction->method == TextureFunction::BIAS)
1209 {
1210 out << " lod += bias;\n";
1211 }
1212 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05001213 else if (textureFunction->method == TextureFunction::LOD)
1214 {
1215 out << " x.GetDimensions(0, width, height, levels);\n";
1216 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001217 else if (textureFunction->method == TextureFunction::GRAD)
1218 {
1219 out << " x.GetDimensions(0, width, height, levels);\n"
1220 " float lod = log2(max(length(ddx), length(ddy)));\n";
1221 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001222
1223 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1224 }
1225
1226 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001227 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001228 }
1229 else if (IsSampler3D(textureFunction->sampler))
1230 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001231 out << " float width; float height; float depth; float levels;\n";
1232
1233 if (textureFunction->method == TextureFunction::LOD0)
1234 {
1235 out << " uint mip = 0;\n";
1236 }
1237 else
1238 {
1239 if (textureFunction->method == TextureFunction::IMPLICIT ||
1240 textureFunction->method == TextureFunction::BIAS)
1241 {
1242 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1243 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
1244 " float dx = length(ddx(tSized));\n"
1245 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001246 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001247
1248 if (textureFunction->method == TextureFunction::BIAS)
1249 {
1250 out << " lod += bias;\n";
1251 }
1252 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001253 else if (textureFunction->method == TextureFunction::GRAD)
1254 {
1255 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1256 " float lod = log2(max(length(ddx), length(ddy)));\n";
1257 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001258
1259 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1260 }
1261
1262 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001263 }
1264 else UNREACHABLE();
1265 }
1266
1267 out << " return ";
1268
1269 // HLSL intrinsic
1270 if (mOutputType == SH_HLSL9_OUTPUT)
1271 {
1272 switch(textureFunction->sampler)
1273 {
1274 case EbtSampler2D: out << "tex2D"; break;
1275 case EbtSamplerCube: out << "texCUBE"; break;
1276 default: UNREACHABLE();
1277 }
1278
Nicolas Capens75fb4752013-07-10 15:14:47 -04001279 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001280 {
1281 case TextureFunction::IMPLICIT: out << "(s, "; break;
1282 case TextureFunction::BIAS: out << "bias(s, "; break;
1283 case TextureFunction::LOD: out << "lod(s, "; break;
1284 case TextureFunction::LOD0: out << "lod(s, "; break;
1285 default: UNREACHABLE();
1286 }
1287 }
1288 else if (mOutputType == SH_HLSL11_OUTPUT)
1289 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001290 if (textureFunction->method == TextureFunction::GRAD)
1291 {
1292 if (IsIntegerSampler(textureFunction->sampler))
1293 {
1294 out << "x.Load(";
1295 }
1296 else if (IsShadowSampler(textureFunction->sampler))
1297 {
1298 out << "x.SampleCmpLevelZero(s, ";
1299 }
1300 else
1301 {
1302 out << "x.SampleGrad(s, ";
1303 }
1304 }
1305 else if (IsIntegerSampler(textureFunction->sampler) ||
1306 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001307 {
1308 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001309 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001310 else if (IsShadowSampler(textureFunction->sampler))
1311 {
1312 out << "x.SampleCmp(s, ";
1313 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001314 else
1315 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001316 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001317 {
1318 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1319 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1320 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1321 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
1322 default: UNREACHABLE();
1323 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001324 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001325 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001326 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001327
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001328 // Integer sampling requires integer addresses
1329 TString addressx = "";
1330 TString addressy = "";
1331 TString addressz = "";
1332 TString close = "";
1333
Nicolas Capensfc014542014-02-18 14:47:13 -05001334 if (IsIntegerSampler(textureFunction->sampler) ||
1335 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001336 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001337 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001338 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001339 case 2: out << "int3("; break;
1340 case 3: out << "int4("; break;
1341 default: UNREACHABLE();
1342 }
1343
Nicolas Capensfc014542014-02-18 14:47:13 -05001344 // Convert from normalized floating-point to integer
1345 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001346 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001347 addressx = "int(floor(width * frac((";
1348 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001349
Nicolas Capensfc014542014-02-18 14:47:13 -05001350 if (IsSamplerArray(textureFunction->sampler))
1351 {
1352 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1353 }
1354 else
1355 {
1356 addressz = "int(floor(depth * frac((";
1357 }
1358
1359 close = "))))";
1360 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001361 }
1362 else
1363 {
1364 switch(hlslCoords)
1365 {
1366 case 2: out << "float2("; break;
1367 case 3: out << "float3("; break;
1368 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001369 default: UNREACHABLE();
1370 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001371 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001372
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001373 TString proj = ""; // Only used for projected textures
1374
1375 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001376 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001377 switch(textureFunction->coords)
1378 {
1379 case 3: proj = " / t.z"; break;
1380 case 4: proj = " / t.w"; break;
1381 default: UNREACHABLE();
1382 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001383 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001384
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001385 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001386
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001387 if (mOutputType == SH_HLSL9_OUTPUT)
1388 {
1389 if (hlslCoords >= 3)
1390 {
1391 if (textureFunction->coords < 3)
1392 {
1393 out << ", 0";
1394 }
1395 else
1396 {
1397 out << ", t.z" + proj;
1398 }
1399 }
1400
1401 if (hlslCoords == 4)
1402 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001403 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001404 {
1405 case TextureFunction::BIAS: out << ", bias"; break;
1406 case TextureFunction::LOD: out << ", lod"; break;
1407 case TextureFunction::LOD0: out << ", 0"; break;
1408 default: UNREACHABLE();
1409 }
1410 }
1411
1412 out << "));\n";
1413 }
1414 else if (mOutputType == SH_HLSL11_OUTPUT)
1415 {
1416 if (hlslCoords >= 3)
1417 {
1418 out << ", " + addressz + ("t.z" + proj) + close;
1419 }
1420
Nicolas Capensd11d5492014-02-19 17:06:10 -05001421 if (textureFunction->method == TextureFunction::GRAD)
1422 {
1423 if (IsIntegerSampler(textureFunction->sampler))
1424 {
1425 out << ", mip)";
1426 }
1427 else if (IsShadowSampler(textureFunction->sampler))
1428 {
1429 // Compare value
1430 switch(textureFunction->coords)
1431 {
1432 case 3: out << "), t.z"; break;
1433 case 4: out << "), t.w"; break;
1434 default: UNREACHABLE();
1435 }
1436 }
1437 else
1438 {
1439 out << "), ddx, ddy";
1440 }
1441 }
1442 else if (IsIntegerSampler(textureFunction->sampler) ||
1443 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001444 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001445 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001446 }
1447 else if (IsShadowSampler(textureFunction->sampler))
1448 {
1449 // Compare value
1450 switch(textureFunction->coords)
1451 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001452 case 3: out << "), t.z"; break;
1453 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001454 default: UNREACHABLE();
1455 }
1456 }
1457 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001458 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001459 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001460 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001461 case TextureFunction::IMPLICIT: out << ")"; break;
1462 case TextureFunction::BIAS: out << "), bias"; break;
1463 case TextureFunction::LOD: out << "), lod"; break;
1464 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001465 default: UNREACHABLE();
1466 }
1467 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001468
1469 if (textureFunction->offset)
1470 {
1471 out << ", offset";
1472 }
1473
1474 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001475 }
1476 else UNREACHABLE();
1477 }
1478
1479 out << "\n"
1480 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001481 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001482 }
1483
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001484 if (mUsesFragCoord)
1485 {
1486 out << "#define GL_USES_FRAG_COORD\n";
1487 }
1488
1489 if (mUsesPointCoord)
1490 {
1491 out << "#define GL_USES_POINT_COORD\n";
1492 }
1493
1494 if (mUsesFrontFacing)
1495 {
1496 out << "#define GL_USES_FRONT_FACING\n";
1497 }
1498
1499 if (mUsesPointSize)
1500 {
1501 out << "#define GL_USES_POINT_SIZE\n";
1502 }
1503
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001504 if (mUsesFragDepth)
1505 {
1506 out << "#define GL_USES_FRAG_DEPTH\n";
1507 }
1508
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001509 if (mUsesDepthRange)
1510 {
1511 out << "#define GL_USES_DEPTH_RANGE\n";
1512 }
1513
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001514 if (mUsesXor)
1515 {
1516 out << "bool xor(bool p, bool q)\n"
1517 "{\n"
1518 " return (p || q) && !(p && q);\n"
1519 "}\n"
1520 "\n";
1521 }
1522
1523 if (mUsesMod1)
1524 {
1525 out << "float mod(float x, float y)\n"
1526 "{\n"
1527 " return x - y * floor(x / y);\n"
1528 "}\n"
1529 "\n";
1530 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001531
1532 if (mUsesMod2v)
1533 {
1534 out << "float2 mod(float2 x, float2 y)\n"
1535 "{\n"
1536 " return x - y * floor(x / y);\n"
1537 "}\n"
1538 "\n";
1539 }
1540
1541 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001542 {
1543 out << "float2 mod(float2 x, float y)\n"
1544 "{\n"
1545 " return x - y * floor(x / y);\n"
1546 "}\n"
1547 "\n";
1548 }
1549
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001550 if (mUsesMod3v)
1551 {
1552 out << "float3 mod(float3 x, float3 y)\n"
1553 "{\n"
1554 " return x - y * floor(x / y);\n"
1555 "}\n"
1556 "\n";
1557 }
1558
1559 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001560 {
1561 out << "float3 mod(float3 x, float y)\n"
1562 "{\n"
1563 " return x - y * floor(x / y);\n"
1564 "}\n"
1565 "\n";
1566 }
1567
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001568 if (mUsesMod4v)
1569 {
1570 out << "float4 mod(float4 x, float4 y)\n"
1571 "{\n"
1572 " return x - y * floor(x / y);\n"
1573 "}\n"
1574 "\n";
1575 }
1576
1577 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001578 {
1579 out << "float4 mod(float4 x, float y)\n"
1580 "{\n"
1581 " return x - y * floor(x / y);\n"
1582 "}\n"
1583 "\n";
1584 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001585
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001586 if (mUsesFaceforward1)
1587 {
1588 out << "float faceforward(float N, float I, float Nref)\n"
1589 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001590 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001591 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001592 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001593 " }\n"
1594 " else\n"
1595 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001596 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001597 " }\n"
1598 "}\n"
1599 "\n";
1600 }
1601
1602 if (mUsesFaceforward2)
1603 {
1604 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1605 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001606 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001607 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001608 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001609 " }\n"
1610 " else\n"
1611 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001612 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001613 " }\n"
1614 "}\n"
1615 "\n";
1616 }
1617
1618 if (mUsesFaceforward3)
1619 {
1620 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1621 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001622 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001623 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001624 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001625 " }\n"
1626 " else\n"
1627 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001628 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001629 " }\n"
1630 "}\n"
1631 "\n";
1632 }
1633
1634 if (mUsesFaceforward4)
1635 {
1636 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1637 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001638 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001639 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001640 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001641 " }\n"
1642 " else\n"
1643 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001644 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001645 " }\n"
1646 "}\n"
1647 "\n";
1648 }
1649
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001650 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001651 {
1652 out << "float atanyx(float y, float x)\n"
1653 "{\n"
1654 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1655 " return atan2(y, x);\n"
1656 "}\n";
1657 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001658
1659 if (mUsesAtan2_2)
1660 {
1661 out << "float2 atanyx(float2 y, float2 x)\n"
1662 "{\n"
1663 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1664 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1665 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1666 "}\n";
1667 }
1668
1669 if (mUsesAtan2_3)
1670 {
1671 out << "float3 atanyx(float3 y, float3 x)\n"
1672 "{\n"
1673 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1674 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1675 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1676 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1677 "}\n";
1678 }
1679
1680 if (mUsesAtan2_4)
1681 {
1682 out << "float4 atanyx(float4 y, float4 x)\n"
1683 "{\n"
1684 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1685 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1686 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1687 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1688 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1689 "}\n";
1690 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001691}
1692
1693void OutputHLSL::visitSymbol(TIntermSymbol *node)
1694{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001695 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001696
Jamie Madill570e04d2013-06-21 09:15:33 -04001697 // Handle accessing std140 structs by value
1698 if (mFlaggedStructMappedNames.count(node) > 0)
1699 {
1700 out << mFlaggedStructMappedNames[node];
1701 return;
1702 }
1703
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001704 TString name = node->getSymbol();
1705
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001706 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001707 {
1708 mUsesDepthRange = true;
1709 out << name;
1710 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001711 else
1712 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001713 TQualifier qualifier = node->getQualifier();
1714
1715 if (qualifier == EvqUniform)
1716 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001717 const TType& nodeType = node->getType();
1718 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1719
1720 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001721 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001722 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001723 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001724 else
1725 {
1726 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001727 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001728
1729 out << decorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001730 }
Jamie Madill19571812013-08-12 15:26:34 -07001731 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001732 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001733 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001734 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001735 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001736 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001737 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001738 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001739 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001740 }
Jamie Madill19571812013-08-12 15:26:34 -07001741 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001742 {
1743 mReferencedOutputVariables[name] = node;
1744 out << "out_" << name;
1745 }
1746 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001747 {
1748 out << "gl_Color[0]";
1749 mUsesFragColor = true;
1750 }
1751 else if (qualifier == EvqFragData)
1752 {
1753 out << "gl_Color";
1754 mUsesFragData = true;
1755 }
1756 else if (qualifier == EvqFragCoord)
1757 {
1758 mUsesFragCoord = true;
1759 out << name;
1760 }
1761 else if (qualifier == EvqPointCoord)
1762 {
1763 mUsesPointCoord = true;
1764 out << name;
1765 }
1766 else if (qualifier == EvqFrontFacing)
1767 {
1768 mUsesFrontFacing = true;
1769 out << name;
1770 }
1771 else if (qualifier == EvqPointSize)
1772 {
1773 mUsesPointSize = true;
1774 out << name;
1775 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001776 else if (name == "gl_FragDepthEXT")
1777 {
1778 mUsesFragDepth = true;
1779 out << "gl_Depth";
1780 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001781 else if (qualifier == EvqInternal)
1782 {
1783 out << name;
1784 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001785 else
1786 {
1787 out << decorate(name);
1788 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001789 }
1790}
1791
1792bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1793{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001794 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001795
Jamie Madill570e04d2013-06-21 09:15:33 -04001796 // Handle accessing std140 structs by value
1797 if (mFlaggedStructMappedNames.count(node) > 0)
1798 {
1799 out << mFlaggedStructMappedNames[node];
1800 return false;
1801 }
1802
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001803 switch (node->getOp())
1804 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001805 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001806 case EOpInitialize:
1807 if (visit == PreVisit)
1808 {
1809 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1810 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1811 // new variable is created before the assignment is evaluated), so we need to convert
1812 // this to "float t = x, x = t;".
1813
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001814 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1815 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001816
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001817 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1818 expression->traverse(&searchSymbol);
1819 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001820
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001821 if (sameSymbol)
1822 {
1823 // Type already printed
1824 out << "t" + str(mUniqueIndex) + " = ";
1825 expression->traverse(this);
1826 out << ", ";
1827 symbolNode->traverse(this);
1828 out << " = t" + str(mUniqueIndex);
1829
1830 mUniqueIndex++;
1831 return false;
1832 }
1833 }
1834 else if (visit == InVisit)
1835 {
1836 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001837 }
1838 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001839 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1840 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1841 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1842 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1843 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1844 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001845 if (visit == PreVisit)
1846 {
1847 out << "(";
1848 }
1849 else if (visit == InVisit)
1850 {
1851 out << " = mul(";
1852 node->getLeft()->traverse(this);
1853 out << ", transpose(";
1854 }
1855 else
1856 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001857 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001858 }
1859 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001860 case EOpMatrixTimesMatrixAssign:
1861 if (visit == PreVisit)
1862 {
1863 out << "(";
1864 }
1865 else if (visit == InVisit)
1866 {
1867 out << " = mul(";
1868 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001869 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001870 }
1871 else
1872 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001873 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001874 }
1875 break;
1876 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001877 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001878 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001879 const TType& leftType = node->getLeft()->getType();
1880 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001881 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001882 if (visit == PreVisit)
1883 {
1884 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1885 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1886
1887 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1888 out << interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1889
1890 return false;
1891 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001892 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001893 else
1894 {
1895 outputTriplet(visit, "", "[", "]");
1896 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001897 }
1898 break;
1899 case EOpIndexIndirect:
1900 // We do not currently support indirect references to interface blocks
1901 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1902 outputTriplet(visit, "", "[", "]");
1903 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001904 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001905 if (visit == InVisit)
1906 {
1907 const TStructure* structure = node->getLeft()->getType().getStruct();
1908 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1909 const TField* field = structure->fields()[index->getIConst(0)];
1910 out << "." + decorateField(field->name(), *structure);
1911
1912 return false;
1913 }
1914 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001915 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001916 if (visit == InVisit)
1917 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001918 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1919 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1920 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
1921 out << "." + decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001922
1923 return false;
1924 }
1925 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001926 case EOpVectorSwizzle:
1927 if (visit == InVisit)
1928 {
1929 out << ".";
1930
1931 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1932
1933 if (swizzle)
1934 {
1935 TIntermSequence &sequence = swizzle->getSequence();
1936
1937 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1938 {
1939 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1940
1941 if (element)
1942 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001943 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001944
1945 switch (i)
1946 {
1947 case 0: out << "x"; break;
1948 case 1: out << "y"; break;
1949 case 2: out << "z"; break;
1950 case 3: out << "w"; break;
1951 default: UNREACHABLE();
1952 }
1953 }
1954 else UNREACHABLE();
1955 }
1956 }
1957 else UNREACHABLE();
1958
1959 return false; // Fully processed
1960 }
1961 break;
1962 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1963 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1964 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1965 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001966 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001967 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001968 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001969 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001970 if (node->getOp() == EOpEqual)
1971 {
1972 outputTriplet(visit, "(", " == ", ")");
1973 }
1974 else
1975 {
1976 outputTriplet(visit, "(", " != ", ")");
1977 }
1978 }
1979 else if (node->getLeft()->getBasicType() == EbtStruct)
1980 {
1981 if (node->getOp() == EOpEqual)
1982 {
1983 out << "(";
1984 }
1985 else
1986 {
1987 out << "!(";
1988 }
1989
Jamie Madill98493dd2013-07-08 14:39:03 -04001990 const TStructure &structure = *node->getLeft()->getType().getStruct();
1991 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001992
Jamie Madill98493dd2013-07-08 14:39:03 -04001993 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001994 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001995 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001996
1997 node->getLeft()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001998 out << "." + decorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001999 node->getRight()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04002000 out << "." + decorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002001
Jamie Madill98493dd2013-07-08 14:39:03 -04002002 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002003 {
2004 out << " && ";
2005 }
2006 }
2007
2008 out << ")";
2009
2010 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002011 }
2012 else
2013 {
Jamie Madill0b20c942013-07-19 16:36:56 -04002014 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002015
2016 if (node->getOp() == EOpEqual)
2017 {
Jamie Madill0b20c942013-07-19 16:36:56 -04002018 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002019 }
2020 else
2021 {
Jamie Madill0b20c942013-07-19 16:36:56 -04002022 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002023 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002024 }
2025 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002026 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2027 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2028 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2029 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2030 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00002031 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00002032 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
2033 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00002034 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00002035 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002036 if (node->getRight()->hasSideEffects())
2037 {
2038 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
2039 return false;
2040 }
2041 else
2042 {
2043 outputTriplet(visit, "(", " || ", ")");
2044 return true;
2045 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002046 case EOpLogicalXor:
2047 mUsesXor = true;
2048 outputTriplet(visit, "xor(", ", ", ")");
2049 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00002050 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002051 if (node->getRight()->hasSideEffects())
2052 {
2053 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
2054 return false;
2055 }
2056 else
2057 {
2058 outputTriplet(visit, "(", " && ", ")");
2059 return true;
2060 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002061 default: UNREACHABLE();
2062 }
2063
2064 return true;
2065}
2066
2067bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
2068{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002069 switch (node->getOp())
2070 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002071 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
2072 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
2073 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
2074 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
2075 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
2076 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
2077 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002078 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04002079 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002080 case EOpConvFloatToBool:
2081 switch (node->getOperand()->getType().getNominalSize())
2082 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002083 case 1: outputTriplet(visit, "bool(", "", ")"); break;
2084 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
2085 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
2086 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002087 default: UNREACHABLE();
2088 }
2089 break;
2090 case EOpConvBoolToFloat:
2091 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04002092 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002093 switch (node->getOperand()->getType().getNominalSize())
2094 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002095 case 1: outputTriplet(visit, "float(", "", ")"); break;
2096 case 2: outputTriplet(visit, "float2(", "", ")"); break;
2097 case 3: outputTriplet(visit, "float3(", "", ")"); break;
2098 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002099 default: UNREACHABLE();
2100 }
2101 break;
2102 case EOpConvFloatToInt:
2103 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04002104 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002105 switch (node->getOperand()->getType().getNominalSize())
2106 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002107 case 1: outputTriplet(visit, "int(", "", ")"); break;
2108 case 2: outputTriplet(visit, "int2(", "", ")"); break;
2109 case 3: outputTriplet(visit, "int3(", "", ")"); break;
2110 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002111 default: UNREACHABLE();
2112 }
2113 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002114 case EOpConvFloatToUInt:
2115 case EOpConvBoolToUInt:
2116 case EOpConvIntToUInt:
Jamie Madilla16f1672013-07-03 15:15:17 -04002117 switch (node->getOperand()->getType().getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002118 {
2119 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002120 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
2121 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
2122 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002123 default: UNREACHABLE();
2124 }
2125 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002126 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
2127 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
2128 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
2129 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
2130 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
2131 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
2132 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
2133 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
2134 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
2135 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
2136 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
2137 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
2138 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
2139 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
2140 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
2141 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
2142 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
2143 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
2144 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
2145 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
2146 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002147 case EOpDFdx:
2148 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2149 {
2150 outputTriplet(visit, "(", "", ", 0.0)");
2151 }
2152 else
2153 {
2154 outputTriplet(visit, "ddx(", "", ")");
2155 }
2156 break;
2157 case EOpDFdy:
2158 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2159 {
2160 outputTriplet(visit, "(", "", ", 0.0)");
2161 }
2162 else
2163 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00002164 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002165 }
2166 break;
2167 case EOpFwidth:
2168 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2169 {
2170 outputTriplet(visit, "(", "", ", 0.0)");
2171 }
2172 else
2173 {
2174 outputTriplet(visit, "fwidth(", "", ")");
2175 }
2176 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002177 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
2178 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002179 default: UNREACHABLE();
2180 }
2181
2182 return true;
2183}
2184
2185bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2186{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002187 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002188
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002189 switch (node->getOp())
2190 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002191 case EOpSequence:
2192 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002193 if (mInsideFunction)
2194 {
Jamie Madill075edd82013-07-08 13:30:19 -04002195 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002196 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002197
2198 mScopeDepth++;
2199
2200 if (mScopeBracket.size() < mScopeDepth)
2201 {
2202 mScopeBracket.push_back(0); // New scope level
2203 }
2204 else
2205 {
2206 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
2207 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002208 }
2209
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002210 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
2211 {
Jamie Madill075edd82013-07-08 13:30:19 -04002212 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002213
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002214 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002215
2216 out << ";\n";
2217 }
2218
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002219 if (mInsideFunction)
2220 {
Jamie Madill075edd82013-07-08 13:30:19 -04002221 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002222 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002223
2224 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002225 }
2226
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002227 return false;
2228 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002229 case EOpDeclaration:
2230 if (visit == PreVisit)
2231 {
2232 TIntermSequence &sequence = node->getSequence();
2233 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002234
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00002235 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002236 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002237 if (variable->getType().getStruct())
2238 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002239 addConstructor(variable->getType(), scopedStruct(variable->getType().getStruct()->name()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00002240 }
2241
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002242 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002243 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00002244 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002245 {
2246 out << "static ";
2247 }
2248
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002249 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002250
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002251 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002252 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002253 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002254
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002255 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002256 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002257 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002258 out << arrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05002259 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002260 }
2261 else
2262 {
2263 (*sit)->traverse(this);
2264 }
2265
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002266 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002267 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002268 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002269 }
2270 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002271 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002272 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2273 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002274 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002275 }
2276 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002277 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002278 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002279 {
2280 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2281 {
2282 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2283
2284 if (symbol)
2285 {
2286 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2287 mReferencedVaryings[symbol->getSymbol()] = symbol;
2288 }
2289 else
2290 {
2291 (*sit)->traverse(this);
2292 }
2293 }
2294 }
2295
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002296 return false;
2297 }
2298 else if (visit == InVisit)
2299 {
2300 out << ", ";
2301 }
2302 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002303 case EOpPrototype:
2304 if (visit == PreVisit)
2305 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002306 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002307
2308 TIntermSequence &arguments = node->getSequence();
2309
2310 for (unsigned int i = 0; i < arguments.size(); i++)
2311 {
2312 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2313
2314 if (symbol)
2315 {
2316 out << argumentString(symbol);
2317
2318 if (i < arguments.size() - 1)
2319 {
2320 out << ", ";
2321 }
2322 }
2323 else UNREACHABLE();
2324 }
2325
2326 out << ");\n";
2327
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002328 // Also prototype the Lod0 variant if needed
2329 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2330 {
2331 mOutputLod0Function = true;
2332 node->traverse(this);
2333 mOutputLod0Function = false;
2334 }
2335
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002336 return false;
2337 }
2338 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002339 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002340 case EOpFunction:
2341 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002342 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002343
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002344 out << typeString(node->getType()) << " ";
2345
2346 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002347 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002348 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002349 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002350 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002351 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002352 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002353 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002354
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002355 TIntermSequence &sequence = node->getSequence();
2356 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2357
2358 for (unsigned int i = 0; i < arguments.size(); i++)
2359 {
2360 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2361
2362 if (symbol)
2363 {
2364 if (symbol->getType().getStruct())
2365 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002366 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getStruct()->name()), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002367 }
2368
2369 out << argumentString(symbol);
2370
2371 if (i < arguments.size() - 1)
2372 {
2373 out << ", ";
2374 }
2375 }
2376 else UNREACHABLE();
2377 }
2378
2379 out << ")\n"
2380 "{\n";
2381
2382 if (sequence.size() > 1)
2383 {
2384 mInsideFunction = true;
2385 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002386 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002387 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002388
2389 out << "}\n";
2390
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002391 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2392 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002393 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002394 {
2395 mOutputLod0Function = true;
2396 node->traverse(this);
2397 mOutputLod0Function = false;
2398 }
2399 }
2400
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002401 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002402 }
2403 break;
2404 case EOpFunctionCall:
2405 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002406 TString name = TFunction::unmangleName(node->getName());
2407 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002408 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002409
2410 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002411 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002412 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002413 }
2414 else
2415 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002416 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2417
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002418 TextureFunction textureFunction;
2419 textureFunction.sampler = samplerType;
2420 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002421 textureFunction.method = TextureFunction::IMPLICIT;
2422 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002423 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002424
2425 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002426 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002427 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002428 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002429 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002430 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002431 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002432 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002433 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002434 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002435 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002436 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002437 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002438 else if (name == "texture2DProjLod" || name == "textureProjLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002439 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002440 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002441 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002442 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002443 else if (name == "textureSize")
2444 {
2445 textureFunction.method = TextureFunction::SIZE;
2446 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002447 else if (name == "textureOffset")
2448 {
2449 textureFunction.method = TextureFunction::IMPLICIT;
2450 textureFunction.offset = true;
2451 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002452 else if (name == "textureProjOffset")
2453 {
2454 textureFunction.method = TextureFunction::IMPLICIT;
2455 textureFunction.offset = true;
2456 textureFunction.proj = true;
2457 }
2458 else if (name == "textureLodOffset")
2459 {
2460 textureFunction.method = TextureFunction::LOD;
2461 textureFunction.offset = true;
2462 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002463 else if (name == "textureProjLod")
2464 {
2465 textureFunction.method = TextureFunction::LOD;
2466 textureFunction.proj = true;
2467 }
2468 else if (name == "textureProjLodOffset")
2469 {
2470 textureFunction.method = TextureFunction::LOD;
2471 textureFunction.proj = true;
2472 textureFunction.offset = true;
2473 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002474 else if (name == "texelFetch")
2475 {
2476 textureFunction.method = TextureFunction::FETCH;
2477 }
2478 else if (name == "texelFetchOffset")
2479 {
2480 textureFunction.method = TextureFunction::FETCH;
2481 textureFunction.offset = true;
2482 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05002483 else if (name == "textureGrad")
2484 {
2485 textureFunction.method = TextureFunction::GRAD;
2486 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002487 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002488
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002489 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002490 {
2491 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2492 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002493 textureFunction.method = TextureFunction::LOD0;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002494 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002495 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002496 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002497 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2498
2499 if (textureFunction.offset)
2500 {
2501 mandatoryArgumentCount++;
2502 }
2503
2504 if (arguments.size() > mandatoryArgumentCount) // Bias argument is optional
2505 {
2506 textureFunction.method = TextureFunction::BIAS;
2507 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002508 }
2509 }
2510
2511 mUsesTexture.insert(textureFunction);
2512
2513 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002514 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002515
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002516 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2517 {
2518 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2519 {
2520 out << "texture_";
2521 (*arg)->traverse(this);
2522 out << ", sampler_";
2523 }
2524
2525 (*arg)->traverse(this);
2526
2527 if (arg < arguments.end() - 1)
2528 {
2529 out << ", ";
2530 }
2531 }
2532
2533 out << ")";
2534
2535 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002536 }
2537 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002538 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002539 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002540 addConstructor(node->getType(), "vec1", &node->getSequence());
2541 outputTriplet(visit, "vec1(", "", ")");
2542 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002543 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002544 addConstructor(node->getType(), "vec2", &node->getSequence());
2545 outputTriplet(visit, "vec2(", ", ", ")");
2546 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002547 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002548 addConstructor(node->getType(), "vec3", &node->getSequence());
2549 outputTriplet(visit, "vec3(", ", ", ")");
2550 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002551 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002552 addConstructor(node->getType(), "vec4", &node->getSequence());
2553 outputTriplet(visit, "vec4(", ", ", ")");
2554 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002555 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002556 addConstructor(node->getType(), "bvec1", &node->getSequence());
2557 outputTriplet(visit, "bvec1(", "", ")");
2558 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002559 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002560 addConstructor(node->getType(), "bvec2", &node->getSequence());
2561 outputTriplet(visit, "bvec2(", ", ", ")");
2562 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002563 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002564 addConstructor(node->getType(), "bvec3", &node->getSequence());
2565 outputTriplet(visit, "bvec3(", ", ", ")");
2566 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002567 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002568 addConstructor(node->getType(), "bvec4", &node->getSequence());
2569 outputTriplet(visit, "bvec4(", ", ", ")");
2570 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002571 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002572 addConstructor(node->getType(), "ivec1", &node->getSequence());
2573 outputTriplet(visit, "ivec1(", "", ")");
2574 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002575 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002576 addConstructor(node->getType(), "ivec2", &node->getSequence());
2577 outputTriplet(visit, "ivec2(", ", ", ")");
2578 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002579 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002580 addConstructor(node->getType(), "ivec3", &node->getSequence());
2581 outputTriplet(visit, "ivec3(", ", ", ")");
2582 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002583 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002584 addConstructor(node->getType(), "ivec4", &node->getSequence());
2585 outputTriplet(visit, "ivec4(", ", ", ")");
2586 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002587 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002588 addConstructor(node->getType(), "uvec1", &node->getSequence());
2589 outputTriplet(visit, "uvec1(", "", ")");
2590 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002591 case EOpConstructUVec2:
2592 addConstructor(node->getType(), "uvec2", &node->getSequence());
2593 outputTriplet(visit, "uvec2(", ", ", ")");
2594 break;
2595 case EOpConstructUVec3:
2596 addConstructor(node->getType(), "uvec3", &node->getSequence());
2597 outputTriplet(visit, "uvec3(", ", ", ")");
2598 break;
2599 case EOpConstructUVec4:
2600 addConstructor(node->getType(), "uvec4", &node->getSequence());
2601 outputTriplet(visit, "uvec4(", ", ", ")");
2602 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002603 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002604 addConstructor(node->getType(), "mat2", &node->getSequence());
2605 outputTriplet(visit, "mat2(", ", ", ")");
2606 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002607 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002608 addConstructor(node->getType(), "mat3", &node->getSequence());
2609 outputTriplet(visit, "mat3(", ", ", ")");
2610 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002611 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002612 addConstructor(node->getType(), "mat4", &node->getSequence());
2613 outputTriplet(visit, "mat4(", ", ", ")");
2614 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002615 case EOpConstructStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04002616 addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence());
2617 outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002618 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002619 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2620 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2621 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2622 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2623 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2624 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002625 case EOpMod:
2626 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002627 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002628 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2629 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2630 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002631 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002632 case 11: mUsesMod1 = true; break;
2633 case 22: mUsesMod2v = true; break;
2634 case 21: mUsesMod2f = true; break;
2635 case 33: mUsesMod3v = true; break;
2636 case 31: mUsesMod3f = true; break;
2637 case 44: mUsesMod4v = true; break;
2638 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002639 default: UNREACHABLE();
2640 }
2641
2642 outputTriplet(visit, "mod(", ", ", ")");
2643 }
2644 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002645 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002646 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002647 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002648 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2649 {
2650 case 1: mUsesAtan2_1 = true; break;
2651 case 2: mUsesAtan2_2 = true; break;
2652 case 3: mUsesAtan2_3 = true; break;
2653 case 4: mUsesAtan2_4 = true; break;
2654 default: UNREACHABLE();
2655 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002656 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002657 break;
2658 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2659 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2660 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2661 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2662 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2663 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2664 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2665 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2666 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002667 case EOpFaceForward:
2668 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002669 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002670 {
2671 case 1: mUsesFaceforward1 = true; break;
2672 case 2: mUsesFaceforward2 = true; break;
2673 case 3: mUsesFaceforward3 = true; break;
2674 case 4: mUsesFaceforward4 = true; break;
2675 default: UNREACHABLE();
2676 }
2677
2678 outputTriplet(visit, "faceforward(", ", ", ")");
2679 }
2680 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002681 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2682 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2683 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002684 default: UNREACHABLE();
2685 }
2686
2687 return true;
2688}
2689
2690bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2691{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002692 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002693
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002694 if (node->usesTernaryOperator())
2695 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002696 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002697 }
2698 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002699 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002700 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002701
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002702 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002703
2704 node->getCondition()->traverse(this);
2705
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002706 out << ")\n";
2707
Jamie Madill075edd82013-07-08 13:30:19 -04002708 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002709 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002710
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002711 bool discard = false;
2712
daniel@transgaming.combb885322010-04-15 20:45:24 +00002713 if (node->getTrueBlock())
2714 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002715 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002716
2717 // Detect true discard
2718 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002719 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002720
Jamie Madill075edd82013-07-08 13:30:19 -04002721 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002722 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002723
2724 if (node->getFalseBlock())
2725 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002726 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002727
Jamie Madill075edd82013-07-08 13:30:19 -04002728 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002729 out << "{\n";
2730
Jamie Madill075edd82013-07-08 13:30:19 -04002731 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002732 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002733
Jamie Madill075edd82013-07-08 13:30:19 -04002734 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002735 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002736
2737 // Detect false discard
2738 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2739 }
2740
2741 // ANGLE issue 486: Detect problematic conditional discard
2742 if (discard && FindSideEffectRewriting::search(node))
2743 {
2744 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002745 }
2746 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002747
2748 return false;
2749}
2750
2751void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2752{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002753 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002754}
2755
2756bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2757{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002758 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2759
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002760 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002761 {
2762 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2763 }
2764
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002765 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002766 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002767 if (handleExcessiveLoop(node))
2768 {
2769 return false;
2770 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002771 }
2772
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002773 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002774
alokp@chromium.org52813552010-11-16 18:36:09 +00002775 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002776 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002777 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002778
Jamie Madill075edd82013-07-08 13:30:19 -04002779 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002780 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002781 }
2782 else
2783 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002784 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002785
2786 if (node->getInit())
2787 {
2788 node->getInit()->traverse(this);
2789 }
2790
2791 out << "; ";
2792
alokp@chromium.org52813552010-11-16 18:36:09 +00002793 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002794 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002795 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002796 }
2797
2798 out << "; ";
2799
alokp@chromium.org52813552010-11-16 18:36:09 +00002800 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002801 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002802 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002803 }
2804
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002805 out << ")\n";
2806
Jamie Madill075edd82013-07-08 13:30:19 -04002807 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002808 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002809 }
2810
2811 if (node->getBody())
2812 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002813 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002814 }
2815
Jamie Madill075edd82013-07-08 13:30:19 -04002816 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002817 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002818
alokp@chromium.org52813552010-11-16 18:36:09 +00002819 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002820 {
Jamie Madill075edd82013-07-08 13:30:19 -04002821 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002822 out << "while(\n";
2823
alokp@chromium.org52813552010-11-16 18:36:09 +00002824 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002825
daniel@transgaming.com73536982012-03-21 20:45:49 +00002826 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002827 }
2828
daniel@transgaming.com73536982012-03-21 20:45:49 +00002829 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002830
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002831 mInsideDiscontinuousLoop = wasDiscontinuous;
2832
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002833 return false;
2834}
2835
2836bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2837{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002838 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002839
2840 switch (node->getFlowOp())
2841 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002842 case EOpKill:
2843 outputTriplet(visit, "discard;\n", "", "");
2844 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002845 case EOpBreak:
2846 if (visit == PreVisit)
2847 {
2848 if (mExcessiveLoopIndex)
2849 {
2850 out << "{Break";
2851 mExcessiveLoopIndex->traverse(this);
2852 out << " = true; break;}\n";
2853 }
2854 else
2855 {
2856 out << "break;\n";
2857 }
2858 }
2859 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002860 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002861 case EOpReturn:
2862 if (visit == PreVisit)
2863 {
2864 if (node->getExpression())
2865 {
2866 out << "return ";
2867 }
2868 else
2869 {
2870 out << "return;\n";
2871 }
2872 }
2873 else if (visit == PostVisit)
2874 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002875 if (node->getExpression())
2876 {
2877 out << ";\n";
2878 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002879 }
2880 break;
2881 default: UNREACHABLE();
2882 }
2883
2884 return true;
2885}
2886
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002887void OutputHLSL::traverseStatements(TIntermNode *node)
2888{
2889 if (isSingleStatement(node))
2890 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002891 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002892 }
2893
2894 node->traverse(this);
2895}
2896
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002897bool OutputHLSL::isSingleStatement(TIntermNode *node)
2898{
2899 TIntermAggregate *aggregate = node->getAsAggregate();
2900
2901 if (aggregate)
2902 {
2903 if (aggregate->getOp() == EOpSequence)
2904 {
2905 return false;
2906 }
2907 else
2908 {
2909 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2910 {
2911 if (!isSingleStatement(*sit))
2912 {
2913 return false;
2914 }
2915 }
2916
2917 return true;
2918 }
2919 }
2920
2921 return true;
2922}
2923
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002924// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2925// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002926bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2927{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002928 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002929 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002930
2931 // Parse loops of the form:
2932 // for(int index = initial; index [comparator] limit; index += increment)
2933 TIntermSymbol *index = NULL;
2934 TOperator comparator = EOpNull;
2935 int initial = 0;
2936 int limit = 0;
2937 int increment = 0;
2938
2939 // Parse index name and intial value
2940 if (node->getInit())
2941 {
2942 TIntermAggregate *init = node->getInit()->getAsAggregate();
2943
2944 if (init)
2945 {
2946 TIntermSequence &sequence = init->getSequence();
2947 TIntermTyped *variable = sequence[0]->getAsTyped();
2948
2949 if (variable && variable->getQualifier() == EvqTemporary)
2950 {
2951 TIntermBinary *assign = variable->getAsBinaryNode();
2952
2953 if (assign->getOp() == EOpInitialize)
2954 {
2955 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2956 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2957
2958 if (symbol && constant)
2959 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002960 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002961 {
2962 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002963 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002964 }
2965 }
2966 }
2967 }
2968 }
2969 }
2970
2971 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002972 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002973 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002974 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002975
2976 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2977 {
2978 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2979
2980 if (constant)
2981 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002982 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002983 {
2984 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002985 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002986 }
2987 }
2988 }
2989 }
2990
2991 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002992 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002993 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002994 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2995 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002996
2997 if (binaryTerminal)
2998 {
2999 TOperator op = binaryTerminal->getOp();
3000 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3001
3002 if (constant)
3003 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003004 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003005 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003006 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003007
3008 switch (op)
3009 {
3010 case EOpAddAssign: increment = value; break;
3011 case EOpSubAssign: increment = -value; break;
3012 default: UNIMPLEMENTED();
3013 }
3014 }
3015 }
3016 }
3017 else if (unaryTerminal)
3018 {
3019 TOperator op = unaryTerminal->getOp();
3020
3021 switch (op)
3022 {
3023 case EOpPostIncrement: increment = 1; break;
3024 case EOpPostDecrement: increment = -1; break;
3025 case EOpPreIncrement: increment = 1; break;
3026 case EOpPreDecrement: increment = -1; break;
3027 default: UNIMPLEMENTED();
3028 }
3029 }
3030 }
3031
3032 if (index != NULL && comparator != EOpNull && increment != 0)
3033 {
3034 if (comparator == EOpLessThanEqual)
3035 {
3036 comparator = EOpLessThan;
3037 limit += 1;
3038 }
3039
3040 if (comparator == EOpLessThan)
3041 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00003042 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003043
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003044 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003045 {
3046 return false; // Not an excessive loop
3047 }
3048
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003049 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
3050 mExcessiveLoopIndex = index;
3051
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003052 out << "{int ";
3053 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003054 out << ";\n"
3055 "bool Break";
3056 index->traverse(this);
3057 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003058
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003059 bool firstLoopFragment = true;
3060
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003061 while (iterations > 0)
3062 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003063 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003064
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003065 if (!firstLoopFragment)
3066 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003067 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003068 index->traverse(this);
3069 out << ") {\n";
3070 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00003071
3072 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
3073 {
3074 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
3075 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003076
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003077 // for(int index = initial; index < clampedLimit; index += increment)
3078
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003079 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003080 index->traverse(this);
3081 out << " = ";
3082 out << initial;
3083
3084 out << "; ";
3085 index->traverse(this);
3086 out << " < ";
3087 out << clampedLimit;
3088
3089 out << "; ";
3090 index->traverse(this);
3091 out << " += ";
3092 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003093 out << ")\n";
3094
Jamie Madill075edd82013-07-08 13:30:19 -04003095 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003096 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003097
3098 if (node->getBody())
3099 {
3100 node->getBody()->traverse(this);
3101 }
3102
Jamie Madill075edd82013-07-08 13:30:19 -04003103 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003104 out << ";}\n";
3105
3106 if (!firstLoopFragment)
3107 {
3108 out << "}\n";
3109 }
3110
3111 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003112
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003113 initial += MAX_LOOP_ITERATIONS * increment;
3114 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003115 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003116
3117 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003118
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003119 mExcessiveLoopIndex = restoreIndex;
3120
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003121 return true;
3122 }
3123 else UNIMPLEMENTED();
3124 }
3125
3126 return false; // Not handled as an excessive loop
3127}
3128
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003129void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003130{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00003131 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003132
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003133 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003134 {
3135 out << preString;
3136 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003137 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003138 {
3139 out << inString;
3140 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003141 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003142 {
3143 out << postString;
3144 }
3145}
3146
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003147void OutputHLSL::outputLineDirective(int line)
3148{
3149 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
3150 {
baustin@google.com8ab69842011-06-02 21:53:45 +00003151 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003152 mBody << "#line " << line;
3153
3154 if (mContext.sourcePath)
3155 {
3156 mBody << " \"" << mContext.sourcePath << "\"";
3157 }
3158
3159 mBody << "\n";
3160 }
3161}
3162
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003163TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3164{
3165 TQualifier qualifier = symbol->getQualifier();
3166 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003167 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003168
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003169 if (name.empty()) // HLSL demands named arguments, also for prototypes
3170 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00003171 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003172 }
3173 else
3174 {
3175 name = decorate(name);
3176 }
3177
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003178 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
3179 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003180 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
3181 qualifierString(qualifier) + " " + samplerString(type) + " sampler_" + name + arrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003182 }
3183
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003184 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003185}
3186
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003187TString OutputHLSL::interpolationString(TQualifier qualifier)
3188{
3189 switch(qualifier)
3190 {
3191 case EvqVaryingIn: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003192 case EvqFragmentIn: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003193 case EvqInvariantVaryingIn: return "";
3194 case EvqSmoothIn: return "linear";
3195 case EvqFlatIn: return "nointerpolation";
3196 case EvqCentroidIn: return "centroid";
3197 case EvqVaryingOut: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003198 case EvqVertexOut: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003199 case EvqInvariantVaryingOut: return "";
3200 case EvqSmoothOut: return "linear";
3201 case EvqFlatOut: return "nointerpolation";
3202 case EvqCentroidOut: return "centroid";
3203 default: UNREACHABLE();
3204 }
3205
3206 return "";
3207}
3208
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003209TString OutputHLSL::qualifierString(TQualifier qualifier)
3210{
3211 switch(qualifier)
3212 {
3213 case EvqIn: return "in";
3214 case EvqOut: return "out";
3215 case EvqInOut: return "inout";
3216 case EvqConstReadOnly: return "const";
3217 default: UNREACHABLE();
3218 }
3219
3220 return "";
3221}
3222
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003223TString OutputHLSL::typeString(const TType &type)
3224{
Jamie Madill98493dd2013-07-08 14:39:03 -04003225 const TStructure* structure = type.getStruct();
3226 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003227 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003228 const TString& typeName = structure->name();
3229 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003230 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003231 return structLookup(typeName);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003232 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003233 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003234 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003235 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003236 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003237 }
3238 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003239 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003240 int cols = type.getCols();
3241 int rows = type.getRows();
3242 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003243 }
3244 else
3245 {
3246 switch (type.getBasicType())
3247 {
3248 case EbtFloat:
3249 switch (type.getNominalSize())
3250 {
3251 case 1: return "float";
3252 case 2: return "float2";
3253 case 3: return "float3";
3254 case 4: return "float4";
3255 }
3256 case EbtInt:
3257 switch (type.getNominalSize())
3258 {
3259 case 1: return "int";
3260 case 2: return "int2";
3261 case 3: return "int3";
3262 case 4: return "int4";
3263 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003264 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04003265 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003266 {
3267 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003268 case 2: return "uint2";
3269 case 3: return "uint3";
3270 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003271 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003272 case EbtBool:
3273 switch (type.getNominalSize())
3274 {
3275 case 1: return "bool";
3276 case 2: return "bool2";
3277 case 3: return "bool3";
3278 case 4: return "bool4";
3279 }
3280 case EbtVoid:
3281 return "void";
3282 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003283 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04003284 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003285 case EbtSampler2DArray:
3286 case EbtISampler2DArray:
3287 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003288 return "sampler2D";
3289 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003290 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04003291 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003292 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00003293 case EbtSamplerExternalOES:
3294 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00003295 default:
3296 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003297 }
3298 }
3299
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003300 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003301 return "<unknown type>";
3302}
3303
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003304TString OutputHLSL::textureString(const TType &type)
3305{
3306 switch (type.getBasicType())
3307 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003308 case EbtSampler2D: return "Texture2D";
3309 case EbtSamplerCube: return "TextureCube";
3310 case EbtSamplerExternalOES: return "Texture2D";
3311 case EbtSampler2DArray: return "Texture2DArray";
3312 case EbtSampler3D: return "Texture3D";
3313 case EbtISampler2D: return "Texture2D<int4>";
3314 case EbtISampler3D: return "Texture3D<int4>";
3315 case EbtISamplerCube: return "TextureCube<int4>";
3316 case EbtISampler2DArray: return "Texture2DArray<int4>";
3317 case EbtUSampler2D: return "Texture2D<uint4>";
3318 case EbtUSampler3D: return "Texture3D<uint4>";
3319 case EbtUSamplerCube: return "TextureCube<uint4>";
3320 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
3321 case EbtSampler2DShadow: return "Texture2D";
3322 case EbtSamplerCubeShadow: return "TextureCube";
3323 case EbtSampler2DArrayShadow: return "Texture2DArray";
3324 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003325 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04003326
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003327 return "<unknown texture type>";
3328}
3329
Nicolas Capenscb127d32013-07-15 17:26:18 -04003330TString OutputHLSL::samplerString(const TType &type)
3331{
3332 if (IsShadowSampler(type.getBasicType()))
3333 {
3334 return "SamplerComparisonState";
3335 }
3336 else
3337 {
3338 return "SamplerState";
3339 }
3340}
3341
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003342TString OutputHLSL::arrayString(const TType &type)
3343{
3344 if (!type.isArray())
3345 {
3346 return "";
3347 }
3348
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003349 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003350}
3351
3352TString OutputHLSL::initializer(const TType &type)
3353{
3354 TString string;
3355
Jamie Madill94bf7f22013-07-08 13:31:15 -04003356 size_t size = type.getObjectSize();
3357 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003358 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003359 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003360
Jamie Madill94bf7f22013-07-08 13:31:15 -04003361 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003362 {
3363 string += ", ";
3364 }
3365 }
3366
daniel@transgaming.comead23042010-04-29 03:35:36 +00003367 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003368}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003369
Jamie Madill98493dd2013-07-08 14:39:03 -04003370TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003371{
Jamie Madill98493dd2013-07-08 14:39:03 -04003372 const TFieldList &fields = structure.fields();
3373 const bool isNameless = (structure.name() == "");
3374 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003375 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3376
Jamie Madill98493dd2013-07-08 14:39:03 -04003377 TString string;
3378 string += declareString + "\n"
3379 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003380
Jamie Madillc835df62013-06-21 09:15:32 -04003381 int elementIndex = 0;
3382
Jamie Madill9cf6c072013-06-20 11:55:53 -04003383 for (unsigned int i = 0; i < fields.size(); i++)
3384 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003385 const TField &field = *fields[i];
3386 const TType &fieldType = *field.type();
3387 const TStructure *fieldStruct = fieldType.getStruct();
3388 const TString &fieldTypeString = fieldStruct ? structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) : typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003389
Jamie Madillc835df62013-06-21 09:15:32 -04003390 if (useStd140Packing)
3391 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003392 string += std140PrePaddingString(*field.type(), &elementIndex);
Jamie Madillc835df62013-06-21 09:15:32 -04003393 }
3394
Jamie Madill98493dd2013-07-08 14:39:03 -04003395 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003396
3397 if (useStd140Packing)
3398 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003399 string += std140PostPaddingString(*field.type(), useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003400 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003401 }
3402
3403 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003404 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003405
Jamie Madille4075c92013-06-21 09:15:32 -04003406 // Add remaining element index to the global map, for use with nested structs in standard layouts
3407 if (useStd140Packing)
3408 {
3409 mStd140StructElementIndexes[structName] = elementIndex;
3410 }
3411
Jamie Madill98493dd2013-07-08 14:39:03 -04003412 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003413}
3414
Jamie Madill98493dd2013-07-08 14:39:03 -04003415TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003416{
Jamie Madill98493dd2013-07-08 14:39:03 -04003417 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003418 {
3419 return "";
3420 }
3421
3422 TString prefix = "";
3423
3424 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3425 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003426
3427 if (useStd140Packing)
3428 {
3429 prefix += "std";
3430 }
3431
Jamie Madill9cf6c072013-06-20 11:55:53 -04003432 if (useHLSLRowMajorPacking)
3433 {
Jamie Madillc835df62013-06-21 09:15:32 -04003434 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003435 prefix += "rm";
3436 }
3437
Jamie Madill98493dd2013-07-08 14:39:03 -04003438 return prefix + structLookup(structure.name());
Jamie Madill9cf6c072013-06-20 11:55:53 -04003439}
3440
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003441void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003442{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003443 if (name == "")
3444 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003445 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003446 }
3447
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003448 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
3449 {
3450 return; // Already added
3451 }
3452
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003453 TType ctorType = type;
3454 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003455 ctorType.setPrecision(EbpHigh);
3456 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003457
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003458 TString ctorName = type.getStruct() ? decorate(name) : name;
3459
3460 typedef std::vector<TType> ParameterArray;
3461 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003462
Jamie Madill98493dd2013-07-08 14:39:03 -04003463 const TStructure* structure = type.getStruct();
3464 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003465 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003466 mStructNames.insert(decorate(name));
3467
Jamie Madill98493dd2013-07-08 14:39:03 -04003468 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003469
Jamie Madill98493dd2013-07-08 14:39:03 -04003470 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003471 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003472 // Add row-major packed struct for interface blocks
3473 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003474 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003475 "#pragma pack_matrix(column_major)\n";
3476
Jamie Madillc835df62013-06-21 09:15:32 -04003477 const TString &std140Prefix = "std";
Jamie Madill98493dd2013-07-08 14:39:03 -04003478 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003479
3480 const TString &std140RowMajorPrefix = "std_rm";
3481 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003482 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003483 "#pragma pack_matrix(column_major)\n";
3484
Jamie Madill98493dd2013-07-08 14:39:03 -04003485 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003486 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003487 mStructDeclarations.push_back(std140String);
3488 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003489 }
3490
Jamie Madill98493dd2013-07-08 14:39:03 -04003491 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003492 for (unsigned int i = 0; i < fields.size(); i++)
3493 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003494 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003495 }
3496 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003497 else if (parameters)
3498 {
3499 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3500 {
3501 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3502 }
3503 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003504 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003505
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003506 TString constructor;
3507
3508 if (ctorType.getStruct())
3509 {
3510 constructor += ctorName + " " + ctorName + "_ctor(";
3511 }
3512 else // Built-in type
3513 {
3514 constructor += typeString(ctorType) + " " + ctorName + "(";
3515 }
3516
3517 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3518 {
3519 const TType &type = ctorParameters[parameter];
3520
3521 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3522
3523 if (parameter < ctorParameters.size() - 1)
3524 {
3525 constructor += ", ";
3526 }
3527 }
3528
3529 constructor += ")\n"
3530 "{\n";
3531
3532 if (ctorType.getStruct())
3533 {
3534 constructor += " " + ctorName + " structure = {";
3535 }
3536 else
3537 {
3538 constructor += " return " + typeString(ctorType) + "(";
3539 }
3540
3541 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3542 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003543 int rows = ctorType.getRows();
3544 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003545 const TType &parameter = ctorParameters[0];
3546
3547 if (parameter.isScalar())
3548 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003549 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003550 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003551 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003552 {
3553 constructor += TString((row == col) ? "x0" : "0.0");
3554
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003555 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003556 {
3557 constructor += ", ";
3558 }
3559 }
3560 }
3561 }
3562 else if (parameter.isMatrix())
3563 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003564 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003565 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003566 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003567 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003568 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003569 {
3570 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3571 }
3572 else
3573 {
3574 constructor += TString((row == col) ? "1.0" : "0.0");
3575 }
3576
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003577 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003578 {
3579 constructor += ", ";
3580 }
3581 }
3582 }
3583 }
3584 else UNREACHABLE();
3585 }
3586 else
3587 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003588 size_t remainingComponents = ctorType.getObjectSize();
3589 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003590
3591 while (remainingComponents > 0)
3592 {
3593 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003594 const size_t parameterSize = parameter.getObjectSize();
3595 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003596
3597 constructor += "x" + str(parameterIndex);
3598
3599 if (parameter.isScalar())
3600 {
3601 remainingComponents -= parameter.getObjectSize();
3602 }
3603 else if (parameter.isVector())
3604 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003605 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003606 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003607 ASSERT(parameterSize <= remainingComponents);
3608 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003609 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003610 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003611 {
3612 switch (remainingComponents)
3613 {
3614 case 1: constructor += ".x"; break;
3615 case 2: constructor += ".xy"; break;
3616 case 3: constructor += ".xyz"; break;
3617 case 4: constructor += ".xyzw"; break;
3618 default: UNREACHABLE();
3619 }
3620
3621 remainingComponents = 0;
3622 }
3623 else UNREACHABLE();
3624 }
3625 else if (parameter.isMatrix() || parameter.getStruct())
3626 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003627 ASSERT(remainingComponents == parameterSize || moreParameters);
3628 ASSERT(parameterSize <= remainingComponents);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003629
Jamie Madill94bf7f22013-07-08 13:31:15 -04003630 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003631 }
3632 else UNREACHABLE();
3633
3634 if (moreParameters)
3635 {
3636 parameterIndex++;
3637 }
3638
3639 if (remainingComponents)
3640 {
3641 constructor += ", ";
3642 }
3643 }
3644 }
3645
3646 if (ctorType.getStruct())
3647 {
3648 constructor += "};\n"
3649 " return structure;\n"
3650 "}\n";
3651 }
3652 else
3653 {
3654 constructor += ");\n"
3655 "}\n";
3656 }
3657
daniel@transgaming.com63691862010-04-29 03:32:42 +00003658 mConstructors.insert(constructor);
3659}
3660
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003661const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3662{
3663 TInfoSinkBase &out = mBody;
3664
Jamie Madill98493dd2013-07-08 14:39:03 -04003665 const TStructure* structure = type.getStruct();
3666 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003667 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003668 out << structLookup(structure->name()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003669
Jamie Madill98493dd2013-07-08 14:39:03 -04003670 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003671
Jamie Madill98493dd2013-07-08 14:39:03 -04003672 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003673 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003674 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003675
3676 constUnion = writeConstantUnion(*fieldType, constUnion);
3677
Jamie Madill98493dd2013-07-08 14:39:03 -04003678 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003679 {
3680 out << ", ";
3681 }
3682 }
3683
3684 out << ")";
3685 }
3686 else
3687 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003688 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003689 bool writeType = size > 1;
3690
3691 if (writeType)
3692 {
3693 out << typeString(type) << "(";
3694 }
3695
Jamie Madill94bf7f22013-07-08 13:31:15 -04003696 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003697 {
3698 switch (constUnion->getType())
3699 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003700 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003701 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003702 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003703 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003704 default: UNREACHABLE();
3705 }
3706
3707 if (i != size - 1)
3708 {
3709 out << ", ";
3710 }
3711 }
3712
3713 if (writeType)
3714 {
3715 out << ")";
3716 }
3717 }
3718
3719 return constUnion;
3720}
3721
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003722TString OutputHLSL::scopeString(unsigned int depthLimit)
3723{
3724 TString string;
3725
3726 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3727 {
3728 string += "_" + str(i);
3729 }
3730
3731 return string;
3732}
3733
3734TString OutputHLSL::scopedStruct(const TString &typeName)
3735{
3736 if (typeName == "")
3737 {
3738 return typeName;
3739 }
3740
3741 return typeName + scopeString(mScopeDepth);
3742}
3743
3744TString OutputHLSL::structLookup(const TString &typeName)
3745{
3746 for (int depth = mScopeDepth; depth >= 0; depth--)
3747 {
3748 TString scopedName = decorate(typeName + scopeString(depth));
3749
3750 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3751 {
3752 if (*structName == scopedName)
3753 {
3754 return scopedName;
3755 }
3756 }
3757 }
3758
3759 UNREACHABLE(); // Should have found a matching constructor
3760
3761 return typeName;
3762}
3763
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003764TString OutputHLSL::decorate(const TString &string)
3765{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003766 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003767 {
3768 return "_" + string;
3769 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003770
3771 return string;
3772}
3773
apatrick@chromium.org65756022012-01-17 21:45:38 +00003774TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003775{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003776 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003777 {
3778 return "ex_" + string;
3779 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003780
3781 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003782}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003783
Jamie Madill98493dd2013-07-08 14:39:03 -04003784TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003785{
Jamie Madill98493dd2013-07-08 14:39:03 -04003786 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003787 {
3788 return decorate(string);
3789 }
3790
3791 return string;
3792}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003793
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003794void OutputHLSL::declareInterfaceBlockField(const TType &type, const TString &name, std::vector<InterfaceBlockField>& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003795{
Jamie Madill98493dd2013-07-08 14:39:03 -04003796 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003797
3798 if (!structure)
3799 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003800 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003801 InterfaceBlockField field(glVariableType(type), glVariablePrecision(type), name.c_str(),
3802 (unsigned int)type.getArraySize(), isRowMajorMatrix);
3803 output.push_back(field);
3804 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003805 else
3806 {
Jamie Madill28167c62013-08-30 13:21:10 -04003807 InterfaceBlockField structField(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), false);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003808
3809 const TFieldList &fields = structure->fields();
3810
3811 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3812 {
3813 TField *field = fields[fieldIndex];
3814 TType *fieldType = field->type();
3815
3816 // make sure to copy matrix packing information
3817 fieldType->setLayoutQualifier(type.getLayoutQualifier());
3818
3819 declareInterfaceBlockField(*fieldType, field->name(), structField.fields);
3820 }
3821
3822 output.push_back(structField);
3823 }
3824}
3825
Jamie Madillc2141fb2013-08-30 13:21:08 -04003826Uniform OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, std::vector<Uniform>& output)
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003827{
3828 const TStructure *structure = type.getStruct();
3829
3830 if (!structure)
3831 {
3832 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
3833 Uniform uniform(glVariableType(type), glVariablePrecision(type), name.c_str(),
Jamie Madill56093782013-08-30 13:21:11 -04003834 (unsigned int)type.getArraySize(), (unsigned int)registerIndex, 0);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003835 output.push_back(uniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003836
3837 return uniform;
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003838 }
3839 else
3840 {
Jamie Madill56093782013-08-30 13:21:11 -04003841 Uniform structUniform(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(),
3842 (unsigned int)registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003843
Jamie Madill98493dd2013-07-08 14:39:03 -04003844 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003845
Jamie Madill98493dd2013-07-08 14:39:03 -04003846 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003847 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003848 TField *field = fields[fieldIndex];
3849 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003850
Jamie Madill56093782013-08-30 13:21:11 -04003851 declareUniformToList(*fieldType, field->name(), GL_INVALID_INDEX, structUniform.fields);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003852 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003853
Jamie Madill56093782013-08-30 13:21:11 -04003854 // assign register offset information -- this will override the information in any sub-structures.
3855 HLSLVariableGetRegisterInfo(registerIndex, &structUniform);
3856
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003857 output.push_back(structUniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003858
3859 return structUniform;
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003860 }
3861}
3862
Jamie Madill139b9092013-08-30 13:21:06 -04003863InterpolationType getInterpolationType(TQualifier qualifier)
3864{
3865 switch (qualifier)
3866 {
3867 case EvqFlatIn:
3868 case EvqFlatOut:
3869 return INTERPOLATION_FLAT;
3870
3871 case EvqSmoothIn:
3872 case EvqSmoothOut:
3873 case EvqVertexOut:
3874 case EvqFragmentIn:
Jamie Madill384b6042013-09-13 10:06:24 -04003875 case EvqVaryingIn:
3876 case EvqVaryingOut:
Jamie Madill139b9092013-08-30 13:21:06 -04003877 return INTERPOLATION_SMOOTH;
3878
3879 case EvqCentroidIn:
3880 case EvqCentroidOut:
3881 return INTERPOLATION_CENTROID;
3882
3883 default: UNREACHABLE();
3884 return INTERPOLATION_SMOOTH;
3885 }
3886}
3887
Jamie Madill94599662013-08-30 13:21:10 -04003888void OutputHLSL::declareVaryingToList(const TType &type, TQualifier baseTypeQualifier, const TString &name, std::vector<Varying>& fieldsOut)
Jamie Madill47fdd132013-08-30 13:21:04 -04003889{
3890 const TStructure *structure = type.getStruct();
3891
Jamie Madill94599662013-08-30 13:21:10 -04003892 InterpolationType interpolation = getInterpolationType(baseTypeQualifier);
Jamie Madill47fdd132013-08-30 13:21:04 -04003893 if (!structure)
3894 {
Jamie Madill139b9092013-08-30 13:21:06 -04003895 Varying varying(glVariableType(type), glVariablePrecision(type), name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003896 fieldsOut.push_back(varying);
3897 }
3898 else
3899 {
Jamie Madill28167c62013-08-30 13:21:10 -04003900 Varying structVarying(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003901 const TFieldList &fields = structure->fields();
3902
Jamie Madill28167c62013-08-30 13:21:10 -04003903 structVarying.structName = structure->name().c_str();
3904
Jamie Madill47fdd132013-08-30 13:21:04 -04003905 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3906 {
3907 const TField &field = *fields[fieldIndex];
Jamie Madill94599662013-08-30 13:21:10 -04003908 declareVaryingToList(*field.type(), baseTypeQualifier, field.name(), structVarying.fields);
Jamie Madill47fdd132013-08-30 13:21:04 -04003909 }
3910
3911 fieldsOut.push_back(structVarying);
3912 }
3913}
3914
Jamie Madillc2141fb2013-08-30 13:21:08 -04003915int OutputHLSL::declareUniformAndAssignRegister(const TType &type, const TString &name)
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003916{
Jamie Madillc2141fb2013-08-30 13:21:08 -04003917 int registerIndex = (IsSampler(type.getBasicType()) ? mSamplerRegister : mUniformRegister);
3918
3919 const Uniform &uniform = declareUniformToList(type, name, registerIndex, mActiveUniforms);
3920
3921 if (IsSampler(type.getBasicType()))
3922 {
3923 mSamplerRegister += HLSLVariableRegisterCount(uniform);
3924 }
3925 else
3926 {
3927 mUniformRegister += HLSLVariableRegisterCount(uniform);
3928 }
3929
3930 return registerIndex;
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003931}
3932
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003933GLenum OutputHLSL::glVariableType(const TType &type)
3934{
3935 if (type.getBasicType() == EbtFloat)
3936 {
3937 if (type.isScalar())
3938 {
3939 return GL_FLOAT;
3940 }
3941 else if (type.isVector())
3942 {
3943 switch(type.getNominalSize())
3944 {
3945 case 2: return GL_FLOAT_VEC2;
3946 case 3: return GL_FLOAT_VEC3;
3947 case 4: return GL_FLOAT_VEC4;
3948 default: UNREACHABLE();
3949 }
3950 }
3951 else if (type.isMatrix())
3952 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003953 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003954 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003955 case 2:
3956 switch(type.getRows())
3957 {
3958 case 2: return GL_FLOAT_MAT2;
3959 case 3: return GL_FLOAT_MAT2x3;
3960 case 4: return GL_FLOAT_MAT2x4;
3961 default: UNREACHABLE();
3962 }
3963
3964 case 3:
3965 switch(type.getRows())
3966 {
3967 case 2: return GL_FLOAT_MAT3x2;
3968 case 3: return GL_FLOAT_MAT3;
3969 case 4: return GL_FLOAT_MAT3x4;
3970 default: UNREACHABLE();
3971 }
3972
3973 case 4:
3974 switch(type.getRows())
3975 {
3976 case 2: return GL_FLOAT_MAT4x2;
3977 case 3: return GL_FLOAT_MAT4x3;
3978 case 4: return GL_FLOAT_MAT4;
3979 default: UNREACHABLE();
3980 }
3981
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003982 default: UNREACHABLE();
3983 }
3984 }
3985 else UNREACHABLE();
3986 }
3987 else if (type.getBasicType() == EbtInt)
3988 {
3989 if (type.isScalar())
3990 {
3991 return GL_INT;
3992 }
3993 else if (type.isVector())
3994 {
3995 switch(type.getNominalSize())
3996 {
3997 case 2: return GL_INT_VEC2;
3998 case 3: return GL_INT_VEC3;
3999 case 4: return GL_INT_VEC4;
4000 default: UNREACHABLE();
4001 }
4002 }
4003 else UNREACHABLE();
4004 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004005 else if (type.getBasicType() == EbtUInt)
4006 {
4007 if (type.isScalar())
4008 {
4009 return GL_UNSIGNED_INT;
4010 }
4011 else if (type.isVector())
4012 {
Jamie Madill22d63da2013-06-07 12:45:12 -04004013 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00004014 {
4015 case 2: return GL_UNSIGNED_INT_VEC2;
4016 case 3: return GL_UNSIGNED_INT_VEC3;
4017 case 4: return GL_UNSIGNED_INT_VEC4;
4018 default: UNREACHABLE();
4019 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004020 }
4021 else UNREACHABLE();
4022 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004023 else if (type.getBasicType() == EbtBool)
4024 {
4025 if (type.isScalar())
4026 {
4027 return GL_BOOL;
4028 }
4029 else if (type.isVector())
4030 {
4031 switch(type.getNominalSize())
4032 {
4033 case 2: return GL_BOOL_VEC2;
4034 case 3: return GL_BOOL_VEC3;
4035 case 4: return GL_BOOL_VEC4;
4036 default: UNREACHABLE();
4037 }
4038 }
4039 else UNREACHABLE();
4040 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004041
4042 switch(type.getBasicType())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004043 {
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004044 case EbtSampler2D: return GL_SAMPLER_2D;
4045 case EbtSampler3D: return GL_SAMPLER_3D;
4046 case EbtSamplerCube: return GL_SAMPLER_CUBE;
4047 case EbtSampler2DArray: return GL_SAMPLER_2D_ARRAY;
4048 case EbtISampler2D: return GL_INT_SAMPLER_2D;
4049 case EbtISampler3D: return GL_INT_SAMPLER_3D;
4050 case EbtISamplerCube: return GL_INT_SAMPLER_CUBE;
4051 case EbtISampler2DArray: return GL_INT_SAMPLER_2D_ARRAY;
4052 case EbtUSampler2D: return GL_UNSIGNED_INT_SAMPLER_2D;
4053 case EbtUSampler3D: return GL_UNSIGNED_INT_SAMPLER_3D;
4054 case EbtUSamplerCube: return GL_UNSIGNED_INT_SAMPLER_CUBE;
4055 case EbtUSampler2DArray: return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
4056 case EbtSampler2DShadow: return GL_SAMPLER_2D_SHADOW;
4057 case EbtSamplerCubeShadow: return GL_SAMPLER_CUBE_SHADOW;
4058 case EbtSampler2DArrayShadow: return GL_SAMPLER_2D_ARRAY_SHADOW;
4059 default: UNREACHABLE();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004060 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004061
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004062 return GL_NONE;
4063}
4064
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004065GLenum OutputHLSL::glVariablePrecision(const TType &type)
4066{
4067 if (type.getBasicType() == EbtFloat)
4068 {
4069 switch (type.getPrecision())
4070 {
4071 case EbpHigh: return GL_HIGH_FLOAT;
4072 case EbpMedium: return GL_MEDIUM_FLOAT;
4073 case EbpLow: return GL_LOW_FLOAT;
4074 case EbpUndefined:
4075 // Should be defined as the default precision by the parser
4076 default: UNREACHABLE();
4077 }
4078 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004079 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004080 {
4081 switch (type.getPrecision())
4082 {
4083 case EbpHigh: return GL_HIGH_INT;
4084 case EbpMedium: return GL_MEDIUM_INT;
4085 case EbpLow: return GL_LOW_INT;
4086 case EbpUndefined:
4087 // Should be defined as the default precision by the parser
4088 default: UNREACHABLE();
4089 }
4090 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004091
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00004092 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004093 return GL_NONE;
4094}
4095
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004096bool OutputHLSL::isVaryingOut(TQualifier qualifier)
4097{
4098 switch(qualifier)
4099 {
4100 case EvqVaryingOut:
4101 case EvqInvariantVaryingOut:
4102 case EvqSmoothOut:
4103 case EvqFlatOut:
4104 case EvqCentroidOut:
Jamie Madill19571812013-08-12 15:26:34 -07004105 case EvqVertexOut:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004106 return true;
4107 }
4108
4109 return false;
4110}
4111
4112bool OutputHLSL::isVaryingIn(TQualifier qualifier)
4113{
4114 switch(qualifier)
4115 {
4116 case EvqVaryingIn:
4117 case EvqInvariantVaryingIn:
4118 case EvqSmoothIn:
4119 case EvqFlatIn:
4120 case EvqCentroidIn:
Jamie Madill19571812013-08-12 15:26:34 -07004121 case EvqFragmentIn:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004122 return true;
4123 }
4124
4125 return false;
4126}
4127
4128bool OutputHLSL::isVarying(TQualifier qualifier)
4129{
4130 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
4131}
4132
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004133}