blob: 1b0dce169607300ca0fbdf9f6ce14ed6ba6d08d1 [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;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500991 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400992 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;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500995 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400996 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 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001134 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001135 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001136 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1137 {
1138 out << " float width; float height; float layers; float levels;\n";
1139
1140 out << " uint mip = 0;\n";
1141
1142 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
1143
1144 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
1145 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
1146 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
1147 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
1148
1149 // FACE_POSITIVE_X = 000b
1150 // FACE_NEGATIVE_X = 001b
1151 // FACE_POSITIVE_Y = 010b
1152 // FACE_NEGATIVE_Y = 011b
1153 // FACE_POSITIVE_Z = 100b
1154 // FACE_NEGATIVE_Z = 101b
1155 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
1156
1157 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
1158 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
1159 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
1160
1161 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
1162 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
1163 }
1164 else if (IsIntegerSampler(textureFunction->sampler) &&
1165 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001166 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001167 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001168 {
Nicolas Capens93e50de2013-07-09 13:46:28 -04001169 if (IsSamplerArray(textureFunction->sampler))
1170 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001171 out << " float width; float height; float layers; float levels;\n";
1172
1173 if (textureFunction->method == TextureFunction::LOD0)
1174 {
1175 out << " uint mip = 0;\n";
1176 }
1177 else
1178 {
1179 if (textureFunction->method == TextureFunction::IMPLICIT ||
1180 textureFunction->method == TextureFunction::BIAS)
1181 {
1182 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1183 " float2 tSized = float2(t.x * width, t.y * height);\n"
1184 " float dx = length(ddx(tSized));\n"
1185 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001186 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001187
1188 if (textureFunction->method == TextureFunction::BIAS)
1189 {
1190 out << " lod += bias;\n";
1191 }
1192 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001193 else if (textureFunction->method == TextureFunction::GRAD)
1194 {
1195 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1196 " float lod = log2(max(length(ddx), length(ddy)));\n";
1197 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001198
1199 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1200 }
1201
1202 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001203 }
1204 else
1205 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001206 out << " float width; float height; float levels;\n";
1207
1208 if (textureFunction->method == TextureFunction::LOD0)
1209 {
1210 out << " uint mip = 0;\n";
1211 }
1212 else
1213 {
1214 if (textureFunction->method == TextureFunction::IMPLICIT ||
1215 textureFunction->method == TextureFunction::BIAS)
1216 {
1217 out << " x.GetDimensions(0, width, height, levels);\n"
1218 " float2 tSized = float2(t.x * width, t.y * height);\n"
1219 " float dx = length(ddx(tSized));\n"
1220 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001221 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001222
1223 if (textureFunction->method == TextureFunction::BIAS)
1224 {
1225 out << " lod += bias;\n";
1226 }
1227 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05001228 else if (textureFunction->method == TextureFunction::LOD)
1229 {
1230 out << " x.GetDimensions(0, width, height, levels);\n";
1231 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001232 else if (textureFunction->method == TextureFunction::GRAD)
1233 {
1234 out << " x.GetDimensions(0, width, height, levels);\n"
1235 " float lod = log2(max(length(ddx), length(ddy)));\n";
1236 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001237
1238 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1239 }
1240
1241 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001242 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001243 }
1244 else if (IsSampler3D(textureFunction->sampler))
1245 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001246 out << " float width; float height; float depth; float levels;\n";
1247
1248 if (textureFunction->method == TextureFunction::LOD0)
1249 {
1250 out << " uint mip = 0;\n";
1251 }
1252 else
1253 {
1254 if (textureFunction->method == TextureFunction::IMPLICIT ||
1255 textureFunction->method == TextureFunction::BIAS)
1256 {
1257 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1258 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
1259 " float dx = length(ddx(tSized));\n"
1260 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001261 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001262
1263 if (textureFunction->method == TextureFunction::BIAS)
1264 {
1265 out << " lod += bias;\n";
1266 }
1267 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001268 else if (textureFunction->method == TextureFunction::GRAD)
1269 {
1270 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1271 " float lod = log2(max(length(ddx), length(ddy)));\n";
1272 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001273
1274 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1275 }
1276
1277 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001278 }
1279 else UNREACHABLE();
1280 }
1281
1282 out << " return ";
1283
1284 // HLSL intrinsic
1285 if (mOutputType == SH_HLSL9_OUTPUT)
1286 {
1287 switch(textureFunction->sampler)
1288 {
1289 case EbtSampler2D: out << "tex2D"; break;
1290 case EbtSamplerCube: out << "texCUBE"; break;
1291 default: UNREACHABLE();
1292 }
1293
Nicolas Capens75fb4752013-07-10 15:14:47 -04001294 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001295 {
1296 case TextureFunction::IMPLICIT: out << "(s, "; break;
1297 case TextureFunction::BIAS: out << "bias(s, "; break;
1298 case TextureFunction::LOD: out << "lod(s, "; break;
1299 case TextureFunction::LOD0: out << "lod(s, "; break;
1300 default: UNREACHABLE();
1301 }
1302 }
1303 else if (mOutputType == SH_HLSL11_OUTPUT)
1304 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001305 if (textureFunction->method == TextureFunction::GRAD)
1306 {
1307 if (IsIntegerSampler(textureFunction->sampler))
1308 {
1309 out << "x.Load(";
1310 }
1311 else if (IsShadowSampler(textureFunction->sampler))
1312 {
1313 out << "x.SampleCmpLevelZero(s, ";
1314 }
1315 else
1316 {
1317 out << "x.SampleGrad(s, ";
1318 }
1319 }
1320 else if (IsIntegerSampler(textureFunction->sampler) ||
1321 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001322 {
1323 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001324 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001325 else if (IsShadowSampler(textureFunction->sampler))
1326 {
1327 out << "x.SampleCmp(s, ";
1328 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001329 else
1330 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001331 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001332 {
1333 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1334 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1335 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1336 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
1337 default: UNREACHABLE();
1338 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001339 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001340 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001341 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001342
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001343 // Integer sampling requires integer addresses
1344 TString addressx = "";
1345 TString addressy = "";
1346 TString addressz = "";
1347 TString close = "";
1348
Nicolas Capensfc014542014-02-18 14:47:13 -05001349 if (IsIntegerSampler(textureFunction->sampler) ||
1350 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001351 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001352 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001353 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001354 case 2: out << "int3("; break;
1355 case 3: out << "int4("; break;
1356 default: UNREACHABLE();
1357 }
1358
Nicolas Capensfc014542014-02-18 14:47:13 -05001359 // Convert from normalized floating-point to integer
1360 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001361 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001362 addressx = "int(floor(width * frac((";
1363 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001364
Nicolas Capensfc014542014-02-18 14:47:13 -05001365 if (IsSamplerArray(textureFunction->sampler))
1366 {
1367 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1368 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001369 else if (IsSamplerCube(textureFunction->sampler))
1370 {
1371 addressz = "((((";
1372 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001373 else
1374 {
1375 addressz = "int(floor(depth * frac((";
1376 }
1377
1378 close = "))))";
1379 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001380 }
1381 else
1382 {
1383 switch(hlslCoords)
1384 {
1385 case 2: out << "float2("; break;
1386 case 3: out << "float3("; break;
1387 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001388 default: UNREACHABLE();
1389 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001390 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001391
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001392 TString proj = ""; // Only used for projected textures
1393
1394 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001395 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001396 switch(textureFunction->coords)
1397 {
1398 case 3: proj = " / t.z"; break;
1399 case 4: proj = " / t.w"; break;
1400 default: UNREACHABLE();
1401 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001402 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001403
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001404 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001405
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001406 if (mOutputType == SH_HLSL9_OUTPUT)
1407 {
1408 if (hlslCoords >= 3)
1409 {
1410 if (textureFunction->coords < 3)
1411 {
1412 out << ", 0";
1413 }
1414 else
1415 {
1416 out << ", t.z" + proj;
1417 }
1418 }
1419
1420 if (hlslCoords == 4)
1421 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001422 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001423 {
1424 case TextureFunction::BIAS: out << ", bias"; break;
1425 case TextureFunction::LOD: out << ", lod"; break;
1426 case TextureFunction::LOD0: out << ", 0"; break;
1427 default: UNREACHABLE();
1428 }
1429 }
1430
1431 out << "));\n";
1432 }
1433 else if (mOutputType == SH_HLSL11_OUTPUT)
1434 {
1435 if (hlslCoords >= 3)
1436 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001437 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1438 {
1439 out << ", face";
1440 }
1441 else
1442 {
1443 out << ", " + addressz + ("t.z" + proj) + close;
1444 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001445 }
1446
Nicolas Capensd11d5492014-02-19 17:06:10 -05001447 if (textureFunction->method == TextureFunction::GRAD)
1448 {
1449 if (IsIntegerSampler(textureFunction->sampler))
1450 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001451 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001452 }
1453 else if (IsShadowSampler(textureFunction->sampler))
1454 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001455 // Compare value
Nicolas Capensd11d5492014-02-19 17:06:10 -05001456 switch(textureFunction->coords)
1457 {
1458 case 3: out << "), t.z"; break;
1459 case 4: out << "), t.w"; break;
1460 default: UNREACHABLE();
1461 }
1462 }
1463 else
1464 {
1465 out << "), ddx, ddy";
1466 }
1467 }
1468 else if (IsIntegerSampler(textureFunction->sampler) ||
1469 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001470 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001471 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001472 }
1473 else if (IsShadowSampler(textureFunction->sampler))
1474 {
1475 // Compare value
1476 switch(textureFunction->coords)
1477 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001478 case 3: out << "), t.z"; break;
1479 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001480 default: UNREACHABLE();
1481 }
1482 }
1483 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001484 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001485 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001486 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001487 case TextureFunction::IMPLICIT: out << ")"; break;
1488 case TextureFunction::BIAS: out << "), bias"; break;
1489 case TextureFunction::LOD: out << "), lod"; break;
1490 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001491 default: UNREACHABLE();
1492 }
1493 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001494
1495 if (textureFunction->offset)
1496 {
1497 out << ", offset";
1498 }
1499
1500 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001501 }
1502 else UNREACHABLE();
1503 }
1504
1505 out << "\n"
1506 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001507 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001508 }
1509
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001510 if (mUsesFragCoord)
1511 {
1512 out << "#define GL_USES_FRAG_COORD\n";
1513 }
1514
1515 if (mUsesPointCoord)
1516 {
1517 out << "#define GL_USES_POINT_COORD\n";
1518 }
1519
1520 if (mUsesFrontFacing)
1521 {
1522 out << "#define GL_USES_FRONT_FACING\n";
1523 }
1524
1525 if (mUsesPointSize)
1526 {
1527 out << "#define GL_USES_POINT_SIZE\n";
1528 }
1529
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001530 if (mUsesFragDepth)
1531 {
1532 out << "#define GL_USES_FRAG_DEPTH\n";
1533 }
1534
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001535 if (mUsesDepthRange)
1536 {
1537 out << "#define GL_USES_DEPTH_RANGE\n";
1538 }
1539
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001540 if (mUsesXor)
1541 {
1542 out << "bool xor(bool p, bool q)\n"
1543 "{\n"
1544 " return (p || q) && !(p && q);\n"
1545 "}\n"
1546 "\n";
1547 }
1548
1549 if (mUsesMod1)
1550 {
1551 out << "float mod(float x, float y)\n"
1552 "{\n"
1553 " return x - y * floor(x / y);\n"
1554 "}\n"
1555 "\n";
1556 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001557
1558 if (mUsesMod2v)
1559 {
1560 out << "float2 mod(float2 x, float2 y)\n"
1561 "{\n"
1562 " return x - y * floor(x / y);\n"
1563 "}\n"
1564 "\n";
1565 }
1566
1567 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001568 {
1569 out << "float2 mod(float2 x, float y)\n"
1570 "{\n"
1571 " return x - y * floor(x / y);\n"
1572 "}\n"
1573 "\n";
1574 }
1575
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001576 if (mUsesMod3v)
1577 {
1578 out << "float3 mod(float3 x, float3 y)\n"
1579 "{\n"
1580 " return x - y * floor(x / y);\n"
1581 "}\n"
1582 "\n";
1583 }
1584
1585 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001586 {
1587 out << "float3 mod(float3 x, float y)\n"
1588 "{\n"
1589 " return x - y * floor(x / y);\n"
1590 "}\n"
1591 "\n";
1592 }
1593
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001594 if (mUsesMod4v)
1595 {
1596 out << "float4 mod(float4 x, float4 y)\n"
1597 "{\n"
1598 " return x - y * floor(x / y);\n"
1599 "}\n"
1600 "\n";
1601 }
1602
1603 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001604 {
1605 out << "float4 mod(float4 x, float y)\n"
1606 "{\n"
1607 " return x - y * floor(x / y);\n"
1608 "}\n"
1609 "\n";
1610 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001611
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001612 if (mUsesFaceforward1)
1613 {
1614 out << "float faceforward(float N, float I, float Nref)\n"
1615 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001616 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001617 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001618 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001619 " }\n"
1620 " else\n"
1621 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001622 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001623 " }\n"
1624 "}\n"
1625 "\n";
1626 }
1627
1628 if (mUsesFaceforward2)
1629 {
1630 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1631 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001632 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001633 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001634 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001635 " }\n"
1636 " else\n"
1637 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001638 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001639 " }\n"
1640 "}\n"
1641 "\n";
1642 }
1643
1644 if (mUsesFaceforward3)
1645 {
1646 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1647 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001648 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001649 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001650 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001651 " }\n"
1652 " else\n"
1653 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001654 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001655 " }\n"
1656 "}\n"
1657 "\n";
1658 }
1659
1660 if (mUsesFaceforward4)
1661 {
1662 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1663 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001664 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001665 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001666 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001667 " }\n"
1668 " else\n"
1669 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001670 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001671 " }\n"
1672 "}\n"
1673 "\n";
1674 }
1675
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001676 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001677 {
1678 out << "float atanyx(float y, float x)\n"
1679 "{\n"
1680 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1681 " return atan2(y, x);\n"
1682 "}\n";
1683 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001684
1685 if (mUsesAtan2_2)
1686 {
1687 out << "float2 atanyx(float2 y, float2 x)\n"
1688 "{\n"
1689 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1690 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1691 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1692 "}\n";
1693 }
1694
1695 if (mUsesAtan2_3)
1696 {
1697 out << "float3 atanyx(float3 y, float3 x)\n"
1698 "{\n"
1699 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1700 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1701 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1702 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1703 "}\n";
1704 }
1705
1706 if (mUsesAtan2_4)
1707 {
1708 out << "float4 atanyx(float4 y, float4 x)\n"
1709 "{\n"
1710 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1711 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1712 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1713 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1714 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1715 "}\n";
1716 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001717}
1718
1719void OutputHLSL::visitSymbol(TIntermSymbol *node)
1720{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001721 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001722
Jamie Madill570e04d2013-06-21 09:15:33 -04001723 // Handle accessing std140 structs by value
1724 if (mFlaggedStructMappedNames.count(node) > 0)
1725 {
1726 out << mFlaggedStructMappedNames[node];
1727 return;
1728 }
1729
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001730 TString name = node->getSymbol();
1731
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001732 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001733 {
1734 mUsesDepthRange = true;
1735 out << name;
1736 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001737 else
1738 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001739 TQualifier qualifier = node->getQualifier();
1740
1741 if (qualifier == EvqUniform)
1742 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001743 const TType& nodeType = node->getType();
1744 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1745
1746 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001747 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001748 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001749 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001750 else
1751 {
1752 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001753 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001754
1755 out << decorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001756 }
Jamie Madill19571812013-08-12 15:26:34 -07001757 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001758 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001759 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001760 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001761 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001762 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001763 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001764 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001765 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001766 }
Jamie Madill19571812013-08-12 15:26:34 -07001767 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001768 {
1769 mReferencedOutputVariables[name] = node;
1770 out << "out_" << name;
1771 }
1772 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001773 {
1774 out << "gl_Color[0]";
1775 mUsesFragColor = true;
1776 }
1777 else if (qualifier == EvqFragData)
1778 {
1779 out << "gl_Color";
1780 mUsesFragData = true;
1781 }
1782 else if (qualifier == EvqFragCoord)
1783 {
1784 mUsesFragCoord = true;
1785 out << name;
1786 }
1787 else if (qualifier == EvqPointCoord)
1788 {
1789 mUsesPointCoord = true;
1790 out << name;
1791 }
1792 else if (qualifier == EvqFrontFacing)
1793 {
1794 mUsesFrontFacing = true;
1795 out << name;
1796 }
1797 else if (qualifier == EvqPointSize)
1798 {
1799 mUsesPointSize = true;
1800 out << name;
1801 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001802 else if (name == "gl_FragDepthEXT")
1803 {
1804 mUsesFragDepth = true;
1805 out << "gl_Depth";
1806 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001807 else if (qualifier == EvqInternal)
1808 {
1809 out << name;
1810 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001811 else
1812 {
1813 out << decorate(name);
1814 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001815 }
1816}
1817
1818bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1819{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001820 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001821
Jamie Madill570e04d2013-06-21 09:15:33 -04001822 // Handle accessing std140 structs by value
1823 if (mFlaggedStructMappedNames.count(node) > 0)
1824 {
1825 out << mFlaggedStructMappedNames[node];
1826 return false;
1827 }
1828
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001829 switch (node->getOp())
1830 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001831 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001832 case EOpInitialize:
1833 if (visit == PreVisit)
1834 {
1835 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1836 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1837 // new variable is created before the assignment is evaluated), so we need to convert
1838 // this to "float t = x, x = t;".
1839
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001840 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1841 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001842
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001843 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1844 expression->traverse(&searchSymbol);
1845 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001846
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001847 if (sameSymbol)
1848 {
1849 // Type already printed
1850 out << "t" + str(mUniqueIndex) + " = ";
1851 expression->traverse(this);
1852 out << ", ";
1853 symbolNode->traverse(this);
1854 out << " = t" + str(mUniqueIndex);
1855
1856 mUniqueIndex++;
1857 return false;
1858 }
1859 }
1860 else if (visit == InVisit)
1861 {
1862 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001863 }
1864 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001865 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1866 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1867 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1868 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1869 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1870 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001871 if (visit == PreVisit)
1872 {
1873 out << "(";
1874 }
1875 else if (visit == InVisit)
1876 {
1877 out << " = mul(";
1878 node->getLeft()->traverse(this);
1879 out << ", transpose(";
1880 }
1881 else
1882 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001883 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001884 }
1885 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001886 case EOpMatrixTimesMatrixAssign:
1887 if (visit == PreVisit)
1888 {
1889 out << "(";
1890 }
1891 else if (visit == InVisit)
1892 {
1893 out << " = mul(";
1894 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001895 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001896 }
1897 else
1898 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001899 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001900 }
1901 break;
1902 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001903 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001904 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001905 const TType& leftType = node->getLeft()->getType();
1906 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001907 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001908 if (visit == PreVisit)
1909 {
1910 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1911 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1912
1913 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1914 out << interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1915
1916 return false;
1917 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001918 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001919 else
1920 {
1921 outputTriplet(visit, "", "[", "]");
1922 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001923 }
1924 break;
1925 case EOpIndexIndirect:
1926 // We do not currently support indirect references to interface blocks
1927 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1928 outputTriplet(visit, "", "[", "]");
1929 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001930 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001931 if (visit == InVisit)
1932 {
1933 const TStructure* structure = node->getLeft()->getType().getStruct();
1934 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1935 const TField* field = structure->fields()[index->getIConst(0)];
1936 out << "." + decorateField(field->name(), *structure);
1937
1938 return false;
1939 }
1940 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001941 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001942 if (visit == InVisit)
1943 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001944 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1945 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1946 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
1947 out << "." + decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001948
1949 return false;
1950 }
1951 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001952 case EOpVectorSwizzle:
1953 if (visit == InVisit)
1954 {
1955 out << ".";
1956
1957 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1958
1959 if (swizzle)
1960 {
1961 TIntermSequence &sequence = swizzle->getSequence();
1962
1963 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1964 {
1965 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1966
1967 if (element)
1968 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001969 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001970
1971 switch (i)
1972 {
1973 case 0: out << "x"; break;
1974 case 1: out << "y"; break;
1975 case 2: out << "z"; break;
1976 case 3: out << "w"; break;
1977 default: UNREACHABLE();
1978 }
1979 }
1980 else UNREACHABLE();
1981 }
1982 }
1983 else UNREACHABLE();
1984
1985 return false; // Fully processed
1986 }
1987 break;
1988 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1989 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1990 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1991 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001992 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001993 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001994 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001995 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001996 if (node->getOp() == EOpEqual)
1997 {
1998 outputTriplet(visit, "(", " == ", ")");
1999 }
2000 else
2001 {
2002 outputTriplet(visit, "(", " != ", ")");
2003 }
2004 }
2005 else if (node->getLeft()->getBasicType() == EbtStruct)
2006 {
2007 if (node->getOp() == EOpEqual)
2008 {
2009 out << "(";
2010 }
2011 else
2012 {
2013 out << "!(";
2014 }
2015
Jamie Madill98493dd2013-07-08 14:39:03 -04002016 const TStructure &structure = *node->getLeft()->getType().getStruct();
2017 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002018
Jamie Madill98493dd2013-07-08 14:39:03 -04002019 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002020 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002021 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002022
2023 node->getLeft()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04002024 out << "." + decorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002025 node->getRight()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04002026 out << "." + decorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002027
Jamie Madill98493dd2013-07-08 14:39:03 -04002028 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002029 {
2030 out << " && ";
2031 }
2032 }
2033
2034 out << ")";
2035
2036 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002037 }
2038 else
2039 {
Jamie Madill0b20c942013-07-19 16:36:56 -04002040 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002041
2042 if (node->getOp() == EOpEqual)
2043 {
Jamie Madill0b20c942013-07-19 16:36:56 -04002044 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002045 }
2046 else
2047 {
Jamie Madill0b20c942013-07-19 16:36:56 -04002048 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002049 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002050 }
2051 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002052 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2053 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2054 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2055 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2056 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00002057 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00002058 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
2059 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00002060 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00002061 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002062 if (node->getRight()->hasSideEffects())
2063 {
2064 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
2065 return false;
2066 }
2067 else
2068 {
2069 outputTriplet(visit, "(", " || ", ")");
2070 return true;
2071 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002072 case EOpLogicalXor:
2073 mUsesXor = true;
2074 outputTriplet(visit, "xor(", ", ", ")");
2075 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00002076 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002077 if (node->getRight()->hasSideEffects())
2078 {
2079 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
2080 return false;
2081 }
2082 else
2083 {
2084 outputTriplet(visit, "(", " && ", ")");
2085 return true;
2086 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002087 default: UNREACHABLE();
2088 }
2089
2090 return true;
2091}
2092
2093bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
2094{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002095 switch (node->getOp())
2096 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002097 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
2098 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
2099 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
2100 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
2101 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
2102 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
2103 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002104 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04002105 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002106 case EOpConvFloatToBool:
2107 switch (node->getOperand()->getType().getNominalSize())
2108 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002109 case 1: outputTriplet(visit, "bool(", "", ")"); break;
2110 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
2111 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
2112 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002113 default: UNREACHABLE();
2114 }
2115 break;
2116 case EOpConvBoolToFloat:
2117 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04002118 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002119 switch (node->getOperand()->getType().getNominalSize())
2120 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002121 case 1: outputTriplet(visit, "float(", "", ")"); break;
2122 case 2: outputTriplet(visit, "float2(", "", ")"); break;
2123 case 3: outputTriplet(visit, "float3(", "", ")"); break;
2124 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002125 default: UNREACHABLE();
2126 }
2127 break;
2128 case EOpConvFloatToInt:
2129 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04002130 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002131 switch (node->getOperand()->getType().getNominalSize())
2132 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002133 case 1: outputTriplet(visit, "int(", "", ")"); break;
2134 case 2: outputTriplet(visit, "int2(", "", ")"); break;
2135 case 3: outputTriplet(visit, "int3(", "", ")"); break;
2136 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002137 default: UNREACHABLE();
2138 }
2139 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002140 case EOpConvFloatToUInt:
2141 case EOpConvBoolToUInt:
2142 case EOpConvIntToUInt:
Jamie Madilla16f1672013-07-03 15:15:17 -04002143 switch (node->getOperand()->getType().getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002144 {
2145 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002146 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
2147 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
2148 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002149 default: UNREACHABLE();
2150 }
2151 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002152 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
2153 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
2154 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
2155 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
2156 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
2157 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
2158 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
2159 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
2160 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
2161 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
2162 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
2163 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
2164 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
2165 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
2166 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
2167 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
2168 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
2169 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
2170 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
2171 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
2172 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002173 case EOpDFdx:
2174 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2175 {
2176 outputTriplet(visit, "(", "", ", 0.0)");
2177 }
2178 else
2179 {
2180 outputTriplet(visit, "ddx(", "", ")");
2181 }
2182 break;
2183 case EOpDFdy:
2184 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2185 {
2186 outputTriplet(visit, "(", "", ", 0.0)");
2187 }
2188 else
2189 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00002190 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002191 }
2192 break;
2193 case EOpFwidth:
2194 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2195 {
2196 outputTriplet(visit, "(", "", ", 0.0)");
2197 }
2198 else
2199 {
2200 outputTriplet(visit, "fwidth(", "", ")");
2201 }
2202 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002203 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
2204 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002205 default: UNREACHABLE();
2206 }
2207
2208 return true;
2209}
2210
2211bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2212{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002213 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002214
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002215 switch (node->getOp())
2216 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002217 case EOpSequence:
2218 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002219 if (mInsideFunction)
2220 {
Jamie Madill075edd82013-07-08 13:30:19 -04002221 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002222 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002223
2224 mScopeDepth++;
2225
2226 if (mScopeBracket.size() < mScopeDepth)
2227 {
2228 mScopeBracket.push_back(0); // New scope level
2229 }
2230 else
2231 {
2232 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
2233 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002234 }
2235
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002236 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
2237 {
Jamie Madill075edd82013-07-08 13:30:19 -04002238 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002239
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002240 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002241
2242 out << ";\n";
2243 }
2244
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002245 if (mInsideFunction)
2246 {
Jamie Madill075edd82013-07-08 13:30:19 -04002247 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002248 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002249
2250 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002251 }
2252
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002253 return false;
2254 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002255 case EOpDeclaration:
2256 if (visit == PreVisit)
2257 {
2258 TIntermSequence &sequence = node->getSequence();
2259 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002260
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00002261 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002262 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002263 if (variable->getType().getStruct())
2264 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002265 addConstructor(variable->getType(), scopedStruct(variable->getType().getStruct()->name()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00002266 }
2267
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002268 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002269 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00002270 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002271 {
2272 out << "static ";
2273 }
2274
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002275 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002276
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002277 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002278 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002279 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002280
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002281 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002282 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002283 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002284 out << arrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05002285 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002286 }
2287 else
2288 {
2289 (*sit)->traverse(this);
2290 }
2291
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002292 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002293 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002294 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002295 }
2296 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002297 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002298 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2299 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002300 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002301 }
2302 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002303 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002304 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002305 {
2306 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2307 {
2308 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2309
2310 if (symbol)
2311 {
2312 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2313 mReferencedVaryings[symbol->getSymbol()] = symbol;
2314 }
2315 else
2316 {
2317 (*sit)->traverse(this);
2318 }
2319 }
2320 }
2321
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002322 return false;
2323 }
2324 else if (visit == InVisit)
2325 {
2326 out << ", ";
2327 }
2328 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002329 case EOpPrototype:
2330 if (visit == PreVisit)
2331 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002332 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002333
2334 TIntermSequence &arguments = node->getSequence();
2335
2336 for (unsigned int i = 0; i < arguments.size(); i++)
2337 {
2338 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2339
2340 if (symbol)
2341 {
2342 out << argumentString(symbol);
2343
2344 if (i < arguments.size() - 1)
2345 {
2346 out << ", ";
2347 }
2348 }
2349 else UNREACHABLE();
2350 }
2351
2352 out << ");\n";
2353
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002354 // Also prototype the Lod0 variant if needed
2355 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2356 {
2357 mOutputLod0Function = true;
2358 node->traverse(this);
2359 mOutputLod0Function = false;
2360 }
2361
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002362 return false;
2363 }
2364 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002365 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002366 case EOpFunction:
2367 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002368 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002369
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002370 out << typeString(node->getType()) << " ";
2371
2372 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002373 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002374 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002375 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002376 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002377 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002378 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002379 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002380
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002381 TIntermSequence &sequence = node->getSequence();
2382 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2383
2384 for (unsigned int i = 0; i < arguments.size(); i++)
2385 {
2386 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2387
2388 if (symbol)
2389 {
2390 if (symbol->getType().getStruct())
2391 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002392 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getStruct()->name()), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002393 }
2394
2395 out << argumentString(symbol);
2396
2397 if (i < arguments.size() - 1)
2398 {
2399 out << ", ";
2400 }
2401 }
2402 else UNREACHABLE();
2403 }
2404
2405 out << ")\n"
2406 "{\n";
2407
2408 if (sequence.size() > 1)
2409 {
2410 mInsideFunction = true;
2411 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002412 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002413 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002414
2415 out << "}\n";
2416
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002417 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2418 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002419 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002420 {
2421 mOutputLod0Function = true;
2422 node->traverse(this);
2423 mOutputLod0Function = false;
2424 }
2425 }
2426
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002427 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002428 }
2429 break;
2430 case EOpFunctionCall:
2431 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002432 TString name = TFunction::unmangleName(node->getName());
2433 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002434 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002435
2436 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002437 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002438 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002439 }
2440 else
2441 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002442 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2443
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002444 TextureFunction textureFunction;
2445 textureFunction.sampler = samplerType;
2446 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002447 textureFunction.method = TextureFunction::IMPLICIT;
2448 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002449 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002450
2451 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002452 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002453 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002454 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002455 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002456 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002457 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002458 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002459 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002460 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002461 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002462 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002463 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002464 else if (name == "texture2DProjLod" || name == "textureProjLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002465 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002466 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002467 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002468 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002469 else if (name == "textureSize")
2470 {
2471 textureFunction.method = TextureFunction::SIZE;
2472 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002473 else if (name == "textureOffset")
2474 {
2475 textureFunction.method = TextureFunction::IMPLICIT;
2476 textureFunction.offset = true;
2477 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002478 else if (name == "textureProjOffset")
2479 {
2480 textureFunction.method = TextureFunction::IMPLICIT;
2481 textureFunction.offset = true;
2482 textureFunction.proj = true;
2483 }
2484 else if (name == "textureLodOffset")
2485 {
2486 textureFunction.method = TextureFunction::LOD;
2487 textureFunction.offset = true;
2488 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002489 else if (name == "textureProjLod")
2490 {
2491 textureFunction.method = TextureFunction::LOD;
2492 textureFunction.proj = true;
2493 }
2494 else if (name == "textureProjLodOffset")
2495 {
2496 textureFunction.method = TextureFunction::LOD;
2497 textureFunction.proj = true;
2498 textureFunction.offset = true;
2499 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002500 else if (name == "texelFetch")
2501 {
2502 textureFunction.method = TextureFunction::FETCH;
2503 }
2504 else if (name == "texelFetchOffset")
2505 {
2506 textureFunction.method = TextureFunction::FETCH;
2507 textureFunction.offset = true;
2508 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05002509 else if (name == "textureGrad")
2510 {
2511 textureFunction.method = TextureFunction::GRAD;
2512 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002513 else if (name == "textureGradOffset")
2514 {
2515 textureFunction.method = TextureFunction::GRAD;
2516 textureFunction.offset = true;
2517 }
Nicolas Capensf7378e32014-02-19 17:29:32 -05002518 else if (name == "textureProjGrad")
2519 {
2520 textureFunction.method = TextureFunction::GRAD;
2521 textureFunction.proj = true;
2522 }
2523 else if (name == "textureProjGradOffset")
2524 {
2525 textureFunction.method = TextureFunction::GRAD;
2526 textureFunction.proj = true;
2527 textureFunction.offset = true;
2528 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002529 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002530
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002531 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002532 {
2533 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2534 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002535 textureFunction.method = TextureFunction::LOD0;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002536 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002537 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002538 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002539 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2540
2541 if (textureFunction.offset)
2542 {
2543 mandatoryArgumentCount++;
2544 }
2545
2546 if (arguments.size() > mandatoryArgumentCount) // Bias argument is optional
2547 {
2548 textureFunction.method = TextureFunction::BIAS;
2549 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002550 }
2551 }
2552
2553 mUsesTexture.insert(textureFunction);
2554
2555 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002556 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002557
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002558 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2559 {
2560 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2561 {
2562 out << "texture_";
2563 (*arg)->traverse(this);
2564 out << ", sampler_";
2565 }
2566
2567 (*arg)->traverse(this);
2568
2569 if (arg < arguments.end() - 1)
2570 {
2571 out << ", ";
2572 }
2573 }
2574
2575 out << ")";
2576
2577 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002578 }
2579 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002580 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002581 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002582 addConstructor(node->getType(), "vec1", &node->getSequence());
2583 outputTriplet(visit, "vec1(", "", ")");
2584 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002585 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002586 addConstructor(node->getType(), "vec2", &node->getSequence());
2587 outputTriplet(visit, "vec2(", ", ", ")");
2588 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002589 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002590 addConstructor(node->getType(), "vec3", &node->getSequence());
2591 outputTriplet(visit, "vec3(", ", ", ")");
2592 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002593 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002594 addConstructor(node->getType(), "vec4", &node->getSequence());
2595 outputTriplet(visit, "vec4(", ", ", ")");
2596 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002597 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002598 addConstructor(node->getType(), "bvec1", &node->getSequence());
2599 outputTriplet(visit, "bvec1(", "", ")");
2600 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002601 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002602 addConstructor(node->getType(), "bvec2", &node->getSequence());
2603 outputTriplet(visit, "bvec2(", ", ", ")");
2604 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002605 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002606 addConstructor(node->getType(), "bvec3", &node->getSequence());
2607 outputTriplet(visit, "bvec3(", ", ", ")");
2608 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002609 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002610 addConstructor(node->getType(), "bvec4", &node->getSequence());
2611 outputTriplet(visit, "bvec4(", ", ", ")");
2612 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002613 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002614 addConstructor(node->getType(), "ivec1", &node->getSequence());
2615 outputTriplet(visit, "ivec1(", "", ")");
2616 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002617 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002618 addConstructor(node->getType(), "ivec2", &node->getSequence());
2619 outputTriplet(visit, "ivec2(", ", ", ")");
2620 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002621 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002622 addConstructor(node->getType(), "ivec3", &node->getSequence());
2623 outputTriplet(visit, "ivec3(", ", ", ")");
2624 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002625 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002626 addConstructor(node->getType(), "ivec4", &node->getSequence());
2627 outputTriplet(visit, "ivec4(", ", ", ")");
2628 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002629 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002630 addConstructor(node->getType(), "uvec1", &node->getSequence());
2631 outputTriplet(visit, "uvec1(", "", ")");
2632 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002633 case EOpConstructUVec2:
2634 addConstructor(node->getType(), "uvec2", &node->getSequence());
2635 outputTriplet(visit, "uvec2(", ", ", ")");
2636 break;
2637 case EOpConstructUVec3:
2638 addConstructor(node->getType(), "uvec3", &node->getSequence());
2639 outputTriplet(visit, "uvec3(", ", ", ")");
2640 break;
2641 case EOpConstructUVec4:
2642 addConstructor(node->getType(), "uvec4", &node->getSequence());
2643 outputTriplet(visit, "uvec4(", ", ", ")");
2644 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002645 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002646 addConstructor(node->getType(), "mat2", &node->getSequence());
2647 outputTriplet(visit, "mat2(", ", ", ")");
2648 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002649 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002650 addConstructor(node->getType(), "mat3", &node->getSequence());
2651 outputTriplet(visit, "mat3(", ", ", ")");
2652 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002653 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002654 addConstructor(node->getType(), "mat4", &node->getSequence());
2655 outputTriplet(visit, "mat4(", ", ", ")");
2656 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002657 case EOpConstructStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04002658 addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence());
2659 outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002660 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002661 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2662 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2663 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2664 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2665 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2666 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002667 case EOpMod:
2668 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002669 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002670 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2671 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2672 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002673 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002674 case 11: mUsesMod1 = true; break;
2675 case 22: mUsesMod2v = true; break;
2676 case 21: mUsesMod2f = true; break;
2677 case 33: mUsesMod3v = true; break;
2678 case 31: mUsesMod3f = true; break;
2679 case 44: mUsesMod4v = true; break;
2680 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002681 default: UNREACHABLE();
2682 }
2683
2684 outputTriplet(visit, "mod(", ", ", ")");
2685 }
2686 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002687 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002688 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002689 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002690 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2691 {
2692 case 1: mUsesAtan2_1 = true; break;
2693 case 2: mUsesAtan2_2 = true; break;
2694 case 3: mUsesAtan2_3 = true; break;
2695 case 4: mUsesAtan2_4 = true; break;
2696 default: UNREACHABLE();
2697 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002698 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002699 break;
2700 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2701 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2702 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2703 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2704 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2705 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2706 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2707 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2708 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002709 case EOpFaceForward:
2710 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002711 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002712 {
2713 case 1: mUsesFaceforward1 = true; break;
2714 case 2: mUsesFaceforward2 = true; break;
2715 case 3: mUsesFaceforward3 = true; break;
2716 case 4: mUsesFaceforward4 = true; break;
2717 default: UNREACHABLE();
2718 }
2719
2720 outputTriplet(visit, "faceforward(", ", ", ")");
2721 }
2722 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002723 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2724 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2725 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002726 default: UNREACHABLE();
2727 }
2728
2729 return true;
2730}
2731
2732bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2733{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002734 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002735
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002736 if (node->usesTernaryOperator())
2737 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002738 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002739 }
2740 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002741 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002742 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002743
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002744 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002745
2746 node->getCondition()->traverse(this);
2747
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002748 out << ")\n";
2749
Jamie Madill075edd82013-07-08 13:30:19 -04002750 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002751 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002752
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002753 bool discard = false;
2754
daniel@transgaming.combb885322010-04-15 20:45:24 +00002755 if (node->getTrueBlock())
2756 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002757 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002758
2759 // Detect true discard
2760 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002761 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002762
Jamie Madill075edd82013-07-08 13:30:19 -04002763 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002764 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002765
2766 if (node->getFalseBlock())
2767 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002768 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002769
Jamie Madill075edd82013-07-08 13:30:19 -04002770 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002771 out << "{\n";
2772
Jamie Madill075edd82013-07-08 13:30:19 -04002773 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002774 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002775
Jamie Madill075edd82013-07-08 13:30:19 -04002776 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002777 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002778
2779 // Detect false discard
2780 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2781 }
2782
2783 // ANGLE issue 486: Detect problematic conditional discard
2784 if (discard && FindSideEffectRewriting::search(node))
2785 {
2786 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002787 }
2788 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002789
2790 return false;
2791}
2792
2793void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2794{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002795 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002796}
2797
2798bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2799{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002800 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2801
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002802 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002803 {
2804 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2805 }
2806
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002807 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002808 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002809 if (handleExcessiveLoop(node))
2810 {
2811 return false;
2812 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002813 }
2814
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002815 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002816
alokp@chromium.org52813552010-11-16 18:36:09 +00002817 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002818 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002819 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002820
Jamie Madill075edd82013-07-08 13:30:19 -04002821 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002822 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002823 }
2824 else
2825 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002826 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002827
2828 if (node->getInit())
2829 {
2830 node->getInit()->traverse(this);
2831 }
2832
2833 out << "; ";
2834
alokp@chromium.org52813552010-11-16 18:36:09 +00002835 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002836 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002837 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002838 }
2839
2840 out << "; ";
2841
alokp@chromium.org52813552010-11-16 18:36:09 +00002842 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002843 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002844 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002845 }
2846
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002847 out << ")\n";
2848
Jamie Madill075edd82013-07-08 13:30:19 -04002849 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002850 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002851 }
2852
2853 if (node->getBody())
2854 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002855 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002856 }
2857
Jamie Madill075edd82013-07-08 13:30:19 -04002858 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002859 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002860
alokp@chromium.org52813552010-11-16 18:36:09 +00002861 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002862 {
Jamie Madill075edd82013-07-08 13:30:19 -04002863 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002864 out << "while(\n";
2865
alokp@chromium.org52813552010-11-16 18:36:09 +00002866 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002867
daniel@transgaming.com73536982012-03-21 20:45:49 +00002868 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002869 }
2870
daniel@transgaming.com73536982012-03-21 20:45:49 +00002871 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002872
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002873 mInsideDiscontinuousLoop = wasDiscontinuous;
2874
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002875 return false;
2876}
2877
2878bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2879{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002880 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002881
2882 switch (node->getFlowOp())
2883 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002884 case EOpKill:
2885 outputTriplet(visit, "discard;\n", "", "");
2886 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002887 case EOpBreak:
2888 if (visit == PreVisit)
2889 {
2890 if (mExcessiveLoopIndex)
2891 {
2892 out << "{Break";
2893 mExcessiveLoopIndex->traverse(this);
2894 out << " = true; break;}\n";
2895 }
2896 else
2897 {
2898 out << "break;\n";
2899 }
2900 }
2901 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002902 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002903 case EOpReturn:
2904 if (visit == PreVisit)
2905 {
2906 if (node->getExpression())
2907 {
2908 out << "return ";
2909 }
2910 else
2911 {
2912 out << "return;\n";
2913 }
2914 }
2915 else if (visit == PostVisit)
2916 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002917 if (node->getExpression())
2918 {
2919 out << ";\n";
2920 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002921 }
2922 break;
2923 default: UNREACHABLE();
2924 }
2925
2926 return true;
2927}
2928
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002929void OutputHLSL::traverseStatements(TIntermNode *node)
2930{
2931 if (isSingleStatement(node))
2932 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002933 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002934 }
2935
2936 node->traverse(this);
2937}
2938
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002939bool OutputHLSL::isSingleStatement(TIntermNode *node)
2940{
2941 TIntermAggregate *aggregate = node->getAsAggregate();
2942
2943 if (aggregate)
2944 {
2945 if (aggregate->getOp() == EOpSequence)
2946 {
2947 return false;
2948 }
2949 else
2950 {
2951 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2952 {
2953 if (!isSingleStatement(*sit))
2954 {
2955 return false;
2956 }
2957 }
2958
2959 return true;
2960 }
2961 }
2962
2963 return true;
2964}
2965
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002966// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2967// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002968bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2969{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002970 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002971 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002972
2973 // Parse loops of the form:
2974 // for(int index = initial; index [comparator] limit; index += increment)
2975 TIntermSymbol *index = NULL;
2976 TOperator comparator = EOpNull;
2977 int initial = 0;
2978 int limit = 0;
2979 int increment = 0;
2980
2981 // Parse index name and intial value
2982 if (node->getInit())
2983 {
2984 TIntermAggregate *init = node->getInit()->getAsAggregate();
2985
2986 if (init)
2987 {
2988 TIntermSequence &sequence = init->getSequence();
2989 TIntermTyped *variable = sequence[0]->getAsTyped();
2990
2991 if (variable && variable->getQualifier() == EvqTemporary)
2992 {
2993 TIntermBinary *assign = variable->getAsBinaryNode();
2994
2995 if (assign->getOp() == EOpInitialize)
2996 {
2997 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2998 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2999
3000 if (symbol && constant)
3001 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003002 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003003 {
3004 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003005 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003006 }
3007 }
3008 }
3009 }
3010 }
3011 }
3012
3013 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00003014 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003015 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003016 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003017
3018 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
3019 {
3020 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3021
3022 if (constant)
3023 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003024 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003025 {
3026 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003027 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003028 }
3029 }
3030 }
3031 }
3032
3033 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00003034 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003035 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003036 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3037 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003038
3039 if (binaryTerminal)
3040 {
3041 TOperator op = binaryTerminal->getOp();
3042 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3043
3044 if (constant)
3045 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003046 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003047 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003048 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003049
3050 switch (op)
3051 {
3052 case EOpAddAssign: increment = value; break;
3053 case EOpSubAssign: increment = -value; break;
3054 default: UNIMPLEMENTED();
3055 }
3056 }
3057 }
3058 }
3059 else if (unaryTerminal)
3060 {
3061 TOperator op = unaryTerminal->getOp();
3062
3063 switch (op)
3064 {
3065 case EOpPostIncrement: increment = 1; break;
3066 case EOpPostDecrement: increment = -1; break;
3067 case EOpPreIncrement: increment = 1; break;
3068 case EOpPreDecrement: increment = -1; break;
3069 default: UNIMPLEMENTED();
3070 }
3071 }
3072 }
3073
3074 if (index != NULL && comparator != EOpNull && increment != 0)
3075 {
3076 if (comparator == EOpLessThanEqual)
3077 {
3078 comparator = EOpLessThan;
3079 limit += 1;
3080 }
3081
3082 if (comparator == EOpLessThan)
3083 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00003084 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003085
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003086 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003087 {
3088 return false; // Not an excessive loop
3089 }
3090
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003091 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
3092 mExcessiveLoopIndex = index;
3093
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003094 out << "{int ";
3095 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003096 out << ";\n"
3097 "bool Break";
3098 index->traverse(this);
3099 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003100
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003101 bool firstLoopFragment = true;
3102
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003103 while (iterations > 0)
3104 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003105 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003106
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003107 if (!firstLoopFragment)
3108 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003109 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003110 index->traverse(this);
3111 out << ") {\n";
3112 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00003113
3114 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
3115 {
3116 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
3117 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003118
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003119 // for(int index = initial; index < clampedLimit; index += increment)
3120
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003121 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003122 index->traverse(this);
3123 out << " = ";
3124 out << initial;
3125
3126 out << "; ";
3127 index->traverse(this);
3128 out << " < ";
3129 out << clampedLimit;
3130
3131 out << "; ";
3132 index->traverse(this);
3133 out << " += ";
3134 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003135 out << ")\n";
3136
Jamie Madill075edd82013-07-08 13:30:19 -04003137 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003138 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003139
3140 if (node->getBody())
3141 {
3142 node->getBody()->traverse(this);
3143 }
3144
Jamie Madill075edd82013-07-08 13:30:19 -04003145 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003146 out << ";}\n";
3147
3148 if (!firstLoopFragment)
3149 {
3150 out << "}\n";
3151 }
3152
3153 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003154
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003155 initial += MAX_LOOP_ITERATIONS * increment;
3156 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003157 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003158
3159 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003160
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003161 mExcessiveLoopIndex = restoreIndex;
3162
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003163 return true;
3164 }
3165 else UNIMPLEMENTED();
3166 }
3167
3168 return false; // Not handled as an excessive loop
3169}
3170
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003171void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003172{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00003173 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003174
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003175 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003176 {
3177 out << preString;
3178 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003179 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003180 {
3181 out << inString;
3182 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003183 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003184 {
3185 out << postString;
3186 }
3187}
3188
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003189void OutputHLSL::outputLineDirective(int line)
3190{
3191 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
3192 {
baustin@google.com8ab69842011-06-02 21:53:45 +00003193 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003194 mBody << "#line " << line;
3195
3196 if (mContext.sourcePath)
3197 {
3198 mBody << " \"" << mContext.sourcePath << "\"";
3199 }
3200
3201 mBody << "\n";
3202 }
3203}
3204
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003205TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3206{
3207 TQualifier qualifier = symbol->getQualifier();
3208 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003209 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003210
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003211 if (name.empty()) // HLSL demands named arguments, also for prototypes
3212 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00003213 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003214 }
3215 else
3216 {
3217 name = decorate(name);
3218 }
3219
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003220 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
3221 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003222 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
3223 qualifierString(qualifier) + " " + samplerString(type) + " sampler_" + name + arrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003224 }
3225
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003226 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003227}
3228
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003229TString OutputHLSL::interpolationString(TQualifier qualifier)
3230{
3231 switch(qualifier)
3232 {
3233 case EvqVaryingIn: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003234 case EvqFragmentIn: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003235 case EvqInvariantVaryingIn: return "";
3236 case EvqSmoothIn: return "linear";
3237 case EvqFlatIn: return "nointerpolation";
3238 case EvqCentroidIn: return "centroid";
3239 case EvqVaryingOut: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003240 case EvqVertexOut: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003241 case EvqInvariantVaryingOut: return "";
3242 case EvqSmoothOut: return "linear";
3243 case EvqFlatOut: return "nointerpolation";
3244 case EvqCentroidOut: return "centroid";
3245 default: UNREACHABLE();
3246 }
3247
3248 return "";
3249}
3250
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003251TString OutputHLSL::qualifierString(TQualifier qualifier)
3252{
3253 switch(qualifier)
3254 {
3255 case EvqIn: return "in";
3256 case EvqOut: return "out";
3257 case EvqInOut: return "inout";
3258 case EvqConstReadOnly: return "const";
3259 default: UNREACHABLE();
3260 }
3261
3262 return "";
3263}
3264
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003265TString OutputHLSL::typeString(const TType &type)
3266{
Jamie Madill98493dd2013-07-08 14:39:03 -04003267 const TStructure* structure = type.getStruct();
3268 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003269 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003270 const TString& typeName = structure->name();
3271 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003272 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003273 return structLookup(typeName);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003274 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003275 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003276 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003277 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003278 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003279 }
3280 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003281 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003282 int cols = type.getCols();
3283 int rows = type.getRows();
3284 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003285 }
3286 else
3287 {
3288 switch (type.getBasicType())
3289 {
3290 case EbtFloat:
3291 switch (type.getNominalSize())
3292 {
3293 case 1: return "float";
3294 case 2: return "float2";
3295 case 3: return "float3";
3296 case 4: return "float4";
3297 }
3298 case EbtInt:
3299 switch (type.getNominalSize())
3300 {
3301 case 1: return "int";
3302 case 2: return "int2";
3303 case 3: return "int3";
3304 case 4: return "int4";
3305 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003306 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04003307 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003308 {
3309 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003310 case 2: return "uint2";
3311 case 3: return "uint3";
3312 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003313 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003314 case EbtBool:
3315 switch (type.getNominalSize())
3316 {
3317 case 1: return "bool";
3318 case 2: return "bool2";
3319 case 3: return "bool3";
3320 case 4: return "bool4";
3321 }
3322 case EbtVoid:
3323 return "void";
3324 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003325 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04003326 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003327 case EbtSampler2DArray:
3328 case EbtISampler2DArray:
3329 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003330 return "sampler2D";
3331 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003332 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04003333 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003334 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00003335 case EbtSamplerExternalOES:
3336 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00003337 default:
3338 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003339 }
3340 }
3341
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003342 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003343 return "<unknown type>";
3344}
3345
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003346TString OutputHLSL::textureString(const TType &type)
3347{
3348 switch (type.getBasicType())
3349 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003350 case EbtSampler2D: return "Texture2D";
3351 case EbtSamplerCube: return "TextureCube";
3352 case EbtSamplerExternalOES: return "Texture2D";
3353 case EbtSampler2DArray: return "Texture2DArray";
3354 case EbtSampler3D: return "Texture3D";
3355 case EbtISampler2D: return "Texture2D<int4>";
3356 case EbtISampler3D: return "Texture3D<int4>";
Nicolas Capens0027fa92014-02-20 14:26:42 -05003357 case EbtISamplerCube: return "Texture2DArray<int4>";
Nicolas Capenscb127d32013-07-15 17:26:18 -04003358 case EbtISampler2DArray: return "Texture2DArray<int4>";
3359 case EbtUSampler2D: return "Texture2D<uint4>";
3360 case EbtUSampler3D: return "Texture3D<uint4>";
Nicolas Capens0027fa92014-02-20 14:26:42 -05003361 case EbtUSamplerCube: return "Texture2DArray<uint4>";
Nicolas Capenscb127d32013-07-15 17:26:18 -04003362 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
3363 case EbtSampler2DShadow: return "Texture2D";
3364 case EbtSamplerCubeShadow: return "TextureCube";
3365 case EbtSampler2DArrayShadow: return "Texture2DArray";
3366 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003367 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04003368
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003369 return "<unknown texture type>";
3370}
3371
Nicolas Capenscb127d32013-07-15 17:26:18 -04003372TString OutputHLSL::samplerString(const TType &type)
3373{
3374 if (IsShadowSampler(type.getBasicType()))
3375 {
3376 return "SamplerComparisonState";
3377 }
3378 else
3379 {
3380 return "SamplerState";
3381 }
3382}
3383
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003384TString OutputHLSL::arrayString(const TType &type)
3385{
3386 if (!type.isArray())
3387 {
3388 return "";
3389 }
3390
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003391 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003392}
3393
3394TString OutputHLSL::initializer(const TType &type)
3395{
3396 TString string;
3397
Jamie Madill94bf7f22013-07-08 13:31:15 -04003398 size_t size = type.getObjectSize();
3399 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003400 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003401 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003402
Jamie Madill94bf7f22013-07-08 13:31:15 -04003403 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003404 {
3405 string += ", ";
3406 }
3407 }
3408
daniel@transgaming.comead23042010-04-29 03:35:36 +00003409 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003410}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003411
Jamie Madill98493dd2013-07-08 14:39:03 -04003412TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003413{
Jamie Madill98493dd2013-07-08 14:39:03 -04003414 const TFieldList &fields = structure.fields();
3415 const bool isNameless = (structure.name() == "");
3416 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003417 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3418
Jamie Madill98493dd2013-07-08 14:39:03 -04003419 TString string;
3420 string += declareString + "\n"
3421 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003422
Jamie Madillc835df62013-06-21 09:15:32 -04003423 int elementIndex = 0;
3424
Jamie Madill9cf6c072013-06-20 11:55:53 -04003425 for (unsigned int i = 0; i < fields.size(); i++)
3426 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003427 const TField &field = *fields[i];
3428 const TType &fieldType = *field.type();
3429 const TStructure *fieldStruct = fieldType.getStruct();
3430 const TString &fieldTypeString = fieldStruct ? structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) : typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003431
Jamie Madillc835df62013-06-21 09:15:32 -04003432 if (useStd140Packing)
3433 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003434 string += std140PrePaddingString(*field.type(), &elementIndex);
Jamie Madillc835df62013-06-21 09:15:32 -04003435 }
3436
Jamie Madill98493dd2013-07-08 14:39:03 -04003437 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003438
3439 if (useStd140Packing)
3440 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003441 string += std140PostPaddingString(*field.type(), useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003442 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003443 }
3444
3445 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003446 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003447
Jamie Madille4075c92013-06-21 09:15:32 -04003448 // Add remaining element index to the global map, for use with nested structs in standard layouts
3449 if (useStd140Packing)
3450 {
3451 mStd140StructElementIndexes[structName] = elementIndex;
3452 }
3453
Jamie Madill98493dd2013-07-08 14:39:03 -04003454 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003455}
3456
Jamie Madill98493dd2013-07-08 14:39:03 -04003457TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003458{
Jamie Madill98493dd2013-07-08 14:39:03 -04003459 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003460 {
3461 return "";
3462 }
3463
3464 TString prefix = "";
3465
3466 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3467 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003468
3469 if (useStd140Packing)
3470 {
3471 prefix += "std";
3472 }
3473
Jamie Madill9cf6c072013-06-20 11:55:53 -04003474 if (useHLSLRowMajorPacking)
3475 {
Jamie Madillc835df62013-06-21 09:15:32 -04003476 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003477 prefix += "rm";
3478 }
3479
Jamie Madill98493dd2013-07-08 14:39:03 -04003480 return prefix + structLookup(structure.name());
Jamie Madill9cf6c072013-06-20 11:55:53 -04003481}
3482
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003483void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003484{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003485 if (name == "")
3486 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003487 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003488 }
3489
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003490 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
3491 {
3492 return; // Already added
3493 }
3494
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003495 TType ctorType = type;
3496 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003497 ctorType.setPrecision(EbpHigh);
3498 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003499
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003500 TString ctorName = type.getStruct() ? decorate(name) : name;
3501
3502 typedef std::vector<TType> ParameterArray;
3503 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003504
Jamie Madill98493dd2013-07-08 14:39:03 -04003505 const TStructure* structure = type.getStruct();
3506 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003507 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003508 mStructNames.insert(decorate(name));
3509
Jamie Madill98493dd2013-07-08 14:39:03 -04003510 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003511
Jamie Madill98493dd2013-07-08 14:39:03 -04003512 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003513 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003514 // Add row-major packed struct for interface blocks
3515 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003516 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003517 "#pragma pack_matrix(column_major)\n";
3518
Jamie Madillc835df62013-06-21 09:15:32 -04003519 const TString &std140Prefix = "std";
Jamie Madill98493dd2013-07-08 14:39:03 -04003520 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003521
3522 const TString &std140RowMajorPrefix = "std_rm";
3523 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003524 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003525 "#pragma pack_matrix(column_major)\n";
3526
Jamie Madill98493dd2013-07-08 14:39:03 -04003527 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003528 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003529 mStructDeclarations.push_back(std140String);
3530 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003531 }
3532
Jamie Madill98493dd2013-07-08 14:39:03 -04003533 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003534 for (unsigned int i = 0; i < fields.size(); i++)
3535 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003536 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003537 }
3538 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003539 else if (parameters)
3540 {
3541 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3542 {
3543 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3544 }
3545 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003546 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003547
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003548 TString constructor;
3549
3550 if (ctorType.getStruct())
3551 {
3552 constructor += ctorName + " " + ctorName + "_ctor(";
3553 }
3554 else // Built-in type
3555 {
3556 constructor += typeString(ctorType) + " " + ctorName + "(";
3557 }
3558
3559 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3560 {
3561 const TType &type = ctorParameters[parameter];
3562
3563 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3564
3565 if (parameter < ctorParameters.size() - 1)
3566 {
3567 constructor += ", ";
3568 }
3569 }
3570
3571 constructor += ")\n"
3572 "{\n";
3573
3574 if (ctorType.getStruct())
3575 {
3576 constructor += " " + ctorName + " structure = {";
3577 }
3578 else
3579 {
3580 constructor += " return " + typeString(ctorType) + "(";
3581 }
3582
3583 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3584 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003585 int rows = ctorType.getRows();
3586 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003587 const TType &parameter = ctorParameters[0];
3588
3589 if (parameter.isScalar())
3590 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003591 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003592 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003593 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003594 {
3595 constructor += TString((row == col) ? "x0" : "0.0");
3596
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003597 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003598 {
3599 constructor += ", ";
3600 }
3601 }
3602 }
3603 }
3604 else if (parameter.isMatrix())
3605 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003606 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003607 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003608 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003609 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003610 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003611 {
3612 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3613 }
3614 else
3615 {
3616 constructor += TString((row == col) ? "1.0" : "0.0");
3617 }
3618
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003619 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003620 {
3621 constructor += ", ";
3622 }
3623 }
3624 }
3625 }
3626 else UNREACHABLE();
3627 }
3628 else
3629 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003630 size_t remainingComponents = ctorType.getObjectSize();
3631 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003632
3633 while (remainingComponents > 0)
3634 {
3635 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003636 const size_t parameterSize = parameter.getObjectSize();
3637 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003638
3639 constructor += "x" + str(parameterIndex);
3640
3641 if (parameter.isScalar())
3642 {
3643 remainingComponents -= parameter.getObjectSize();
3644 }
3645 else if (parameter.isVector())
3646 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003647 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003648 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003649 ASSERT(parameterSize <= remainingComponents);
3650 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003651 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003652 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003653 {
3654 switch (remainingComponents)
3655 {
3656 case 1: constructor += ".x"; break;
3657 case 2: constructor += ".xy"; break;
3658 case 3: constructor += ".xyz"; break;
3659 case 4: constructor += ".xyzw"; break;
3660 default: UNREACHABLE();
3661 }
3662
3663 remainingComponents = 0;
3664 }
3665 else UNREACHABLE();
3666 }
3667 else if (parameter.isMatrix() || parameter.getStruct())
3668 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003669 ASSERT(remainingComponents == parameterSize || moreParameters);
3670 ASSERT(parameterSize <= remainingComponents);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003671
Jamie Madill94bf7f22013-07-08 13:31:15 -04003672 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003673 }
3674 else UNREACHABLE();
3675
3676 if (moreParameters)
3677 {
3678 parameterIndex++;
3679 }
3680
3681 if (remainingComponents)
3682 {
3683 constructor += ", ";
3684 }
3685 }
3686 }
3687
3688 if (ctorType.getStruct())
3689 {
3690 constructor += "};\n"
3691 " return structure;\n"
3692 "}\n";
3693 }
3694 else
3695 {
3696 constructor += ");\n"
3697 "}\n";
3698 }
3699
daniel@transgaming.com63691862010-04-29 03:32:42 +00003700 mConstructors.insert(constructor);
3701}
3702
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003703const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3704{
3705 TInfoSinkBase &out = mBody;
3706
Jamie Madill98493dd2013-07-08 14:39:03 -04003707 const TStructure* structure = type.getStruct();
3708 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003709 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003710 out << structLookup(structure->name()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003711
Jamie Madill98493dd2013-07-08 14:39:03 -04003712 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003713
Jamie Madill98493dd2013-07-08 14:39:03 -04003714 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003715 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003716 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003717
3718 constUnion = writeConstantUnion(*fieldType, constUnion);
3719
Jamie Madill98493dd2013-07-08 14:39:03 -04003720 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003721 {
3722 out << ", ";
3723 }
3724 }
3725
3726 out << ")";
3727 }
3728 else
3729 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003730 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003731 bool writeType = size > 1;
3732
3733 if (writeType)
3734 {
3735 out << typeString(type) << "(";
3736 }
3737
Jamie Madill94bf7f22013-07-08 13:31:15 -04003738 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003739 {
3740 switch (constUnion->getType())
3741 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003742 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003743 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003744 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003745 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003746 default: UNREACHABLE();
3747 }
3748
3749 if (i != size - 1)
3750 {
3751 out << ", ";
3752 }
3753 }
3754
3755 if (writeType)
3756 {
3757 out << ")";
3758 }
3759 }
3760
3761 return constUnion;
3762}
3763
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003764TString OutputHLSL::scopeString(unsigned int depthLimit)
3765{
3766 TString string;
3767
3768 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3769 {
3770 string += "_" + str(i);
3771 }
3772
3773 return string;
3774}
3775
3776TString OutputHLSL::scopedStruct(const TString &typeName)
3777{
3778 if (typeName == "")
3779 {
3780 return typeName;
3781 }
3782
3783 return typeName + scopeString(mScopeDepth);
3784}
3785
3786TString OutputHLSL::structLookup(const TString &typeName)
3787{
3788 for (int depth = mScopeDepth; depth >= 0; depth--)
3789 {
3790 TString scopedName = decorate(typeName + scopeString(depth));
3791
3792 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3793 {
3794 if (*structName == scopedName)
3795 {
3796 return scopedName;
3797 }
3798 }
3799 }
3800
3801 UNREACHABLE(); // Should have found a matching constructor
3802
3803 return typeName;
3804}
3805
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003806TString OutputHLSL::decorate(const TString &string)
3807{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003808 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003809 {
3810 return "_" + string;
3811 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003812
3813 return string;
3814}
3815
apatrick@chromium.org65756022012-01-17 21:45:38 +00003816TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003817{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003818 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003819 {
3820 return "ex_" + string;
3821 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003822
3823 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003824}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003825
Jamie Madill98493dd2013-07-08 14:39:03 -04003826TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003827{
Jamie Madill98493dd2013-07-08 14:39:03 -04003828 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003829 {
3830 return decorate(string);
3831 }
3832
3833 return string;
3834}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003835
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003836void OutputHLSL::declareInterfaceBlockField(const TType &type, const TString &name, std::vector<InterfaceBlockField>& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003837{
Jamie Madill98493dd2013-07-08 14:39:03 -04003838 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003839
3840 if (!structure)
3841 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003842 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003843 InterfaceBlockField field(glVariableType(type), glVariablePrecision(type), name.c_str(),
3844 (unsigned int)type.getArraySize(), isRowMajorMatrix);
3845 output.push_back(field);
3846 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003847 else
3848 {
Jamie Madill28167c62013-08-30 13:21:10 -04003849 InterfaceBlockField structField(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), false);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003850
3851 const TFieldList &fields = structure->fields();
3852
3853 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3854 {
3855 TField *field = fields[fieldIndex];
3856 TType *fieldType = field->type();
3857
3858 // make sure to copy matrix packing information
3859 fieldType->setLayoutQualifier(type.getLayoutQualifier());
3860
3861 declareInterfaceBlockField(*fieldType, field->name(), structField.fields);
3862 }
3863
3864 output.push_back(structField);
3865 }
3866}
3867
Jamie Madillc2141fb2013-08-30 13:21:08 -04003868Uniform OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, std::vector<Uniform>& output)
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003869{
3870 const TStructure *structure = type.getStruct();
3871
3872 if (!structure)
3873 {
3874 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
3875 Uniform uniform(glVariableType(type), glVariablePrecision(type), name.c_str(),
Jamie Madill56093782013-08-30 13:21:11 -04003876 (unsigned int)type.getArraySize(), (unsigned int)registerIndex, 0);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003877 output.push_back(uniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003878
3879 return uniform;
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003880 }
3881 else
3882 {
Jamie Madill56093782013-08-30 13:21:11 -04003883 Uniform structUniform(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(),
3884 (unsigned int)registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003885
Jamie Madill98493dd2013-07-08 14:39:03 -04003886 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003887
Jamie Madill98493dd2013-07-08 14:39:03 -04003888 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003889 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003890 TField *field = fields[fieldIndex];
3891 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003892
Jamie Madill56093782013-08-30 13:21:11 -04003893 declareUniformToList(*fieldType, field->name(), GL_INVALID_INDEX, structUniform.fields);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003894 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003895
Jamie Madill56093782013-08-30 13:21:11 -04003896 // assign register offset information -- this will override the information in any sub-structures.
3897 HLSLVariableGetRegisterInfo(registerIndex, &structUniform);
3898
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003899 output.push_back(structUniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003900
3901 return structUniform;
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003902 }
3903}
3904
Jamie Madill139b9092013-08-30 13:21:06 -04003905InterpolationType getInterpolationType(TQualifier qualifier)
3906{
3907 switch (qualifier)
3908 {
3909 case EvqFlatIn:
3910 case EvqFlatOut:
3911 return INTERPOLATION_FLAT;
3912
3913 case EvqSmoothIn:
3914 case EvqSmoothOut:
3915 case EvqVertexOut:
3916 case EvqFragmentIn:
Jamie Madill384b6042013-09-13 10:06:24 -04003917 case EvqVaryingIn:
3918 case EvqVaryingOut:
Jamie Madill139b9092013-08-30 13:21:06 -04003919 return INTERPOLATION_SMOOTH;
3920
3921 case EvqCentroidIn:
3922 case EvqCentroidOut:
3923 return INTERPOLATION_CENTROID;
3924
3925 default: UNREACHABLE();
3926 return INTERPOLATION_SMOOTH;
3927 }
3928}
3929
Jamie Madill94599662013-08-30 13:21:10 -04003930void OutputHLSL::declareVaryingToList(const TType &type, TQualifier baseTypeQualifier, const TString &name, std::vector<Varying>& fieldsOut)
Jamie Madill47fdd132013-08-30 13:21:04 -04003931{
3932 const TStructure *structure = type.getStruct();
3933
Jamie Madill94599662013-08-30 13:21:10 -04003934 InterpolationType interpolation = getInterpolationType(baseTypeQualifier);
Jamie Madill47fdd132013-08-30 13:21:04 -04003935 if (!structure)
3936 {
Jamie Madill139b9092013-08-30 13:21:06 -04003937 Varying varying(glVariableType(type), glVariablePrecision(type), name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003938 fieldsOut.push_back(varying);
3939 }
3940 else
3941 {
Jamie Madill28167c62013-08-30 13:21:10 -04003942 Varying structVarying(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003943 const TFieldList &fields = structure->fields();
3944
Jamie Madill28167c62013-08-30 13:21:10 -04003945 structVarying.structName = structure->name().c_str();
3946
Jamie Madill47fdd132013-08-30 13:21:04 -04003947 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3948 {
3949 const TField &field = *fields[fieldIndex];
Jamie Madill94599662013-08-30 13:21:10 -04003950 declareVaryingToList(*field.type(), baseTypeQualifier, field.name(), structVarying.fields);
Jamie Madill47fdd132013-08-30 13:21:04 -04003951 }
3952
3953 fieldsOut.push_back(structVarying);
3954 }
3955}
3956
Jamie Madillc2141fb2013-08-30 13:21:08 -04003957int OutputHLSL::declareUniformAndAssignRegister(const TType &type, const TString &name)
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003958{
Jamie Madillc2141fb2013-08-30 13:21:08 -04003959 int registerIndex = (IsSampler(type.getBasicType()) ? mSamplerRegister : mUniformRegister);
3960
3961 const Uniform &uniform = declareUniformToList(type, name, registerIndex, mActiveUniforms);
3962
3963 if (IsSampler(type.getBasicType()))
3964 {
3965 mSamplerRegister += HLSLVariableRegisterCount(uniform);
3966 }
3967 else
3968 {
3969 mUniformRegister += HLSLVariableRegisterCount(uniform);
3970 }
3971
3972 return registerIndex;
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003973}
3974
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003975GLenum OutputHLSL::glVariableType(const TType &type)
3976{
3977 if (type.getBasicType() == EbtFloat)
3978 {
3979 if (type.isScalar())
3980 {
3981 return GL_FLOAT;
3982 }
3983 else if (type.isVector())
3984 {
3985 switch(type.getNominalSize())
3986 {
3987 case 2: return GL_FLOAT_VEC2;
3988 case 3: return GL_FLOAT_VEC3;
3989 case 4: return GL_FLOAT_VEC4;
3990 default: UNREACHABLE();
3991 }
3992 }
3993 else if (type.isMatrix())
3994 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003995 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003996 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003997 case 2:
3998 switch(type.getRows())
3999 {
4000 case 2: return GL_FLOAT_MAT2;
4001 case 3: return GL_FLOAT_MAT2x3;
4002 case 4: return GL_FLOAT_MAT2x4;
4003 default: UNREACHABLE();
4004 }
4005
4006 case 3:
4007 switch(type.getRows())
4008 {
4009 case 2: return GL_FLOAT_MAT3x2;
4010 case 3: return GL_FLOAT_MAT3;
4011 case 4: return GL_FLOAT_MAT3x4;
4012 default: UNREACHABLE();
4013 }
4014
4015 case 4:
4016 switch(type.getRows())
4017 {
4018 case 2: return GL_FLOAT_MAT4x2;
4019 case 3: return GL_FLOAT_MAT4x3;
4020 case 4: return GL_FLOAT_MAT4;
4021 default: UNREACHABLE();
4022 }
4023
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004024 default: UNREACHABLE();
4025 }
4026 }
4027 else UNREACHABLE();
4028 }
4029 else if (type.getBasicType() == EbtInt)
4030 {
4031 if (type.isScalar())
4032 {
4033 return GL_INT;
4034 }
4035 else if (type.isVector())
4036 {
4037 switch(type.getNominalSize())
4038 {
4039 case 2: return GL_INT_VEC2;
4040 case 3: return GL_INT_VEC3;
4041 case 4: return GL_INT_VEC4;
4042 default: UNREACHABLE();
4043 }
4044 }
4045 else UNREACHABLE();
4046 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004047 else if (type.getBasicType() == EbtUInt)
4048 {
4049 if (type.isScalar())
4050 {
4051 return GL_UNSIGNED_INT;
4052 }
4053 else if (type.isVector())
4054 {
Jamie Madill22d63da2013-06-07 12:45:12 -04004055 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00004056 {
4057 case 2: return GL_UNSIGNED_INT_VEC2;
4058 case 3: return GL_UNSIGNED_INT_VEC3;
4059 case 4: return GL_UNSIGNED_INT_VEC4;
4060 default: UNREACHABLE();
4061 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004062 }
4063 else UNREACHABLE();
4064 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004065 else if (type.getBasicType() == EbtBool)
4066 {
4067 if (type.isScalar())
4068 {
4069 return GL_BOOL;
4070 }
4071 else if (type.isVector())
4072 {
4073 switch(type.getNominalSize())
4074 {
4075 case 2: return GL_BOOL_VEC2;
4076 case 3: return GL_BOOL_VEC3;
4077 case 4: return GL_BOOL_VEC4;
4078 default: UNREACHABLE();
4079 }
4080 }
4081 else UNREACHABLE();
4082 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004083
4084 switch(type.getBasicType())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004085 {
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004086 case EbtSampler2D: return GL_SAMPLER_2D;
4087 case EbtSampler3D: return GL_SAMPLER_3D;
4088 case EbtSamplerCube: return GL_SAMPLER_CUBE;
4089 case EbtSampler2DArray: return GL_SAMPLER_2D_ARRAY;
4090 case EbtISampler2D: return GL_INT_SAMPLER_2D;
4091 case EbtISampler3D: return GL_INT_SAMPLER_3D;
4092 case EbtISamplerCube: return GL_INT_SAMPLER_CUBE;
4093 case EbtISampler2DArray: return GL_INT_SAMPLER_2D_ARRAY;
4094 case EbtUSampler2D: return GL_UNSIGNED_INT_SAMPLER_2D;
4095 case EbtUSampler3D: return GL_UNSIGNED_INT_SAMPLER_3D;
4096 case EbtUSamplerCube: return GL_UNSIGNED_INT_SAMPLER_CUBE;
4097 case EbtUSampler2DArray: return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
4098 case EbtSampler2DShadow: return GL_SAMPLER_2D_SHADOW;
4099 case EbtSamplerCubeShadow: return GL_SAMPLER_CUBE_SHADOW;
4100 case EbtSampler2DArrayShadow: return GL_SAMPLER_2D_ARRAY_SHADOW;
4101 default: UNREACHABLE();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004102 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004103
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004104 return GL_NONE;
4105}
4106
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004107GLenum OutputHLSL::glVariablePrecision(const TType &type)
4108{
4109 if (type.getBasicType() == EbtFloat)
4110 {
4111 switch (type.getPrecision())
4112 {
4113 case EbpHigh: return GL_HIGH_FLOAT;
4114 case EbpMedium: return GL_MEDIUM_FLOAT;
4115 case EbpLow: return GL_LOW_FLOAT;
4116 case EbpUndefined:
4117 // Should be defined as the default precision by the parser
4118 default: UNREACHABLE();
4119 }
4120 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004121 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004122 {
4123 switch (type.getPrecision())
4124 {
4125 case EbpHigh: return GL_HIGH_INT;
4126 case EbpMedium: return GL_MEDIUM_INT;
4127 case EbpLow: return GL_LOW_INT;
4128 case EbpUndefined:
4129 // Should be defined as the default precision by the parser
4130 default: UNREACHABLE();
4131 }
4132 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004133
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00004134 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004135 return GL_NONE;
4136}
4137
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004138bool OutputHLSL::isVaryingOut(TQualifier qualifier)
4139{
4140 switch(qualifier)
4141 {
4142 case EvqVaryingOut:
4143 case EvqInvariantVaryingOut:
4144 case EvqSmoothOut:
4145 case EvqFlatOut:
4146 case EvqCentroidOut:
Jamie Madill19571812013-08-12 15:26:34 -07004147 case EvqVertexOut:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004148 return true;
4149 }
4150
4151 return false;
4152}
4153
4154bool OutputHLSL::isVaryingIn(TQualifier qualifier)
4155{
4156 switch(qualifier)
4157 {
4158 case EvqVaryingIn:
4159 case EvqInvariantVaryingIn:
4160 case EvqSmoothIn:
4161 case EvqFlatIn:
4162 case EvqCentroidIn:
Jamie Madill19571812013-08-12 15:26:34 -07004163 case EvqFragmentIn:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004164 return true;
4165 }
4166
4167 return false;
4168}
4169
4170bool OutputHLSL::isVarying(TQualifier qualifier)
4171{
4172 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
4173}
4174
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004175}