blob: 9422e3f27d896dc69abd3f0518c6b05e71b8fedb [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;
Nicolas Capensbf7db102014-02-19 17:20:28 -05001079 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001080 default: UNREACHABLE();
1081 }
1082 }
1083
1084 if (textureFunction->method == TextureFunction::BIAS)
1085 {
1086 out << ", float bias";
1087 }
1088
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001089 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001090 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001091
Nicolas Capens75fb4752013-07-10 15:14:47 -04001092 if (textureFunction->method == TextureFunction::SIZE)
1093 {
1094 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
1095 {
1096 if (IsSamplerArray(textureFunction->sampler))
1097 {
1098 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
1099 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
1100 }
1101 else
1102 {
1103 out << " uint width; uint height; uint numberOfLevels;\n"
1104 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
1105 }
1106 }
1107 else if (IsSampler3D(textureFunction->sampler))
1108 {
1109 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
1110 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
1111 }
1112 else UNREACHABLE();
1113
1114 switch(textureFunction->sampler)
1115 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04001116 case EbtSampler2D: out << " return int2(width, height);"; break;
1117 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
1118 case EbtSamplerCube: out << " return int2(width, height);"; break;
1119 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
1120 case EbtISampler2D: out << " return int2(width, height);"; break;
1121 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
1122 case EbtISamplerCube: out << " return int2(width, height);"; break;
1123 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
1124 case EbtUSampler2D: out << " return int2(width, height);"; break;
1125 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
1126 case EbtUSamplerCube: out << " return int2(width, height);"; break;
1127 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
1128 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
1129 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
1130 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001131 default: UNREACHABLE();
1132 }
1133 }
1134 else if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
daniel@transgaming.com15795192011-05-11 15:36:20 +00001135 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001136 // Currently unsupported because TextureCube does not support Load
1137 // This will require emulation using a Texture2DArray with 6 faces
1138 if (textureFunction->sampler == EbtISamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001139 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001140 out << " return int4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001141 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001142 else if (textureFunction->sampler == EbtUSamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001143 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001144 out << " return uint4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001145 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001146 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001147 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001148 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001149 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001150 if (IsIntegerSampler(textureFunction->sampler) &&
1151 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001152 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001153 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001154 {
Nicolas Capens93e50de2013-07-09 13:46:28 -04001155 if (IsSamplerArray(textureFunction->sampler))
1156 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001157 out << " float width; float height; float layers; float levels;\n";
1158
1159 if (textureFunction->method == TextureFunction::LOD0)
1160 {
1161 out << " uint mip = 0;\n";
1162 }
1163 else
1164 {
1165 if (textureFunction->method == TextureFunction::IMPLICIT ||
1166 textureFunction->method == TextureFunction::BIAS)
1167 {
1168 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1169 " float2 tSized = float2(t.x * width, t.y * height);\n"
1170 " float dx = length(ddx(tSized));\n"
1171 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001172 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001173
1174 if (textureFunction->method == TextureFunction::BIAS)
1175 {
1176 out << " lod += bias;\n";
1177 }
1178 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001179 else if (textureFunction->method == TextureFunction::GRAD)
1180 {
1181 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1182 " float lod = log2(max(length(ddx), length(ddy)));\n";
1183 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001184
1185 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1186 }
1187
1188 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001189 }
1190 else
1191 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001192 out << " float width; float height; float levels;\n";
1193
1194 if (textureFunction->method == TextureFunction::LOD0)
1195 {
1196 out << " uint mip = 0;\n";
1197 }
1198 else
1199 {
1200 if (textureFunction->method == TextureFunction::IMPLICIT ||
1201 textureFunction->method == TextureFunction::BIAS)
1202 {
1203 out << " x.GetDimensions(0, width, height, levels);\n"
1204 " float2 tSized = float2(t.x * width, t.y * height);\n"
1205 " float dx = length(ddx(tSized));\n"
1206 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001207 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001208
1209 if (textureFunction->method == TextureFunction::BIAS)
1210 {
1211 out << " lod += bias;\n";
1212 }
1213 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05001214 else if (textureFunction->method == TextureFunction::LOD)
1215 {
1216 out << " x.GetDimensions(0, width, height, levels);\n";
1217 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001218 else if (textureFunction->method == TextureFunction::GRAD)
1219 {
1220 out << " x.GetDimensions(0, width, height, levels);\n"
1221 " float lod = log2(max(length(ddx), length(ddy)));\n";
1222 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001223
1224 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1225 }
1226
1227 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001228 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001229 }
1230 else if (IsSampler3D(textureFunction->sampler))
1231 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001232 out << " float width; float height; float depth; float levels;\n";
1233
1234 if (textureFunction->method == TextureFunction::LOD0)
1235 {
1236 out << " uint mip = 0;\n";
1237 }
1238 else
1239 {
1240 if (textureFunction->method == TextureFunction::IMPLICIT ||
1241 textureFunction->method == TextureFunction::BIAS)
1242 {
1243 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1244 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
1245 " float dx = length(ddx(tSized));\n"
1246 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001247 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001248
1249 if (textureFunction->method == TextureFunction::BIAS)
1250 {
1251 out << " lod += bias;\n";
1252 }
1253 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001254 else if (textureFunction->method == TextureFunction::GRAD)
1255 {
1256 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1257 " float lod = log2(max(length(ddx), length(ddy)));\n";
1258 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001259
1260 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1261 }
1262
1263 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001264 }
1265 else UNREACHABLE();
1266 }
1267
1268 out << " return ";
1269
1270 // HLSL intrinsic
1271 if (mOutputType == SH_HLSL9_OUTPUT)
1272 {
1273 switch(textureFunction->sampler)
1274 {
1275 case EbtSampler2D: out << "tex2D"; break;
1276 case EbtSamplerCube: out << "texCUBE"; break;
1277 default: UNREACHABLE();
1278 }
1279
Nicolas Capens75fb4752013-07-10 15:14:47 -04001280 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001281 {
1282 case TextureFunction::IMPLICIT: out << "(s, "; break;
1283 case TextureFunction::BIAS: out << "bias(s, "; break;
1284 case TextureFunction::LOD: out << "lod(s, "; break;
1285 case TextureFunction::LOD0: out << "lod(s, "; break;
1286 default: UNREACHABLE();
1287 }
1288 }
1289 else if (mOutputType == SH_HLSL11_OUTPUT)
1290 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001291 if (textureFunction->method == TextureFunction::GRAD)
1292 {
1293 if (IsIntegerSampler(textureFunction->sampler))
1294 {
1295 out << "x.Load(";
1296 }
1297 else if (IsShadowSampler(textureFunction->sampler))
1298 {
1299 out << "x.SampleCmpLevelZero(s, ";
1300 }
1301 else
1302 {
1303 out << "x.SampleGrad(s, ";
1304 }
1305 }
1306 else if (IsIntegerSampler(textureFunction->sampler) ||
1307 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001308 {
1309 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001310 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001311 else if (IsShadowSampler(textureFunction->sampler))
1312 {
1313 out << "x.SampleCmp(s, ";
1314 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001315 else
1316 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001317 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001318 {
1319 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1320 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1321 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1322 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
1323 default: UNREACHABLE();
1324 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001325 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001326 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001327 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001328
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001329 // Integer sampling requires integer addresses
1330 TString addressx = "";
1331 TString addressy = "";
1332 TString addressz = "";
1333 TString close = "";
1334
Nicolas Capensfc014542014-02-18 14:47:13 -05001335 if (IsIntegerSampler(textureFunction->sampler) ||
1336 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001337 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001338 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001339 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001340 case 2: out << "int3("; break;
1341 case 3: out << "int4("; break;
1342 default: UNREACHABLE();
1343 }
1344
Nicolas Capensfc014542014-02-18 14:47:13 -05001345 // Convert from normalized floating-point to integer
1346 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001347 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001348 addressx = "int(floor(width * frac((";
1349 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001350
Nicolas Capensfc014542014-02-18 14:47:13 -05001351 if (IsSamplerArray(textureFunction->sampler))
1352 {
1353 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1354 }
1355 else
1356 {
1357 addressz = "int(floor(depth * frac((";
1358 }
1359
1360 close = "))))";
1361 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001362 }
1363 else
1364 {
1365 switch(hlslCoords)
1366 {
1367 case 2: out << "float2("; break;
1368 case 3: out << "float3("; break;
1369 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001370 default: UNREACHABLE();
1371 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001372 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001373
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001374 TString proj = ""; // Only used for projected textures
1375
1376 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001377 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001378 switch(textureFunction->coords)
1379 {
1380 case 3: proj = " / t.z"; break;
1381 case 4: proj = " / t.w"; break;
1382 default: UNREACHABLE();
1383 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001384 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001385
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001386 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001387
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001388 if (mOutputType == SH_HLSL9_OUTPUT)
1389 {
1390 if (hlslCoords >= 3)
1391 {
1392 if (textureFunction->coords < 3)
1393 {
1394 out << ", 0";
1395 }
1396 else
1397 {
1398 out << ", t.z" + proj;
1399 }
1400 }
1401
1402 if (hlslCoords == 4)
1403 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001404 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001405 {
1406 case TextureFunction::BIAS: out << ", bias"; break;
1407 case TextureFunction::LOD: out << ", lod"; break;
1408 case TextureFunction::LOD0: out << ", 0"; break;
1409 default: UNREACHABLE();
1410 }
1411 }
1412
1413 out << "));\n";
1414 }
1415 else if (mOutputType == SH_HLSL11_OUTPUT)
1416 {
1417 if (hlslCoords >= 3)
1418 {
1419 out << ", " + addressz + ("t.z" + proj) + close;
1420 }
1421
Nicolas Capensd11d5492014-02-19 17:06:10 -05001422 if (textureFunction->method == TextureFunction::GRAD)
1423 {
1424 if (IsIntegerSampler(textureFunction->sampler))
1425 {
1426 out << ", mip)";
1427 }
1428 else if (IsShadowSampler(textureFunction->sampler))
1429 {
1430 // Compare value
1431 switch(textureFunction->coords)
1432 {
1433 case 3: out << "), t.z"; break;
1434 case 4: out << "), t.w"; break;
1435 default: UNREACHABLE();
1436 }
1437 }
1438 else
1439 {
1440 out << "), ddx, ddy";
1441 }
1442 }
1443 else if (IsIntegerSampler(textureFunction->sampler) ||
1444 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001445 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001446 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001447 }
1448 else if (IsShadowSampler(textureFunction->sampler))
1449 {
1450 // Compare value
1451 switch(textureFunction->coords)
1452 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001453 case 3: out << "), t.z"; break;
1454 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001455 default: UNREACHABLE();
1456 }
1457 }
1458 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001459 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001460 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001461 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001462 case TextureFunction::IMPLICIT: out << ")"; break;
1463 case TextureFunction::BIAS: out << "), bias"; break;
1464 case TextureFunction::LOD: out << "), lod"; break;
1465 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001466 default: UNREACHABLE();
1467 }
1468 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001469
1470 if (textureFunction->offset)
1471 {
1472 out << ", offset";
1473 }
1474
1475 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001476 }
1477 else UNREACHABLE();
1478 }
1479
1480 out << "\n"
1481 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001482 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001483 }
1484
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001485 if (mUsesFragCoord)
1486 {
1487 out << "#define GL_USES_FRAG_COORD\n";
1488 }
1489
1490 if (mUsesPointCoord)
1491 {
1492 out << "#define GL_USES_POINT_COORD\n";
1493 }
1494
1495 if (mUsesFrontFacing)
1496 {
1497 out << "#define GL_USES_FRONT_FACING\n";
1498 }
1499
1500 if (mUsesPointSize)
1501 {
1502 out << "#define GL_USES_POINT_SIZE\n";
1503 }
1504
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001505 if (mUsesFragDepth)
1506 {
1507 out << "#define GL_USES_FRAG_DEPTH\n";
1508 }
1509
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001510 if (mUsesDepthRange)
1511 {
1512 out << "#define GL_USES_DEPTH_RANGE\n";
1513 }
1514
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001515 if (mUsesXor)
1516 {
1517 out << "bool xor(bool p, bool q)\n"
1518 "{\n"
1519 " return (p || q) && !(p && q);\n"
1520 "}\n"
1521 "\n";
1522 }
1523
1524 if (mUsesMod1)
1525 {
1526 out << "float mod(float x, float y)\n"
1527 "{\n"
1528 " return x - y * floor(x / y);\n"
1529 "}\n"
1530 "\n";
1531 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001532
1533 if (mUsesMod2v)
1534 {
1535 out << "float2 mod(float2 x, float2 y)\n"
1536 "{\n"
1537 " return x - y * floor(x / y);\n"
1538 "}\n"
1539 "\n";
1540 }
1541
1542 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001543 {
1544 out << "float2 mod(float2 x, float y)\n"
1545 "{\n"
1546 " return x - y * floor(x / y);\n"
1547 "}\n"
1548 "\n";
1549 }
1550
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001551 if (mUsesMod3v)
1552 {
1553 out << "float3 mod(float3 x, float3 y)\n"
1554 "{\n"
1555 " return x - y * floor(x / y);\n"
1556 "}\n"
1557 "\n";
1558 }
1559
1560 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001561 {
1562 out << "float3 mod(float3 x, float y)\n"
1563 "{\n"
1564 " return x - y * floor(x / y);\n"
1565 "}\n"
1566 "\n";
1567 }
1568
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001569 if (mUsesMod4v)
1570 {
1571 out << "float4 mod(float4 x, float4 y)\n"
1572 "{\n"
1573 " return x - y * floor(x / y);\n"
1574 "}\n"
1575 "\n";
1576 }
1577
1578 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001579 {
1580 out << "float4 mod(float4 x, float y)\n"
1581 "{\n"
1582 " return x - y * floor(x / y);\n"
1583 "}\n"
1584 "\n";
1585 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001586
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001587 if (mUsesFaceforward1)
1588 {
1589 out << "float faceforward(float N, float I, float Nref)\n"
1590 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001591 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001592 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001593 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001594 " }\n"
1595 " else\n"
1596 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001597 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001598 " }\n"
1599 "}\n"
1600 "\n";
1601 }
1602
1603 if (mUsesFaceforward2)
1604 {
1605 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1606 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001607 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001608 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001609 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001610 " }\n"
1611 " else\n"
1612 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001613 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001614 " }\n"
1615 "}\n"
1616 "\n";
1617 }
1618
1619 if (mUsesFaceforward3)
1620 {
1621 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1622 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001623 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001624 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001625 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001626 " }\n"
1627 " else\n"
1628 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001629 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001630 " }\n"
1631 "}\n"
1632 "\n";
1633 }
1634
1635 if (mUsesFaceforward4)
1636 {
1637 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1638 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001639 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001640 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001641 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001642 " }\n"
1643 " else\n"
1644 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001645 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001646 " }\n"
1647 "}\n"
1648 "\n";
1649 }
1650
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001651 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001652 {
1653 out << "float atanyx(float y, float x)\n"
1654 "{\n"
1655 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1656 " return atan2(y, x);\n"
1657 "}\n";
1658 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001659
1660 if (mUsesAtan2_2)
1661 {
1662 out << "float2 atanyx(float2 y, float2 x)\n"
1663 "{\n"
1664 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1665 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1666 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1667 "}\n";
1668 }
1669
1670 if (mUsesAtan2_3)
1671 {
1672 out << "float3 atanyx(float3 y, float3 x)\n"
1673 "{\n"
1674 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1675 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1676 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1677 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1678 "}\n";
1679 }
1680
1681 if (mUsesAtan2_4)
1682 {
1683 out << "float4 atanyx(float4 y, float4 x)\n"
1684 "{\n"
1685 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1686 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1687 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1688 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1689 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1690 "}\n";
1691 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001692}
1693
1694void OutputHLSL::visitSymbol(TIntermSymbol *node)
1695{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001696 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001697
Jamie Madill570e04d2013-06-21 09:15:33 -04001698 // Handle accessing std140 structs by value
1699 if (mFlaggedStructMappedNames.count(node) > 0)
1700 {
1701 out << mFlaggedStructMappedNames[node];
1702 return;
1703 }
1704
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001705 TString name = node->getSymbol();
1706
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001707 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001708 {
1709 mUsesDepthRange = true;
1710 out << name;
1711 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001712 else
1713 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001714 TQualifier qualifier = node->getQualifier();
1715
1716 if (qualifier == EvqUniform)
1717 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001718 const TType& nodeType = node->getType();
1719 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1720
1721 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001722 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001723 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001724 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001725 else
1726 {
1727 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001728 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001729
1730 out << decorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001731 }
Jamie Madill19571812013-08-12 15:26:34 -07001732 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001733 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001734 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001735 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001736 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001737 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001738 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001739 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001740 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001741 }
Jamie Madill19571812013-08-12 15:26:34 -07001742 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001743 {
1744 mReferencedOutputVariables[name] = node;
1745 out << "out_" << name;
1746 }
1747 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001748 {
1749 out << "gl_Color[0]";
1750 mUsesFragColor = true;
1751 }
1752 else if (qualifier == EvqFragData)
1753 {
1754 out << "gl_Color";
1755 mUsesFragData = true;
1756 }
1757 else if (qualifier == EvqFragCoord)
1758 {
1759 mUsesFragCoord = true;
1760 out << name;
1761 }
1762 else if (qualifier == EvqPointCoord)
1763 {
1764 mUsesPointCoord = true;
1765 out << name;
1766 }
1767 else if (qualifier == EvqFrontFacing)
1768 {
1769 mUsesFrontFacing = true;
1770 out << name;
1771 }
1772 else if (qualifier == EvqPointSize)
1773 {
1774 mUsesPointSize = true;
1775 out << name;
1776 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001777 else if (name == "gl_FragDepthEXT")
1778 {
1779 mUsesFragDepth = true;
1780 out << "gl_Depth";
1781 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001782 else if (qualifier == EvqInternal)
1783 {
1784 out << name;
1785 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001786 else
1787 {
1788 out << decorate(name);
1789 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001790 }
1791}
1792
1793bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1794{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001795 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001796
Jamie Madill570e04d2013-06-21 09:15:33 -04001797 // Handle accessing std140 structs by value
1798 if (mFlaggedStructMappedNames.count(node) > 0)
1799 {
1800 out << mFlaggedStructMappedNames[node];
1801 return false;
1802 }
1803
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001804 switch (node->getOp())
1805 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001806 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001807 case EOpInitialize:
1808 if (visit == PreVisit)
1809 {
1810 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1811 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1812 // new variable is created before the assignment is evaluated), so we need to convert
1813 // this to "float t = x, x = t;".
1814
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001815 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1816 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001817
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001818 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1819 expression->traverse(&searchSymbol);
1820 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001821
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001822 if (sameSymbol)
1823 {
1824 // Type already printed
1825 out << "t" + str(mUniqueIndex) + " = ";
1826 expression->traverse(this);
1827 out << ", ";
1828 symbolNode->traverse(this);
1829 out << " = t" + str(mUniqueIndex);
1830
1831 mUniqueIndex++;
1832 return false;
1833 }
1834 }
1835 else if (visit == InVisit)
1836 {
1837 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001838 }
1839 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001840 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1841 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1842 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1843 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1844 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1845 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001846 if (visit == PreVisit)
1847 {
1848 out << "(";
1849 }
1850 else if (visit == InVisit)
1851 {
1852 out << " = mul(";
1853 node->getLeft()->traverse(this);
1854 out << ", transpose(";
1855 }
1856 else
1857 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001858 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001859 }
1860 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001861 case EOpMatrixTimesMatrixAssign:
1862 if (visit == PreVisit)
1863 {
1864 out << "(";
1865 }
1866 else if (visit == InVisit)
1867 {
1868 out << " = mul(";
1869 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001870 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001871 }
1872 else
1873 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001874 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001875 }
1876 break;
1877 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001878 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001879 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001880 const TType& leftType = node->getLeft()->getType();
1881 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001882 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001883 if (visit == PreVisit)
1884 {
1885 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1886 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1887
1888 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1889 out << interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1890
1891 return false;
1892 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001893 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001894 else
1895 {
1896 outputTriplet(visit, "", "[", "]");
1897 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001898 }
1899 break;
1900 case EOpIndexIndirect:
1901 // We do not currently support indirect references to interface blocks
1902 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1903 outputTriplet(visit, "", "[", "]");
1904 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001905 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001906 if (visit == InVisit)
1907 {
1908 const TStructure* structure = node->getLeft()->getType().getStruct();
1909 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1910 const TField* field = structure->fields()[index->getIConst(0)];
1911 out << "." + decorateField(field->name(), *structure);
1912
1913 return false;
1914 }
1915 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001916 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001917 if (visit == InVisit)
1918 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001919 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1920 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1921 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
1922 out << "." + decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001923
1924 return false;
1925 }
1926 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001927 case EOpVectorSwizzle:
1928 if (visit == InVisit)
1929 {
1930 out << ".";
1931
1932 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1933
1934 if (swizzle)
1935 {
1936 TIntermSequence &sequence = swizzle->getSequence();
1937
1938 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1939 {
1940 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1941
1942 if (element)
1943 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001944 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001945
1946 switch (i)
1947 {
1948 case 0: out << "x"; break;
1949 case 1: out << "y"; break;
1950 case 2: out << "z"; break;
1951 case 3: out << "w"; break;
1952 default: UNREACHABLE();
1953 }
1954 }
1955 else UNREACHABLE();
1956 }
1957 }
1958 else UNREACHABLE();
1959
1960 return false; // Fully processed
1961 }
1962 break;
1963 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1964 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1965 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1966 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001967 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001968 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001969 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001970 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001971 if (node->getOp() == EOpEqual)
1972 {
1973 outputTriplet(visit, "(", " == ", ")");
1974 }
1975 else
1976 {
1977 outputTriplet(visit, "(", " != ", ")");
1978 }
1979 }
1980 else if (node->getLeft()->getBasicType() == EbtStruct)
1981 {
1982 if (node->getOp() == EOpEqual)
1983 {
1984 out << "(";
1985 }
1986 else
1987 {
1988 out << "!(";
1989 }
1990
Jamie Madill98493dd2013-07-08 14:39:03 -04001991 const TStructure &structure = *node->getLeft()->getType().getStruct();
1992 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001993
Jamie Madill98493dd2013-07-08 14:39:03 -04001994 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001995 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001996 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001997
1998 node->getLeft()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001999 out << "." + decorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002000 node->getRight()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04002001 out << "." + decorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002002
Jamie Madill98493dd2013-07-08 14:39:03 -04002003 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002004 {
2005 out << " && ";
2006 }
2007 }
2008
2009 out << ")";
2010
2011 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002012 }
2013 else
2014 {
Jamie Madill0b20c942013-07-19 16:36:56 -04002015 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002016
2017 if (node->getOp() == EOpEqual)
2018 {
Jamie Madill0b20c942013-07-19 16:36:56 -04002019 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002020 }
2021 else
2022 {
Jamie Madill0b20c942013-07-19 16:36:56 -04002023 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002024 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002025 }
2026 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002027 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2028 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2029 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2030 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2031 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00002032 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00002033 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
2034 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00002035 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00002036 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002037 if (node->getRight()->hasSideEffects())
2038 {
2039 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
2040 return false;
2041 }
2042 else
2043 {
2044 outputTriplet(visit, "(", " || ", ")");
2045 return true;
2046 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002047 case EOpLogicalXor:
2048 mUsesXor = true;
2049 outputTriplet(visit, "xor(", ", ", ")");
2050 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00002051 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002052 if (node->getRight()->hasSideEffects())
2053 {
2054 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
2055 return false;
2056 }
2057 else
2058 {
2059 outputTriplet(visit, "(", " && ", ")");
2060 return true;
2061 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002062 default: UNREACHABLE();
2063 }
2064
2065 return true;
2066}
2067
2068bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
2069{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002070 switch (node->getOp())
2071 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002072 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
2073 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
2074 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
2075 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
2076 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
2077 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
2078 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002079 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04002080 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002081 case EOpConvFloatToBool:
2082 switch (node->getOperand()->getType().getNominalSize())
2083 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002084 case 1: outputTriplet(visit, "bool(", "", ")"); break;
2085 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
2086 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
2087 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002088 default: UNREACHABLE();
2089 }
2090 break;
2091 case EOpConvBoolToFloat:
2092 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04002093 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002094 switch (node->getOperand()->getType().getNominalSize())
2095 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002096 case 1: outputTriplet(visit, "float(", "", ")"); break;
2097 case 2: outputTriplet(visit, "float2(", "", ")"); break;
2098 case 3: outputTriplet(visit, "float3(", "", ")"); break;
2099 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002100 default: UNREACHABLE();
2101 }
2102 break;
2103 case EOpConvFloatToInt:
2104 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04002105 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002106 switch (node->getOperand()->getType().getNominalSize())
2107 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002108 case 1: outputTriplet(visit, "int(", "", ")"); break;
2109 case 2: outputTriplet(visit, "int2(", "", ")"); break;
2110 case 3: outputTriplet(visit, "int3(", "", ")"); break;
2111 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002112 default: UNREACHABLE();
2113 }
2114 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002115 case EOpConvFloatToUInt:
2116 case EOpConvBoolToUInt:
2117 case EOpConvIntToUInt:
Jamie Madilla16f1672013-07-03 15:15:17 -04002118 switch (node->getOperand()->getType().getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002119 {
2120 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002121 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
2122 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
2123 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002124 default: UNREACHABLE();
2125 }
2126 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002127 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
2128 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
2129 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
2130 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
2131 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
2132 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
2133 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
2134 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
2135 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
2136 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
2137 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
2138 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
2139 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
2140 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
2141 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
2142 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
2143 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
2144 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
2145 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
2146 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
2147 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002148 case EOpDFdx:
2149 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2150 {
2151 outputTriplet(visit, "(", "", ", 0.0)");
2152 }
2153 else
2154 {
2155 outputTriplet(visit, "ddx(", "", ")");
2156 }
2157 break;
2158 case EOpDFdy:
2159 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2160 {
2161 outputTriplet(visit, "(", "", ", 0.0)");
2162 }
2163 else
2164 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00002165 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002166 }
2167 break;
2168 case EOpFwidth:
2169 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2170 {
2171 outputTriplet(visit, "(", "", ", 0.0)");
2172 }
2173 else
2174 {
2175 outputTriplet(visit, "fwidth(", "", ")");
2176 }
2177 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002178 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
2179 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002180 default: UNREACHABLE();
2181 }
2182
2183 return true;
2184}
2185
2186bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2187{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002188 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002189
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002190 switch (node->getOp())
2191 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002192 case EOpSequence:
2193 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002194 if (mInsideFunction)
2195 {
Jamie Madill075edd82013-07-08 13:30:19 -04002196 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002197 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002198
2199 mScopeDepth++;
2200
2201 if (mScopeBracket.size() < mScopeDepth)
2202 {
2203 mScopeBracket.push_back(0); // New scope level
2204 }
2205 else
2206 {
2207 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
2208 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002209 }
2210
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002211 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
2212 {
Jamie Madill075edd82013-07-08 13:30:19 -04002213 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002214
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002215 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002216
2217 out << ";\n";
2218 }
2219
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002220 if (mInsideFunction)
2221 {
Jamie Madill075edd82013-07-08 13:30:19 -04002222 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002223 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002224
2225 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002226 }
2227
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002228 return false;
2229 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002230 case EOpDeclaration:
2231 if (visit == PreVisit)
2232 {
2233 TIntermSequence &sequence = node->getSequence();
2234 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002235
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00002236 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002237 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002238 if (variable->getType().getStruct())
2239 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002240 addConstructor(variable->getType(), scopedStruct(variable->getType().getStruct()->name()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00002241 }
2242
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002243 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002244 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00002245 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002246 {
2247 out << "static ";
2248 }
2249
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002250 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002251
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002252 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002253 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002254 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002255
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002256 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002257 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002258 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002259 out << arrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05002260 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002261 }
2262 else
2263 {
2264 (*sit)->traverse(this);
2265 }
2266
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002267 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002268 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002269 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002270 }
2271 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002272 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002273 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2274 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002275 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002276 }
2277 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002278 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002279 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002280 {
2281 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2282 {
2283 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2284
2285 if (symbol)
2286 {
2287 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2288 mReferencedVaryings[symbol->getSymbol()] = symbol;
2289 }
2290 else
2291 {
2292 (*sit)->traverse(this);
2293 }
2294 }
2295 }
2296
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002297 return false;
2298 }
2299 else if (visit == InVisit)
2300 {
2301 out << ", ";
2302 }
2303 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002304 case EOpPrototype:
2305 if (visit == PreVisit)
2306 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002307 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002308
2309 TIntermSequence &arguments = node->getSequence();
2310
2311 for (unsigned int i = 0; i < arguments.size(); i++)
2312 {
2313 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2314
2315 if (symbol)
2316 {
2317 out << argumentString(symbol);
2318
2319 if (i < arguments.size() - 1)
2320 {
2321 out << ", ";
2322 }
2323 }
2324 else UNREACHABLE();
2325 }
2326
2327 out << ");\n";
2328
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002329 // Also prototype the Lod0 variant if needed
2330 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2331 {
2332 mOutputLod0Function = true;
2333 node->traverse(this);
2334 mOutputLod0Function = false;
2335 }
2336
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002337 return false;
2338 }
2339 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002340 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002341 case EOpFunction:
2342 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002343 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002344
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002345 out << typeString(node->getType()) << " ";
2346
2347 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002348 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002349 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002350 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002351 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002352 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002353 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002354 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002355
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002356 TIntermSequence &sequence = node->getSequence();
2357 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2358
2359 for (unsigned int i = 0; i < arguments.size(); i++)
2360 {
2361 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2362
2363 if (symbol)
2364 {
2365 if (symbol->getType().getStruct())
2366 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002367 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getStruct()->name()), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002368 }
2369
2370 out << argumentString(symbol);
2371
2372 if (i < arguments.size() - 1)
2373 {
2374 out << ", ";
2375 }
2376 }
2377 else UNREACHABLE();
2378 }
2379
2380 out << ")\n"
2381 "{\n";
2382
2383 if (sequence.size() > 1)
2384 {
2385 mInsideFunction = true;
2386 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002387 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002388 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002389
2390 out << "}\n";
2391
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002392 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2393 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002394 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002395 {
2396 mOutputLod0Function = true;
2397 node->traverse(this);
2398 mOutputLod0Function = false;
2399 }
2400 }
2401
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002402 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002403 }
2404 break;
2405 case EOpFunctionCall:
2406 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002407 TString name = TFunction::unmangleName(node->getName());
2408 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002409 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002410
2411 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002412 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002413 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002414 }
2415 else
2416 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002417 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2418
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002419 TextureFunction textureFunction;
2420 textureFunction.sampler = samplerType;
2421 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002422 textureFunction.method = TextureFunction::IMPLICIT;
2423 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002424 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002425
2426 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002427 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002428 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002429 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002430 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002431 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002432 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002433 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002434 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002435 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002436 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002437 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002438 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002439 else if (name == "texture2DProjLod" || name == "textureProjLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002440 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002441 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002442 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002443 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002444 else if (name == "textureSize")
2445 {
2446 textureFunction.method = TextureFunction::SIZE;
2447 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002448 else if (name == "textureOffset")
2449 {
2450 textureFunction.method = TextureFunction::IMPLICIT;
2451 textureFunction.offset = true;
2452 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002453 else if (name == "textureProjOffset")
2454 {
2455 textureFunction.method = TextureFunction::IMPLICIT;
2456 textureFunction.offset = true;
2457 textureFunction.proj = true;
2458 }
2459 else if (name == "textureLodOffset")
2460 {
2461 textureFunction.method = TextureFunction::LOD;
2462 textureFunction.offset = true;
2463 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002464 else if (name == "textureProjLod")
2465 {
2466 textureFunction.method = TextureFunction::LOD;
2467 textureFunction.proj = true;
2468 }
2469 else if (name == "textureProjLodOffset")
2470 {
2471 textureFunction.method = TextureFunction::LOD;
2472 textureFunction.proj = true;
2473 textureFunction.offset = true;
2474 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002475 else if (name == "texelFetch")
2476 {
2477 textureFunction.method = TextureFunction::FETCH;
2478 }
2479 else if (name == "texelFetchOffset")
2480 {
2481 textureFunction.method = TextureFunction::FETCH;
2482 textureFunction.offset = true;
2483 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05002484 else if (name == "textureGrad")
2485 {
2486 textureFunction.method = TextureFunction::GRAD;
2487 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002488 else if (name == "textureGradOffset")
2489 {
2490 textureFunction.method = TextureFunction::GRAD;
2491 textureFunction.offset = true;
2492 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002493 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002494
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002495 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002496 {
2497 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2498 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002499 textureFunction.method = TextureFunction::LOD0;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002500 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002501 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002502 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002503 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2504
2505 if (textureFunction.offset)
2506 {
2507 mandatoryArgumentCount++;
2508 }
2509
2510 if (arguments.size() > mandatoryArgumentCount) // Bias argument is optional
2511 {
2512 textureFunction.method = TextureFunction::BIAS;
2513 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002514 }
2515 }
2516
2517 mUsesTexture.insert(textureFunction);
2518
2519 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002520 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002521
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002522 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2523 {
2524 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2525 {
2526 out << "texture_";
2527 (*arg)->traverse(this);
2528 out << ", sampler_";
2529 }
2530
2531 (*arg)->traverse(this);
2532
2533 if (arg < arguments.end() - 1)
2534 {
2535 out << ", ";
2536 }
2537 }
2538
2539 out << ")";
2540
2541 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002542 }
2543 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002544 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002545 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002546 addConstructor(node->getType(), "vec1", &node->getSequence());
2547 outputTriplet(visit, "vec1(", "", ")");
2548 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002549 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002550 addConstructor(node->getType(), "vec2", &node->getSequence());
2551 outputTriplet(visit, "vec2(", ", ", ")");
2552 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002553 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002554 addConstructor(node->getType(), "vec3", &node->getSequence());
2555 outputTriplet(visit, "vec3(", ", ", ")");
2556 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002557 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002558 addConstructor(node->getType(), "vec4", &node->getSequence());
2559 outputTriplet(visit, "vec4(", ", ", ")");
2560 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002561 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002562 addConstructor(node->getType(), "bvec1", &node->getSequence());
2563 outputTriplet(visit, "bvec1(", "", ")");
2564 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002565 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002566 addConstructor(node->getType(), "bvec2", &node->getSequence());
2567 outputTriplet(visit, "bvec2(", ", ", ")");
2568 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002569 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002570 addConstructor(node->getType(), "bvec3", &node->getSequence());
2571 outputTriplet(visit, "bvec3(", ", ", ")");
2572 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002573 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002574 addConstructor(node->getType(), "bvec4", &node->getSequence());
2575 outputTriplet(visit, "bvec4(", ", ", ")");
2576 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002577 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002578 addConstructor(node->getType(), "ivec1", &node->getSequence());
2579 outputTriplet(visit, "ivec1(", "", ")");
2580 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002581 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002582 addConstructor(node->getType(), "ivec2", &node->getSequence());
2583 outputTriplet(visit, "ivec2(", ", ", ")");
2584 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002585 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002586 addConstructor(node->getType(), "ivec3", &node->getSequence());
2587 outputTriplet(visit, "ivec3(", ", ", ")");
2588 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002589 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002590 addConstructor(node->getType(), "ivec4", &node->getSequence());
2591 outputTriplet(visit, "ivec4(", ", ", ")");
2592 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002593 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002594 addConstructor(node->getType(), "uvec1", &node->getSequence());
2595 outputTriplet(visit, "uvec1(", "", ")");
2596 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002597 case EOpConstructUVec2:
2598 addConstructor(node->getType(), "uvec2", &node->getSequence());
2599 outputTriplet(visit, "uvec2(", ", ", ")");
2600 break;
2601 case EOpConstructUVec3:
2602 addConstructor(node->getType(), "uvec3", &node->getSequence());
2603 outputTriplet(visit, "uvec3(", ", ", ")");
2604 break;
2605 case EOpConstructUVec4:
2606 addConstructor(node->getType(), "uvec4", &node->getSequence());
2607 outputTriplet(visit, "uvec4(", ", ", ")");
2608 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002609 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002610 addConstructor(node->getType(), "mat2", &node->getSequence());
2611 outputTriplet(visit, "mat2(", ", ", ")");
2612 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002613 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002614 addConstructor(node->getType(), "mat3", &node->getSequence());
2615 outputTriplet(visit, "mat3(", ", ", ")");
2616 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002617 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002618 addConstructor(node->getType(), "mat4", &node->getSequence());
2619 outputTriplet(visit, "mat4(", ", ", ")");
2620 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002621 case EOpConstructStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04002622 addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence());
2623 outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002624 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002625 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2626 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2627 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2628 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2629 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2630 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002631 case EOpMod:
2632 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002633 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002634 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2635 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2636 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002637 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002638 case 11: mUsesMod1 = true; break;
2639 case 22: mUsesMod2v = true; break;
2640 case 21: mUsesMod2f = true; break;
2641 case 33: mUsesMod3v = true; break;
2642 case 31: mUsesMod3f = true; break;
2643 case 44: mUsesMod4v = true; break;
2644 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002645 default: UNREACHABLE();
2646 }
2647
2648 outputTriplet(visit, "mod(", ", ", ")");
2649 }
2650 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002651 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002652 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002653 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002654 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2655 {
2656 case 1: mUsesAtan2_1 = true; break;
2657 case 2: mUsesAtan2_2 = true; break;
2658 case 3: mUsesAtan2_3 = true; break;
2659 case 4: mUsesAtan2_4 = true; break;
2660 default: UNREACHABLE();
2661 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002662 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002663 break;
2664 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2665 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2666 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2667 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2668 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2669 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2670 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2671 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2672 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002673 case EOpFaceForward:
2674 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002675 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002676 {
2677 case 1: mUsesFaceforward1 = true; break;
2678 case 2: mUsesFaceforward2 = true; break;
2679 case 3: mUsesFaceforward3 = true; break;
2680 case 4: mUsesFaceforward4 = true; break;
2681 default: UNREACHABLE();
2682 }
2683
2684 outputTriplet(visit, "faceforward(", ", ", ")");
2685 }
2686 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002687 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2688 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2689 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002690 default: UNREACHABLE();
2691 }
2692
2693 return true;
2694}
2695
2696bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2697{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002698 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002699
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002700 if (node->usesTernaryOperator())
2701 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002702 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002703 }
2704 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002705 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002706 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002707
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002708 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002709
2710 node->getCondition()->traverse(this);
2711
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002712 out << ")\n";
2713
Jamie Madill075edd82013-07-08 13:30:19 -04002714 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002715 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002716
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002717 bool discard = false;
2718
daniel@transgaming.combb885322010-04-15 20:45:24 +00002719 if (node->getTrueBlock())
2720 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002721 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002722
2723 // Detect true discard
2724 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002725 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002726
Jamie Madill075edd82013-07-08 13:30:19 -04002727 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002728 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002729
2730 if (node->getFalseBlock())
2731 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002732 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002733
Jamie Madill075edd82013-07-08 13:30:19 -04002734 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002735 out << "{\n";
2736
Jamie Madill075edd82013-07-08 13:30:19 -04002737 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002738 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002739
Jamie Madill075edd82013-07-08 13:30:19 -04002740 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002741 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002742
2743 // Detect false discard
2744 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2745 }
2746
2747 // ANGLE issue 486: Detect problematic conditional discard
2748 if (discard && FindSideEffectRewriting::search(node))
2749 {
2750 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002751 }
2752 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002753
2754 return false;
2755}
2756
2757void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2758{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002759 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002760}
2761
2762bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2763{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002764 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2765
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002766 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002767 {
2768 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2769 }
2770
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002771 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002772 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002773 if (handleExcessiveLoop(node))
2774 {
2775 return false;
2776 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002777 }
2778
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002779 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002780
alokp@chromium.org52813552010-11-16 18:36:09 +00002781 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002782 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002783 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002784
Jamie Madill075edd82013-07-08 13:30:19 -04002785 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002786 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002787 }
2788 else
2789 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002790 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002791
2792 if (node->getInit())
2793 {
2794 node->getInit()->traverse(this);
2795 }
2796
2797 out << "; ";
2798
alokp@chromium.org52813552010-11-16 18:36:09 +00002799 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002800 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002801 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002802 }
2803
2804 out << "; ";
2805
alokp@chromium.org52813552010-11-16 18:36:09 +00002806 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002807 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002808 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002809 }
2810
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002811 out << ")\n";
2812
Jamie Madill075edd82013-07-08 13:30:19 -04002813 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002814 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002815 }
2816
2817 if (node->getBody())
2818 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002819 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002820 }
2821
Jamie Madill075edd82013-07-08 13:30:19 -04002822 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002823 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002824
alokp@chromium.org52813552010-11-16 18:36:09 +00002825 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002826 {
Jamie Madill075edd82013-07-08 13:30:19 -04002827 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002828 out << "while(\n";
2829
alokp@chromium.org52813552010-11-16 18:36:09 +00002830 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002831
daniel@transgaming.com73536982012-03-21 20:45:49 +00002832 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002833 }
2834
daniel@transgaming.com73536982012-03-21 20:45:49 +00002835 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002836
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002837 mInsideDiscontinuousLoop = wasDiscontinuous;
2838
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002839 return false;
2840}
2841
2842bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2843{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002844 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002845
2846 switch (node->getFlowOp())
2847 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002848 case EOpKill:
2849 outputTriplet(visit, "discard;\n", "", "");
2850 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002851 case EOpBreak:
2852 if (visit == PreVisit)
2853 {
2854 if (mExcessiveLoopIndex)
2855 {
2856 out << "{Break";
2857 mExcessiveLoopIndex->traverse(this);
2858 out << " = true; break;}\n";
2859 }
2860 else
2861 {
2862 out << "break;\n";
2863 }
2864 }
2865 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002866 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002867 case EOpReturn:
2868 if (visit == PreVisit)
2869 {
2870 if (node->getExpression())
2871 {
2872 out << "return ";
2873 }
2874 else
2875 {
2876 out << "return;\n";
2877 }
2878 }
2879 else if (visit == PostVisit)
2880 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002881 if (node->getExpression())
2882 {
2883 out << ";\n";
2884 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002885 }
2886 break;
2887 default: UNREACHABLE();
2888 }
2889
2890 return true;
2891}
2892
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002893void OutputHLSL::traverseStatements(TIntermNode *node)
2894{
2895 if (isSingleStatement(node))
2896 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002897 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002898 }
2899
2900 node->traverse(this);
2901}
2902
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002903bool OutputHLSL::isSingleStatement(TIntermNode *node)
2904{
2905 TIntermAggregate *aggregate = node->getAsAggregate();
2906
2907 if (aggregate)
2908 {
2909 if (aggregate->getOp() == EOpSequence)
2910 {
2911 return false;
2912 }
2913 else
2914 {
2915 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2916 {
2917 if (!isSingleStatement(*sit))
2918 {
2919 return false;
2920 }
2921 }
2922
2923 return true;
2924 }
2925 }
2926
2927 return true;
2928}
2929
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002930// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2931// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002932bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2933{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002934 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002935 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002936
2937 // Parse loops of the form:
2938 // for(int index = initial; index [comparator] limit; index += increment)
2939 TIntermSymbol *index = NULL;
2940 TOperator comparator = EOpNull;
2941 int initial = 0;
2942 int limit = 0;
2943 int increment = 0;
2944
2945 // Parse index name and intial value
2946 if (node->getInit())
2947 {
2948 TIntermAggregate *init = node->getInit()->getAsAggregate();
2949
2950 if (init)
2951 {
2952 TIntermSequence &sequence = init->getSequence();
2953 TIntermTyped *variable = sequence[0]->getAsTyped();
2954
2955 if (variable && variable->getQualifier() == EvqTemporary)
2956 {
2957 TIntermBinary *assign = variable->getAsBinaryNode();
2958
2959 if (assign->getOp() == EOpInitialize)
2960 {
2961 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2962 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2963
2964 if (symbol && constant)
2965 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002966 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002967 {
2968 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002969 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002970 }
2971 }
2972 }
2973 }
2974 }
2975 }
2976
2977 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002978 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002979 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002980 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002981
2982 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2983 {
2984 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2985
2986 if (constant)
2987 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002988 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002989 {
2990 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002991 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002992 }
2993 }
2994 }
2995 }
2996
2997 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002998 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002999 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003000 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3001 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003002
3003 if (binaryTerminal)
3004 {
3005 TOperator op = binaryTerminal->getOp();
3006 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3007
3008 if (constant)
3009 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003010 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003011 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003012 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003013
3014 switch (op)
3015 {
3016 case EOpAddAssign: increment = value; break;
3017 case EOpSubAssign: increment = -value; break;
3018 default: UNIMPLEMENTED();
3019 }
3020 }
3021 }
3022 }
3023 else if (unaryTerminal)
3024 {
3025 TOperator op = unaryTerminal->getOp();
3026
3027 switch (op)
3028 {
3029 case EOpPostIncrement: increment = 1; break;
3030 case EOpPostDecrement: increment = -1; break;
3031 case EOpPreIncrement: increment = 1; break;
3032 case EOpPreDecrement: increment = -1; break;
3033 default: UNIMPLEMENTED();
3034 }
3035 }
3036 }
3037
3038 if (index != NULL && comparator != EOpNull && increment != 0)
3039 {
3040 if (comparator == EOpLessThanEqual)
3041 {
3042 comparator = EOpLessThan;
3043 limit += 1;
3044 }
3045
3046 if (comparator == EOpLessThan)
3047 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00003048 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003049
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003050 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003051 {
3052 return false; // Not an excessive loop
3053 }
3054
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003055 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
3056 mExcessiveLoopIndex = index;
3057
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003058 out << "{int ";
3059 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003060 out << ";\n"
3061 "bool Break";
3062 index->traverse(this);
3063 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003064
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003065 bool firstLoopFragment = true;
3066
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003067 while (iterations > 0)
3068 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003069 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003070
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003071 if (!firstLoopFragment)
3072 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003073 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003074 index->traverse(this);
3075 out << ") {\n";
3076 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00003077
3078 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
3079 {
3080 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
3081 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003082
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003083 // for(int index = initial; index < clampedLimit; index += increment)
3084
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003085 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003086 index->traverse(this);
3087 out << " = ";
3088 out << initial;
3089
3090 out << "; ";
3091 index->traverse(this);
3092 out << " < ";
3093 out << clampedLimit;
3094
3095 out << "; ";
3096 index->traverse(this);
3097 out << " += ";
3098 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003099 out << ")\n";
3100
Jamie Madill075edd82013-07-08 13:30:19 -04003101 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003102 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003103
3104 if (node->getBody())
3105 {
3106 node->getBody()->traverse(this);
3107 }
3108
Jamie Madill075edd82013-07-08 13:30:19 -04003109 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003110 out << ";}\n";
3111
3112 if (!firstLoopFragment)
3113 {
3114 out << "}\n";
3115 }
3116
3117 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003118
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003119 initial += MAX_LOOP_ITERATIONS * increment;
3120 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003121 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003122
3123 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003124
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003125 mExcessiveLoopIndex = restoreIndex;
3126
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003127 return true;
3128 }
3129 else UNIMPLEMENTED();
3130 }
3131
3132 return false; // Not handled as an excessive loop
3133}
3134
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003135void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003136{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00003137 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003138
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003139 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003140 {
3141 out << preString;
3142 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003143 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003144 {
3145 out << inString;
3146 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003147 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003148 {
3149 out << postString;
3150 }
3151}
3152
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003153void OutputHLSL::outputLineDirective(int line)
3154{
3155 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
3156 {
baustin@google.com8ab69842011-06-02 21:53:45 +00003157 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003158 mBody << "#line " << line;
3159
3160 if (mContext.sourcePath)
3161 {
3162 mBody << " \"" << mContext.sourcePath << "\"";
3163 }
3164
3165 mBody << "\n";
3166 }
3167}
3168
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003169TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3170{
3171 TQualifier qualifier = symbol->getQualifier();
3172 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003173 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003174
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003175 if (name.empty()) // HLSL demands named arguments, also for prototypes
3176 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00003177 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003178 }
3179 else
3180 {
3181 name = decorate(name);
3182 }
3183
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003184 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
3185 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003186 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
3187 qualifierString(qualifier) + " " + samplerString(type) + " sampler_" + name + arrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003188 }
3189
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003190 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003191}
3192
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003193TString OutputHLSL::interpolationString(TQualifier qualifier)
3194{
3195 switch(qualifier)
3196 {
3197 case EvqVaryingIn: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003198 case EvqFragmentIn: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003199 case EvqInvariantVaryingIn: return "";
3200 case EvqSmoothIn: return "linear";
3201 case EvqFlatIn: return "nointerpolation";
3202 case EvqCentroidIn: return "centroid";
3203 case EvqVaryingOut: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003204 case EvqVertexOut: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003205 case EvqInvariantVaryingOut: return "";
3206 case EvqSmoothOut: return "linear";
3207 case EvqFlatOut: return "nointerpolation";
3208 case EvqCentroidOut: return "centroid";
3209 default: UNREACHABLE();
3210 }
3211
3212 return "";
3213}
3214
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003215TString OutputHLSL::qualifierString(TQualifier qualifier)
3216{
3217 switch(qualifier)
3218 {
3219 case EvqIn: return "in";
3220 case EvqOut: return "out";
3221 case EvqInOut: return "inout";
3222 case EvqConstReadOnly: return "const";
3223 default: UNREACHABLE();
3224 }
3225
3226 return "";
3227}
3228
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003229TString OutputHLSL::typeString(const TType &type)
3230{
Jamie Madill98493dd2013-07-08 14:39:03 -04003231 const TStructure* structure = type.getStruct();
3232 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003233 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003234 const TString& typeName = structure->name();
3235 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003236 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003237 return structLookup(typeName);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003238 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003239 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003240 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003241 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003242 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003243 }
3244 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003245 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003246 int cols = type.getCols();
3247 int rows = type.getRows();
3248 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003249 }
3250 else
3251 {
3252 switch (type.getBasicType())
3253 {
3254 case EbtFloat:
3255 switch (type.getNominalSize())
3256 {
3257 case 1: return "float";
3258 case 2: return "float2";
3259 case 3: return "float3";
3260 case 4: return "float4";
3261 }
3262 case EbtInt:
3263 switch (type.getNominalSize())
3264 {
3265 case 1: return "int";
3266 case 2: return "int2";
3267 case 3: return "int3";
3268 case 4: return "int4";
3269 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003270 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04003271 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003272 {
3273 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003274 case 2: return "uint2";
3275 case 3: return "uint3";
3276 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003277 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003278 case EbtBool:
3279 switch (type.getNominalSize())
3280 {
3281 case 1: return "bool";
3282 case 2: return "bool2";
3283 case 3: return "bool3";
3284 case 4: return "bool4";
3285 }
3286 case EbtVoid:
3287 return "void";
3288 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003289 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04003290 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003291 case EbtSampler2DArray:
3292 case EbtISampler2DArray:
3293 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003294 return "sampler2D";
3295 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003296 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04003297 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003298 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00003299 case EbtSamplerExternalOES:
3300 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00003301 default:
3302 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003303 }
3304 }
3305
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003306 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003307 return "<unknown type>";
3308}
3309
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003310TString OutputHLSL::textureString(const TType &type)
3311{
3312 switch (type.getBasicType())
3313 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003314 case EbtSampler2D: return "Texture2D";
3315 case EbtSamplerCube: return "TextureCube";
3316 case EbtSamplerExternalOES: return "Texture2D";
3317 case EbtSampler2DArray: return "Texture2DArray";
3318 case EbtSampler3D: return "Texture3D";
3319 case EbtISampler2D: return "Texture2D<int4>";
3320 case EbtISampler3D: return "Texture3D<int4>";
3321 case EbtISamplerCube: return "TextureCube<int4>";
3322 case EbtISampler2DArray: return "Texture2DArray<int4>";
3323 case EbtUSampler2D: return "Texture2D<uint4>";
3324 case EbtUSampler3D: return "Texture3D<uint4>";
3325 case EbtUSamplerCube: return "TextureCube<uint4>";
3326 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
3327 case EbtSampler2DShadow: return "Texture2D";
3328 case EbtSamplerCubeShadow: return "TextureCube";
3329 case EbtSampler2DArrayShadow: return "Texture2DArray";
3330 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003331 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04003332
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003333 return "<unknown texture type>";
3334}
3335
Nicolas Capenscb127d32013-07-15 17:26:18 -04003336TString OutputHLSL::samplerString(const TType &type)
3337{
3338 if (IsShadowSampler(type.getBasicType()))
3339 {
3340 return "SamplerComparisonState";
3341 }
3342 else
3343 {
3344 return "SamplerState";
3345 }
3346}
3347
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003348TString OutputHLSL::arrayString(const TType &type)
3349{
3350 if (!type.isArray())
3351 {
3352 return "";
3353 }
3354
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003355 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003356}
3357
3358TString OutputHLSL::initializer(const TType &type)
3359{
3360 TString string;
3361
Jamie Madill94bf7f22013-07-08 13:31:15 -04003362 size_t size = type.getObjectSize();
3363 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003364 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003365 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003366
Jamie Madill94bf7f22013-07-08 13:31:15 -04003367 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003368 {
3369 string += ", ";
3370 }
3371 }
3372
daniel@transgaming.comead23042010-04-29 03:35:36 +00003373 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003374}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003375
Jamie Madill98493dd2013-07-08 14:39:03 -04003376TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003377{
Jamie Madill98493dd2013-07-08 14:39:03 -04003378 const TFieldList &fields = structure.fields();
3379 const bool isNameless = (structure.name() == "");
3380 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003381 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3382
Jamie Madill98493dd2013-07-08 14:39:03 -04003383 TString string;
3384 string += declareString + "\n"
3385 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003386
Jamie Madillc835df62013-06-21 09:15:32 -04003387 int elementIndex = 0;
3388
Jamie Madill9cf6c072013-06-20 11:55:53 -04003389 for (unsigned int i = 0; i < fields.size(); i++)
3390 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003391 const TField &field = *fields[i];
3392 const TType &fieldType = *field.type();
3393 const TStructure *fieldStruct = fieldType.getStruct();
3394 const TString &fieldTypeString = fieldStruct ? structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) : typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003395
Jamie Madillc835df62013-06-21 09:15:32 -04003396 if (useStd140Packing)
3397 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003398 string += std140PrePaddingString(*field.type(), &elementIndex);
Jamie Madillc835df62013-06-21 09:15:32 -04003399 }
3400
Jamie Madill98493dd2013-07-08 14:39:03 -04003401 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003402
3403 if (useStd140Packing)
3404 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003405 string += std140PostPaddingString(*field.type(), useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003406 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003407 }
3408
3409 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003410 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003411
Jamie Madille4075c92013-06-21 09:15:32 -04003412 // Add remaining element index to the global map, for use with nested structs in standard layouts
3413 if (useStd140Packing)
3414 {
3415 mStd140StructElementIndexes[structName] = elementIndex;
3416 }
3417
Jamie Madill98493dd2013-07-08 14:39:03 -04003418 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003419}
3420
Jamie Madill98493dd2013-07-08 14:39:03 -04003421TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003422{
Jamie Madill98493dd2013-07-08 14:39:03 -04003423 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003424 {
3425 return "";
3426 }
3427
3428 TString prefix = "";
3429
3430 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3431 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003432
3433 if (useStd140Packing)
3434 {
3435 prefix += "std";
3436 }
3437
Jamie Madill9cf6c072013-06-20 11:55:53 -04003438 if (useHLSLRowMajorPacking)
3439 {
Jamie Madillc835df62013-06-21 09:15:32 -04003440 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003441 prefix += "rm";
3442 }
3443
Jamie Madill98493dd2013-07-08 14:39:03 -04003444 return prefix + structLookup(structure.name());
Jamie Madill9cf6c072013-06-20 11:55:53 -04003445}
3446
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003447void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003448{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003449 if (name == "")
3450 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003451 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003452 }
3453
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003454 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
3455 {
3456 return; // Already added
3457 }
3458
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003459 TType ctorType = type;
3460 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003461 ctorType.setPrecision(EbpHigh);
3462 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003463
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003464 TString ctorName = type.getStruct() ? decorate(name) : name;
3465
3466 typedef std::vector<TType> ParameterArray;
3467 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003468
Jamie Madill98493dd2013-07-08 14:39:03 -04003469 const TStructure* structure = type.getStruct();
3470 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003471 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003472 mStructNames.insert(decorate(name));
3473
Jamie Madill98493dd2013-07-08 14:39:03 -04003474 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003475
Jamie Madill98493dd2013-07-08 14:39:03 -04003476 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003477 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003478 // Add row-major packed struct for interface blocks
3479 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003480 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003481 "#pragma pack_matrix(column_major)\n";
3482
Jamie Madillc835df62013-06-21 09:15:32 -04003483 const TString &std140Prefix = "std";
Jamie Madill98493dd2013-07-08 14:39:03 -04003484 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003485
3486 const TString &std140RowMajorPrefix = "std_rm";
3487 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003488 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003489 "#pragma pack_matrix(column_major)\n";
3490
Jamie Madill98493dd2013-07-08 14:39:03 -04003491 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003492 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003493 mStructDeclarations.push_back(std140String);
3494 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003495 }
3496
Jamie Madill98493dd2013-07-08 14:39:03 -04003497 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003498 for (unsigned int i = 0; i < fields.size(); i++)
3499 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003500 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003501 }
3502 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003503 else if (parameters)
3504 {
3505 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3506 {
3507 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3508 }
3509 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003510 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003511
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003512 TString constructor;
3513
3514 if (ctorType.getStruct())
3515 {
3516 constructor += ctorName + " " + ctorName + "_ctor(";
3517 }
3518 else // Built-in type
3519 {
3520 constructor += typeString(ctorType) + " " + ctorName + "(";
3521 }
3522
3523 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3524 {
3525 const TType &type = ctorParameters[parameter];
3526
3527 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3528
3529 if (parameter < ctorParameters.size() - 1)
3530 {
3531 constructor += ", ";
3532 }
3533 }
3534
3535 constructor += ")\n"
3536 "{\n";
3537
3538 if (ctorType.getStruct())
3539 {
3540 constructor += " " + ctorName + " structure = {";
3541 }
3542 else
3543 {
3544 constructor += " return " + typeString(ctorType) + "(";
3545 }
3546
3547 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3548 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003549 int rows = ctorType.getRows();
3550 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003551 const TType &parameter = ctorParameters[0];
3552
3553 if (parameter.isScalar())
3554 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003555 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003556 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003557 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003558 {
3559 constructor += TString((row == col) ? "x0" : "0.0");
3560
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003561 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003562 {
3563 constructor += ", ";
3564 }
3565 }
3566 }
3567 }
3568 else if (parameter.isMatrix())
3569 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003570 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003571 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003572 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003573 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003574 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003575 {
3576 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3577 }
3578 else
3579 {
3580 constructor += TString((row == col) ? "1.0" : "0.0");
3581 }
3582
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003583 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003584 {
3585 constructor += ", ";
3586 }
3587 }
3588 }
3589 }
3590 else UNREACHABLE();
3591 }
3592 else
3593 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003594 size_t remainingComponents = ctorType.getObjectSize();
3595 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003596
3597 while (remainingComponents > 0)
3598 {
3599 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003600 const size_t parameterSize = parameter.getObjectSize();
3601 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003602
3603 constructor += "x" + str(parameterIndex);
3604
3605 if (parameter.isScalar())
3606 {
3607 remainingComponents -= parameter.getObjectSize();
3608 }
3609 else if (parameter.isVector())
3610 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003611 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003612 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003613 ASSERT(parameterSize <= remainingComponents);
3614 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003615 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003616 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003617 {
3618 switch (remainingComponents)
3619 {
3620 case 1: constructor += ".x"; break;
3621 case 2: constructor += ".xy"; break;
3622 case 3: constructor += ".xyz"; break;
3623 case 4: constructor += ".xyzw"; break;
3624 default: UNREACHABLE();
3625 }
3626
3627 remainingComponents = 0;
3628 }
3629 else UNREACHABLE();
3630 }
3631 else if (parameter.isMatrix() || parameter.getStruct())
3632 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003633 ASSERT(remainingComponents == parameterSize || moreParameters);
3634 ASSERT(parameterSize <= remainingComponents);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003635
Jamie Madill94bf7f22013-07-08 13:31:15 -04003636 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003637 }
3638 else UNREACHABLE();
3639
3640 if (moreParameters)
3641 {
3642 parameterIndex++;
3643 }
3644
3645 if (remainingComponents)
3646 {
3647 constructor += ", ";
3648 }
3649 }
3650 }
3651
3652 if (ctorType.getStruct())
3653 {
3654 constructor += "};\n"
3655 " return structure;\n"
3656 "}\n";
3657 }
3658 else
3659 {
3660 constructor += ");\n"
3661 "}\n";
3662 }
3663
daniel@transgaming.com63691862010-04-29 03:32:42 +00003664 mConstructors.insert(constructor);
3665}
3666
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003667const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3668{
3669 TInfoSinkBase &out = mBody;
3670
Jamie Madill98493dd2013-07-08 14:39:03 -04003671 const TStructure* structure = type.getStruct();
3672 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003673 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003674 out << structLookup(structure->name()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003675
Jamie Madill98493dd2013-07-08 14:39:03 -04003676 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003677
Jamie Madill98493dd2013-07-08 14:39:03 -04003678 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003679 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003680 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003681
3682 constUnion = writeConstantUnion(*fieldType, constUnion);
3683
Jamie Madill98493dd2013-07-08 14:39:03 -04003684 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003685 {
3686 out << ", ";
3687 }
3688 }
3689
3690 out << ")";
3691 }
3692 else
3693 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003694 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003695 bool writeType = size > 1;
3696
3697 if (writeType)
3698 {
3699 out << typeString(type) << "(";
3700 }
3701
Jamie Madill94bf7f22013-07-08 13:31:15 -04003702 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003703 {
3704 switch (constUnion->getType())
3705 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003706 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003707 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003708 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003709 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003710 default: UNREACHABLE();
3711 }
3712
3713 if (i != size - 1)
3714 {
3715 out << ", ";
3716 }
3717 }
3718
3719 if (writeType)
3720 {
3721 out << ")";
3722 }
3723 }
3724
3725 return constUnion;
3726}
3727
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003728TString OutputHLSL::scopeString(unsigned int depthLimit)
3729{
3730 TString string;
3731
3732 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3733 {
3734 string += "_" + str(i);
3735 }
3736
3737 return string;
3738}
3739
3740TString OutputHLSL::scopedStruct(const TString &typeName)
3741{
3742 if (typeName == "")
3743 {
3744 return typeName;
3745 }
3746
3747 return typeName + scopeString(mScopeDepth);
3748}
3749
3750TString OutputHLSL::structLookup(const TString &typeName)
3751{
3752 for (int depth = mScopeDepth; depth >= 0; depth--)
3753 {
3754 TString scopedName = decorate(typeName + scopeString(depth));
3755
3756 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3757 {
3758 if (*structName == scopedName)
3759 {
3760 return scopedName;
3761 }
3762 }
3763 }
3764
3765 UNREACHABLE(); // Should have found a matching constructor
3766
3767 return typeName;
3768}
3769
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003770TString OutputHLSL::decorate(const TString &string)
3771{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003772 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003773 {
3774 return "_" + string;
3775 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003776
3777 return string;
3778}
3779
apatrick@chromium.org65756022012-01-17 21:45:38 +00003780TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003781{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003782 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003783 {
3784 return "ex_" + string;
3785 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003786
3787 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003788}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003789
Jamie Madill98493dd2013-07-08 14:39:03 -04003790TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003791{
Jamie Madill98493dd2013-07-08 14:39:03 -04003792 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003793 {
3794 return decorate(string);
3795 }
3796
3797 return string;
3798}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003799
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003800void OutputHLSL::declareInterfaceBlockField(const TType &type, const TString &name, std::vector<InterfaceBlockField>& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003801{
Jamie Madill98493dd2013-07-08 14:39:03 -04003802 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003803
3804 if (!structure)
3805 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003806 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003807 InterfaceBlockField field(glVariableType(type), glVariablePrecision(type), name.c_str(),
3808 (unsigned int)type.getArraySize(), isRowMajorMatrix);
3809 output.push_back(field);
3810 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003811 else
3812 {
Jamie Madill28167c62013-08-30 13:21:10 -04003813 InterfaceBlockField structField(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), false);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003814
3815 const TFieldList &fields = structure->fields();
3816
3817 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3818 {
3819 TField *field = fields[fieldIndex];
3820 TType *fieldType = field->type();
3821
3822 // make sure to copy matrix packing information
3823 fieldType->setLayoutQualifier(type.getLayoutQualifier());
3824
3825 declareInterfaceBlockField(*fieldType, field->name(), structField.fields);
3826 }
3827
3828 output.push_back(structField);
3829 }
3830}
3831
Jamie Madillc2141fb2013-08-30 13:21:08 -04003832Uniform OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, std::vector<Uniform>& output)
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003833{
3834 const TStructure *structure = type.getStruct();
3835
3836 if (!structure)
3837 {
3838 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
3839 Uniform uniform(glVariableType(type), glVariablePrecision(type), name.c_str(),
Jamie Madill56093782013-08-30 13:21:11 -04003840 (unsigned int)type.getArraySize(), (unsigned int)registerIndex, 0);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003841 output.push_back(uniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003842
3843 return uniform;
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003844 }
3845 else
3846 {
Jamie Madill56093782013-08-30 13:21:11 -04003847 Uniform structUniform(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(),
3848 (unsigned int)registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003849
Jamie Madill98493dd2013-07-08 14:39:03 -04003850 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003851
Jamie Madill98493dd2013-07-08 14:39:03 -04003852 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003853 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003854 TField *field = fields[fieldIndex];
3855 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003856
Jamie Madill56093782013-08-30 13:21:11 -04003857 declareUniformToList(*fieldType, field->name(), GL_INVALID_INDEX, structUniform.fields);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003858 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003859
Jamie Madill56093782013-08-30 13:21:11 -04003860 // assign register offset information -- this will override the information in any sub-structures.
3861 HLSLVariableGetRegisterInfo(registerIndex, &structUniform);
3862
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003863 output.push_back(structUniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003864
3865 return structUniform;
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003866 }
3867}
3868
Jamie Madill139b9092013-08-30 13:21:06 -04003869InterpolationType getInterpolationType(TQualifier qualifier)
3870{
3871 switch (qualifier)
3872 {
3873 case EvqFlatIn:
3874 case EvqFlatOut:
3875 return INTERPOLATION_FLAT;
3876
3877 case EvqSmoothIn:
3878 case EvqSmoothOut:
3879 case EvqVertexOut:
3880 case EvqFragmentIn:
Jamie Madill384b6042013-09-13 10:06:24 -04003881 case EvqVaryingIn:
3882 case EvqVaryingOut:
Jamie Madill139b9092013-08-30 13:21:06 -04003883 return INTERPOLATION_SMOOTH;
3884
3885 case EvqCentroidIn:
3886 case EvqCentroidOut:
3887 return INTERPOLATION_CENTROID;
3888
3889 default: UNREACHABLE();
3890 return INTERPOLATION_SMOOTH;
3891 }
3892}
3893
Jamie Madill94599662013-08-30 13:21:10 -04003894void OutputHLSL::declareVaryingToList(const TType &type, TQualifier baseTypeQualifier, const TString &name, std::vector<Varying>& fieldsOut)
Jamie Madill47fdd132013-08-30 13:21:04 -04003895{
3896 const TStructure *structure = type.getStruct();
3897
Jamie Madill94599662013-08-30 13:21:10 -04003898 InterpolationType interpolation = getInterpolationType(baseTypeQualifier);
Jamie Madill47fdd132013-08-30 13:21:04 -04003899 if (!structure)
3900 {
Jamie Madill139b9092013-08-30 13:21:06 -04003901 Varying varying(glVariableType(type), glVariablePrecision(type), name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003902 fieldsOut.push_back(varying);
3903 }
3904 else
3905 {
Jamie Madill28167c62013-08-30 13:21:10 -04003906 Varying structVarying(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003907 const TFieldList &fields = structure->fields();
3908
Jamie Madill28167c62013-08-30 13:21:10 -04003909 structVarying.structName = structure->name().c_str();
3910
Jamie Madill47fdd132013-08-30 13:21:04 -04003911 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3912 {
3913 const TField &field = *fields[fieldIndex];
Jamie Madill94599662013-08-30 13:21:10 -04003914 declareVaryingToList(*field.type(), baseTypeQualifier, field.name(), structVarying.fields);
Jamie Madill47fdd132013-08-30 13:21:04 -04003915 }
3916
3917 fieldsOut.push_back(structVarying);
3918 }
3919}
3920
Jamie Madillc2141fb2013-08-30 13:21:08 -04003921int OutputHLSL::declareUniformAndAssignRegister(const TType &type, const TString &name)
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003922{
Jamie Madillc2141fb2013-08-30 13:21:08 -04003923 int registerIndex = (IsSampler(type.getBasicType()) ? mSamplerRegister : mUniformRegister);
3924
3925 const Uniform &uniform = declareUniformToList(type, name, registerIndex, mActiveUniforms);
3926
3927 if (IsSampler(type.getBasicType()))
3928 {
3929 mSamplerRegister += HLSLVariableRegisterCount(uniform);
3930 }
3931 else
3932 {
3933 mUniformRegister += HLSLVariableRegisterCount(uniform);
3934 }
3935
3936 return registerIndex;
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003937}
3938
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003939GLenum OutputHLSL::glVariableType(const TType &type)
3940{
3941 if (type.getBasicType() == EbtFloat)
3942 {
3943 if (type.isScalar())
3944 {
3945 return GL_FLOAT;
3946 }
3947 else if (type.isVector())
3948 {
3949 switch(type.getNominalSize())
3950 {
3951 case 2: return GL_FLOAT_VEC2;
3952 case 3: return GL_FLOAT_VEC3;
3953 case 4: return GL_FLOAT_VEC4;
3954 default: UNREACHABLE();
3955 }
3956 }
3957 else if (type.isMatrix())
3958 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003959 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003960 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003961 case 2:
3962 switch(type.getRows())
3963 {
3964 case 2: return GL_FLOAT_MAT2;
3965 case 3: return GL_FLOAT_MAT2x3;
3966 case 4: return GL_FLOAT_MAT2x4;
3967 default: UNREACHABLE();
3968 }
3969
3970 case 3:
3971 switch(type.getRows())
3972 {
3973 case 2: return GL_FLOAT_MAT3x2;
3974 case 3: return GL_FLOAT_MAT3;
3975 case 4: return GL_FLOAT_MAT3x4;
3976 default: UNREACHABLE();
3977 }
3978
3979 case 4:
3980 switch(type.getRows())
3981 {
3982 case 2: return GL_FLOAT_MAT4x2;
3983 case 3: return GL_FLOAT_MAT4x3;
3984 case 4: return GL_FLOAT_MAT4;
3985 default: UNREACHABLE();
3986 }
3987
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003988 default: UNREACHABLE();
3989 }
3990 }
3991 else UNREACHABLE();
3992 }
3993 else if (type.getBasicType() == EbtInt)
3994 {
3995 if (type.isScalar())
3996 {
3997 return GL_INT;
3998 }
3999 else if (type.isVector())
4000 {
4001 switch(type.getNominalSize())
4002 {
4003 case 2: return GL_INT_VEC2;
4004 case 3: return GL_INT_VEC3;
4005 case 4: return GL_INT_VEC4;
4006 default: UNREACHABLE();
4007 }
4008 }
4009 else UNREACHABLE();
4010 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004011 else if (type.getBasicType() == EbtUInt)
4012 {
4013 if (type.isScalar())
4014 {
4015 return GL_UNSIGNED_INT;
4016 }
4017 else if (type.isVector())
4018 {
Jamie Madill22d63da2013-06-07 12:45:12 -04004019 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00004020 {
4021 case 2: return GL_UNSIGNED_INT_VEC2;
4022 case 3: return GL_UNSIGNED_INT_VEC3;
4023 case 4: return GL_UNSIGNED_INT_VEC4;
4024 default: UNREACHABLE();
4025 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004026 }
4027 else UNREACHABLE();
4028 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004029 else if (type.getBasicType() == EbtBool)
4030 {
4031 if (type.isScalar())
4032 {
4033 return GL_BOOL;
4034 }
4035 else if (type.isVector())
4036 {
4037 switch(type.getNominalSize())
4038 {
4039 case 2: return GL_BOOL_VEC2;
4040 case 3: return GL_BOOL_VEC3;
4041 case 4: return GL_BOOL_VEC4;
4042 default: UNREACHABLE();
4043 }
4044 }
4045 else UNREACHABLE();
4046 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004047
4048 switch(type.getBasicType())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004049 {
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004050 case EbtSampler2D: return GL_SAMPLER_2D;
4051 case EbtSampler3D: return GL_SAMPLER_3D;
4052 case EbtSamplerCube: return GL_SAMPLER_CUBE;
4053 case EbtSampler2DArray: return GL_SAMPLER_2D_ARRAY;
4054 case EbtISampler2D: return GL_INT_SAMPLER_2D;
4055 case EbtISampler3D: return GL_INT_SAMPLER_3D;
4056 case EbtISamplerCube: return GL_INT_SAMPLER_CUBE;
4057 case EbtISampler2DArray: return GL_INT_SAMPLER_2D_ARRAY;
4058 case EbtUSampler2D: return GL_UNSIGNED_INT_SAMPLER_2D;
4059 case EbtUSampler3D: return GL_UNSIGNED_INT_SAMPLER_3D;
4060 case EbtUSamplerCube: return GL_UNSIGNED_INT_SAMPLER_CUBE;
4061 case EbtUSampler2DArray: return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
4062 case EbtSampler2DShadow: return GL_SAMPLER_2D_SHADOW;
4063 case EbtSamplerCubeShadow: return GL_SAMPLER_CUBE_SHADOW;
4064 case EbtSampler2DArrayShadow: return GL_SAMPLER_2D_ARRAY_SHADOW;
4065 default: UNREACHABLE();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004066 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004067
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004068 return GL_NONE;
4069}
4070
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004071GLenum OutputHLSL::glVariablePrecision(const TType &type)
4072{
4073 if (type.getBasicType() == EbtFloat)
4074 {
4075 switch (type.getPrecision())
4076 {
4077 case EbpHigh: return GL_HIGH_FLOAT;
4078 case EbpMedium: return GL_MEDIUM_FLOAT;
4079 case EbpLow: return GL_LOW_FLOAT;
4080 case EbpUndefined:
4081 // Should be defined as the default precision by the parser
4082 default: UNREACHABLE();
4083 }
4084 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004085 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004086 {
4087 switch (type.getPrecision())
4088 {
4089 case EbpHigh: return GL_HIGH_INT;
4090 case EbpMedium: return GL_MEDIUM_INT;
4091 case EbpLow: return GL_LOW_INT;
4092 case EbpUndefined:
4093 // Should be defined as the default precision by the parser
4094 default: UNREACHABLE();
4095 }
4096 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004097
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00004098 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004099 return GL_NONE;
4100}
4101
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004102bool OutputHLSL::isVaryingOut(TQualifier qualifier)
4103{
4104 switch(qualifier)
4105 {
4106 case EvqVaryingOut:
4107 case EvqInvariantVaryingOut:
4108 case EvqSmoothOut:
4109 case EvqFlatOut:
4110 case EvqCentroidOut:
Jamie Madill19571812013-08-12 15:26:34 -07004111 case EvqVertexOut:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004112 return true;
4113 }
4114
4115 return false;
4116}
4117
4118bool OutputHLSL::isVaryingIn(TQualifier qualifier)
4119{
4120 switch(qualifier)
4121 {
4122 case EvqVaryingIn:
4123 case EvqInvariantVaryingIn:
4124 case EvqSmoothIn:
4125 case EvqFlatIn:
4126 case EvqCentroidIn:
Jamie Madill19571812013-08-12 15:26:34 -07004127 case EvqFragmentIn:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004128 return true;
4129 }
4130
4131 return false;
4132}
4133
4134bool OutputHLSL::isVarying(TQualifier qualifier)
4135{
4136 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
4137}
4138
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004139}