blob: c25ca86b24ba95769bf3720bd0fea054465fb83f [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"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000019
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000020#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000021#include <cfloat>
22#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000023
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000024namespace sh
25{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000026// Integer to TString conversion
Geoff Lang036aa492013-10-09 16:23:30 -040027template <typename T>
28TString str(T i)
daniel@transgaming.com005c7392010-04-15 20:45:27 +000029{
Geoff Lang036aa492013-10-09 16:23:30 -040030 ASSERT(std::numeric_limits<T>::is_integer);
31 char buffer[(CHAR_BIT * sizeof(T) / 3) + 3];
32 const char *formatStr = std::numeric_limits<T>::is_signed ? "%d" : "%u";
33 snprintf(buffer, sizeof(buffer), formatStr, i);
daniel@transgaming.com005c7392010-04-15 20:45:27 +000034 return buffer;
35}
36
Nicolas Capense0ba27a2013-06-24 16:10:52 -040037TString OutputHLSL::TextureFunction::name() const
38{
39 TString name = "gl_texture";
40
Nicolas Capens6d232bb2013-07-08 15:56:38 -040041 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040042 {
43 name += "2D";
44 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040045 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040046 {
47 name += "3D";
48 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040049 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040050 {
51 name += "Cube";
52 }
53 else UNREACHABLE();
54
55 if (proj)
56 {
57 name += "Proj";
58 }
59
Nicolas Capensb1f45b72013-12-19 17:37:19 -050060 if (offset)
61 {
62 name += "Offset";
63 }
64
Nicolas Capens75fb4752013-07-10 15:14:47 -040065 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040066 {
67 case IMPLICIT: break;
68 case BIAS: break;
69 case LOD: name += "Lod"; break;
70 case LOD0: name += "Lod0"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -040071 case SIZE: name += "Size"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040072 default: UNREACHABLE();
73 }
74
75 return name + "(";
76}
77
Jamie Madillc2141fb2013-08-30 13:21:08 -040078const char *RegisterPrefix(const TType &type)
79{
80 if (IsSampler(type.getBasicType()))
81 {
82 return "s";
83 }
84 else
85 {
86 return "c";
87 }
88}
89
Nicolas Capense0ba27a2013-06-24 16:10:52 -040090bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
91{
92 if (sampler < rhs.sampler) return true;
93 if (coords < rhs.coords) return true;
94 if (!proj && rhs.proj) return true;
Nicolas Capens75fb4752013-07-10 15:14:47 -040095 if (method < rhs.method) return true;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040096
97 return false;
98}
99
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000100OutputHLSL::OutputHLSL(TParseContext &context, const ShBuiltInResources& resources, ShShaderOutput outputType)
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +0000101 : TIntermTraverser(true, true, true), mContext(context), mOutputType(outputType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000103 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000104 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000105
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000106 mUsesFragColor = false;
107 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000108 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000109 mUsesFragCoord = false;
110 mUsesPointCoord = false;
111 mUsesFrontFacing = false;
112 mUsesPointSize = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400113 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000114 mUsesXor = false;
115 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000116 mUsesMod2v = false;
117 mUsesMod2f = false;
118 mUsesMod3v = false;
119 mUsesMod3f = false;
120 mUsesMod4v = false;
121 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000122 mUsesFaceforward1 = false;
123 mUsesFaceforward2 = false;
124 mUsesFaceforward3 = false;
125 mUsesFaceforward4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000126 mUsesAtan2_1 = false;
127 mUsesAtan2_2 = false;
128 mUsesAtan2_3 = false;
129 mUsesAtan2_4 = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500130 mUsesDiscardRewriting = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000131
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000132 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
133
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000134 mScopeDepth = 0;
135
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000136 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000137
138 mContainsLoopDiscontinuity = false;
139 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000140 mInsideDiscontinuousLoop = false;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000141
142 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000143
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000144 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000145 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000146 if (mContext.shaderType == SH_FRAGMENT_SHADER)
147 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000148 mUniformRegister = 3; // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000149 }
150 else
151 {
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000152 mUniformRegister = 2; // Reserve registers for dx_DepthRange and dx_ViewAdjust
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000153 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000154 }
155 else
156 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000157 mUniformRegister = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000158 }
159
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000160 mSamplerRegister = 0;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000161 mInterfaceBlockRegister = 2; // Reserve registers for the default uniform block and driver constants
Jamie Madill574d9dd2013-06-20 11:55:56 -0400162 mPaddingCounter = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000163}
164
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000165OutputHLSL::~OutputHLSL()
166{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000167 delete mUnfoldShortCircuit;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000168}
169
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000170void OutputHLSL::output()
171{
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +0000172 mContainsLoopDiscontinuity = mContext.shaderType == SH_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400173 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
174 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000175
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000176 mContext.treeRoot->traverse(this); // Output the body first to determine what has to go in the header
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000177 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000178
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000179 mContext.infoSink().obj << mHeader.c_str();
180 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000181}
182
Jamie Madill570e04d2013-06-21 09:15:33 -0400183void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
184{
185 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
186 {
187 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
188
189 // This will mark the necessary block elements as referenced
190 flaggedNode->traverse(this);
191 TString structName(mBody.c_str());
192 mBody.erase();
193
194 mFlaggedStructOriginalNames[flaggedNode] = structName;
195
196 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
197 {
198 structName.erase(pos, 1);
199 }
200
201 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
202 }
203}
204
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000205TInfoSinkBase &OutputHLSL::getBodyStream()
206{
207 return mBody;
208}
209
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400210const std::vector<Uniform> &OutputHLSL::getUniforms()
daniel@transgaming.com043da132012-12-20 21:12:22 +0000211{
212 return mActiveUniforms;
213}
214
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000215const ActiveInterfaceBlocks &OutputHLSL::getInterfaceBlocks() const
216{
217 return mActiveInterfaceBlocks;
218}
219
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400220const std::vector<Attribute> &OutputHLSL::getOutputVariables() const
Jamie Madill46131a32013-06-20 11:55:50 -0400221{
222 return mActiveOutputVariables;
223}
224
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400225const std::vector<Attribute> &OutputHLSL::getAttributes() const
Jamie Madilldefb6742013-06-20 11:55:51 -0400226{
227 return mActiveAttributes;
228}
229
Jamie Madill47fdd132013-08-30 13:21:04 -0400230const std::vector<Varying> &OutputHLSL::getVaryings() const
231{
232 return mActiveVaryings;
233}
234
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000235int OutputHLSL::vectorSize(const TType &type) const
236{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000237 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000238 int arraySize = type.isArray() ? type.getArraySize() : 1;
239
240 return elementSize * arraySize;
241}
242
Jamie Madill98493dd2013-07-08 14:39:03 -0400243TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, const TField &field)
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000244{
Jamie Madill98493dd2013-07-08 14:39:03 -0400245 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000246 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400247 return interfaceBlock.name() + "." + field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000248 }
249 else
250 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400251 return field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000252 }
253}
254
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000255TString OutputHLSL::decoratePrivate(const TString &privateText)
256{
257 return "dx_" + privateText;
258}
259
Jamie Madill98493dd2013-07-08 14:39:03 -0400260TString OutputHLSL::interfaceBlockStructNameString(const TInterfaceBlock &interfaceBlock)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000261{
Jamie Madill98493dd2013-07-08 14:39:03 -0400262 return decoratePrivate(interfaceBlock.name()) + "_type";
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000263}
264
Jamie Madill98493dd2013-07-08 14:39:03 -0400265TString OutputHLSL::interfaceBlockInstanceString(const TInterfaceBlock& interfaceBlock, unsigned int arrayIndex)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000266{
Jamie Madill98493dd2013-07-08 14:39:03 -0400267 if (!interfaceBlock.hasInstanceName())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000268 {
269 return "";
270 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400271 else if (interfaceBlock.isArray())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000272 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400273 return decoratePrivate(interfaceBlock.instanceName()) + "_" + str(arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000274 }
275 else
276 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400277 return decorate(interfaceBlock.instanceName());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000278 }
279}
280
Jamie Madill98493dd2013-07-08 14:39:03 -0400281TString OutputHLSL::interfaceBlockFieldTypeString(const TField &field, TLayoutBlockStorage blockStorage)
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000282{
Jamie Madill98493dd2013-07-08 14:39:03 -0400283 const TType &fieldType = *field.type();
284 const TLayoutMatrixPacking matrixPacking = fieldType.getLayoutQualifier().matrixPacking;
Jamie Madill529077d2013-06-20 11:55:54 -0400285 ASSERT(matrixPacking != EmpUnspecified);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000286
Jamie Madill98493dd2013-07-08 14:39:03 -0400287 if (fieldType.isMatrix())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000288 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400289 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill529077d2013-06-20 11:55:54 -0400290 const TString &matrixPackString = (matrixPacking == EmpRowMajor ? "column_major" : "row_major");
Jamie Madill98493dd2013-07-08 14:39:03 -0400291 return matrixPackString + " " + typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000292 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400293 else if (fieldType.getStruct())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000294 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400295 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill98493dd2013-07-08 14:39:03 -0400296 return structureTypeName(*fieldType.getStruct(), matrixPacking == EmpColumnMajor, blockStorage == EbsStd140);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000297 }
298 else
299 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400300 return typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000301 }
302}
303
Jamie Madill98493dd2013-07-08 14:39:03 -0400304TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, TLayoutBlockStorage blockStorage)
305{
306 TString hlsl;
307
308 int elementIndex = 0;
309
310 for (unsigned int typeIndex = 0; typeIndex < interfaceBlock.fields().size(); typeIndex++)
311 {
312 const TField &field = *interfaceBlock.fields()[typeIndex];
313 const TType &fieldType = *field.type();
314
315 if (blockStorage == EbsStd140)
316 {
317 // 2 and 3 component vector types in some cases need pre-padding
318 hlsl += std140PrePaddingString(fieldType, &elementIndex);
319 }
320
321 hlsl += " " + interfaceBlockFieldTypeString(field, blockStorage) +
322 " " + decorate(field.name()) + arrayString(fieldType) + ";\n";
323
324 // must pad out after matrices and arrays, where HLSL usually allows itself room to pack stuff
325 if (blockStorage == EbsStd140)
326 {
327 const bool useHLSLRowMajorPacking = (fieldType.getLayoutQualifier().matrixPacking == EmpColumnMajor);
328 hlsl += std140PostPaddingString(fieldType, useHLSLRowMajorPacking);
329 }
330 }
331
332 return hlsl;
333}
334
335TString OutputHLSL::interfaceBlockStructString(const TInterfaceBlock &interfaceBlock)
336{
337 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
338
339 return "struct " + interfaceBlockStructNameString(interfaceBlock) + "\n"
340 "{\n" +
341 interfaceBlockFieldString(interfaceBlock, blockStorage) +
342 "};\n\n";
343}
344
345TString OutputHLSL::interfaceBlockString(const TInterfaceBlock &interfaceBlock, unsigned int registerIndex, unsigned int arrayIndex)
346{
347 const TString &arrayIndexString = (arrayIndex != GL_INVALID_INDEX ? decorate(str(arrayIndex)) : "");
348 const TString &blockName = interfaceBlock.name() + arrayIndexString;
349 TString hlsl;
350
351 hlsl += "cbuffer " + blockName + " : register(b" + str(registerIndex) + ")\n"
352 "{\n";
353
354 if (interfaceBlock.hasInstanceName())
355 {
356 hlsl += " " + interfaceBlockStructNameString(interfaceBlock) + " " + interfaceBlockInstanceString(interfaceBlock, arrayIndex) + ";\n";
357 }
358 else
359 {
360 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
361 hlsl += interfaceBlockFieldString(interfaceBlock, blockStorage);
362 }
363
364 hlsl += "};\n\n";
365
366 return hlsl;
367}
368
Jamie Madill574d9dd2013-06-20 11:55:56 -0400369TString OutputHLSL::std140PrePaddingString(const TType &type, int *elementIndex)
370{
371 if (type.getBasicType() == EbtStruct || type.isMatrix() || type.isArray())
372 {
373 // no padding needed, HLSL will align the field to a new register
374 *elementIndex = 0;
375 return "";
376 }
377
378 const GLenum glType = glVariableType(type);
379 const int numComponents = gl::UniformComponentCount(glType);
380
381 if (numComponents >= 4)
382 {
383 // no padding needed, HLSL will align the field to a new register
384 *elementIndex = 0;
385 return "";
386 }
387
388 if (*elementIndex + numComponents > 4)
389 {
390 // no padding needed, HLSL will align the field to a new register
391 *elementIndex = numComponents;
392 return "";
393 }
394
395 TString padding;
396
397 const int alignment = numComponents == 3 ? 4 : numComponents;
398 const int paddingOffset = (*elementIndex % alignment);
399
400 if (paddingOffset != 0)
401 {
402 // padding is neccessary
403 for (int paddingIndex = paddingOffset; paddingIndex < alignment; paddingIndex++)
404 {
405 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
406 }
407
408 *elementIndex += (alignment - paddingOffset);
409 }
410
411 *elementIndex += numComponents;
412 *elementIndex %= 4;
413
414 return padding;
415}
416
Jamie Madille4075c92013-06-21 09:15:32 -0400417TString OutputHLSL::std140PostPaddingString(const TType &type, bool useHLSLRowMajorPacking)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400418{
Jamie Madillc835df62013-06-21 09:15:32 -0400419 if (!type.isMatrix() && !type.isArray() && type.getBasicType() != EbtStruct)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400420 {
421 return "";
422 }
423
Jamie Madill574d9dd2013-06-20 11:55:56 -0400424 int numComponents = 0;
425
426 if (type.isMatrix())
427 {
Jamie Madille4075c92013-06-21 09:15:32 -0400428 // This method can also be called from structureString, which does not use layout qualifiers.
429 // Thus, use the method parameter for determining the matrix packing.
430 //
431 // Note HLSL row major packing corresponds to GL API column-major, and vice-versa, since we
432 // wish to always transpose GL matrices to play well with HLSL's matrix array indexing.
433 //
434 const bool isRowMajorMatrix = !useHLSLRowMajorPacking;
Jamie Madillc835df62013-06-21 09:15:32 -0400435 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400436 numComponents = gl::MatrixComponentCount(glType, isRowMajorMatrix);
437 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400438 else if (type.getStruct())
Jamie Madillc835df62013-06-21 09:15:32 -0400439 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400440 const TString &structName = structureTypeName(*type.getStruct(), useHLSLRowMajorPacking, true);
Jamie Madille4075c92013-06-21 09:15:32 -0400441 numComponents = mStd140StructElementIndexes[structName];
442
443 if (numComponents == 0)
444 {
445 return "";
446 }
Jamie Madillc835df62013-06-21 09:15:32 -0400447 }
Jamie Madill574d9dd2013-06-20 11:55:56 -0400448 else
449 {
Jamie Madillc835df62013-06-21 09:15:32 -0400450 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400451 numComponents = gl::UniformComponentCount(glType);
452 }
453
454 TString padding;
455 for (int paddingOffset = numComponents; paddingOffset < 4; paddingOffset++)
456 {
457 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
458 }
459 return padding;
460}
461
Jamie Madill440dc742013-06-20 11:55:55 -0400462// Use the same layout for packed and shared
463void setBlockLayout(InterfaceBlock *interfaceBlock, BlockLayoutType newLayout)
464{
465 interfaceBlock->layout = newLayout;
466 interfaceBlock->blockInfo.clear();
467
468 switch (newLayout)
469 {
470 case BLOCKLAYOUT_SHARED:
471 case BLOCKLAYOUT_PACKED:
472 {
473 HLSLBlockEncoder hlslEncoder(&interfaceBlock->blockInfo);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400474 hlslEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400475 interfaceBlock->dataSize = hlslEncoder.getBlockSize();
476 }
477 break;
478
479 case BLOCKLAYOUT_STANDARD:
480 {
481 Std140BlockEncoder stdEncoder(&interfaceBlock->blockInfo);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400482 stdEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400483 interfaceBlock->dataSize = stdEncoder.getBlockSize();
484 }
485 break;
486
487 default:
488 UNREACHABLE();
489 break;
490 }
491}
492
Jamie Madill574d9dd2013-06-20 11:55:56 -0400493BlockLayoutType convertBlockLayoutType(TLayoutBlockStorage blockStorage)
494{
495 switch (blockStorage)
496 {
497 case EbsPacked: return BLOCKLAYOUT_PACKED;
498 case EbsShared: return BLOCKLAYOUT_SHARED;
499 case EbsStd140: return BLOCKLAYOUT_STANDARD;
500 default: UNREACHABLE(); return BLOCKLAYOUT_SHARED;
501 }
502}
503
Jamie Madill98493dd2013-07-08 14:39:03 -0400504TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400505{
506 TString init;
507
508 TString preIndentString;
509 TString fullIndentString;
510
511 for (int spaces = 0; spaces < (indent * 4); spaces++)
512 {
513 preIndentString += ' ';
514 }
515
516 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
517 {
518 fullIndentString += ' ';
519 }
520
521 init += preIndentString + "{\n";
522
Jamie Madill98493dd2013-07-08 14:39:03 -0400523 const TFieldList &fields = structure.fields();
524 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400525 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400526 const TField &field = *fields[fieldIndex];
527 const TString &fieldName = rhsStructName + "." + decorate(field.name());
528 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400529
Jamie Madill98493dd2013-07-08 14:39:03 -0400530 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400531 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400532 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400533 }
534 else
535 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400536 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400537 }
538 }
539
540 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
541
542 return init;
543}
544
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000545void OutputHLSL::header()
546{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000547 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000548
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000549 TString uniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000550 TString interfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000551 TString varyings;
552 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400553 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000554
Jamie Madillc2141fb2013-08-30 13:21:08 -0400555 for (ReferencedSymbols::const_iterator uniformIt = mReferencedUniforms.begin(); uniformIt != mReferencedUniforms.end(); uniformIt++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000556 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400557 const TIntermSymbol &uniform = *uniformIt->second;
558 const TType &type = uniform.getType();
559 const TString &name = uniform.getSymbol();
560
561 int registerIndex = declareUniformAndAssignRegister(type, name);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000562
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000563 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture
564 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400565 uniforms += "uniform " + samplerString(type) + " sampler_" + decorateUniform(name, type) + arrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400566 " : register(s" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000567
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000568 uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400569 " : register(t" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000570 }
571 else
572 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400573 const TStructure *structure = type.getStruct();
574 const TString &typeName = (structure ? structureTypeName(*structure, false, false) : typeString(type));
575
576 const TString &registerString = TString("register(") + RegisterPrefix(type) + str(registerIndex) + ")";
577
578 uniforms += "uniform " + typeName + " " + decorateUniform(name, type) + arrayString(type) + " : " + registerString + ";\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000579 }
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000580 }
581
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000582 for (ReferencedSymbols::const_iterator interfaceBlockIt = mReferencedInterfaceBlocks.begin(); interfaceBlockIt != mReferencedInterfaceBlocks.end(); interfaceBlockIt++)
583 {
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000584 const TType &nodeType = interfaceBlockIt->second->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -0400585 const TInterfaceBlock &interfaceBlock = *nodeType.getInterfaceBlock();
586 const TFieldList &fieldList = interfaceBlock.fields();
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000587
Jamie Madill98493dd2013-07-08 14:39:03 -0400588 unsigned int arraySize = static_cast<unsigned int>(interfaceBlock.arraySize());
589 sh::InterfaceBlock activeBlock(interfaceBlock.name().c_str(), arraySize, mInterfaceBlockRegister);
590 for (unsigned int typeIndex = 0; typeIndex < fieldList.size(); typeIndex++)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000591 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400592 const TField &field = *fieldList[typeIndex];
593 const TString &fullUniformName = interfaceBlockFieldString(interfaceBlock, field);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400594 declareInterfaceBlockField(*field.type(), fullUniformName, activeBlock.fields);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000595 }
596
Jamie Madill98493dd2013-07-08 14:39:03 -0400597 mInterfaceBlockRegister += std::max(1u, arraySize);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000598
Jamie Madill98493dd2013-07-08 14:39:03 -0400599 BlockLayoutType blockLayoutType = convertBlockLayoutType(interfaceBlock.blockStorage());
600 setBlockLayout(&activeBlock, blockLayoutType);
Jamie Madill9060a4e2013-08-12 16:22:57 -0700601
602 if (interfaceBlock.matrixPacking() == EmpRowMajor)
603 {
604 activeBlock.isRowMajorLayout = true;
605 }
606
Jamie Madill98493dd2013-07-08 14:39:03 -0400607 mActiveInterfaceBlocks.push_back(activeBlock);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000608
Jamie Madill98493dd2013-07-08 14:39:03 -0400609 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000610 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400611 interfaceBlocks += interfaceBlockStructString(interfaceBlock);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000612 }
613
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000614 if (arraySize > 0)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000615 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000616 for (unsigned int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
617 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400618 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex + arrayIndex, arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000619 }
620 }
621 else
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000622 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400623 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000624 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000625 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000626
Jamie Madill829f59e2013-11-13 19:40:54 -0500627 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400628 {
629 TIntermTyped *structNode = flaggedStructIt->first;
630 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400631 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400632 const TString &originalName = mFlaggedStructOriginalNames[structNode];
633
Jamie Madill98493dd2013-07-08 14:39:03 -0400634 flaggedStructs += "static " + decorate(structure.name()) + " " + mappedName + " =\n";
635 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400636 flaggedStructs += "\n";
637 }
638
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000639 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
640 {
641 const TType &type = varying->second->getType();
642 const TString &name = varying->second->getSymbol();
643
644 // Program linking depends on this exact format
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +0000645 varyings += "static " + interpolationString(type.getQualifier()) + " " + typeString(type) + " " +
646 decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madill47fdd132013-08-30 13:21:04 -0400647
Jamie Madill94599662013-08-30 13:21:10 -0400648 declareVaryingToList(type, type.getQualifier(), name, mActiveVaryings);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000649 }
650
651 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
652 {
653 const TType &type = attribute->second->getType();
654 const TString &name = attribute->second->getSymbol();
655
656 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400657
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400658 Attribute attributeVar(glVariableType(type), glVariablePrecision(type), name.c_str(),
659 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
660 mActiveAttributes.push_back(attributeVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000661 }
662
Jamie Madill529077d2013-06-20 11:55:54 -0400663 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
664 {
665 out << *structDeclaration;
666 }
667
668 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
669 {
670 out << *constructor;
671 }
672
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500673 if (mUsesDiscardRewriting)
674 {
675 out << "#define ANGLE_USES_DISCARD_REWRITING" << "\n";
676 }
677
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400678 if (mContext.shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000679 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000680 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000681 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000682
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000683 out << "// Varyings\n";
684 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400685 out << "\n";
686
687 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000688 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500689 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000690 {
Jamie Madill46131a32013-06-20 11:55:50 -0400691 const TString &variableName = outputVariableIt->first;
692 const TType &variableType = outputVariableIt->second->getType();
693 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
694
695 out << "static " + typeString(variableType) + " out_" + variableName + arrayString(variableType) +
696 " = " + initializer(variableType) + ";\n";
697
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400698 Attribute outputVar(glVariableType(variableType), glVariablePrecision(variableType), variableName.c_str(),
699 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
Jamie Madill46131a32013-06-20 11:55:50 -0400700 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000701 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000702 }
Jamie Madill46131a32013-06-20 11:55:50 -0400703 else
704 {
705 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
706
707 out << "static float4 gl_Color[" << numColorValues << "] =\n"
708 "{\n";
709 for (unsigned int i = 0; i < numColorValues; i++)
710 {
711 out << " float4(0, 0, 0, 0)";
712 if (i + 1 != numColorValues)
713 {
714 out << ",";
715 }
716 out << "\n";
717 }
718
719 out << "};\n";
720 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000721
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400722 if (mUsesFragDepth)
723 {
724 out << "static float gl_Depth = 0.0;\n";
725 }
726
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000727 if (mUsesFragCoord)
728 {
729 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
730 }
731
732 if (mUsesPointCoord)
733 {
734 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
735 }
736
737 if (mUsesFrontFacing)
738 {
739 out << "static bool gl_FrontFacing = false;\n";
740 }
741
742 out << "\n";
743
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000744 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000745 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000746 out << "struct gl_DepthRangeParameters\n"
747 "{\n"
748 " float near;\n"
749 " float far;\n"
750 " float diff;\n"
751 "};\n"
752 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000753 }
754
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000755 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000756 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000757 out << "cbuffer DriverConstants : register(b1)\n"
758 "{\n";
759
760 if (mUsesDepthRange)
761 {
762 out << " float3 dx_DepthRange : packoffset(c0);\n";
763 }
764
765 if (mUsesFragCoord)
766 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000767 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000768 }
769
770 if (mUsesFragCoord || mUsesFrontFacing)
771 {
772 out << " float3 dx_DepthFront : packoffset(c2);\n";
773 }
774
775 out << "};\n";
776 }
777 else
778 {
779 if (mUsesDepthRange)
780 {
781 out << "uniform float3 dx_DepthRange : register(c0);";
782 }
783
784 if (mUsesFragCoord)
785 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000786 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000787 }
788
789 if (mUsesFragCoord || mUsesFrontFacing)
790 {
791 out << "uniform float3 dx_DepthFront : register(c2);\n";
792 }
793 }
794
795 out << "\n";
796
797 if (mUsesDepthRange)
798 {
799 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
800 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000801 }
802
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000803 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000804 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000805
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000806 if (!interfaceBlocks.empty())
807 {
808 out << interfaceBlocks;
809 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400810
811 if (!flaggedStructs.empty())
812 {
813 out << "// Std140 Structures accessed by value\n";
814 out << "\n";
815 out << flaggedStructs;
816 out << "\n";
817 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000818 }
819
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000820 if (usingMRTExtension && mNumRenderTargets > 1)
821 {
822 out << "#define GL_USES_MRT\n";
823 }
824
825 if (mUsesFragColor)
826 {
827 out << "#define GL_USES_FRAG_COLOR\n";
828 }
829
830 if (mUsesFragData)
831 {
832 out << "#define GL_USES_FRAG_DATA\n";
833 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000834 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000835 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000836 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000837 out << "// Attributes\n";
838 out << attributes;
839 out << "\n"
840 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
841
842 if (mUsesPointSize)
843 {
844 out << "static float gl_PointSize = float(1);\n";
845 }
846
847 out << "\n"
848 "// Varyings\n";
849 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000850 out << "\n";
851
852 if (mUsesDepthRange)
853 {
854 out << "struct gl_DepthRangeParameters\n"
855 "{\n"
856 " float near;\n"
857 " float far;\n"
858 " float diff;\n"
859 "};\n"
860 "\n";
861 }
862
863 if (mOutputType == SH_HLSL11_OUTPUT)
864 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000865 if (mUsesDepthRange)
866 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000867 out << "cbuffer DriverConstants : register(b1)\n"
868 "{\n"
869 " float3 dx_DepthRange : packoffset(c0);\n"
870 "};\n"
871 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000872 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000873 }
874 else
875 {
876 if (mUsesDepthRange)
877 {
878 out << "uniform float3 dx_DepthRange : register(c0);\n";
879 }
880
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000881 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000882 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000883 }
884
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000885 if (mUsesDepthRange)
886 {
887 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
888 "\n";
889 }
890
891 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000893
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000894 if (!interfaceBlocks.empty())
895 {
896 out << interfaceBlocks;
897 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400898
899 if (!flaggedStructs.empty())
900 {
901 out << "// Std140 Structures accessed by value\n";
902 out << "\n";
903 out << flaggedStructs;
904 out << "\n";
905 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000906 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400907 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000908
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400909 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
910 {
911 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400912 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000913 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400914 switch(textureFunction->sampler)
915 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400916 case EbtSampler2D: out << "int2 "; break;
917 case EbtSampler3D: out << "int3 "; break;
918 case EbtSamplerCube: out << "int2 "; break;
919 case EbtSampler2DArray: out << "int3 "; break;
920 case EbtISampler2D: out << "int2 "; break;
921 case EbtISampler3D: out << "int3 "; break;
922 case EbtISamplerCube: out << "int2 "; break;
923 case EbtISampler2DArray: out << "int3 "; break;
924 case EbtUSampler2D: out << "int2 "; break;
925 case EbtUSampler3D: out << "int3 "; break;
926 case EbtUSamplerCube: out << "int2 "; break;
927 case EbtUSampler2DArray: out << "int3 "; break;
928 case EbtSampler2DShadow: out << "int2 "; break;
929 case EbtSamplerCubeShadow: out << "int2 "; break;
930 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400931 default: UNREACHABLE();
932 }
933 }
934 else // Sampling function
935 {
936 switch(textureFunction->sampler)
937 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400938 case EbtSampler2D: out << "float4 "; break;
939 case EbtSampler3D: out << "float4 "; break;
940 case EbtSamplerCube: out << "float4 "; break;
941 case EbtSampler2DArray: out << "float4 "; break;
942 case EbtISampler2D: out << "int4 "; break;
943 case EbtISampler3D: out << "int4 "; break;
944 case EbtISamplerCube: out << "int4 "; break;
945 case EbtISampler2DArray: out << "int4 "; break;
946 case EbtUSampler2D: out << "uint4 "; break;
947 case EbtUSampler3D: out << "uint4 "; break;
948 case EbtUSamplerCube: out << "uint4 "; break;
949 case EbtUSampler2DArray: out << "uint4 "; break;
950 case EbtSampler2DShadow: out << "float "; break;
951 case EbtSamplerCubeShadow: out << "float "; break;
952 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400953 default: UNREACHABLE();
954 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000955 }
956
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400957 // Function name
958 out << textureFunction->name();
959
960 // Argument list
961 int hlslCoords = 4;
962
963 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000964 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400965 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000966 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400967 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
968 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
969 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000970 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400971
Nicolas Capens75fb4752013-07-10 15:14:47 -0400972 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000973 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400974 case TextureFunction::IMPLICIT: break;
975 case TextureFunction::BIAS: hlslCoords = 4; break;
976 case TextureFunction::LOD: hlslCoords = 4; break;
977 case TextureFunction::LOD0: hlslCoords = 4; break;
978 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000979 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400980 }
981 else if (mOutputType == SH_HLSL11_OUTPUT)
982 {
983 switch(textureFunction->sampler)
984 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400985 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
986 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
987 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
988 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
989 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
990 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
991 case EbtISamplerCube: out << "TextureCube<int4> x, SamplerState s"; hlslCoords = 3; break;
992 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
993 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
994 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
995 case EbtUSamplerCube: out << "TextureCube<uint4> x, SamplerState s"; hlslCoords = 3; break;
996 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
997 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
998 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
999 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001000 default: UNREACHABLE();
1001 }
1002 }
1003 else UNREACHABLE();
1004
1005 switch(textureFunction->coords)
1006 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001007 case 1: out << ", int lod"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001008 case 2: out << ", float2 t"; break;
1009 case 3: out << ", float3 t"; break;
1010 case 4: out << ", float4 t"; break;
1011 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +00001012 }
1013
Nicolas Capens75fb4752013-07-10 15:14:47 -04001014 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +00001015 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001016 case TextureFunction::IMPLICIT: break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001017 case TextureFunction::BIAS: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001018 case TextureFunction::LOD: out << ", float lod"; break;
1019 case TextureFunction::LOD0: break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001020 case TextureFunction::SIZE: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001021 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +00001022 }
1023
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001024 if (textureFunction->offset)
1025 {
1026 switch(textureFunction->sampler)
1027 {
1028 case EbtSampler2D: out << ", int2 offset"; break;
1029 case EbtSampler3D: out << ", int3 offset"; break;
1030 case EbtSampler2DArray: out << ", int2 offset"; break;
1031 case EbtISampler2D: out << ", int2 offset"; break;
1032 case EbtISampler3D: out << ", int3 offset"; break;
1033 case EbtISampler2DArray: out << ", int2 offset"; break;
1034 case EbtUSampler2D: out << ", int2 offset"; break;
1035 case EbtUSampler3D: out << ", int3 offset"; break;
1036 case EbtUSampler2DArray: out << ", int2 offset"; break;
1037 case EbtSampler2DShadow: out << ", int2 offset"; break;
1038 default: UNREACHABLE();
1039 }
1040 }
1041
1042 if (textureFunction->method == TextureFunction::BIAS)
1043 {
1044 out << ", float bias";
1045 }
1046
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001047 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001048 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001049
Nicolas Capens75fb4752013-07-10 15:14:47 -04001050 if (textureFunction->method == TextureFunction::SIZE)
1051 {
1052 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
1053 {
1054 if (IsSamplerArray(textureFunction->sampler))
1055 {
1056 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
1057 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
1058 }
1059 else
1060 {
1061 out << " uint width; uint height; uint numberOfLevels;\n"
1062 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
1063 }
1064 }
1065 else if (IsSampler3D(textureFunction->sampler))
1066 {
1067 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
1068 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
1069 }
1070 else UNREACHABLE();
1071
1072 switch(textureFunction->sampler)
1073 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04001074 case EbtSampler2D: out << " return int2(width, height);"; break;
1075 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
1076 case EbtSamplerCube: out << " return int2(width, height);"; break;
1077 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
1078 case EbtISampler2D: out << " return int2(width, height);"; break;
1079 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
1080 case EbtISamplerCube: out << " return int2(width, height);"; break;
1081 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
1082 case EbtUSampler2D: out << " return int2(width, height);"; break;
1083 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
1084 case EbtUSamplerCube: out << " return int2(width, height);"; break;
1085 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
1086 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
1087 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
1088 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001089 default: UNREACHABLE();
1090 }
1091 }
1092 else if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
daniel@transgaming.com15795192011-05-11 15:36:20 +00001093 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001094 // Currently unsupported because TextureCube does not support Load
1095 // This will require emulation using a Texture2DArray with 6 faces
1096 if (textureFunction->sampler == EbtISamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001097 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001098 out << " return int4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001099 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001100 else if (textureFunction->sampler == EbtUSamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001101 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001102 out << " return uint4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001103 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001104 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001105 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001106 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001107 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001108 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001109 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001110 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001111 {
Nicolas Capens93e50de2013-07-09 13:46:28 -04001112 if (IsSamplerArray(textureFunction->sampler))
1113 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001114 out << " float width; float height; float layers; float levels;\n";
1115
1116 if (textureFunction->method == TextureFunction::LOD0)
1117 {
1118 out << " uint mip = 0;\n";
1119 }
1120 else
1121 {
1122 if (textureFunction->method == TextureFunction::IMPLICIT ||
1123 textureFunction->method == TextureFunction::BIAS)
1124 {
1125 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1126 " float2 tSized = float2(t.x * width, t.y * height);\n"
1127 " float dx = length(ddx(tSized));\n"
1128 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001129 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001130
1131 if (textureFunction->method == TextureFunction::BIAS)
1132 {
1133 out << " lod += bias;\n";
1134 }
1135 }
1136
1137 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1138 }
1139
1140 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001141 }
1142 else
1143 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001144 out << " float width; float height; float levels;\n";
1145
1146 if (textureFunction->method == TextureFunction::LOD0)
1147 {
1148 out << " uint mip = 0;\n";
1149 }
1150 else
1151 {
1152 if (textureFunction->method == TextureFunction::IMPLICIT ||
1153 textureFunction->method == TextureFunction::BIAS)
1154 {
1155 out << " x.GetDimensions(0, width, height, levels);\n"
1156 " float2 tSized = float2(t.x * width, t.y * height);\n"
1157 " float dx = length(ddx(tSized));\n"
1158 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001159 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001160
1161 if (textureFunction->method == TextureFunction::BIAS)
1162 {
1163 out << " lod += bias;\n";
1164 }
1165 }
1166
1167 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1168 }
1169
1170 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001171 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001172 }
1173 else if (IsSampler3D(textureFunction->sampler))
1174 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001175 out << " float width; float height; float depth; float levels;\n";
1176
1177 if (textureFunction->method == TextureFunction::LOD0)
1178 {
1179 out << " uint mip = 0;\n";
1180 }
1181 else
1182 {
1183 if (textureFunction->method == TextureFunction::IMPLICIT ||
1184 textureFunction->method == TextureFunction::BIAS)
1185 {
1186 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1187 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
1188 " float dx = length(ddx(tSized));\n"
1189 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001190 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001191
1192 if (textureFunction->method == TextureFunction::BIAS)
1193 {
1194 out << " lod += bias;\n";
1195 }
1196 }
1197
1198 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1199 }
1200
1201 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001202 }
1203 else UNREACHABLE();
1204 }
1205
1206 out << " return ";
1207
1208 // HLSL intrinsic
1209 if (mOutputType == SH_HLSL9_OUTPUT)
1210 {
1211 switch(textureFunction->sampler)
1212 {
1213 case EbtSampler2D: out << "tex2D"; break;
1214 case EbtSamplerCube: out << "texCUBE"; break;
1215 default: UNREACHABLE();
1216 }
1217
Nicolas Capens75fb4752013-07-10 15:14:47 -04001218 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001219 {
1220 case TextureFunction::IMPLICIT: out << "(s, "; break;
1221 case TextureFunction::BIAS: out << "bias(s, "; break;
1222 case TextureFunction::LOD: out << "lod(s, "; break;
1223 case TextureFunction::LOD0: out << "lod(s, "; break;
1224 default: UNREACHABLE();
1225 }
1226 }
1227 else if (mOutputType == SH_HLSL11_OUTPUT)
1228 {
1229 if (IsIntegerSampler(textureFunction->sampler))
1230 {
1231 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001232 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001233 else if (IsShadowSampler(textureFunction->sampler))
1234 {
1235 out << "x.SampleCmp(s, ";
1236 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001237 else
1238 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001239 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001240 {
1241 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1242 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1243 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1244 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
1245 default: UNREACHABLE();
1246 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001247 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001248 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001249 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001250
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001251 // Integer sampling requires integer addresses
1252 TString addressx = "";
1253 TString addressy = "";
1254 TString addressz = "";
1255 TString close = "";
1256
1257 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001258 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001259 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001260 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001261 case 2: out << "int3("; break;
1262 case 3: out << "int4("; break;
1263 default: UNREACHABLE();
1264 }
1265
Nicolas Capensc98406a2013-07-10 14:52:44 -04001266 addressx = "int(floor(width * frac((";
1267 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001268
1269 if (IsSamplerArray(textureFunction->sampler))
1270 {
1271 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1272 }
1273 else
1274 {
Nicolas Capensc98406a2013-07-10 14:52:44 -04001275 addressz = "int(floor(depth * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001276 }
1277
1278 close = "))))";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001279 }
1280 else
1281 {
1282 switch(hlslCoords)
1283 {
1284 case 2: out << "float2("; break;
1285 case 3: out << "float3("; break;
1286 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001287 default: UNREACHABLE();
1288 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001289 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001290
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001291 TString proj = ""; // Only used for projected textures
1292
1293 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001294 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001295 switch(textureFunction->coords)
1296 {
1297 case 3: proj = " / t.z"; break;
1298 case 4: proj = " / t.w"; break;
1299 default: UNREACHABLE();
1300 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001301 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001302
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001303 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001304
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001305 if (mOutputType == SH_HLSL9_OUTPUT)
1306 {
1307 if (hlslCoords >= 3)
1308 {
1309 if (textureFunction->coords < 3)
1310 {
1311 out << ", 0";
1312 }
1313 else
1314 {
1315 out << ", t.z" + proj;
1316 }
1317 }
1318
1319 if (hlslCoords == 4)
1320 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001321 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001322 {
1323 case TextureFunction::BIAS: out << ", bias"; break;
1324 case TextureFunction::LOD: out << ", lod"; break;
1325 case TextureFunction::LOD0: out << ", 0"; break;
1326 default: UNREACHABLE();
1327 }
1328 }
1329
1330 out << "));\n";
1331 }
1332 else if (mOutputType == SH_HLSL11_OUTPUT)
1333 {
1334 if (hlslCoords >= 3)
1335 {
1336 out << ", " + addressz + ("t.z" + proj) + close;
1337 }
1338
Nicolas Capenscb127d32013-07-15 17:26:18 -04001339 if (IsIntegerSampler(textureFunction->sampler))
1340 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001341 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001342 }
1343 else if (IsShadowSampler(textureFunction->sampler))
1344 {
1345 // Compare value
1346 switch(textureFunction->coords)
1347 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001348 case 3: out << "), t.z"; break;
1349 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001350 default: UNREACHABLE();
1351 }
1352 }
1353 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001354 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001355 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001356 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001357 case TextureFunction::IMPLICIT: out << ")"; break;
1358 case TextureFunction::BIAS: out << "), bias"; break;
1359 case TextureFunction::LOD: out << "), lod"; break;
1360 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001361 default: UNREACHABLE();
1362 }
1363 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001364
1365 if (textureFunction->offset)
1366 {
1367 out << ", offset";
1368 }
1369
1370 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001371 }
1372 else UNREACHABLE();
1373 }
1374
1375 out << "\n"
1376 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001377 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001378 }
1379
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001380 if (mUsesFragCoord)
1381 {
1382 out << "#define GL_USES_FRAG_COORD\n";
1383 }
1384
1385 if (mUsesPointCoord)
1386 {
1387 out << "#define GL_USES_POINT_COORD\n";
1388 }
1389
1390 if (mUsesFrontFacing)
1391 {
1392 out << "#define GL_USES_FRONT_FACING\n";
1393 }
1394
1395 if (mUsesPointSize)
1396 {
1397 out << "#define GL_USES_POINT_SIZE\n";
1398 }
1399
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001400 if (mUsesFragDepth)
1401 {
1402 out << "#define GL_USES_FRAG_DEPTH\n";
1403 }
1404
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001405 if (mUsesDepthRange)
1406 {
1407 out << "#define GL_USES_DEPTH_RANGE\n";
1408 }
1409
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001410 if (mUsesXor)
1411 {
1412 out << "bool xor(bool p, bool q)\n"
1413 "{\n"
1414 " return (p || q) && !(p && q);\n"
1415 "}\n"
1416 "\n";
1417 }
1418
1419 if (mUsesMod1)
1420 {
1421 out << "float mod(float x, float y)\n"
1422 "{\n"
1423 " return x - y * floor(x / y);\n"
1424 "}\n"
1425 "\n";
1426 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001427
1428 if (mUsesMod2v)
1429 {
1430 out << "float2 mod(float2 x, float2 y)\n"
1431 "{\n"
1432 " return x - y * floor(x / y);\n"
1433 "}\n"
1434 "\n";
1435 }
1436
1437 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001438 {
1439 out << "float2 mod(float2 x, float y)\n"
1440 "{\n"
1441 " return x - y * floor(x / y);\n"
1442 "}\n"
1443 "\n";
1444 }
1445
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001446 if (mUsesMod3v)
1447 {
1448 out << "float3 mod(float3 x, float3 y)\n"
1449 "{\n"
1450 " return x - y * floor(x / y);\n"
1451 "}\n"
1452 "\n";
1453 }
1454
1455 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001456 {
1457 out << "float3 mod(float3 x, float y)\n"
1458 "{\n"
1459 " return x - y * floor(x / y);\n"
1460 "}\n"
1461 "\n";
1462 }
1463
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001464 if (mUsesMod4v)
1465 {
1466 out << "float4 mod(float4 x, float4 y)\n"
1467 "{\n"
1468 " return x - y * floor(x / y);\n"
1469 "}\n"
1470 "\n";
1471 }
1472
1473 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001474 {
1475 out << "float4 mod(float4 x, float y)\n"
1476 "{\n"
1477 " return x - y * floor(x / y);\n"
1478 "}\n"
1479 "\n";
1480 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001481
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001482 if (mUsesFaceforward1)
1483 {
1484 out << "float faceforward(float N, float I, float Nref)\n"
1485 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001486 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001487 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001488 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001489 " }\n"
1490 " else\n"
1491 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001492 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001493 " }\n"
1494 "}\n"
1495 "\n";
1496 }
1497
1498 if (mUsesFaceforward2)
1499 {
1500 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1501 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001502 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001503 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001504 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001505 " }\n"
1506 " else\n"
1507 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001508 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001509 " }\n"
1510 "}\n"
1511 "\n";
1512 }
1513
1514 if (mUsesFaceforward3)
1515 {
1516 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1517 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001518 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001519 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001520 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001521 " }\n"
1522 " else\n"
1523 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001524 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001525 " }\n"
1526 "}\n"
1527 "\n";
1528 }
1529
1530 if (mUsesFaceforward4)
1531 {
1532 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1533 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001534 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001535 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001536 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001537 " }\n"
1538 " else\n"
1539 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001540 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001541 " }\n"
1542 "}\n"
1543 "\n";
1544 }
1545
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001546 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001547 {
1548 out << "float atanyx(float y, float x)\n"
1549 "{\n"
1550 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1551 " return atan2(y, x);\n"
1552 "}\n";
1553 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001554
1555 if (mUsesAtan2_2)
1556 {
1557 out << "float2 atanyx(float2 y, float2 x)\n"
1558 "{\n"
1559 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1560 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1561 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1562 "}\n";
1563 }
1564
1565 if (mUsesAtan2_3)
1566 {
1567 out << "float3 atanyx(float3 y, float3 x)\n"
1568 "{\n"
1569 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1570 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1571 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1572 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1573 "}\n";
1574 }
1575
1576 if (mUsesAtan2_4)
1577 {
1578 out << "float4 atanyx(float4 y, float4 x)\n"
1579 "{\n"
1580 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1581 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1582 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1583 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1584 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1585 "}\n";
1586 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001587}
1588
1589void OutputHLSL::visitSymbol(TIntermSymbol *node)
1590{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001591 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001592
Jamie Madill570e04d2013-06-21 09:15:33 -04001593 // Handle accessing std140 structs by value
1594 if (mFlaggedStructMappedNames.count(node) > 0)
1595 {
1596 out << mFlaggedStructMappedNames[node];
1597 return;
1598 }
1599
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001600 TString name = node->getSymbol();
1601
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001602 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001603 {
1604 mUsesDepthRange = true;
1605 out << name;
1606 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001607 else
1608 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001609 TQualifier qualifier = node->getQualifier();
1610
1611 if (qualifier == EvqUniform)
1612 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001613 const TType& nodeType = node->getType();
1614 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1615
1616 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001617 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001618 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001619 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001620 else
1621 {
1622 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001623 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001624
1625 out << decorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001626 }
Jamie Madill19571812013-08-12 15:26:34 -07001627 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001628 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001629 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001630 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001631 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001632 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001633 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001634 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001635 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001636 }
Jamie Madill19571812013-08-12 15:26:34 -07001637 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001638 {
1639 mReferencedOutputVariables[name] = node;
1640 out << "out_" << name;
1641 }
1642 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001643 {
1644 out << "gl_Color[0]";
1645 mUsesFragColor = true;
1646 }
1647 else if (qualifier == EvqFragData)
1648 {
1649 out << "gl_Color";
1650 mUsesFragData = true;
1651 }
1652 else if (qualifier == EvqFragCoord)
1653 {
1654 mUsesFragCoord = true;
1655 out << name;
1656 }
1657 else if (qualifier == EvqPointCoord)
1658 {
1659 mUsesPointCoord = true;
1660 out << name;
1661 }
1662 else if (qualifier == EvqFrontFacing)
1663 {
1664 mUsesFrontFacing = true;
1665 out << name;
1666 }
1667 else if (qualifier == EvqPointSize)
1668 {
1669 mUsesPointSize = true;
1670 out << name;
1671 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001672 else if (name == "gl_FragDepthEXT")
1673 {
1674 mUsesFragDepth = true;
1675 out << "gl_Depth";
1676 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001677 else
1678 {
1679 out << decorate(name);
1680 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001681 }
1682}
1683
1684bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1685{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001686 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001687
Jamie Madill570e04d2013-06-21 09:15:33 -04001688 // Handle accessing std140 structs by value
1689 if (mFlaggedStructMappedNames.count(node) > 0)
1690 {
1691 out << mFlaggedStructMappedNames[node];
1692 return false;
1693 }
1694
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001695 switch (node->getOp())
1696 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001697 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001698 case EOpInitialize:
1699 if (visit == PreVisit)
1700 {
1701 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1702 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1703 // new variable is created before the assignment is evaluated), so we need to convert
1704 // this to "float t = x, x = t;".
1705
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001706 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1707 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001708
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001709 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1710 expression->traverse(&searchSymbol);
1711 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001712
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001713 if (sameSymbol)
1714 {
1715 // Type already printed
1716 out << "t" + str(mUniqueIndex) + " = ";
1717 expression->traverse(this);
1718 out << ", ";
1719 symbolNode->traverse(this);
1720 out << " = t" + str(mUniqueIndex);
1721
1722 mUniqueIndex++;
1723 return false;
1724 }
1725 }
1726 else if (visit == InVisit)
1727 {
1728 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001729 }
1730 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001731 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1732 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1733 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1734 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1735 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1736 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001737 if (visit == PreVisit)
1738 {
1739 out << "(";
1740 }
1741 else if (visit == InVisit)
1742 {
1743 out << " = mul(";
1744 node->getLeft()->traverse(this);
1745 out << ", transpose(";
1746 }
1747 else
1748 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001749 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001750 }
1751 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001752 case EOpMatrixTimesMatrixAssign:
1753 if (visit == PreVisit)
1754 {
1755 out << "(";
1756 }
1757 else if (visit == InVisit)
1758 {
1759 out << " = mul(";
1760 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001761 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001762 }
1763 else
1764 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001765 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001766 }
1767 break;
1768 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001769 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001770 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001771 const TType& leftType = node->getLeft()->getType();
1772 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001773 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001774 if (visit == PreVisit)
1775 {
1776 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1777 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1778
1779 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1780 out << interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1781
1782 return false;
1783 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001784 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001785 else
1786 {
1787 outputTriplet(visit, "", "[", "]");
1788 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001789 }
1790 break;
1791 case EOpIndexIndirect:
1792 // We do not currently support indirect references to interface blocks
1793 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1794 outputTriplet(visit, "", "[", "]");
1795 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001796 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001797 if (visit == InVisit)
1798 {
1799 const TStructure* structure = node->getLeft()->getType().getStruct();
1800 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1801 const TField* field = structure->fields()[index->getIConst(0)];
1802 out << "." + decorateField(field->name(), *structure);
1803
1804 return false;
1805 }
1806 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001807 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001808 if (visit == InVisit)
1809 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001810 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1811 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1812 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
1813 out << "." + decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001814
1815 return false;
1816 }
1817 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001818 case EOpVectorSwizzle:
1819 if (visit == InVisit)
1820 {
1821 out << ".";
1822
1823 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1824
1825 if (swizzle)
1826 {
1827 TIntermSequence &sequence = swizzle->getSequence();
1828
1829 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1830 {
1831 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1832
1833 if (element)
1834 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001835 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001836
1837 switch (i)
1838 {
1839 case 0: out << "x"; break;
1840 case 1: out << "y"; break;
1841 case 2: out << "z"; break;
1842 case 3: out << "w"; break;
1843 default: UNREACHABLE();
1844 }
1845 }
1846 else UNREACHABLE();
1847 }
1848 }
1849 else UNREACHABLE();
1850
1851 return false; // Fully processed
1852 }
1853 break;
1854 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1855 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1856 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1857 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001858 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001859 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001860 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001861 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001862 if (node->getOp() == EOpEqual)
1863 {
1864 outputTriplet(visit, "(", " == ", ")");
1865 }
1866 else
1867 {
1868 outputTriplet(visit, "(", " != ", ")");
1869 }
1870 }
1871 else if (node->getLeft()->getBasicType() == EbtStruct)
1872 {
1873 if (node->getOp() == EOpEqual)
1874 {
1875 out << "(";
1876 }
1877 else
1878 {
1879 out << "!(";
1880 }
1881
Jamie Madill98493dd2013-07-08 14:39:03 -04001882 const TStructure &structure = *node->getLeft()->getType().getStruct();
1883 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001884
Jamie Madill98493dd2013-07-08 14:39:03 -04001885 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001886 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001887 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001888
1889 node->getLeft()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001890 out << "." + decorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001891 node->getRight()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001892 out << "." + decorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001893
Jamie Madill98493dd2013-07-08 14:39:03 -04001894 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001895 {
1896 out << " && ";
1897 }
1898 }
1899
1900 out << ")";
1901
1902 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001903 }
1904 else
1905 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001906 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001907
1908 if (node->getOp() == EOpEqual)
1909 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001910 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001911 }
1912 else
1913 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001914 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001915 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001916 }
1917 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001918 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1919 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1920 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1921 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1922 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001923 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001924 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1925 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001926 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001927 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001928 if (node->getRight()->hasSideEffects())
1929 {
1930 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1931 return false;
1932 }
1933 else
1934 {
1935 outputTriplet(visit, "(", " || ", ")");
1936 return true;
1937 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001938 case EOpLogicalXor:
1939 mUsesXor = true;
1940 outputTriplet(visit, "xor(", ", ", ")");
1941 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001942 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001943 if (node->getRight()->hasSideEffects())
1944 {
1945 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
1946 return false;
1947 }
1948 else
1949 {
1950 outputTriplet(visit, "(", " && ", ")");
1951 return true;
1952 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001953 default: UNREACHABLE();
1954 }
1955
1956 return true;
1957}
1958
1959bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1960{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001961 switch (node->getOp())
1962 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001963 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1964 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1965 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1966 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1967 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1968 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1969 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001970 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04001971 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001972 case EOpConvFloatToBool:
1973 switch (node->getOperand()->getType().getNominalSize())
1974 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001975 case 1: outputTriplet(visit, "bool(", "", ")"); break;
1976 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
1977 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
1978 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001979 default: UNREACHABLE();
1980 }
1981 break;
1982 case EOpConvBoolToFloat:
1983 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04001984 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001985 switch (node->getOperand()->getType().getNominalSize())
1986 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001987 case 1: outputTriplet(visit, "float(", "", ")"); break;
1988 case 2: outputTriplet(visit, "float2(", "", ")"); break;
1989 case 3: outputTriplet(visit, "float3(", "", ")"); break;
1990 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001991 default: UNREACHABLE();
1992 }
1993 break;
1994 case EOpConvFloatToInt:
1995 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04001996 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001997 switch (node->getOperand()->getType().getNominalSize())
1998 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001999 case 1: outputTriplet(visit, "int(", "", ")"); break;
2000 case 2: outputTriplet(visit, "int2(", "", ")"); break;
2001 case 3: outputTriplet(visit, "int3(", "", ")"); break;
2002 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002003 default: UNREACHABLE();
2004 }
2005 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002006 case EOpConvFloatToUInt:
2007 case EOpConvBoolToUInt:
2008 case EOpConvIntToUInt:
Jamie Madilla16f1672013-07-03 15:15:17 -04002009 switch (node->getOperand()->getType().getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002010 {
2011 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002012 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
2013 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
2014 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002015 default: UNREACHABLE();
2016 }
2017 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002018 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
2019 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
2020 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
2021 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
2022 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
2023 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
2024 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
2025 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
2026 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
2027 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
2028 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
2029 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
2030 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
2031 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
2032 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
2033 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
2034 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
2035 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
2036 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
2037 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
2038 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002039 case EOpDFdx:
2040 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2041 {
2042 outputTriplet(visit, "(", "", ", 0.0)");
2043 }
2044 else
2045 {
2046 outputTriplet(visit, "ddx(", "", ")");
2047 }
2048 break;
2049 case EOpDFdy:
2050 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2051 {
2052 outputTriplet(visit, "(", "", ", 0.0)");
2053 }
2054 else
2055 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00002056 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002057 }
2058 break;
2059 case EOpFwidth:
2060 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2061 {
2062 outputTriplet(visit, "(", "", ", 0.0)");
2063 }
2064 else
2065 {
2066 outputTriplet(visit, "fwidth(", "", ")");
2067 }
2068 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002069 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
2070 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002071 default: UNREACHABLE();
2072 }
2073
2074 return true;
2075}
2076
2077bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2078{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002079 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002080
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002081 switch (node->getOp())
2082 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002083 case EOpSequence:
2084 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002085 if (mInsideFunction)
2086 {
Jamie Madill075edd82013-07-08 13:30:19 -04002087 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002088 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002089
2090 mScopeDepth++;
2091
2092 if (mScopeBracket.size() < mScopeDepth)
2093 {
2094 mScopeBracket.push_back(0); // New scope level
2095 }
2096 else
2097 {
2098 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
2099 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002100 }
2101
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002102 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
2103 {
Jamie Madill075edd82013-07-08 13:30:19 -04002104 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002105
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002106 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002107
2108 out << ";\n";
2109 }
2110
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002111 if (mInsideFunction)
2112 {
Jamie Madill075edd82013-07-08 13:30:19 -04002113 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002114 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002115
2116 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002117 }
2118
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002119 return false;
2120 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002121 case EOpDeclaration:
2122 if (visit == PreVisit)
2123 {
2124 TIntermSequence &sequence = node->getSequence();
2125 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002126
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00002127 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002128 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002129 if (variable->getType().getStruct())
2130 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002131 addConstructor(variable->getType(), scopedStruct(variable->getType().getStruct()->name()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00002132 }
2133
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002134 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002135 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00002136 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002137 {
2138 out << "static ";
2139 }
2140
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002141 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002142
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002143 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002144 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002145 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002146
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002147 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002148 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002149 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002150 out << arrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05002151 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002152 }
2153 else
2154 {
2155 (*sit)->traverse(this);
2156 }
2157
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002158 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002159 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002160 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002161 }
2162 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002163 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002164 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2165 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002166 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002167 }
2168 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002169 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002170 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002171 {
2172 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2173 {
2174 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2175
2176 if (symbol)
2177 {
2178 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2179 mReferencedVaryings[symbol->getSymbol()] = symbol;
2180 }
2181 else
2182 {
2183 (*sit)->traverse(this);
2184 }
2185 }
2186 }
2187
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002188 return false;
2189 }
2190 else if (visit == InVisit)
2191 {
2192 out << ", ";
2193 }
2194 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002195 case EOpPrototype:
2196 if (visit == PreVisit)
2197 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002198 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002199
2200 TIntermSequence &arguments = node->getSequence();
2201
2202 for (unsigned int i = 0; i < arguments.size(); i++)
2203 {
2204 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2205
2206 if (symbol)
2207 {
2208 out << argumentString(symbol);
2209
2210 if (i < arguments.size() - 1)
2211 {
2212 out << ", ";
2213 }
2214 }
2215 else UNREACHABLE();
2216 }
2217
2218 out << ");\n";
2219
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002220 // Also prototype the Lod0 variant if needed
2221 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2222 {
2223 mOutputLod0Function = true;
2224 node->traverse(this);
2225 mOutputLod0Function = false;
2226 }
2227
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002228 return false;
2229 }
2230 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002231 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002232 case EOpFunction:
2233 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002234 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002235
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002236 out << typeString(node->getType()) << " ";
2237
2238 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002239 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002240 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002241 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002242 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002243 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002244 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002245 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002246
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002247 TIntermSequence &sequence = node->getSequence();
2248 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2249
2250 for (unsigned int i = 0; i < arguments.size(); i++)
2251 {
2252 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2253
2254 if (symbol)
2255 {
2256 if (symbol->getType().getStruct())
2257 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002258 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getStruct()->name()), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002259 }
2260
2261 out << argumentString(symbol);
2262
2263 if (i < arguments.size() - 1)
2264 {
2265 out << ", ";
2266 }
2267 }
2268 else UNREACHABLE();
2269 }
2270
2271 out << ")\n"
2272 "{\n";
2273
2274 if (sequence.size() > 1)
2275 {
2276 mInsideFunction = true;
2277 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002278 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002279 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002280
2281 out << "}\n";
2282
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002283 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2284 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002285 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002286 {
2287 mOutputLod0Function = true;
2288 node->traverse(this);
2289 mOutputLod0Function = false;
2290 }
2291 }
2292
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002293 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002294 }
2295 break;
2296 case EOpFunctionCall:
2297 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002298 TString name = TFunction::unmangleName(node->getName());
2299 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002300 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002301
2302 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002303 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002304 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002305 }
2306 else
2307 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002308 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2309
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002310 TextureFunction textureFunction;
2311 textureFunction.sampler = samplerType;
2312 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002313 textureFunction.method = TextureFunction::IMPLICIT;
2314 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002315 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002316
2317 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002318 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002319 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002320 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002321 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002322 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002323 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002324 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002325 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002326 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002327 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002328 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002329 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002330 else if (name == "texture2DProjLod" || name == "textureProjLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002331 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002332 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002333 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002334 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002335 else if (name == "textureSize")
2336 {
2337 textureFunction.method = TextureFunction::SIZE;
2338 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002339 else if (name == "textureOffset")
2340 {
2341 textureFunction.method = TextureFunction::IMPLICIT;
2342 textureFunction.offset = true;
2343 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002344 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002345
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002346 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002347 {
2348 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2349 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002350 textureFunction.method = TextureFunction::LOD0;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002351 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002352 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002353 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002354 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2355
2356 if (textureFunction.offset)
2357 {
2358 mandatoryArgumentCount++;
2359 }
2360
2361 if (arguments.size() > mandatoryArgumentCount) // Bias argument is optional
2362 {
2363 textureFunction.method = TextureFunction::BIAS;
2364 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002365 }
2366 }
2367
2368 mUsesTexture.insert(textureFunction);
2369
2370 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002371 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002372
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002373 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2374 {
2375 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2376 {
2377 out << "texture_";
2378 (*arg)->traverse(this);
2379 out << ", sampler_";
2380 }
2381
2382 (*arg)->traverse(this);
2383
2384 if (arg < arguments.end() - 1)
2385 {
2386 out << ", ";
2387 }
2388 }
2389
2390 out << ")";
2391
2392 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002393 }
2394 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002395 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002396 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002397 addConstructor(node->getType(), "vec1", &node->getSequence());
2398 outputTriplet(visit, "vec1(", "", ")");
2399 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002400 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002401 addConstructor(node->getType(), "vec2", &node->getSequence());
2402 outputTriplet(visit, "vec2(", ", ", ")");
2403 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002404 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002405 addConstructor(node->getType(), "vec3", &node->getSequence());
2406 outputTriplet(visit, "vec3(", ", ", ")");
2407 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002408 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002409 addConstructor(node->getType(), "vec4", &node->getSequence());
2410 outputTriplet(visit, "vec4(", ", ", ")");
2411 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002412 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002413 addConstructor(node->getType(), "bvec1", &node->getSequence());
2414 outputTriplet(visit, "bvec1(", "", ")");
2415 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002416 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002417 addConstructor(node->getType(), "bvec2", &node->getSequence());
2418 outputTriplet(visit, "bvec2(", ", ", ")");
2419 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002420 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002421 addConstructor(node->getType(), "bvec3", &node->getSequence());
2422 outputTriplet(visit, "bvec3(", ", ", ")");
2423 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002424 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002425 addConstructor(node->getType(), "bvec4", &node->getSequence());
2426 outputTriplet(visit, "bvec4(", ", ", ")");
2427 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002428 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002429 addConstructor(node->getType(), "ivec1", &node->getSequence());
2430 outputTriplet(visit, "ivec1(", "", ")");
2431 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002432 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002433 addConstructor(node->getType(), "ivec2", &node->getSequence());
2434 outputTriplet(visit, "ivec2(", ", ", ")");
2435 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002436 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002437 addConstructor(node->getType(), "ivec3", &node->getSequence());
2438 outputTriplet(visit, "ivec3(", ", ", ")");
2439 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002440 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002441 addConstructor(node->getType(), "ivec4", &node->getSequence());
2442 outputTriplet(visit, "ivec4(", ", ", ")");
2443 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002444 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002445 addConstructor(node->getType(), "uvec1", &node->getSequence());
2446 outputTriplet(visit, "uvec1(", "", ")");
2447 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002448 case EOpConstructUVec2:
2449 addConstructor(node->getType(), "uvec2", &node->getSequence());
2450 outputTriplet(visit, "uvec2(", ", ", ")");
2451 break;
2452 case EOpConstructUVec3:
2453 addConstructor(node->getType(), "uvec3", &node->getSequence());
2454 outputTriplet(visit, "uvec3(", ", ", ")");
2455 break;
2456 case EOpConstructUVec4:
2457 addConstructor(node->getType(), "uvec4", &node->getSequence());
2458 outputTriplet(visit, "uvec4(", ", ", ")");
2459 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002460 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002461 addConstructor(node->getType(), "mat2", &node->getSequence());
2462 outputTriplet(visit, "mat2(", ", ", ")");
2463 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002464 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002465 addConstructor(node->getType(), "mat3", &node->getSequence());
2466 outputTriplet(visit, "mat3(", ", ", ")");
2467 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002468 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002469 addConstructor(node->getType(), "mat4", &node->getSequence());
2470 outputTriplet(visit, "mat4(", ", ", ")");
2471 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002472 case EOpConstructStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04002473 addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence());
2474 outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002475 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002476 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2477 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2478 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2479 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2480 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2481 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002482 case EOpMod:
2483 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002484 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002485 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2486 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2487 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002488 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002489 case 11: mUsesMod1 = true; break;
2490 case 22: mUsesMod2v = true; break;
2491 case 21: mUsesMod2f = true; break;
2492 case 33: mUsesMod3v = true; break;
2493 case 31: mUsesMod3f = true; break;
2494 case 44: mUsesMod4v = true; break;
2495 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002496 default: UNREACHABLE();
2497 }
2498
2499 outputTriplet(visit, "mod(", ", ", ")");
2500 }
2501 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002502 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002503 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002504 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002505 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2506 {
2507 case 1: mUsesAtan2_1 = true; break;
2508 case 2: mUsesAtan2_2 = true; break;
2509 case 3: mUsesAtan2_3 = true; break;
2510 case 4: mUsesAtan2_4 = true; break;
2511 default: UNREACHABLE();
2512 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002513 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002514 break;
2515 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2516 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2517 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2518 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2519 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2520 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2521 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2522 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2523 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002524 case EOpFaceForward:
2525 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002526 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002527 {
2528 case 1: mUsesFaceforward1 = true; break;
2529 case 2: mUsesFaceforward2 = true; break;
2530 case 3: mUsesFaceforward3 = true; break;
2531 case 4: mUsesFaceforward4 = true; break;
2532 default: UNREACHABLE();
2533 }
2534
2535 outputTriplet(visit, "faceforward(", ", ", ")");
2536 }
2537 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002538 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2539 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2540 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002541 default: UNREACHABLE();
2542 }
2543
2544 return true;
2545}
2546
2547bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2548{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002549 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002550
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002551 if (node->usesTernaryOperator())
2552 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002553 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002554 }
2555 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002556 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002557 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002558
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002559 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002560
2561 node->getCondition()->traverse(this);
2562
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002563 out << ")\n";
2564
Jamie Madill075edd82013-07-08 13:30:19 -04002565 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002566 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002567
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002568 bool discard = false;
2569
daniel@transgaming.combb885322010-04-15 20:45:24 +00002570 if (node->getTrueBlock())
2571 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002572 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002573
2574 // Detect true discard
2575 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002576 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002577
Jamie Madill075edd82013-07-08 13:30:19 -04002578 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002579 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002580
2581 if (node->getFalseBlock())
2582 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002583 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002584
Jamie Madill075edd82013-07-08 13:30:19 -04002585 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002586 out << "{\n";
2587
Jamie Madill075edd82013-07-08 13:30:19 -04002588 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002589 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002590
Jamie Madill075edd82013-07-08 13:30:19 -04002591 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002592 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002593
2594 // Detect false discard
2595 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2596 }
2597
2598 // ANGLE issue 486: Detect problematic conditional discard
2599 if (discard && FindSideEffectRewriting::search(node))
2600 {
2601 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002602 }
2603 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002604
2605 return false;
2606}
2607
2608void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2609{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002610 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002611}
2612
2613bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2614{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002615 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2616
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002617 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002618 {
2619 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2620 }
2621
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002622 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002623 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002624 if (handleExcessiveLoop(node))
2625 {
2626 return false;
2627 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002628 }
2629
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002630 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002631
alokp@chromium.org52813552010-11-16 18:36:09 +00002632 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002633 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002634 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002635
Jamie Madill075edd82013-07-08 13:30:19 -04002636 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002637 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002638 }
2639 else
2640 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002641 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002642
2643 if (node->getInit())
2644 {
2645 node->getInit()->traverse(this);
2646 }
2647
2648 out << "; ";
2649
alokp@chromium.org52813552010-11-16 18:36:09 +00002650 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002651 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002652 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002653 }
2654
2655 out << "; ";
2656
alokp@chromium.org52813552010-11-16 18:36:09 +00002657 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002658 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002659 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002660 }
2661
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002662 out << ")\n";
2663
Jamie Madill075edd82013-07-08 13:30:19 -04002664 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002665 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002666 }
2667
2668 if (node->getBody())
2669 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002670 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002671 }
2672
Jamie Madill075edd82013-07-08 13:30:19 -04002673 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002674 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002675
alokp@chromium.org52813552010-11-16 18:36:09 +00002676 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002677 {
Jamie Madill075edd82013-07-08 13:30:19 -04002678 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002679 out << "while(\n";
2680
alokp@chromium.org52813552010-11-16 18:36:09 +00002681 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002682
daniel@transgaming.com73536982012-03-21 20:45:49 +00002683 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002684 }
2685
daniel@transgaming.com73536982012-03-21 20:45:49 +00002686 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002687
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002688 mInsideDiscontinuousLoop = wasDiscontinuous;
2689
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002690 return false;
2691}
2692
2693bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2694{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002695 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002696
2697 switch (node->getFlowOp())
2698 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002699 case EOpKill:
2700 outputTriplet(visit, "discard;\n", "", "");
2701 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002702 case EOpBreak:
2703 if (visit == PreVisit)
2704 {
2705 if (mExcessiveLoopIndex)
2706 {
2707 out << "{Break";
2708 mExcessiveLoopIndex->traverse(this);
2709 out << " = true; break;}\n";
2710 }
2711 else
2712 {
2713 out << "break;\n";
2714 }
2715 }
2716 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002717 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002718 case EOpReturn:
2719 if (visit == PreVisit)
2720 {
2721 if (node->getExpression())
2722 {
2723 out << "return ";
2724 }
2725 else
2726 {
2727 out << "return;\n";
2728 }
2729 }
2730 else if (visit == PostVisit)
2731 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002732 if (node->getExpression())
2733 {
2734 out << ";\n";
2735 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002736 }
2737 break;
2738 default: UNREACHABLE();
2739 }
2740
2741 return true;
2742}
2743
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002744void OutputHLSL::traverseStatements(TIntermNode *node)
2745{
2746 if (isSingleStatement(node))
2747 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002748 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002749 }
2750
2751 node->traverse(this);
2752}
2753
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002754bool OutputHLSL::isSingleStatement(TIntermNode *node)
2755{
2756 TIntermAggregate *aggregate = node->getAsAggregate();
2757
2758 if (aggregate)
2759 {
2760 if (aggregate->getOp() == EOpSequence)
2761 {
2762 return false;
2763 }
2764 else
2765 {
2766 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2767 {
2768 if (!isSingleStatement(*sit))
2769 {
2770 return false;
2771 }
2772 }
2773
2774 return true;
2775 }
2776 }
2777
2778 return true;
2779}
2780
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002781// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2782// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002783bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2784{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002785 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002786 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002787
2788 // Parse loops of the form:
2789 // for(int index = initial; index [comparator] limit; index += increment)
2790 TIntermSymbol *index = NULL;
2791 TOperator comparator = EOpNull;
2792 int initial = 0;
2793 int limit = 0;
2794 int increment = 0;
2795
2796 // Parse index name and intial value
2797 if (node->getInit())
2798 {
2799 TIntermAggregate *init = node->getInit()->getAsAggregate();
2800
2801 if (init)
2802 {
2803 TIntermSequence &sequence = init->getSequence();
2804 TIntermTyped *variable = sequence[0]->getAsTyped();
2805
2806 if (variable && variable->getQualifier() == EvqTemporary)
2807 {
2808 TIntermBinary *assign = variable->getAsBinaryNode();
2809
2810 if (assign->getOp() == EOpInitialize)
2811 {
2812 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2813 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2814
2815 if (symbol && constant)
2816 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002817 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002818 {
2819 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002820 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002821 }
2822 }
2823 }
2824 }
2825 }
2826 }
2827
2828 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002829 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002830 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002831 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002832
2833 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2834 {
2835 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2836
2837 if (constant)
2838 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002839 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002840 {
2841 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002842 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002843 }
2844 }
2845 }
2846 }
2847
2848 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002849 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002850 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002851 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2852 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002853
2854 if (binaryTerminal)
2855 {
2856 TOperator op = binaryTerminal->getOp();
2857 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2858
2859 if (constant)
2860 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002861 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002862 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002863 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002864
2865 switch (op)
2866 {
2867 case EOpAddAssign: increment = value; break;
2868 case EOpSubAssign: increment = -value; break;
2869 default: UNIMPLEMENTED();
2870 }
2871 }
2872 }
2873 }
2874 else if (unaryTerminal)
2875 {
2876 TOperator op = unaryTerminal->getOp();
2877
2878 switch (op)
2879 {
2880 case EOpPostIncrement: increment = 1; break;
2881 case EOpPostDecrement: increment = -1; break;
2882 case EOpPreIncrement: increment = 1; break;
2883 case EOpPreDecrement: increment = -1; break;
2884 default: UNIMPLEMENTED();
2885 }
2886 }
2887 }
2888
2889 if (index != NULL && comparator != EOpNull && increment != 0)
2890 {
2891 if (comparator == EOpLessThanEqual)
2892 {
2893 comparator = EOpLessThan;
2894 limit += 1;
2895 }
2896
2897 if (comparator == EOpLessThan)
2898 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002899 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002900
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002901 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002902 {
2903 return false; // Not an excessive loop
2904 }
2905
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002906 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2907 mExcessiveLoopIndex = index;
2908
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002909 out << "{int ";
2910 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002911 out << ";\n"
2912 "bool Break";
2913 index->traverse(this);
2914 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002915
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002916 bool firstLoopFragment = true;
2917
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002918 while (iterations > 0)
2919 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002920 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002921
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002922 if (!firstLoopFragment)
2923 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002924 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002925 index->traverse(this);
2926 out << ") {\n";
2927 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002928
2929 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2930 {
2931 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2932 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002933
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002934 // for(int index = initial; index < clampedLimit; index += increment)
2935
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002936 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002937 index->traverse(this);
2938 out << " = ";
2939 out << initial;
2940
2941 out << "; ";
2942 index->traverse(this);
2943 out << " < ";
2944 out << clampedLimit;
2945
2946 out << "; ";
2947 index->traverse(this);
2948 out << " += ";
2949 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002950 out << ")\n";
2951
Jamie Madill075edd82013-07-08 13:30:19 -04002952 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002953 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002954
2955 if (node->getBody())
2956 {
2957 node->getBody()->traverse(this);
2958 }
2959
Jamie Madill075edd82013-07-08 13:30:19 -04002960 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002961 out << ";}\n";
2962
2963 if (!firstLoopFragment)
2964 {
2965 out << "}\n";
2966 }
2967
2968 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002969
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002970 initial += MAX_LOOP_ITERATIONS * increment;
2971 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002972 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002973
2974 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002975
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002976 mExcessiveLoopIndex = restoreIndex;
2977
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002978 return true;
2979 }
2980 else UNIMPLEMENTED();
2981 }
2982
2983 return false; // Not handled as an excessive loop
2984}
2985
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002986void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002987{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002988 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002989
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002990 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002991 {
2992 out << preString;
2993 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002994 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002995 {
2996 out << inString;
2997 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002998 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002999 {
3000 out << postString;
3001 }
3002}
3003
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003004void OutputHLSL::outputLineDirective(int line)
3005{
3006 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
3007 {
baustin@google.com8ab69842011-06-02 21:53:45 +00003008 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003009 mBody << "#line " << line;
3010
3011 if (mContext.sourcePath)
3012 {
3013 mBody << " \"" << mContext.sourcePath << "\"";
3014 }
3015
3016 mBody << "\n";
3017 }
3018}
3019
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003020TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3021{
3022 TQualifier qualifier = symbol->getQualifier();
3023 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003024 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003025
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003026 if (name.empty()) // HLSL demands named arguments, also for prototypes
3027 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00003028 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003029 }
3030 else
3031 {
3032 name = decorate(name);
3033 }
3034
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003035 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
3036 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003037 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
3038 qualifierString(qualifier) + " " + samplerString(type) + " sampler_" + name + arrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003039 }
3040
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003041 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003042}
3043
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003044TString OutputHLSL::interpolationString(TQualifier qualifier)
3045{
3046 switch(qualifier)
3047 {
3048 case EvqVaryingIn: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003049 case EvqFragmentIn: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003050 case EvqInvariantVaryingIn: return "";
3051 case EvqSmoothIn: return "linear";
3052 case EvqFlatIn: return "nointerpolation";
3053 case EvqCentroidIn: return "centroid";
3054 case EvqVaryingOut: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003055 case EvqVertexOut: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003056 case EvqInvariantVaryingOut: return "";
3057 case EvqSmoothOut: return "linear";
3058 case EvqFlatOut: return "nointerpolation";
3059 case EvqCentroidOut: return "centroid";
3060 default: UNREACHABLE();
3061 }
3062
3063 return "";
3064}
3065
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003066TString OutputHLSL::qualifierString(TQualifier qualifier)
3067{
3068 switch(qualifier)
3069 {
3070 case EvqIn: return "in";
3071 case EvqOut: return "out";
3072 case EvqInOut: return "inout";
3073 case EvqConstReadOnly: return "const";
3074 default: UNREACHABLE();
3075 }
3076
3077 return "";
3078}
3079
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003080TString OutputHLSL::typeString(const TType &type)
3081{
Jamie Madill98493dd2013-07-08 14:39:03 -04003082 const TStructure* structure = type.getStruct();
3083 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003084 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003085 const TString& typeName = structure->name();
3086 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003087 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003088 return structLookup(typeName);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003089 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003090 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003091 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003092 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003093 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003094 }
3095 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003096 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003097 int cols = type.getCols();
3098 int rows = type.getRows();
3099 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003100 }
3101 else
3102 {
3103 switch (type.getBasicType())
3104 {
3105 case EbtFloat:
3106 switch (type.getNominalSize())
3107 {
3108 case 1: return "float";
3109 case 2: return "float2";
3110 case 3: return "float3";
3111 case 4: return "float4";
3112 }
3113 case EbtInt:
3114 switch (type.getNominalSize())
3115 {
3116 case 1: return "int";
3117 case 2: return "int2";
3118 case 3: return "int3";
3119 case 4: return "int4";
3120 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003121 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04003122 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003123 {
3124 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003125 case 2: return "uint2";
3126 case 3: return "uint3";
3127 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003128 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003129 case EbtBool:
3130 switch (type.getNominalSize())
3131 {
3132 case 1: return "bool";
3133 case 2: return "bool2";
3134 case 3: return "bool3";
3135 case 4: return "bool4";
3136 }
3137 case EbtVoid:
3138 return "void";
3139 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003140 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04003141 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003142 case EbtSampler2DArray:
3143 case EbtISampler2DArray:
3144 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003145 return "sampler2D";
3146 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003147 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04003148 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003149 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00003150 case EbtSamplerExternalOES:
3151 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00003152 default:
3153 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003154 }
3155 }
3156
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003157 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003158 return "<unknown type>";
3159}
3160
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003161TString OutputHLSL::textureString(const TType &type)
3162{
3163 switch (type.getBasicType())
3164 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003165 case EbtSampler2D: return "Texture2D";
3166 case EbtSamplerCube: return "TextureCube";
3167 case EbtSamplerExternalOES: return "Texture2D";
3168 case EbtSampler2DArray: return "Texture2DArray";
3169 case EbtSampler3D: return "Texture3D";
3170 case EbtISampler2D: return "Texture2D<int4>";
3171 case EbtISampler3D: return "Texture3D<int4>";
3172 case EbtISamplerCube: return "TextureCube<int4>";
3173 case EbtISampler2DArray: return "Texture2DArray<int4>";
3174 case EbtUSampler2D: return "Texture2D<uint4>";
3175 case EbtUSampler3D: return "Texture3D<uint4>";
3176 case EbtUSamplerCube: return "TextureCube<uint4>";
3177 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
3178 case EbtSampler2DShadow: return "Texture2D";
3179 case EbtSamplerCubeShadow: return "TextureCube";
3180 case EbtSampler2DArrayShadow: return "Texture2DArray";
3181 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003182 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04003183
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003184 return "<unknown texture type>";
3185}
3186
Nicolas Capenscb127d32013-07-15 17:26:18 -04003187TString OutputHLSL::samplerString(const TType &type)
3188{
3189 if (IsShadowSampler(type.getBasicType()))
3190 {
3191 return "SamplerComparisonState";
3192 }
3193 else
3194 {
3195 return "SamplerState";
3196 }
3197}
3198
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003199TString OutputHLSL::arrayString(const TType &type)
3200{
3201 if (!type.isArray())
3202 {
3203 return "";
3204 }
3205
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003206 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003207}
3208
3209TString OutputHLSL::initializer(const TType &type)
3210{
3211 TString string;
3212
Jamie Madill94bf7f22013-07-08 13:31:15 -04003213 size_t size = type.getObjectSize();
3214 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003215 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003216 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003217
Jamie Madill94bf7f22013-07-08 13:31:15 -04003218 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003219 {
3220 string += ", ";
3221 }
3222 }
3223
daniel@transgaming.comead23042010-04-29 03:35:36 +00003224 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003225}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003226
Jamie Madill98493dd2013-07-08 14:39:03 -04003227TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003228{
Jamie Madill98493dd2013-07-08 14:39:03 -04003229 const TFieldList &fields = structure.fields();
3230 const bool isNameless = (structure.name() == "");
3231 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003232 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3233
Jamie Madill98493dd2013-07-08 14:39:03 -04003234 TString string;
3235 string += declareString + "\n"
3236 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003237
Jamie Madillc835df62013-06-21 09:15:32 -04003238 int elementIndex = 0;
3239
Jamie Madill9cf6c072013-06-20 11:55:53 -04003240 for (unsigned int i = 0; i < fields.size(); i++)
3241 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003242 const TField &field = *fields[i];
3243 const TType &fieldType = *field.type();
3244 const TStructure *fieldStruct = fieldType.getStruct();
3245 const TString &fieldTypeString = fieldStruct ? structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) : typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003246
Jamie Madillc835df62013-06-21 09:15:32 -04003247 if (useStd140Packing)
3248 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003249 string += std140PrePaddingString(*field.type(), &elementIndex);
Jamie Madillc835df62013-06-21 09:15:32 -04003250 }
3251
Jamie Madill98493dd2013-07-08 14:39:03 -04003252 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003253
3254 if (useStd140Packing)
3255 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003256 string += std140PostPaddingString(*field.type(), useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003257 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003258 }
3259
3260 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003261 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003262
Jamie Madille4075c92013-06-21 09:15:32 -04003263 // Add remaining element index to the global map, for use with nested structs in standard layouts
3264 if (useStd140Packing)
3265 {
3266 mStd140StructElementIndexes[structName] = elementIndex;
3267 }
3268
Jamie Madill98493dd2013-07-08 14:39:03 -04003269 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003270}
3271
Jamie Madill98493dd2013-07-08 14:39:03 -04003272TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003273{
Jamie Madill98493dd2013-07-08 14:39:03 -04003274 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003275 {
3276 return "";
3277 }
3278
3279 TString prefix = "";
3280
3281 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3282 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003283
3284 if (useStd140Packing)
3285 {
3286 prefix += "std";
3287 }
3288
Jamie Madill9cf6c072013-06-20 11:55:53 -04003289 if (useHLSLRowMajorPacking)
3290 {
Jamie Madillc835df62013-06-21 09:15:32 -04003291 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003292 prefix += "rm";
3293 }
3294
Jamie Madill98493dd2013-07-08 14:39:03 -04003295 return prefix + structLookup(structure.name());
Jamie Madill9cf6c072013-06-20 11:55:53 -04003296}
3297
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003298void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003299{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003300 if (name == "")
3301 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003302 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003303 }
3304
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003305 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
3306 {
3307 return; // Already added
3308 }
3309
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003310 TType ctorType = type;
3311 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003312 ctorType.setPrecision(EbpHigh);
3313 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003314
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003315 TString ctorName = type.getStruct() ? decorate(name) : name;
3316
3317 typedef std::vector<TType> ParameterArray;
3318 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003319
Jamie Madill98493dd2013-07-08 14:39:03 -04003320 const TStructure* structure = type.getStruct();
3321 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003322 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003323 mStructNames.insert(decorate(name));
3324
Jamie Madill98493dd2013-07-08 14:39:03 -04003325 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003326
Jamie Madill98493dd2013-07-08 14:39:03 -04003327 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003328 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003329 // Add row-major packed struct for interface blocks
3330 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003331 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003332 "#pragma pack_matrix(column_major)\n";
3333
Jamie Madillc835df62013-06-21 09:15:32 -04003334 const TString &std140Prefix = "std";
Jamie Madill98493dd2013-07-08 14:39:03 -04003335 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003336
3337 const TString &std140RowMajorPrefix = "std_rm";
3338 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003339 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003340 "#pragma pack_matrix(column_major)\n";
3341
Jamie Madill98493dd2013-07-08 14:39:03 -04003342 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003343 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003344 mStructDeclarations.push_back(std140String);
3345 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003346 }
3347
Jamie Madill98493dd2013-07-08 14:39:03 -04003348 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003349 for (unsigned int i = 0; i < fields.size(); i++)
3350 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003351 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003352 }
3353 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003354 else if (parameters)
3355 {
3356 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3357 {
3358 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3359 }
3360 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003361 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003362
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003363 TString constructor;
3364
3365 if (ctorType.getStruct())
3366 {
3367 constructor += ctorName + " " + ctorName + "_ctor(";
3368 }
3369 else // Built-in type
3370 {
3371 constructor += typeString(ctorType) + " " + ctorName + "(";
3372 }
3373
3374 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3375 {
3376 const TType &type = ctorParameters[parameter];
3377
3378 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3379
3380 if (parameter < ctorParameters.size() - 1)
3381 {
3382 constructor += ", ";
3383 }
3384 }
3385
3386 constructor += ")\n"
3387 "{\n";
3388
3389 if (ctorType.getStruct())
3390 {
3391 constructor += " " + ctorName + " structure = {";
3392 }
3393 else
3394 {
3395 constructor += " return " + typeString(ctorType) + "(";
3396 }
3397
3398 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3399 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003400 int rows = ctorType.getRows();
3401 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003402 const TType &parameter = ctorParameters[0];
3403
3404 if (parameter.isScalar())
3405 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003406 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003407 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003408 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003409 {
3410 constructor += TString((row == col) ? "x0" : "0.0");
3411
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003412 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003413 {
3414 constructor += ", ";
3415 }
3416 }
3417 }
3418 }
3419 else if (parameter.isMatrix())
3420 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003421 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003422 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003423 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003424 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003425 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003426 {
3427 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3428 }
3429 else
3430 {
3431 constructor += TString((row == col) ? "1.0" : "0.0");
3432 }
3433
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003434 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003435 {
3436 constructor += ", ";
3437 }
3438 }
3439 }
3440 }
3441 else UNREACHABLE();
3442 }
3443 else
3444 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003445 size_t remainingComponents = ctorType.getObjectSize();
3446 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003447
3448 while (remainingComponents > 0)
3449 {
3450 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003451 const size_t parameterSize = parameter.getObjectSize();
3452 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003453
3454 constructor += "x" + str(parameterIndex);
3455
3456 if (parameter.isScalar())
3457 {
3458 remainingComponents -= parameter.getObjectSize();
3459 }
3460 else if (parameter.isVector())
3461 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003462 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003463 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003464 ASSERT(parameterSize <= remainingComponents);
3465 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003466 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003467 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003468 {
3469 switch (remainingComponents)
3470 {
3471 case 1: constructor += ".x"; break;
3472 case 2: constructor += ".xy"; break;
3473 case 3: constructor += ".xyz"; break;
3474 case 4: constructor += ".xyzw"; break;
3475 default: UNREACHABLE();
3476 }
3477
3478 remainingComponents = 0;
3479 }
3480 else UNREACHABLE();
3481 }
3482 else if (parameter.isMatrix() || parameter.getStruct())
3483 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003484 ASSERT(remainingComponents == parameterSize || moreParameters);
3485 ASSERT(parameterSize <= remainingComponents);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003486
Jamie Madill94bf7f22013-07-08 13:31:15 -04003487 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003488 }
3489 else UNREACHABLE();
3490
3491 if (moreParameters)
3492 {
3493 parameterIndex++;
3494 }
3495
3496 if (remainingComponents)
3497 {
3498 constructor += ", ";
3499 }
3500 }
3501 }
3502
3503 if (ctorType.getStruct())
3504 {
3505 constructor += "};\n"
3506 " return structure;\n"
3507 "}\n";
3508 }
3509 else
3510 {
3511 constructor += ");\n"
3512 "}\n";
3513 }
3514
daniel@transgaming.com63691862010-04-29 03:32:42 +00003515 mConstructors.insert(constructor);
3516}
3517
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003518const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3519{
3520 TInfoSinkBase &out = mBody;
3521
Jamie Madill98493dd2013-07-08 14:39:03 -04003522 const TStructure* structure = type.getStruct();
3523 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003524 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003525 out << structLookup(structure->name()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003526
Jamie Madill98493dd2013-07-08 14:39:03 -04003527 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003528
Jamie Madill98493dd2013-07-08 14:39:03 -04003529 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003530 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003531 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003532
3533 constUnion = writeConstantUnion(*fieldType, constUnion);
3534
Jamie Madill98493dd2013-07-08 14:39:03 -04003535 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003536 {
3537 out << ", ";
3538 }
3539 }
3540
3541 out << ")";
3542 }
3543 else
3544 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003545 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003546 bool writeType = size > 1;
3547
3548 if (writeType)
3549 {
3550 out << typeString(type) << "(";
3551 }
3552
Jamie Madill94bf7f22013-07-08 13:31:15 -04003553 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003554 {
3555 switch (constUnion->getType())
3556 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003557 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003558 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003559 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003560 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003561 default: UNREACHABLE();
3562 }
3563
3564 if (i != size - 1)
3565 {
3566 out << ", ";
3567 }
3568 }
3569
3570 if (writeType)
3571 {
3572 out << ")";
3573 }
3574 }
3575
3576 return constUnion;
3577}
3578
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003579TString OutputHLSL::scopeString(unsigned int depthLimit)
3580{
3581 TString string;
3582
3583 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3584 {
3585 string += "_" + str(i);
3586 }
3587
3588 return string;
3589}
3590
3591TString OutputHLSL::scopedStruct(const TString &typeName)
3592{
3593 if (typeName == "")
3594 {
3595 return typeName;
3596 }
3597
3598 return typeName + scopeString(mScopeDepth);
3599}
3600
3601TString OutputHLSL::structLookup(const TString &typeName)
3602{
3603 for (int depth = mScopeDepth; depth >= 0; depth--)
3604 {
3605 TString scopedName = decorate(typeName + scopeString(depth));
3606
3607 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3608 {
3609 if (*structName == scopedName)
3610 {
3611 return scopedName;
3612 }
3613 }
3614 }
3615
3616 UNREACHABLE(); // Should have found a matching constructor
3617
3618 return typeName;
3619}
3620
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003621TString OutputHLSL::decorate(const TString &string)
3622{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003623 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003624 {
3625 return "_" + string;
3626 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003627
3628 return string;
3629}
3630
apatrick@chromium.org65756022012-01-17 21:45:38 +00003631TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003632{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003633 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003634 {
3635 return "ex_" + string;
3636 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003637
3638 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003639}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003640
Jamie Madill98493dd2013-07-08 14:39:03 -04003641TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003642{
Jamie Madill98493dd2013-07-08 14:39:03 -04003643 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003644 {
3645 return decorate(string);
3646 }
3647
3648 return string;
3649}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003650
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003651void OutputHLSL::declareInterfaceBlockField(const TType &type, const TString &name, std::vector<InterfaceBlockField>& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003652{
Jamie Madill98493dd2013-07-08 14:39:03 -04003653 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003654
3655 if (!structure)
3656 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003657 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003658 InterfaceBlockField field(glVariableType(type), glVariablePrecision(type), name.c_str(),
3659 (unsigned int)type.getArraySize(), isRowMajorMatrix);
3660 output.push_back(field);
3661 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003662 else
3663 {
Jamie Madill28167c62013-08-30 13:21:10 -04003664 InterfaceBlockField structField(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), false);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003665
3666 const TFieldList &fields = structure->fields();
3667
3668 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3669 {
3670 TField *field = fields[fieldIndex];
3671 TType *fieldType = field->type();
3672
3673 // make sure to copy matrix packing information
3674 fieldType->setLayoutQualifier(type.getLayoutQualifier());
3675
3676 declareInterfaceBlockField(*fieldType, field->name(), structField.fields);
3677 }
3678
3679 output.push_back(structField);
3680 }
3681}
3682
Jamie Madillc2141fb2013-08-30 13:21:08 -04003683Uniform OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, std::vector<Uniform>& output)
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003684{
3685 const TStructure *structure = type.getStruct();
3686
3687 if (!structure)
3688 {
3689 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
3690 Uniform uniform(glVariableType(type), glVariablePrecision(type), name.c_str(),
Jamie Madill56093782013-08-30 13:21:11 -04003691 (unsigned int)type.getArraySize(), (unsigned int)registerIndex, 0);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003692 output.push_back(uniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003693
3694 return uniform;
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003695 }
3696 else
3697 {
Jamie Madill56093782013-08-30 13:21:11 -04003698 Uniform structUniform(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(),
3699 (unsigned int)registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003700
Jamie Madill98493dd2013-07-08 14:39:03 -04003701 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003702
Jamie Madill98493dd2013-07-08 14:39:03 -04003703 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003704 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003705 TField *field = fields[fieldIndex];
3706 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003707
Jamie Madill56093782013-08-30 13:21:11 -04003708 declareUniformToList(*fieldType, field->name(), GL_INVALID_INDEX, structUniform.fields);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003709 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003710
Jamie Madill56093782013-08-30 13:21:11 -04003711 // assign register offset information -- this will override the information in any sub-structures.
3712 HLSLVariableGetRegisterInfo(registerIndex, &structUniform);
3713
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003714 output.push_back(structUniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003715
3716 return structUniform;
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003717 }
3718}
3719
Jamie Madill139b9092013-08-30 13:21:06 -04003720InterpolationType getInterpolationType(TQualifier qualifier)
3721{
3722 switch (qualifier)
3723 {
3724 case EvqFlatIn:
3725 case EvqFlatOut:
3726 return INTERPOLATION_FLAT;
3727
3728 case EvqSmoothIn:
3729 case EvqSmoothOut:
3730 case EvqVertexOut:
3731 case EvqFragmentIn:
Jamie Madill384b6042013-09-13 10:06:24 -04003732 case EvqVaryingIn:
3733 case EvqVaryingOut:
Jamie Madill139b9092013-08-30 13:21:06 -04003734 return INTERPOLATION_SMOOTH;
3735
3736 case EvqCentroidIn:
3737 case EvqCentroidOut:
3738 return INTERPOLATION_CENTROID;
3739
3740 default: UNREACHABLE();
3741 return INTERPOLATION_SMOOTH;
3742 }
3743}
3744
Jamie Madill94599662013-08-30 13:21:10 -04003745void OutputHLSL::declareVaryingToList(const TType &type, TQualifier baseTypeQualifier, const TString &name, std::vector<Varying>& fieldsOut)
Jamie Madill47fdd132013-08-30 13:21:04 -04003746{
3747 const TStructure *structure = type.getStruct();
3748
Jamie Madill94599662013-08-30 13:21:10 -04003749 InterpolationType interpolation = getInterpolationType(baseTypeQualifier);
Jamie Madill47fdd132013-08-30 13:21:04 -04003750 if (!structure)
3751 {
Jamie Madill139b9092013-08-30 13:21:06 -04003752 Varying varying(glVariableType(type), glVariablePrecision(type), name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003753 fieldsOut.push_back(varying);
3754 }
3755 else
3756 {
Jamie Madill28167c62013-08-30 13:21:10 -04003757 Varying structVarying(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003758 const TFieldList &fields = structure->fields();
3759
Jamie Madill28167c62013-08-30 13:21:10 -04003760 structVarying.structName = structure->name().c_str();
3761
Jamie Madill47fdd132013-08-30 13:21:04 -04003762 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3763 {
3764 const TField &field = *fields[fieldIndex];
Jamie Madill94599662013-08-30 13:21:10 -04003765 declareVaryingToList(*field.type(), baseTypeQualifier, field.name(), structVarying.fields);
Jamie Madill47fdd132013-08-30 13:21:04 -04003766 }
3767
3768 fieldsOut.push_back(structVarying);
3769 }
3770}
3771
Jamie Madillc2141fb2013-08-30 13:21:08 -04003772int OutputHLSL::declareUniformAndAssignRegister(const TType &type, const TString &name)
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003773{
Jamie Madillc2141fb2013-08-30 13:21:08 -04003774 int registerIndex = (IsSampler(type.getBasicType()) ? mSamplerRegister : mUniformRegister);
3775
3776 const Uniform &uniform = declareUniformToList(type, name, registerIndex, mActiveUniforms);
3777
3778 if (IsSampler(type.getBasicType()))
3779 {
3780 mSamplerRegister += HLSLVariableRegisterCount(uniform);
3781 }
3782 else
3783 {
3784 mUniformRegister += HLSLVariableRegisterCount(uniform);
3785 }
3786
3787 return registerIndex;
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003788}
3789
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003790GLenum OutputHLSL::glVariableType(const TType &type)
3791{
3792 if (type.getBasicType() == EbtFloat)
3793 {
3794 if (type.isScalar())
3795 {
3796 return GL_FLOAT;
3797 }
3798 else if (type.isVector())
3799 {
3800 switch(type.getNominalSize())
3801 {
3802 case 2: return GL_FLOAT_VEC2;
3803 case 3: return GL_FLOAT_VEC3;
3804 case 4: return GL_FLOAT_VEC4;
3805 default: UNREACHABLE();
3806 }
3807 }
3808 else if (type.isMatrix())
3809 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003810 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003811 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003812 case 2:
3813 switch(type.getRows())
3814 {
3815 case 2: return GL_FLOAT_MAT2;
3816 case 3: return GL_FLOAT_MAT2x3;
3817 case 4: return GL_FLOAT_MAT2x4;
3818 default: UNREACHABLE();
3819 }
3820
3821 case 3:
3822 switch(type.getRows())
3823 {
3824 case 2: return GL_FLOAT_MAT3x2;
3825 case 3: return GL_FLOAT_MAT3;
3826 case 4: return GL_FLOAT_MAT3x4;
3827 default: UNREACHABLE();
3828 }
3829
3830 case 4:
3831 switch(type.getRows())
3832 {
3833 case 2: return GL_FLOAT_MAT4x2;
3834 case 3: return GL_FLOAT_MAT4x3;
3835 case 4: return GL_FLOAT_MAT4;
3836 default: UNREACHABLE();
3837 }
3838
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003839 default: UNREACHABLE();
3840 }
3841 }
3842 else UNREACHABLE();
3843 }
3844 else if (type.getBasicType() == EbtInt)
3845 {
3846 if (type.isScalar())
3847 {
3848 return GL_INT;
3849 }
3850 else if (type.isVector())
3851 {
3852 switch(type.getNominalSize())
3853 {
3854 case 2: return GL_INT_VEC2;
3855 case 3: return GL_INT_VEC3;
3856 case 4: return GL_INT_VEC4;
3857 default: UNREACHABLE();
3858 }
3859 }
3860 else UNREACHABLE();
3861 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003862 else if (type.getBasicType() == EbtUInt)
3863 {
3864 if (type.isScalar())
3865 {
3866 return GL_UNSIGNED_INT;
3867 }
3868 else if (type.isVector())
3869 {
Jamie Madill22d63da2013-06-07 12:45:12 -04003870 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003871 {
3872 case 2: return GL_UNSIGNED_INT_VEC2;
3873 case 3: return GL_UNSIGNED_INT_VEC3;
3874 case 4: return GL_UNSIGNED_INT_VEC4;
3875 default: UNREACHABLE();
3876 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003877 }
3878 else UNREACHABLE();
3879 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003880 else if (type.getBasicType() == EbtBool)
3881 {
3882 if (type.isScalar())
3883 {
3884 return GL_BOOL;
3885 }
3886 else if (type.isVector())
3887 {
3888 switch(type.getNominalSize())
3889 {
3890 case 2: return GL_BOOL_VEC2;
3891 case 3: return GL_BOOL_VEC3;
3892 case 4: return GL_BOOL_VEC4;
3893 default: UNREACHABLE();
3894 }
3895 }
3896 else UNREACHABLE();
3897 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003898
3899 switch(type.getBasicType())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003900 {
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003901 case EbtSampler2D: return GL_SAMPLER_2D;
3902 case EbtSampler3D: return GL_SAMPLER_3D;
3903 case EbtSamplerCube: return GL_SAMPLER_CUBE;
3904 case EbtSampler2DArray: return GL_SAMPLER_2D_ARRAY;
3905 case EbtISampler2D: return GL_INT_SAMPLER_2D;
3906 case EbtISampler3D: return GL_INT_SAMPLER_3D;
3907 case EbtISamplerCube: return GL_INT_SAMPLER_CUBE;
3908 case EbtISampler2DArray: return GL_INT_SAMPLER_2D_ARRAY;
3909 case EbtUSampler2D: return GL_UNSIGNED_INT_SAMPLER_2D;
3910 case EbtUSampler3D: return GL_UNSIGNED_INT_SAMPLER_3D;
3911 case EbtUSamplerCube: return GL_UNSIGNED_INT_SAMPLER_CUBE;
3912 case EbtUSampler2DArray: return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3913 case EbtSampler2DShadow: return GL_SAMPLER_2D_SHADOW;
3914 case EbtSamplerCubeShadow: return GL_SAMPLER_CUBE_SHADOW;
3915 case EbtSampler2DArrayShadow: return GL_SAMPLER_2D_ARRAY_SHADOW;
3916 default: UNREACHABLE();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003917 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003918
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003919
3920 return GL_NONE;
3921}
3922
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003923GLenum OutputHLSL::glVariablePrecision(const TType &type)
3924{
3925 if (type.getBasicType() == EbtFloat)
3926 {
3927 switch (type.getPrecision())
3928 {
3929 case EbpHigh: return GL_HIGH_FLOAT;
3930 case EbpMedium: return GL_MEDIUM_FLOAT;
3931 case EbpLow: return GL_LOW_FLOAT;
3932 case EbpUndefined:
3933 // Should be defined as the default precision by the parser
3934 default: UNREACHABLE();
3935 }
3936 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003937 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003938 {
3939 switch (type.getPrecision())
3940 {
3941 case EbpHigh: return GL_HIGH_INT;
3942 case EbpMedium: return GL_MEDIUM_INT;
3943 case EbpLow: return GL_LOW_INT;
3944 case EbpUndefined:
3945 // Should be defined as the default precision by the parser
3946 default: UNREACHABLE();
3947 }
3948 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003949
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00003950 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003951 return GL_NONE;
3952}
3953
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003954bool OutputHLSL::isVaryingOut(TQualifier qualifier)
3955{
3956 switch(qualifier)
3957 {
3958 case EvqVaryingOut:
3959 case EvqInvariantVaryingOut:
3960 case EvqSmoothOut:
3961 case EvqFlatOut:
3962 case EvqCentroidOut:
Jamie Madill19571812013-08-12 15:26:34 -07003963 case EvqVertexOut:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003964 return true;
3965 }
3966
3967 return false;
3968}
3969
3970bool OutputHLSL::isVaryingIn(TQualifier qualifier)
3971{
3972 switch(qualifier)
3973 {
3974 case EvqVaryingIn:
3975 case EvqInvariantVaryingIn:
3976 case EvqSmoothIn:
3977 case EvqFlatIn:
3978 case EvqCentroidIn:
Jamie Madill19571812013-08-12 15:26:34 -07003979 case EvqFragmentIn:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003980 return true;
3981 }
3982
3983 return false;
3984}
3985
3986bool OutputHLSL::isVarying(TQualifier qualifier)
3987{
3988 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
3989}
3990
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003991}