blob: b1c3888c634a6133189e04145bbfc5ce722a0c8a [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00007#include "compiler/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"
alokp@chromium.org91b72322010-06-02 15:50:56 +000011#include "compiler/debug.h"
daniel@transgaming.com89431aa2012-05-31 01:20:29 +000012#include "compiler/DetectDiscontinuity.h"
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000013#include "compiler/InfoSink.h"
14#include "compiler/SearchSymbol.h"
15#include "compiler/UnfoldShortCircuit.h"
Jamie Madill440dc742013-06-20 11:55:55 -040016#include "compiler/HLSLLayoutEncoder.h"
Jamie Madill570e04d2013-06-21 09:15:33 -040017#include "compiler/FlagStd140Structs.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000018
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000019#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000020#include <cfloat>
21#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000022
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000023namespace sh
24{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000025// Integer to TString conversion
26TString str(int i)
27{
28 char buffer[20];
kbr@chromium.orgddb6e8e2012-04-25 00:48:13 +000029 snprintf(buffer, sizeof(buffer), "%d", i);
daniel@transgaming.com005c7392010-04-15 20:45:27 +000030 return buffer;
31}
32
Nicolas Capense0ba27a2013-06-24 16:10:52 -040033TString OutputHLSL::TextureFunction::name() const
34{
35 TString name = "gl_texture";
36
Nicolas Capens6d232bb2013-07-08 15:56:38 -040037 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040038 {
39 name += "2D";
40 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040041 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040042 {
43 name += "3D";
44 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040045 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040046 {
47 name += "Cube";
48 }
49 else UNREACHABLE();
50
51 if (proj)
52 {
53 name += "Proj";
54 }
55
Nicolas Capens75fb4752013-07-10 15:14:47 -040056 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040057 {
58 case IMPLICIT: break;
59 case BIAS: break;
60 case LOD: name += "Lod"; break;
61 case LOD0: name += "Lod0"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -040062 case SIZE: name += "Size"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040063 default: UNREACHABLE();
64 }
65
66 return name + "(";
67}
68
69bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
70{
71 if (sampler < rhs.sampler) return true;
72 if (coords < rhs.coords) return true;
73 if (!proj && rhs.proj) return true;
Nicolas Capens75fb4752013-07-10 15:14:47 -040074 if (method < rhs.method) return true;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040075
76 return false;
77}
78
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +000079OutputHLSL::OutputHLSL(TParseContext &context, const ShBuiltInResources& resources, ShShaderOutput outputType)
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +000080 : TIntermTraverser(true, true, true), mContext(context), mOutputType(outputType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000081{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000082 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +000083 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000084
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +000085 mUsesFragColor = false;
86 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +000087 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +000088 mUsesFragCoord = false;
89 mUsesPointCoord = false;
90 mUsesFrontFacing = false;
91 mUsesPointSize = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -040092 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +000093 mUsesXor = false;
94 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +000095 mUsesMod2v = false;
96 mUsesMod2f = false;
97 mUsesMod3v = false;
98 mUsesMod3f = false;
99 mUsesMod4v = false;
100 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000101 mUsesFaceforward1 = false;
102 mUsesFaceforward2 = false;
103 mUsesFaceforward3 = false;
104 mUsesFaceforward4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000105 mUsesAtan2_1 = false;
106 mUsesAtan2_2 = false;
107 mUsesAtan2_3 = false;
108 mUsesAtan2_4 = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000109
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000110 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
111
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000112 mScopeDepth = 0;
113
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000114 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000115
116 mContainsLoopDiscontinuity = false;
117 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000118 mInsideDiscontinuousLoop = false;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000119
120 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000121
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000122 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000123 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000124 if (mContext.shaderType == SH_FRAGMENT_SHADER)
125 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000126 mUniformRegister = 3; // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000127 }
128 else
129 {
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000130 mUniformRegister = 2; // Reserve registers for dx_DepthRange and dx_ViewAdjust
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000131 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000132 }
133 else
134 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000135 mUniformRegister = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000136 }
137
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000138 mSamplerRegister = 0;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000139 mInterfaceBlockRegister = 2; // Reserve registers for the default uniform block and driver constants
Jamie Madill574d9dd2013-06-20 11:55:56 -0400140 mPaddingCounter = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000141}
142
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000143OutputHLSL::~OutputHLSL()
144{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000145 delete mUnfoldShortCircuit;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000146}
147
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000148void OutputHLSL::output()
149{
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +0000150 mContainsLoopDiscontinuity = mContext.shaderType == SH_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400151 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
152 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000153
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000154 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 +0000155 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000156
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000157 mContext.infoSink().obj << mHeader.c_str();
158 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000159}
160
Jamie Madill570e04d2013-06-21 09:15:33 -0400161void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
162{
163 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
164 {
165 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
166
167 // This will mark the necessary block elements as referenced
168 flaggedNode->traverse(this);
169 TString structName(mBody.c_str());
170 mBody.erase();
171
172 mFlaggedStructOriginalNames[flaggedNode] = structName;
173
174 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
175 {
176 structName.erase(pos, 1);
177 }
178
179 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
180 }
181}
182
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000183TInfoSinkBase &OutputHLSL::getBodyStream()
184{
185 return mBody;
186}
187
daniel@transgaming.com043da132012-12-20 21:12:22 +0000188const ActiveUniforms &OutputHLSL::getUniforms()
189{
190 return mActiveUniforms;
191}
192
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000193const ActiveInterfaceBlocks &OutputHLSL::getInterfaceBlocks() const
194{
195 return mActiveInterfaceBlocks;
196}
197
Jamie Madill46131a32013-06-20 11:55:50 -0400198const ActiveShaderVariables &OutputHLSL::getOutputVariables() const
199{
200 return mActiveOutputVariables;
201}
202
Jamie Madilldefb6742013-06-20 11:55:51 -0400203const ActiveShaderVariables &OutputHLSL::getAttributes() const
204{
205 return mActiveAttributes;
206}
207
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000208int OutputHLSL::vectorSize(const TType &type) const
209{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000210 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000211 int arraySize = type.isArray() ? type.getArraySize() : 1;
212
213 return elementSize * arraySize;
214}
215
Jamie Madill98493dd2013-07-08 14:39:03 -0400216TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, const TField &field)
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000217{
Jamie Madill98493dd2013-07-08 14:39:03 -0400218 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000219 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400220 return interfaceBlock.name() + "." + field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000221 }
222 else
223 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400224 return field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000225 }
226}
227
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000228TString OutputHLSL::decoratePrivate(const TString &privateText)
229{
230 return "dx_" + privateText;
231}
232
Jamie Madill98493dd2013-07-08 14:39:03 -0400233TString OutputHLSL::interfaceBlockStructNameString(const TInterfaceBlock &interfaceBlock)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000234{
Jamie Madill98493dd2013-07-08 14:39:03 -0400235 return decoratePrivate(interfaceBlock.name()) + "_type";
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000236}
237
Jamie Madill98493dd2013-07-08 14:39:03 -0400238TString OutputHLSL::interfaceBlockInstanceString(const TInterfaceBlock& interfaceBlock, unsigned int arrayIndex)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000239{
Jamie Madill98493dd2013-07-08 14:39:03 -0400240 if (!interfaceBlock.hasInstanceName())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000241 {
242 return "";
243 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400244 else if (interfaceBlock.isArray())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000245 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400246 return decoratePrivate(interfaceBlock.instanceName()) + "_" + str(arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000247 }
248 else
249 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400250 return decorate(interfaceBlock.instanceName());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000251 }
252}
253
Jamie Madill98493dd2013-07-08 14:39:03 -0400254TString OutputHLSL::interfaceBlockFieldTypeString(const TField &field, TLayoutBlockStorage blockStorage)
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000255{
Jamie Madill98493dd2013-07-08 14:39:03 -0400256 const TType &fieldType = *field.type();
257 const TLayoutMatrixPacking matrixPacking = fieldType.getLayoutQualifier().matrixPacking;
Jamie Madill529077d2013-06-20 11:55:54 -0400258 ASSERT(matrixPacking != EmpUnspecified);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000259
Jamie Madill98493dd2013-07-08 14:39:03 -0400260 if (fieldType.isMatrix())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000261 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400262 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill529077d2013-06-20 11:55:54 -0400263 const TString &matrixPackString = (matrixPacking == EmpRowMajor ? "column_major" : "row_major");
Jamie Madill98493dd2013-07-08 14:39:03 -0400264 return matrixPackString + " " + typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000265 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400266 else if (fieldType.getStruct())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000267 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400268 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill98493dd2013-07-08 14:39:03 -0400269 return structureTypeName(*fieldType.getStruct(), matrixPacking == EmpColumnMajor, blockStorage == EbsStd140);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000270 }
271 else
272 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400273 return typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000274 }
275}
276
Jamie Madill98493dd2013-07-08 14:39:03 -0400277TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, TLayoutBlockStorage blockStorage)
278{
279 TString hlsl;
280
281 int elementIndex = 0;
282
283 for (unsigned int typeIndex = 0; typeIndex < interfaceBlock.fields().size(); typeIndex++)
284 {
285 const TField &field = *interfaceBlock.fields()[typeIndex];
286 const TType &fieldType = *field.type();
287
288 if (blockStorage == EbsStd140)
289 {
290 // 2 and 3 component vector types in some cases need pre-padding
291 hlsl += std140PrePaddingString(fieldType, &elementIndex);
292 }
293
294 hlsl += " " + interfaceBlockFieldTypeString(field, blockStorage) +
295 " " + decorate(field.name()) + arrayString(fieldType) + ";\n";
296
297 // must pad out after matrices and arrays, where HLSL usually allows itself room to pack stuff
298 if (blockStorage == EbsStd140)
299 {
300 const bool useHLSLRowMajorPacking = (fieldType.getLayoutQualifier().matrixPacking == EmpColumnMajor);
301 hlsl += std140PostPaddingString(fieldType, useHLSLRowMajorPacking);
302 }
303 }
304
305 return hlsl;
306}
307
308TString OutputHLSL::interfaceBlockStructString(const TInterfaceBlock &interfaceBlock)
309{
310 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
311
312 return "struct " + interfaceBlockStructNameString(interfaceBlock) + "\n"
313 "{\n" +
314 interfaceBlockFieldString(interfaceBlock, blockStorage) +
315 "};\n\n";
316}
317
318TString OutputHLSL::interfaceBlockString(const TInterfaceBlock &interfaceBlock, unsigned int registerIndex, unsigned int arrayIndex)
319{
320 const TString &arrayIndexString = (arrayIndex != GL_INVALID_INDEX ? decorate(str(arrayIndex)) : "");
321 const TString &blockName = interfaceBlock.name() + arrayIndexString;
322 TString hlsl;
323
324 hlsl += "cbuffer " + blockName + " : register(b" + str(registerIndex) + ")\n"
325 "{\n";
326
327 if (interfaceBlock.hasInstanceName())
328 {
329 hlsl += " " + interfaceBlockStructNameString(interfaceBlock) + " " + interfaceBlockInstanceString(interfaceBlock, arrayIndex) + ";\n";
330 }
331 else
332 {
333 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
334 hlsl += interfaceBlockFieldString(interfaceBlock, blockStorage);
335 }
336
337 hlsl += "};\n\n";
338
339 return hlsl;
340}
341
Jamie Madill574d9dd2013-06-20 11:55:56 -0400342TString OutputHLSL::std140PrePaddingString(const TType &type, int *elementIndex)
343{
344 if (type.getBasicType() == EbtStruct || type.isMatrix() || type.isArray())
345 {
346 // no padding needed, HLSL will align the field to a new register
347 *elementIndex = 0;
348 return "";
349 }
350
351 const GLenum glType = glVariableType(type);
352 const int numComponents = gl::UniformComponentCount(glType);
353
354 if (numComponents >= 4)
355 {
356 // no padding needed, HLSL will align the field to a new register
357 *elementIndex = 0;
358 return "";
359 }
360
361 if (*elementIndex + numComponents > 4)
362 {
363 // no padding needed, HLSL will align the field to a new register
364 *elementIndex = numComponents;
365 return "";
366 }
367
368 TString padding;
369
370 const int alignment = numComponents == 3 ? 4 : numComponents;
371 const int paddingOffset = (*elementIndex % alignment);
372
373 if (paddingOffset != 0)
374 {
375 // padding is neccessary
376 for (int paddingIndex = paddingOffset; paddingIndex < alignment; paddingIndex++)
377 {
378 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
379 }
380
381 *elementIndex += (alignment - paddingOffset);
382 }
383
384 *elementIndex += numComponents;
385 *elementIndex %= 4;
386
387 return padding;
388}
389
Jamie Madille4075c92013-06-21 09:15:32 -0400390TString OutputHLSL::std140PostPaddingString(const TType &type, bool useHLSLRowMajorPacking)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400391{
Jamie Madillc835df62013-06-21 09:15:32 -0400392 if (!type.isMatrix() && !type.isArray() && type.getBasicType() != EbtStruct)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400393 {
394 return "";
395 }
396
Jamie Madill574d9dd2013-06-20 11:55:56 -0400397 int numComponents = 0;
398
399 if (type.isMatrix())
400 {
Jamie Madille4075c92013-06-21 09:15:32 -0400401 // This method can also be called from structureString, which does not use layout qualifiers.
402 // Thus, use the method parameter for determining the matrix packing.
403 //
404 // Note HLSL row major packing corresponds to GL API column-major, and vice-versa, since we
405 // wish to always transpose GL matrices to play well with HLSL's matrix array indexing.
406 //
407 const bool isRowMajorMatrix = !useHLSLRowMajorPacking;
Jamie Madillc835df62013-06-21 09:15:32 -0400408 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400409 numComponents = gl::MatrixComponentCount(glType, isRowMajorMatrix);
410 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400411 else if (type.getStruct())
Jamie Madillc835df62013-06-21 09:15:32 -0400412 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400413 const TString &structName = structureTypeName(*type.getStruct(), useHLSLRowMajorPacking, true);
Jamie Madille4075c92013-06-21 09:15:32 -0400414 numComponents = mStd140StructElementIndexes[structName];
415
416 if (numComponents == 0)
417 {
418 return "";
419 }
Jamie Madillc835df62013-06-21 09:15:32 -0400420 }
Jamie Madill574d9dd2013-06-20 11:55:56 -0400421 else
422 {
Jamie Madillc835df62013-06-21 09:15:32 -0400423 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400424 numComponents = gl::UniformComponentCount(glType);
425 }
426
427 TString padding;
428 for (int paddingOffset = numComponents; paddingOffset < 4; paddingOffset++)
429 {
430 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
431 }
432 return padding;
433}
434
Jamie Madill440dc742013-06-20 11:55:55 -0400435// Use the same layout for packed and shared
436void setBlockLayout(InterfaceBlock *interfaceBlock, BlockLayoutType newLayout)
437{
438 interfaceBlock->layout = newLayout;
439 interfaceBlock->blockInfo.clear();
440
441 switch (newLayout)
442 {
443 case BLOCKLAYOUT_SHARED:
444 case BLOCKLAYOUT_PACKED:
445 {
446 HLSLBlockEncoder hlslEncoder(&interfaceBlock->blockInfo);
447 hlslEncoder.encodeFields(interfaceBlock->activeUniforms);
448 interfaceBlock->dataSize = hlslEncoder.getBlockSize();
449 }
450 break;
451
452 case BLOCKLAYOUT_STANDARD:
453 {
454 Std140BlockEncoder stdEncoder(&interfaceBlock->blockInfo);
455 stdEncoder.encodeFields(interfaceBlock->activeUniforms);
456 interfaceBlock->dataSize = stdEncoder.getBlockSize();
457 }
458 break;
459
460 default:
461 UNREACHABLE();
462 break;
463 }
464}
465
Jamie Madill574d9dd2013-06-20 11:55:56 -0400466BlockLayoutType convertBlockLayoutType(TLayoutBlockStorage blockStorage)
467{
468 switch (blockStorage)
469 {
470 case EbsPacked: return BLOCKLAYOUT_PACKED;
471 case EbsShared: return BLOCKLAYOUT_SHARED;
472 case EbsStd140: return BLOCKLAYOUT_STANDARD;
473 default: UNREACHABLE(); return BLOCKLAYOUT_SHARED;
474 }
475}
476
Jamie Madill98493dd2013-07-08 14:39:03 -0400477TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400478{
479 TString init;
480
481 TString preIndentString;
482 TString fullIndentString;
483
484 for (int spaces = 0; spaces < (indent * 4); spaces++)
485 {
486 preIndentString += ' ';
487 }
488
489 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
490 {
491 fullIndentString += ' ';
492 }
493
494 init += preIndentString + "{\n";
495
Jamie Madill98493dd2013-07-08 14:39:03 -0400496 const TFieldList &fields = structure.fields();
497 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400498 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400499 const TField &field = *fields[fieldIndex];
500 const TString &fieldName = rhsStructName + "." + decorate(field.name());
501 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400502
Jamie Madill98493dd2013-07-08 14:39:03 -0400503 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400504 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400505 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400506 }
507 else
508 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400509 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400510 }
511 }
512
513 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
514
515 return init;
516}
517
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000518void OutputHLSL::header()
519{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000520 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000521
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000522 TString uniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000523 TString interfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000524 TString varyings;
525 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400526 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000527
528 for (ReferencedSymbols::const_iterator uniform = mReferencedUniforms.begin(); uniform != mReferencedUniforms.end(); uniform++)
529 {
530 const TType &type = uniform->second->getType();
531 const TString &name = uniform->second->getSymbol();
532
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000533 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture
534 {
535 int index = samplerRegister(mReferencedUniforms[name]);
536
Nicolas Capenscb127d32013-07-15 17:26:18 -0400537 uniforms += "uniform " + samplerString(type) + " sampler_" + decorateUniform(name, type) + arrayString(type) +
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000538 " : register(s" + str(index) + ");\n";
539
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000540 uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) +
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000541 " : register(t" + str(index) + ");\n";
542 }
543 else
544 {
545 uniforms += "uniform " + typeString(type) + " " + decorateUniform(name, type) + arrayString(type) +
546 " : register(" + registerString(mReferencedUniforms[name]) + ");\n";
547 }
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000548 }
549
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000550 for (ReferencedSymbols::const_iterator interfaceBlockIt = mReferencedInterfaceBlocks.begin(); interfaceBlockIt != mReferencedInterfaceBlocks.end(); interfaceBlockIt++)
551 {
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000552 const TType &nodeType = interfaceBlockIt->second->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -0400553 const TInterfaceBlock &interfaceBlock = *nodeType.getInterfaceBlock();
554 const TFieldList &fieldList = interfaceBlock.fields();
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000555
Jamie Madill98493dd2013-07-08 14:39:03 -0400556 unsigned int arraySize = static_cast<unsigned int>(interfaceBlock.arraySize());
557 sh::InterfaceBlock activeBlock(interfaceBlock.name().c_str(), arraySize, mInterfaceBlockRegister);
558 for (unsigned int typeIndex = 0; typeIndex < fieldList.size(); typeIndex++)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000559 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400560 const TField &field = *fieldList[typeIndex];
561 const TString &fullUniformName = interfaceBlockFieldString(interfaceBlock, field);
562 declareUniformToList(*field.type(), fullUniformName, typeIndex, activeBlock.activeUniforms);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000563 }
564
Jamie Madill98493dd2013-07-08 14:39:03 -0400565 mInterfaceBlockRegister += std::max(1u, arraySize);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000566
Jamie Madill98493dd2013-07-08 14:39:03 -0400567 BlockLayoutType blockLayoutType = convertBlockLayoutType(interfaceBlock.blockStorage());
568 setBlockLayout(&activeBlock, blockLayoutType);
569 mActiveInterfaceBlocks.push_back(activeBlock);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000570
Jamie Madill98493dd2013-07-08 14:39:03 -0400571 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000572 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400573 interfaceBlocks += interfaceBlockStructString(interfaceBlock);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000574 }
575
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000576 if (arraySize > 0)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000577 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000578 for (unsigned int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
579 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400580 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex + arrayIndex, arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000581 }
582 }
583 else
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000584 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400585 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000586 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000587 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000588
Jamie Madill570e04d2013-06-21 09:15:33 -0400589 for (auto flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
590 {
591 TIntermTyped *structNode = flaggedStructIt->first;
592 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400593 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400594 const TString &originalName = mFlaggedStructOriginalNames[structNode];
595
Jamie Madill98493dd2013-07-08 14:39:03 -0400596 flaggedStructs += "static " + decorate(structure.name()) + " " + mappedName + " =\n";
597 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400598 flaggedStructs += "\n";
599 }
600
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000601 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
602 {
603 const TType &type = varying->second->getType();
604 const TString &name = varying->second->getSymbol();
605
606 // Program linking depends on this exact format
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +0000607 varyings += "static " + interpolationString(type.getQualifier()) + " " + typeString(type) + " " +
608 decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000609 }
610
611 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
612 {
613 const TType &type = attribute->second->getType();
614 const TString &name = attribute->second->getSymbol();
615
616 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400617
618 ShaderVariable shaderVar(glVariableType(type), glVariablePrecision(type), name.c_str(),
619 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
620 mActiveAttributes.push_back(shaderVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000621 }
622
Jamie Madill529077d2013-06-20 11:55:54 -0400623 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
624 {
625 out << *structDeclaration;
626 }
627
628 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
629 {
630 out << *constructor;
631 }
632
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400633 if (mContext.shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000634 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000635 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000636 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000637
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000638 out << "// Varyings\n";
639 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400640 out << "\n";
641
642 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000643 {
Jamie Madill46131a32013-06-20 11:55:50 -0400644 for (auto outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000645 {
Jamie Madill46131a32013-06-20 11:55:50 -0400646 const TString &variableName = outputVariableIt->first;
647 const TType &variableType = outputVariableIt->second->getType();
648 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
649
650 out << "static " + typeString(variableType) + " out_" + variableName + arrayString(variableType) +
651 " = " + initializer(variableType) + ";\n";
652
653 ShaderVariable outputVar(glVariableType(variableType), glVariablePrecision(variableType), variableName.c_str(),
654 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
655 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000656 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000657 }
Jamie Madill46131a32013-06-20 11:55:50 -0400658 else
659 {
660 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
661
662 out << "static float4 gl_Color[" << numColorValues << "] =\n"
663 "{\n";
664 for (unsigned int i = 0; i < numColorValues; i++)
665 {
666 out << " float4(0, 0, 0, 0)";
667 if (i + 1 != numColorValues)
668 {
669 out << ",";
670 }
671 out << "\n";
672 }
673
674 out << "};\n";
675 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000676
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400677 if (mUsesFragDepth)
678 {
679 out << "static float gl_Depth = 0.0;\n";
680 }
681
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000682 if (mUsesFragCoord)
683 {
684 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
685 }
686
687 if (mUsesPointCoord)
688 {
689 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
690 }
691
692 if (mUsesFrontFacing)
693 {
694 out << "static bool gl_FrontFacing = false;\n";
695 }
696
697 out << "\n";
698
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000699 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000700 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000701 out << "struct gl_DepthRangeParameters\n"
702 "{\n"
703 " float near;\n"
704 " float far;\n"
705 " float diff;\n"
706 "};\n"
707 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000708 }
709
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000710 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000711 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000712 out << "cbuffer DriverConstants : register(b1)\n"
713 "{\n";
714
715 if (mUsesDepthRange)
716 {
717 out << " float3 dx_DepthRange : packoffset(c0);\n";
718 }
719
720 if (mUsesFragCoord)
721 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000722 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000723 }
724
725 if (mUsesFragCoord || mUsesFrontFacing)
726 {
727 out << " float3 dx_DepthFront : packoffset(c2);\n";
728 }
729
730 out << "};\n";
731 }
732 else
733 {
734 if (mUsesDepthRange)
735 {
736 out << "uniform float3 dx_DepthRange : register(c0);";
737 }
738
739 if (mUsesFragCoord)
740 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000741 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000742 }
743
744 if (mUsesFragCoord || mUsesFrontFacing)
745 {
746 out << "uniform float3 dx_DepthFront : register(c2);\n";
747 }
748 }
749
750 out << "\n";
751
752 if (mUsesDepthRange)
753 {
754 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
755 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000756 }
757
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000758 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000759 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000760
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000761 if (!interfaceBlocks.empty())
762 {
763 out << interfaceBlocks;
764 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400765
766 if (!flaggedStructs.empty())
767 {
768 out << "// Std140 Structures accessed by value\n";
769 out << "\n";
770 out << flaggedStructs;
771 out << "\n";
772 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000773 }
774
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000775 if (usingMRTExtension && mNumRenderTargets > 1)
776 {
777 out << "#define GL_USES_MRT\n";
778 }
779
780 if (mUsesFragColor)
781 {
782 out << "#define GL_USES_FRAG_COLOR\n";
783 }
784
785 if (mUsesFragData)
786 {
787 out << "#define GL_USES_FRAG_DATA\n";
788 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000789 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000790 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000791 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000792 out << "// Attributes\n";
793 out << attributes;
794 out << "\n"
795 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
796
797 if (mUsesPointSize)
798 {
799 out << "static float gl_PointSize = float(1);\n";
800 }
801
802 out << "\n"
803 "// Varyings\n";
804 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000805 out << "\n";
806
807 if (mUsesDepthRange)
808 {
809 out << "struct gl_DepthRangeParameters\n"
810 "{\n"
811 " float near;\n"
812 " float far;\n"
813 " float diff;\n"
814 "};\n"
815 "\n";
816 }
817
818 if (mOutputType == SH_HLSL11_OUTPUT)
819 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000820 if (mUsesDepthRange)
821 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000822 out << "cbuffer DriverConstants : register(b1)\n"
823 "{\n"
824 " float3 dx_DepthRange : packoffset(c0);\n"
825 "};\n"
826 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000827 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000828 }
829 else
830 {
831 if (mUsesDepthRange)
832 {
833 out << "uniform float3 dx_DepthRange : register(c0);\n";
834 }
835
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000836 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000837 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000838 }
839
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000840 if (mUsesDepthRange)
841 {
842 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
843 "\n";
844 }
845
846 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000848
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000849 if (!interfaceBlocks.empty())
850 {
851 out << interfaceBlocks;
852 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400853
854 if (!flaggedStructs.empty())
855 {
856 out << "// Std140 Structures accessed by value\n";
857 out << "\n";
858 out << flaggedStructs;
859 out << "\n";
860 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000861 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400862 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000863
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400864 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
865 {
866 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400867 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000868 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400869 switch(textureFunction->sampler)
870 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400871 case EbtSampler2D: out << "int2 "; break;
872 case EbtSampler3D: out << "int3 "; break;
873 case EbtSamplerCube: out << "int2 "; break;
874 case EbtSampler2DArray: out << "int3 "; break;
875 case EbtISampler2D: out << "int2 "; break;
876 case EbtISampler3D: out << "int3 "; break;
877 case EbtISamplerCube: out << "int2 "; break;
878 case EbtISampler2DArray: out << "int3 "; break;
879 case EbtUSampler2D: out << "int2 "; break;
880 case EbtUSampler3D: out << "int3 "; break;
881 case EbtUSamplerCube: out << "int2 "; break;
882 case EbtUSampler2DArray: out << "int3 "; break;
883 case EbtSampler2DShadow: out << "int2 "; break;
884 case EbtSamplerCubeShadow: out << "int2 "; break;
885 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400886 default: UNREACHABLE();
887 }
888 }
889 else // Sampling function
890 {
891 switch(textureFunction->sampler)
892 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400893 case EbtSampler2D: out << "float4 "; break;
894 case EbtSampler3D: out << "float4 "; break;
895 case EbtSamplerCube: out << "float4 "; break;
896 case EbtSampler2DArray: out << "float4 "; break;
897 case EbtISampler2D: out << "int4 "; break;
898 case EbtISampler3D: out << "int4 "; break;
899 case EbtISamplerCube: out << "int4 "; break;
900 case EbtISampler2DArray: out << "int4 "; break;
901 case EbtUSampler2D: out << "uint4 "; break;
902 case EbtUSampler3D: out << "uint4 "; break;
903 case EbtUSamplerCube: out << "uint4 "; break;
904 case EbtUSampler2DArray: out << "uint4 "; break;
905 case EbtSampler2DShadow: out << "float "; break;
906 case EbtSamplerCubeShadow: out << "float "; break;
907 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400908 default: UNREACHABLE();
909 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000910 }
911
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400912 // Function name
913 out << textureFunction->name();
914
915 // Argument list
916 int hlslCoords = 4;
917
918 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000919 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400920 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000921 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400922 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
923 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
924 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000925 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400926
Nicolas Capens75fb4752013-07-10 15:14:47 -0400927 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000928 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400929 case TextureFunction::IMPLICIT: break;
930 case TextureFunction::BIAS: hlslCoords = 4; break;
931 case TextureFunction::LOD: hlslCoords = 4; break;
932 case TextureFunction::LOD0: hlslCoords = 4; break;
933 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000934 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400935 }
936 else if (mOutputType == SH_HLSL11_OUTPUT)
937 {
938 switch(textureFunction->sampler)
939 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400940 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
941 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
942 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
943 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
944 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
945 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
946 case EbtISamplerCube: out << "TextureCube<int4> x, SamplerState s"; hlslCoords = 3; break;
947 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
948 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
949 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
950 case EbtUSamplerCube: out << "TextureCube<uint4> x, SamplerState s"; hlslCoords = 3; break;
951 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
952 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
953 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
954 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400955 default: UNREACHABLE();
956 }
957 }
958 else UNREACHABLE();
959
960 switch(textureFunction->coords)
961 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400962 case 1: out << ", int lod"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400963 case 2: out << ", float2 t"; break;
964 case 3: out << ", float3 t"; break;
965 case 4: out << ", float4 t"; break;
966 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000967 }
968
Nicolas Capens75fb4752013-07-10 15:14:47 -0400969 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000970 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400971 case TextureFunction::IMPLICIT: break;
972 case TextureFunction::BIAS: out << ", float bias"; break;
973 case TextureFunction::LOD: out << ", float lod"; break;
974 case TextureFunction::LOD0: break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400975 case TextureFunction::SIZE: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400976 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000977 }
978
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400979 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400980 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400981
Nicolas Capens75fb4752013-07-10 15:14:47 -0400982 if (textureFunction->method == TextureFunction::SIZE)
983 {
984 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
985 {
986 if (IsSamplerArray(textureFunction->sampler))
987 {
988 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
989 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
990 }
991 else
992 {
993 out << " uint width; uint height; uint numberOfLevels;\n"
994 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
995 }
996 }
997 else if (IsSampler3D(textureFunction->sampler))
998 {
999 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
1000 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
1001 }
1002 else UNREACHABLE();
1003
1004 switch(textureFunction->sampler)
1005 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04001006 case EbtSampler2D: out << " return int2(width, height);"; break;
1007 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
1008 case EbtSamplerCube: out << " return int2(width, height);"; break;
1009 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
1010 case EbtISampler2D: out << " return int2(width, height);"; break;
1011 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
1012 case EbtISamplerCube: out << " return int2(width, height);"; break;
1013 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
1014 case EbtUSampler2D: out << " return int2(width, height);"; break;
1015 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
1016 case EbtUSamplerCube: out << " return int2(width, height);"; break;
1017 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
1018 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
1019 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
1020 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001021 default: UNREACHABLE();
1022 }
1023 }
1024 else if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
daniel@transgaming.com15795192011-05-11 15:36:20 +00001025 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001026 // Currently unsupported because TextureCube does not support Load
1027 // This will require emulation using a Texture2DArray with 6 faces
1028 if (textureFunction->sampler == EbtISamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001029 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001030 out << " return int4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001031 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001032 else if (textureFunction->sampler == EbtUSamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001033 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001034 out << " return uint4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001035 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001036 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001037 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001038 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001039 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001040 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001041 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001042 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001043 {
Nicolas Capens93e50de2013-07-09 13:46:28 -04001044 if (IsSamplerArray(textureFunction->sampler))
1045 {
Nicolas Capensc98406a2013-07-10 14:52:44 -04001046 out << " float width; float height; float layers;\n"
1047 " x.GetDimensions(width, height, layers);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001048 }
1049 else
1050 {
Nicolas Capensc98406a2013-07-10 14:52:44 -04001051 out << " float width; float height;\n"
1052 " x.GetDimensions(width, height);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001053 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001054 }
1055 else if (IsSampler3D(textureFunction->sampler))
1056 {
Nicolas Capensc98406a2013-07-10 14:52:44 -04001057 out << " float width; float height; float depth;\n"
1058 " x.GetDimensions(width, height, depth);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001059 }
1060 else UNREACHABLE();
1061 }
1062
1063 out << " return ";
1064
1065 // HLSL intrinsic
1066 if (mOutputType == SH_HLSL9_OUTPUT)
1067 {
1068 switch(textureFunction->sampler)
1069 {
1070 case EbtSampler2D: out << "tex2D"; break;
1071 case EbtSamplerCube: out << "texCUBE"; break;
1072 default: UNREACHABLE();
1073 }
1074
Nicolas Capens75fb4752013-07-10 15:14:47 -04001075 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001076 {
1077 case TextureFunction::IMPLICIT: out << "(s, "; break;
1078 case TextureFunction::BIAS: out << "bias(s, "; break;
1079 case TextureFunction::LOD: out << "lod(s, "; break;
1080 case TextureFunction::LOD0: out << "lod(s, "; break;
1081 default: UNREACHABLE();
1082 }
1083 }
1084 else if (mOutputType == SH_HLSL11_OUTPUT)
1085 {
1086 if (IsIntegerSampler(textureFunction->sampler))
1087 {
1088 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001089 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001090 else if (IsShadowSampler(textureFunction->sampler))
1091 {
1092 out << "x.SampleCmp(s, ";
1093 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001094 else
1095 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001096 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001097 {
1098 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1099 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1100 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1101 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
1102 default: UNREACHABLE();
1103 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001104 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001105 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001106 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001107
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001108 // Integer sampling requires integer addresses
1109 TString addressx = "";
1110 TString addressy = "";
1111 TString addressz = "";
1112 TString close = "";
1113
1114 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001115 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001116 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001117 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001118 case 2: out << "int3("; break;
1119 case 3: out << "int4("; break;
1120 default: UNREACHABLE();
1121 }
1122
Nicolas Capensc98406a2013-07-10 14:52:44 -04001123 addressx = "int(floor(width * frac((";
1124 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001125
1126 if (IsSamplerArray(textureFunction->sampler))
1127 {
1128 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1129 }
1130 else
1131 {
Nicolas Capensc98406a2013-07-10 14:52:44 -04001132 addressz = "int(floor(depth * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001133 }
1134
1135 close = "))))";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001136 }
1137 else
1138 {
1139 switch(hlslCoords)
1140 {
1141 case 2: out << "float2("; break;
1142 case 3: out << "float3("; break;
1143 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001144 default: UNREACHABLE();
1145 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001146 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001147
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001148 TString proj = ""; // Only used for projected textures
1149
1150 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001151 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001152 switch(textureFunction->coords)
1153 {
1154 case 3: proj = " / t.z"; break;
1155 case 4: proj = " / t.w"; break;
1156 default: UNREACHABLE();
1157 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001158 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001159
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001160 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001161
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001162 if (mOutputType == SH_HLSL9_OUTPUT)
1163 {
1164 if (hlslCoords >= 3)
1165 {
1166 if (textureFunction->coords < 3)
1167 {
1168 out << ", 0";
1169 }
1170 else
1171 {
1172 out << ", t.z" + proj;
1173 }
1174 }
1175
1176 if (hlslCoords == 4)
1177 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001178 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001179 {
1180 case TextureFunction::BIAS: out << ", bias"; break;
1181 case TextureFunction::LOD: out << ", lod"; break;
1182 case TextureFunction::LOD0: out << ", 0"; break;
1183 default: UNREACHABLE();
1184 }
1185 }
1186
1187 out << "));\n";
1188 }
1189 else if (mOutputType == SH_HLSL11_OUTPUT)
1190 {
1191 if (hlslCoords >= 3)
1192 {
1193 out << ", " + addressz + ("t.z" + proj) + close;
1194 }
1195
Nicolas Capenscb127d32013-07-15 17:26:18 -04001196 if (IsIntegerSampler(textureFunction->sampler))
1197 {
1198 out << ", 0));"; // Sample from the top level
1199 }
1200 else if (IsShadowSampler(textureFunction->sampler))
1201 {
1202 // Compare value
1203 switch(textureFunction->coords)
1204 {
1205 case 3: out << "), t.z);"; break;
1206 case 4: out << "), t.w);"; break;
1207 default: UNREACHABLE();
1208 }
1209 }
1210 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001211 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001212 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001213 {
1214 case TextureFunction::IMPLICIT: out << "));"; break;
1215 case TextureFunction::BIAS: out << "), bias);"; break;
1216 case TextureFunction::LOD: out << "), lod);"; break;
1217 case TextureFunction::LOD0: out << "), 0);"; break;
1218 default: UNREACHABLE();
1219 }
1220 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001221 }
1222 else UNREACHABLE();
1223 }
1224
1225 out << "\n"
1226 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001227 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001228 }
1229
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001230 if (mUsesFragCoord)
1231 {
1232 out << "#define GL_USES_FRAG_COORD\n";
1233 }
1234
1235 if (mUsesPointCoord)
1236 {
1237 out << "#define GL_USES_POINT_COORD\n";
1238 }
1239
1240 if (mUsesFrontFacing)
1241 {
1242 out << "#define GL_USES_FRONT_FACING\n";
1243 }
1244
1245 if (mUsesPointSize)
1246 {
1247 out << "#define GL_USES_POINT_SIZE\n";
1248 }
1249
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001250 if (mUsesFragDepth)
1251 {
1252 out << "#define GL_USES_FRAG_DEPTH\n";
1253 }
1254
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001255 if (mUsesDepthRange)
1256 {
1257 out << "#define GL_USES_DEPTH_RANGE\n";
1258 }
1259
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001260 if (mUsesXor)
1261 {
1262 out << "bool xor(bool p, bool q)\n"
1263 "{\n"
1264 " return (p || q) && !(p && q);\n"
1265 "}\n"
1266 "\n";
1267 }
1268
1269 if (mUsesMod1)
1270 {
1271 out << "float mod(float x, float y)\n"
1272 "{\n"
1273 " return x - y * floor(x / y);\n"
1274 "}\n"
1275 "\n";
1276 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001277
1278 if (mUsesMod2v)
1279 {
1280 out << "float2 mod(float2 x, float2 y)\n"
1281 "{\n"
1282 " return x - y * floor(x / y);\n"
1283 "}\n"
1284 "\n";
1285 }
1286
1287 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001288 {
1289 out << "float2 mod(float2 x, float y)\n"
1290 "{\n"
1291 " return x - y * floor(x / y);\n"
1292 "}\n"
1293 "\n";
1294 }
1295
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001296 if (mUsesMod3v)
1297 {
1298 out << "float3 mod(float3 x, float3 y)\n"
1299 "{\n"
1300 " return x - y * floor(x / y);\n"
1301 "}\n"
1302 "\n";
1303 }
1304
1305 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001306 {
1307 out << "float3 mod(float3 x, float y)\n"
1308 "{\n"
1309 " return x - y * floor(x / y);\n"
1310 "}\n"
1311 "\n";
1312 }
1313
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001314 if (mUsesMod4v)
1315 {
1316 out << "float4 mod(float4 x, float4 y)\n"
1317 "{\n"
1318 " return x - y * floor(x / y);\n"
1319 "}\n"
1320 "\n";
1321 }
1322
1323 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001324 {
1325 out << "float4 mod(float4 x, float y)\n"
1326 "{\n"
1327 " return x - y * floor(x / y);\n"
1328 "}\n"
1329 "\n";
1330 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001331
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001332 if (mUsesFaceforward1)
1333 {
1334 out << "float faceforward(float N, float I, float Nref)\n"
1335 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001336 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001337 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001338 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001339 " }\n"
1340 " else\n"
1341 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001342 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001343 " }\n"
1344 "}\n"
1345 "\n";
1346 }
1347
1348 if (mUsesFaceforward2)
1349 {
1350 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1351 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001352 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001353 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001354 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001355 " }\n"
1356 " else\n"
1357 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001358 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001359 " }\n"
1360 "}\n"
1361 "\n";
1362 }
1363
1364 if (mUsesFaceforward3)
1365 {
1366 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1367 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001368 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001369 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001370 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001371 " }\n"
1372 " else\n"
1373 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001374 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001375 " }\n"
1376 "}\n"
1377 "\n";
1378 }
1379
1380 if (mUsesFaceforward4)
1381 {
1382 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1383 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001384 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001385 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001386 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001387 " }\n"
1388 " else\n"
1389 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001390 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001391 " }\n"
1392 "}\n"
1393 "\n";
1394 }
1395
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001396 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001397 {
1398 out << "float atanyx(float y, float x)\n"
1399 "{\n"
1400 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1401 " return atan2(y, x);\n"
1402 "}\n";
1403 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001404
1405 if (mUsesAtan2_2)
1406 {
1407 out << "float2 atanyx(float2 y, float2 x)\n"
1408 "{\n"
1409 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1410 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1411 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1412 "}\n";
1413 }
1414
1415 if (mUsesAtan2_3)
1416 {
1417 out << "float3 atanyx(float3 y, float3 x)\n"
1418 "{\n"
1419 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1420 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1421 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1422 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1423 "}\n";
1424 }
1425
1426 if (mUsesAtan2_4)
1427 {
1428 out << "float4 atanyx(float4 y, float4 x)\n"
1429 "{\n"
1430 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1431 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1432 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1433 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1434 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1435 "}\n";
1436 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001437}
1438
1439void OutputHLSL::visitSymbol(TIntermSymbol *node)
1440{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001441 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001442
Jamie Madill570e04d2013-06-21 09:15:33 -04001443 // Handle accessing std140 structs by value
1444 if (mFlaggedStructMappedNames.count(node) > 0)
1445 {
1446 out << mFlaggedStructMappedNames[node];
1447 return;
1448 }
1449
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001450 TString name = node->getSymbol();
1451
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001452 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001453 {
1454 mUsesDepthRange = true;
1455 out << name;
1456 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001457 else
1458 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001459 TQualifier qualifier = node->getQualifier();
1460
1461 if (qualifier == EvqUniform)
1462 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001463 const TType& nodeType = node->getType();
1464 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1465
1466 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001467 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001468 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001469 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001470 else
1471 {
1472 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001473 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001474
1475 out << decorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001476 }
Jamie Madillb120eac2013-06-12 14:08:13 -04001477 else if (qualifier == EvqAttribute || qualifier == EvqVertexInput)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001478 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001479 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001480 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001481 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001482 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001483 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001484 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001485 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001486 }
Jamie Madill46131a32013-06-20 11:55:50 -04001487 else if (qualifier == EvqFragmentOutput)
1488 {
1489 mReferencedOutputVariables[name] = node;
1490 out << "out_" << name;
1491 }
1492 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001493 {
1494 out << "gl_Color[0]";
1495 mUsesFragColor = true;
1496 }
1497 else if (qualifier == EvqFragData)
1498 {
1499 out << "gl_Color";
1500 mUsesFragData = true;
1501 }
1502 else if (qualifier == EvqFragCoord)
1503 {
1504 mUsesFragCoord = true;
1505 out << name;
1506 }
1507 else if (qualifier == EvqPointCoord)
1508 {
1509 mUsesPointCoord = true;
1510 out << name;
1511 }
1512 else if (qualifier == EvqFrontFacing)
1513 {
1514 mUsesFrontFacing = true;
1515 out << name;
1516 }
1517 else if (qualifier == EvqPointSize)
1518 {
1519 mUsesPointSize = true;
1520 out << name;
1521 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001522 else if (name == "gl_FragDepthEXT")
1523 {
1524 mUsesFragDepth = true;
1525 out << "gl_Depth";
1526 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001527 else
1528 {
1529 out << decorate(name);
1530 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001531 }
1532}
1533
1534bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1535{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001536 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001537
Jamie Madill570e04d2013-06-21 09:15:33 -04001538 // Handle accessing std140 structs by value
1539 if (mFlaggedStructMappedNames.count(node) > 0)
1540 {
1541 out << mFlaggedStructMappedNames[node];
1542 return false;
1543 }
1544
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001545 switch (node->getOp())
1546 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001547 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001548 case EOpInitialize:
1549 if (visit == PreVisit)
1550 {
1551 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1552 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1553 // new variable is created before the assignment is evaluated), so we need to convert
1554 // this to "float t = x, x = t;".
1555
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001556 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1557 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001558
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001559 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1560 expression->traverse(&searchSymbol);
1561 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001562
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001563 if (sameSymbol)
1564 {
1565 // Type already printed
1566 out << "t" + str(mUniqueIndex) + " = ";
1567 expression->traverse(this);
1568 out << ", ";
1569 symbolNode->traverse(this);
1570 out << " = t" + str(mUniqueIndex);
1571
1572 mUniqueIndex++;
1573 return false;
1574 }
1575 }
1576 else if (visit == InVisit)
1577 {
1578 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001579 }
1580 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001581 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1582 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1583 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1584 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1585 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1586 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001587 if (visit == PreVisit)
1588 {
1589 out << "(";
1590 }
1591 else if (visit == InVisit)
1592 {
1593 out << " = mul(";
1594 node->getLeft()->traverse(this);
1595 out << ", transpose(";
1596 }
1597 else
1598 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001599 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001600 }
1601 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001602 case EOpMatrixTimesMatrixAssign:
1603 if (visit == PreVisit)
1604 {
1605 out << "(";
1606 }
1607 else if (visit == InVisit)
1608 {
1609 out << " = mul(";
1610 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001611 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001612 }
1613 else
1614 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001615 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001616 }
1617 break;
1618 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001619 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001620 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001621 const TType& leftType = node->getLeft()->getType();
1622 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001623 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001624 if (visit == PreVisit)
1625 {
1626 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1627 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1628
1629 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1630 out << interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1631
1632 return false;
1633 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001634 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001635 else
1636 {
1637 outputTriplet(visit, "", "[", "]");
1638 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001639 }
1640 break;
1641 case EOpIndexIndirect:
1642 // We do not currently support indirect references to interface blocks
1643 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1644 outputTriplet(visit, "", "[", "]");
1645 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001646 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001647 if (visit == InVisit)
1648 {
1649 const TStructure* structure = node->getLeft()->getType().getStruct();
1650 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1651 const TField* field = structure->fields()[index->getIConst(0)];
1652 out << "." + decorateField(field->name(), *structure);
1653
1654 return false;
1655 }
1656 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001657 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001658 if (visit == InVisit)
1659 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001660 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1661 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1662 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
1663 out << "." + decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001664
1665 return false;
1666 }
1667 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001668 case EOpVectorSwizzle:
1669 if (visit == InVisit)
1670 {
1671 out << ".";
1672
1673 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1674
1675 if (swizzle)
1676 {
1677 TIntermSequence &sequence = swizzle->getSequence();
1678
1679 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1680 {
1681 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1682
1683 if (element)
1684 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001685 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001686
1687 switch (i)
1688 {
1689 case 0: out << "x"; break;
1690 case 1: out << "y"; break;
1691 case 2: out << "z"; break;
1692 case 3: out << "w"; break;
1693 default: UNREACHABLE();
1694 }
1695 }
1696 else UNREACHABLE();
1697 }
1698 }
1699 else UNREACHABLE();
1700
1701 return false; // Fully processed
1702 }
1703 break;
1704 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1705 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1706 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1707 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001708 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001709 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001710 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001711 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001712 if (node->getOp() == EOpEqual)
1713 {
1714 outputTriplet(visit, "(", " == ", ")");
1715 }
1716 else
1717 {
1718 outputTriplet(visit, "(", " != ", ")");
1719 }
1720 }
1721 else if (node->getLeft()->getBasicType() == EbtStruct)
1722 {
1723 if (node->getOp() == EOpEqual)
1724 {
1725 out << "(";
1726 }
1727 else
1728 {
1729 out << "!(";
1730 }
1731
Jamie Madill98493dd2013-07-08 14:39:03 -04001732 const TStructure &structure = *node->getLeft()->getType().getStruct();
1733 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001734
Jamie Madill98493dd2013-07-08 14:39:03 -04001735 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001736 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001737 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001738
1739 node->getLeft()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001740 out << "." + decorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001741 node->getRight()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001742 out << "." + decorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001743
Jamie Madill98493dd2013-07-08 14:39:03 -04001744 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001745 {
1746 out << " && ";
1747 }
1748 }
1749
1750 out << ")";
1751
1752 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001753 }
1754 else
1755 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001756 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001757
1758 if (node->getOp() == EOpEqual)
1759 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001760 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001761 }
1762 else
1763 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001764 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001765 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001766 }
1767 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001768 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1769 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1770 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1771 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1772 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001773 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001774 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1775 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001776 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001777 case EOpLogicalOr:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001778 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001779 return false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001780 case EOpLogicalXor:
1781 mUsesXor = true;
1782 outputTriplet(visit, "xor(", ", ", ")");
1783 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001784 case EOpLogicalAnd:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001785 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001786 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001787 default: UNREACHABLE();
1788 }
1789
1790 return true;
1791}
1792
1793bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1794{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001795 switch (node->getOp())
1796 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001797 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1798 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1799 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1800 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1801 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1802 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1803 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001804 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04001805 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001806 case EOpConvFloatToBool:
1807 switch (node->getOperand()->getType().getNominalSize())
1808 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001809 case 1: outputTriplet(visit, "bool(", "", ")"); break;
1810 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
1811 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
1812 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001813 default: UNREACHABLE();
1814 }
1815 break;
1816 case EOpConvBoolToFloat:
1817 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04001818 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001819 switch (node->getOperand()->getType().getNominalSize())
1820 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001821 case 1: outputTriplet(visit, "float(", "", ")"); break;
1822 case 2: outputTriplet(visit, "float2(", "", ")"); break;
1823 case 3: outputTriplet(visit, "float3(", "", ")"); break;
1824 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001825 default: UNREACHABLE();
1826 }
1827 break;
1828 case EOpConvFloatToInt:
1829 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04001830 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001831 switch (node->getOperand()->getType().getNominalSize())
1832 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001833 case 1: outputTriplet(visit, "int(", "", ")"); break;
1834 case 2: outputTriplet(visit, "int2(", "", ")"); break;
1835 case 3: outputTriplet(visit, "int3(", "", ")"); break;
1836 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001837 default: UNREACHABLE();
1838 }
1839 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04001840 case EOpConvFloatToUInt:
1841 case EOpConvBoolToUInt:
1842 case EOpConvIntToUInt:
Jamie Madilla16f1672013-07-03 15:15:17 -04001843 switch (node->getOperand()->getType().getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001844 {
1845 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001846 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
1847 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
1848 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001849 default: UNREACHABLE();
1850 }
1851 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001852 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1853 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1854 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1855 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1856 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1857 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1858 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1859 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1860 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1861 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1862 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1863 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1864 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1865 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1866 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1867 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1868 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1869 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1870 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1871 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1872 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001873 case EOpDFdx:
1874 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1875 {
1876 outputTriplet(visit, "(", "", ", 0.0)");
1877 }
1878 else
1879 {
1880 outputTriplet(visit, "ddx(", "", ")");
1881 }
1882 break;
1883 case EOpDFdy:
1884 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1885 {
1886 outputTriplet(visit, "(", "", ", 0.0)");
1887 }
1888 else
1889 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001890 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001891 }
1892 break;
1893 case EOpFwidth:
1894 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1895 {
1896 outputTriplet(visit, "(", "", ", 0.0)");
1897 }
1898 else
1899 {
1900 outputTriplet(visit, "fwidth(", "", ")");
1901 }
1902 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001903 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1904 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001905 default: UNREACHABLE();
1906 }
1907
1908 return true;
1909}
1910
1911bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1912{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001913 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001914
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001915 switch (node->getOp())
1916 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001917 case EOpSequence:
1918 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001919 if (mInsideFunction)
1920 {
Jamie Madill075edd82013-07-08 13:30:19 -04001921 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001922 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001923
1924 mScopeDepth++;
1925
1926 if (mScopeBracket.size() < mScopeDepth)
1927 {
1928 mScopeBracket.push_back(0); // New scope level
1929 }
1930 else
1931 {
1932 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
1933 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001934 }
1935
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001936 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
1937 {
Jamie Madill075edd82013-07-08 13:30:19 -04001938 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001939
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001940 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001941
1942 out << ";\n";
1943 }
1944
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001945 if (mInsideFunction)
1946 {
Jamie Madill075edd82013-07-08 13:30:19 -04001947 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001948 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001949
1950 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001951 }
1952
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001953 return false;
1954 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001955 case EOpDeclaration:
1956 if (visit == PreVisit)
1957 {
1958 TIntermSequence &sequence = node->getSequence();
1959 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001960
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001961 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001962 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001963 if (variable->getType().getStruct())
1964 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001965 addConstructor(variable->getType(), scopedStruct(variable->getType().getStruct()->name()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001966 }
1967
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001968 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001969 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00001970 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001971 {
1972 out << "static ";
1973 }
1974
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001975 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001976
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001977 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001978 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001979 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001980
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001981 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001982 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001983 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001984 out << arrayString(symbol->getType());
daniel@transgaming.com7127f202010-04-15 20:45:22 +00001985 out << " = " + initializer(variable->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001986 }
1987 else
1988 {
1989 (*sit)->traverse(this);
1990 }
1991
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001992 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001993 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001994 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001995 }
1996 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001997 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001998 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1999 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002000 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002001 }
2002 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002003 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002004 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002005 {
2006 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2007 {
2008 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2009
2010 if (symbol)
2011 {
2012 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2013 mReferencedVaryings[symbol->getSymbol()] = symbol;
2014 }
2015 else
2016 {
2017 (*sit)->traverse(this);
2018 }
2019 }
2020 }
2021
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002022 return false;
2023 }
2024 else if (visit == InVisit)
2025 {
2026 out << ", ";
2027 }
2028 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002029 case EOpPrototype:
2030 if (visit == PreVisit)
2031 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002032 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002033
2034 TIntermSequence &arguments = node->getSequence();
2035
2036 for (unsigned int i = 0; i < arguments.size(); i++)
2037 {
2038 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2039
2040 if (symbol)
2041 {
2042 out << argumentString(symbol);
2043
2044 if (i < arguments.size() - 1)
2045 {
2046 out << ", ";
2047 }
2048 }
2049 else UNREACHABLE();
2050 }
2051
2052 out << ");\n";
2053
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002054 // Also prototype the Lod0 variant if needed
2055 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2056 {
2057 mOutputLod0Function = true;
2058 node->traverse(this);
2059 mOutputLod0Function = false;
2060 }
2061
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002062 return false;
2063 }
2064 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002065 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002066 case EOpFunction:
2067 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002068 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002069
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002070 out << typeString(node->getType()) << " ";
2071
2072 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002073 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002074 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002075 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002076 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002077 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002078 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002079 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002080
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002081 TIntermSequence &sequence = node->getSequence();
2082 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2083
2084 for (unsigned int i = 0; i < arguments.size(); i++)
2085 {
2086 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2087
2088 if (symbol)
2089 {
2090 if (symbol->getType().getStruct())
2091 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002092 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getStruct()->name()), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002093 }
2094
2095 out << argumentString(symbol);
2096
2097 if (i < arguments.size() - 1)
2098 {
2099 out << ", ";
2100 }
2101 }
2102 else UNREACHABLE();
2103 }
2104
2105 out << ")\n"
2106 "{\n";
2107
2108 if (sequence.size() > 1)
2109 {
2110 mInsideFunction = true;
2111 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002112 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002113 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002114
2115 out << "}\n";
2116
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002117 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2118 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002119 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002120 {
2121 mOutputLod0Function = true;
2122 node->traverse(this);
2123 mOutputLod0Function = false;
2124 }
2125 }
2126
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002127 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002128 }
2129 break;
2130 case EOpFunctionCall:
2131 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002132 TString name = TFunction::unmangleName(node->getName());
2133 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002134 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002135
2136 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002137 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002138 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002139 }
2140 else
2141 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002142 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2143
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002144 TextureFunction textureFunction;
2145 textureFunction.sampler = samplerType;
2146 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002147 textureFunction.method = TextureFunction::IMPLICIT;
2148 textureFunction.proj = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002149
2150 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002151 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002152 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002153 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002154 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002155 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002156 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002157 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002158 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002159 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002160 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002161 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002162 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002163 else if (name == "texture2DProjLod" || name == "textureProjLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002164 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002165 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002166 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002167 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002168 else if (name == "textureSize")
2169 {
2170 textureFunction.method = TextureFunction::SIZE;
2171 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002172 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002173
Nicolas Capens75fb4752013-07-10 15:14:47 -04002174 if (textureFunction.method != TextureFunction::LOD &&
2175 textureFunction.method != TextureFunction::SIZE)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002176 {
2177 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2178 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002179 textureFunction.method = TextureFunction::LOD0;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002180 }
2181 else if (arguments.size() == 3)
2182 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002183 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002184 }
2185 }
2186
2187 mUsesTexture.insert(textureFunction);
2188
2189 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002190 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002191
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002192 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2193 {
2194 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2195 {
2196 out << "texture_";
2197 (*arg)->traverse(this);
2198 out << ", sampler_";
2199 }
2200
2201 (*arg)->traverse(this);
2202
2203 if (arg < arguments.end() - 1)
2204 {
2205 out << ", ";
2206 }
2207 }
2208
2209 out << ")";
2210
2211 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002212 }
2213 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002214 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002215 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002216 addConstructor(node->getType(), "vec1", &node->getSequence());
2217 outputTriplet(visit, "vec1(", "", ")");
2218 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002219 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002220 addConstructor(node->getType(), "vec2", &node->getSequence());
2221 outputTriplet(visit, "vec2(", ", ", ")");
2222 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002223 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002224 addConstructor(node->getType(), "vec3", &node->getSequence());
2225 outputTriplet(visit, "vec3(", ", ", ")");
2226 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002227 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002228 addConstructor(node->getType(), "vec4", &node->getSequence());
2229 outputTriplet(visit, "vec4(", ", ", ")");
2230 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002231 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002232 addConstructor(node->getType(), "bvec1", &node->getSequence());
2233 outputTriplet(visit, "bvec1(", "", ")");
2234 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002235 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002236 addConstructor(node->getType(), "bvec2", &node->getSequence());
2237 outputTriplet(visit, "bvec2(", ", ", ")");
2238 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002239 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002240 addConstructor(node->getType(), "bvec3", &node->getSequence());
2241 outputTriplet(visit, "bvec3(", ", ", ")");
2242 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002243 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002244 addConstructor(node->getType(), "bvec4", &node->getSequence());
2245 outputTriplet(visit, "bvec4(", ", ", ")");
2246 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002247 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002248 addConstructor(node->getType(), "ivec1", &node->getSequence());
2249 outputTriplet(visit, "ivec1(", "", ")");
2250 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002251 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002252 addConstructor(node->getType(), "ivec2", &node->getSequence());
2253 outputTriplet(visit, "ivec2(", ", ", ")");
2254 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002255 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002256 addConstructor(node->getType(), "ivec3", &node->getSequence());
2257 outputTriplet(visit, "ivec3(", ", ", ")");
2258 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002259 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002260 addConstructor(node->getType(), "ivec4", &node->getSequence());
2261 outputTriplet(visit, "ivec4(", ", ", ")");
2262 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002263 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002264 addConstructor(node->getType(), "uvec1", &node->getSequence());
2265 outputTriplet(visit, "uvec1(", "", ")");
2266 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002267 case EOpConstructUVec2:
2268 addConstructor(node->getType(), "uvec2", &node->getSequence());
2269 outputTriplet(visit, "uvec2(", ", ", ")");
2270 break;
2271 case EOpConstructUVec3:
2272 addConstructor(node->getType(), "uvec3", &node->getSequence());
2273 outputTriplet(visit, "uvec3(", ", ", ")");
2274 break;
2275 case EOpConstructUVec4:
2276 addConstructor(node->getType(), "uvec4", &node->getSequence());
2277 outputTriplet(visit, "uvec4(", ", ", ")");
2278 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002279 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002280 addConstructor(node->getType(), "mat2", &node->getSequence());
2281 outputTriplet(visit, "mat2(", ", ", ")");
2282 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002283 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002284 addConstructor(node->getType(), "mat3", &node->getSequence());
2285 outputTriplet(visit, "mat3(", ", ", ")");
2286 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002287 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002288 addConstructor(node->getType(), "mat4", &node->getSequence());
2289 outputTriplet(visit, "mat4(", ", ", ")");
2290 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002291 case EOpConstructStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04002292 addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence());
2293 outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002294 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002295 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2296 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2297 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2298 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2299 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2300 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002301 case EOpMod:
2302 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002303 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002304 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2305 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2306 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002307 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002308 case 11: mUsesMod1 = true; break;
2309 case 22: mUsesMod2v = true; break;
2310 case 21: mUsesMod2f = true; break;
2311 case 33: mUsesMod3v = true; break;
2312 case 31: mUsesMod3f = true; break;
2313 case 44: mUsesMod4v = true; break;
2314 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002315 default: UNREACHABLE();
2316 }
2317
2318 outputTriplet(visit, "mod(", ", ", ")");
2319 }
2320 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002321 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002322 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002323 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002324 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2325 {
2326 case 1: mUsesAtan2_1 = true; break;
2327 case 2: mUsesAtan2_2 = true; break;
2328 case 3: mUsesAtan2_3 = true; break;
2329 case 4: mUsesAtan2_4 = true; break;
2330 default: UNREACHABLE();
2331 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002332 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002333 break;
2334 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2335 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2336 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2337 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2338 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2339 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2340 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2341 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2342 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002343 case EOpFaceForward:
2344 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002345 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002346 {
2347 case 1: mUsesFaceforward1 = true; break;
2348 case 2: mUsesFaceforward2 = true; break;
2349 case 3: mUsesFaceforward3 = true; break;
2350 case 4: mUsesFaceforward4 = true; break;
2351 default: UNREACHABLE();
2352 }
2353
2354 outputTriplet(visit, "faceforward(", ", ", ")");
2355 }
2356 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002357 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2358 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2359 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002360 default: UNREACHABLE();
2361 }
2362
2363 return true;
2364}
2365
2366bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2367{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002368 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002369
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002370 if (node->usesTernaryOperator())
2371 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002372 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002373 }
2374 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002375 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002376 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002377
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002378 out << "if(";
2379
2380 node->getCondition()->traverse(this);
2381
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002382 out << ")\n";
2383
Jamie Madill075edd82013-07-08 13:30:19 -04002384 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002385 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002386
daniel@transgaming.combb885322010-04-15 20:45:24 +00002387 if (node->getTrueBlock())
2388 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002389 traverseStatements(node->getTrueBlock());
daniel@transgaming.combb885322010-04-15 20:45:24 +00002390 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002391
Jamie Madill075edd82013-07-08 13:30:19 -04002392 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002393 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002394
2395 if (node->getFalseBlock())
2396 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002397 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002398
Jamie Madill075edd82013-07-08 13:30:19 -04002399 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002400 out << "{\n";
2401
Jamie Madill075edd82013-07-08 13:30:19 -04002402 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002403 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002404
Jamie Madill075edd82013-07-08 13:30:19 -04002405 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002406 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002407 }
2408 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002409
2410 return false;
2411}
2412
2413void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2414{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002415 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002416}
2417
2418bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2419{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002420 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2421
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002422 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002423 {
2424 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2425 }
2426
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002427 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002428 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002429 if (handleExcessiveLoop(node))
2430 {
2431 return false;
2432 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002433 }
2434
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002435 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002436
alokp@chromium.org52813552010-11-16 18:36:09 +00002437 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002438 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002439 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002440
Jamie Madill075edd82013-07-08 13:30:19 -04002441 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002442 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002443 }
2444 else
2445 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002446 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002447
2448 if (node->getInit())
2449 {
2450 node->getInit()->traverse(this);
2451 }
2452
2453 out << "; ";
2454
alokp@chromium.org52813552010-11-16 18:36:09 +00002455 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002456 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002457 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002458 }
2459
2460 out << "; ";
2461
alokp@chromium.org52813552010-11-16 18:36:09 +00002462 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002463 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002464 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002465 }
2466
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002467 out << ")\n";
2468
Jamie Madill075edd82013-07-08 13:30:19 -04002469 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002470 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002471 }
2472
2473 if (node->getBody())
2474 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002475 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002476 }
2477
Jamie Madill075edd82013-07-08 13:30:19 -04002478 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002479 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002480
alokp@chromium.org52813552010-11-16 18:36:09 +00002481 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002482 {
Jamie Madill075edd82013-07-08 13:30:19 -04002483 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002484 out << "while(\n";
2485
alokp@chromium.org52813552010-11-16 18:36:09 +00002486 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002487
daniel@transgaming.com73536982012-03-21 20:45:49 +00002488 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002489 }
2490
daniel@transgaming.com73536982012-03-21 20:45:49 +00002491 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002492
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002493 mInsideDiscontinuousLoop = wasDiscontinuous;
2494
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002495 return false;
2496}
2497
2498bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2499{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002500 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002501
2502 switch (node->getFlowOp())
2503 {
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002504 case EOpKill: outputTriplet(visit, "discard;\n", "", ""); break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002505 case EOpBreak:
2506 if (visit == PreVisit)
2507 {
2508 if (mExcessiveLoopIndex)
2509 {
2510 out << "{Break";
2511 mExcessiveLoopIndex->traverse(this);
2512 out << " = true; break;}\n";
2513 }
2514 else
2515 {
2516 out << "break;\n";
2517 }
2518 }
2519 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002520 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002521 case EOpReturn:
2522 if (visit == PreVisit)
2523 {
2524 if (node->getExpression())
2525 {
2526 out << "return ";
2527 }
2528 else
2529 {
2530 out << "return;\n";
2531 }
2532 }
2533 else if (visit == PostVisit)
2534 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002535 if (node->getExpression())
2536 {
2537 out << ";\n";
2538 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002539 }
2540 break;
2541 default: UNREACHABLE();
2542 }
2543
2544 return true;
2545}
2546
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002547void OutputHLSL::traverseStatements(TIntermNode *node)
2548{
2549 if (isSingleStatement(node))
2550 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002551 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002552 }
2553
2554 node->traverse(this);
2555}
2556
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002557bool OutputHLSL::isSingleStatement(TIntermNode *node)
2558{
2559 TIntermAggregate *aggregate = node->getAsAggregate();
2560
2561 if (aggregate)
2562 {
2563 if (aggregate->getOp() == EOpSequence)
2564 {
2565 return false;
2566 }
2567 else
2568 {
2569 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2570 {
2571 if (!isSingleStatement(*sit))
2572 {
2573 return false;
2574 }
2575 }
2576
2577 return true;
2578 }
2579 }
2580
2581 return true;
2582}
2583
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002584// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2585// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002586bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2587{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002588 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002589 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002590
2591 // Parse loops of the form:
2592 // for(int index = initial; index [comparator] limit; index += increment)
2593 TIntermSymbol *index = NULL;
2594 TOperator comparator = EOpNull;
2595 int initial = 0;
2596 int limit = 0;
2597 int increment = 0;
2598
2599 // Parse index name and intial value
2600 if (node->getInit())
2601 {
2602 TIntermAggregate *init = node->getInit()->getAsAggregate();
2603
2604 if (init)
2605 {
2606 TIntermSequence &sequence = init->getSequence();
2607 TIntermTyped *variable = sequence[0]->getAsTyped();
2608
2609 if (variable && variable->getQualifier() == EvqTemporary)
2610 {
2611 TIntermBinary *assign = variable->getAsBinaryNode();
2612
2613 if (assign->getOp() == EOpInitialize)
2614 {
2615 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2616 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2617
2618 if (symbol && constant)
2619 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002620 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002621 {
2622 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002623 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002624 }
2625 }
2626 }
2627 }
2628 }
2629 }
2630
2631 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002632 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002633 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002634 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002635
2636 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2637 {
2638 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2639
2640 if (constant)
2641 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002642 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002643 {
2644 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002645 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002646 }
2647 }
2648 }
2649 }
2650
2651 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002652 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002653 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002654 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2655 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002656
2657 if (binaryTerminal)
2658 {
2659 TOperator op = binaryTerminal->getOp();
2660 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2661
2662 if (constant)
2663 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002664 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002665 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002666 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002667
2668 switch (op)
2669 {
2670 case EOpAddAssign: increment = value; break;
2671 case EOpSubAssign: increment = -value; break;
2672 default: UNIMPLEMENTED();
2673 }
2674 }
2675 }
2676 }
2677 else if (unaryTerminal)
2678 {
2679 TOperator op = unaryTerminal->getOp();
2680
2681 switch (op)
2682 {
2683 case EOpPostIncrement: increment = 1; break;
2684 case EOpPostDecrement: increment = -1; break;
2685 case EOpPreIncrement: increment = 1; break;
2686 case EOpPreDecrement: increment = -1; break;
2687 default: UNIMPLEMENTED();
2688 }
2689 }
2690 }
2691
2692 if (index != NULL && comparator != EOpNull && increment != 0)
2693 {
2694 if (comparator == EOpLessThanEqual)
2695 {
2696 comparator = EOpLessThan;
2697 limit += 1;
2698 }
2699
2700 if (comparator == EOpLessThan)
2701 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002702 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002703
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002704 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002705 {
2706 return false; // Not an excessive loop
2707 }
2708
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002709 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2710 mExcessiveLoopIndex = index;
2711
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002712 out << "{int ";
2713 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002714 out << ";\n"
2715 "bool Break";
2716 index->traverse(this);
2717 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002718
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002719 bool firstLoopFragment = true;
2720
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002721 while (iterations > 0)
2722 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002723 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002724
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002725 if (!firstLoopFragment)
2726 {
2727 out << "if(!Break";
2728 index->traverse(this);
2729 out << ") {\n";
2730 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002731
2732 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2733 {
2734 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2735 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002736
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002737 // for(int index = initial; index < clampedLimit; index += increment)
2738
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002739 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002740 index->traverse(this);
2741 out << " = ";
2742 out << initial;
2743
2744 out << "; ";
2745 index->traverse(this);
2746 out << " < ";
2747 out << clampedLimit;
2748
2749 out << "; ";
2750 index->traverse(this);
2751 out << " += ";
2752 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002753 out << ")\n";
2754
Jamie Madill075edd82013-07-08 13:30:19 -04002755 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002756 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002757
2758 if (node->getBody())
2759 {
2760 node->getBody()->traverse(this);
2761 }
2762
Jamie Madill075edd82013-07-08 13:30:19 -04002763 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002764 out << ";}\n";
2765
2766 if (!firstLoopFragment)
2767 {
2768 out << "}\n";
2769 }
2770
2771 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002772
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002773 initial += MAX_LOOP_ITERATIONS * increment;
2774 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002775 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002776
2777 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002778
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002779 mExcessiveLoopIndex = restoreIndex;
2780
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002781 return true;
2782 }
2783 else UNIMPLEMENTED();
2784 }
2785
2786 return false; // Not handled as an excessive loop
2787}
2788
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002789void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002790{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002791 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002792
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002793 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002794 {
2795 out << preString;
2796 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002797 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002798 {
2799 out << inString;
2800 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002801 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002802 {
2803 out << postString;
2804 }
2805}
2806
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002807void OutputHLSL::outputLineDirective(int line)
2808{
2809 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2810 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002811 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002812 mBody << "#line " << line;
2813
2814 if (mContext.sourcePath)
2815 {
2816 mBody << " \"" << mContext.sourcePath << "\"";
2817 }
2818
2819 mBody << "\n";
2820 }
2821}
2822
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002823TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2824{
2825 TQualifier qualifier = symbol->getQualifier();
2826 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002827 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002828
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002829 if (name.empty()) // HLSL demands named arguments, also for prototypes
2830 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002831 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002832 }
2833 else
2834 {
2835 name = decorate(name);
2836 }
2837
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002838 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2839 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04002840 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
2841 qualifierString(qualifier) + " " + samplerString(type) + " sampler_" + name + arrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002842 }
2843
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002844 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002845}
2846
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00002847TString OutputHLSL::interpolationString(TQualifier qualifier)
2848{
2849 switch(qualifier)
2850 {
2851 case EvqVaryingIn: return "";
2852 case EvqInvariantVaryingIn: return "";
2853 case EvqSmoothIn: return "linear";
2854 case EvqFlatIn: return "nointerpolation";
2855 case EvqCentroidIn: return "centroid";
2856 case EvqVaryingOut: return "";
2857 case EvqInvariantVaryingOut: return "";
2858 case EvqSmoothOut: return "linear";
2859 case EvqFlatOut: return "nointerpolation";
2860 case EvqCentroidOut: return "centroid";
2861 default: UNREACHABLE();
2862 }
2863
2864 return "";
2865}
2866
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002867TString OutputHLSL::qualifierString(TQualifier qualifier)
2868{
2869 switch(qualifier)
2870 {
2871 case EvqIn: return "in";
2872 case EvqOut: return "out";
2873 case EvqInOut: return "inout";
2874 case EvqConstReadOnly: return "const";
2875 default: UNREACHABLE();
2876 }
2877
2878 return "";
2879}
2880
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002881TString OutputHLSL::typeString(const TType &type)
2882{
Jamie Madill98493dd2013-07-08 14:39:03 -04002883 const TStructure* structure = type.getStruct();
2884 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002885 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002886 const TString& typeName = structure->name();
2887 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002888 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002889 return structLookup(typeName);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002890 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00002891 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002892 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002893 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002894 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002895 }
2896 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002897 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00002898 int cols = type.getCols();
2899 int rows = type.getRows();
2900 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002901 }
2902 else
2903 {
2904 switch (type.getBasicType())
2905 {
2906 case EbtFloat:
2907 switch (type.getNominalSize())
2908 {
2909 case 1: return "float";
2910 case 2: return "float2";
2911 case 3: return "float3";
2912 case 4: return "float4";
2913 }
2914 case EbtInt:
2915 switch (type.getNominalSize())
2916 {
2917 case 1: return "int";
2918 case 2: return "int2";
2919 case 3: return "int3";
2920 case 4: return "int4";
2921 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002922 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04002923 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002924 {
2925 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002926 case 2: return "uint2";
2927 case 3: return "uint3";
2928 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002929 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002930 case EbtBool:
2931 switch (type.getNominalSize())
2932 {
2933 case 1: return "bool";
2934 case 2: return "bool2";
2935 case 3: return "bool3";
2936 case 4: return "bool4";
2937 }
2938 case EbtVoid:
2939 return "void";
2940 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04002941 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04002942 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04002943 case EbtSampler2DArray:
2944 case EbtISampler2DArray:
2945 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002946 return "sampler2D";
2947 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04002948 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04002949 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002950 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00002951 case EbtSamplerExternalOES:
2952 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002953 default:
2954 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002955 }
2956 }
2957
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00002958 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002959 return "<unknown type>";
2960}
2961
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00002962TString OutputHLSL::textureString(const TType &type)
2963{
2964 switch (type.getBasicType())
2965 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04002966 case EbtSampler2D: return "Texture2D";
2967 case EbtSamplerCube: return "TextureCube";
2968 case EbtSamplerExternalOES: return "Texture2D";
2969 case EbtSampler2DArray: return "Texture2DArray";
2970 case EbtSampler3D: return "Texture3D";
2971 case EbtISampler2D: return "Texture2D<int4>";
2972 case EbtISampler3D: return "Texture3D<int4>";
2973 case EbtISamplerCube: return "TextureCube<int4>";
2974 case EbtISampler2DArray: return "Texture2DArray<int4>";
2975 case EbtUSampler2D: return "Texture2D<uint4>";
2976 case EbtUSampler3D: return "Texture3D<uint4>";
2977 case EbtUSamplerCube: return "TextureCube<uint4>";
2978 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
2979 case EbtSampler2DShadow: return "Texture2D";
2980 case EbtSamplerCubeShadow: return "TextureCube";
2981 case EbtSampler2DArrayShadow: return "Texture2DArray";
2982 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00002983 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04002984
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00002985 return "<unknown texture type>";
2986}
2987
Nicolas Capenscb127d32013-07-15 17:26:18 -04002988TString OutputHLSL::samplerString(const TType &type)
2989{
2990 if (IsShadowSampler(type.getBasicType()))
2991 {
2992 return "SamplerComparisonState";
2993 }
2994 else
2995 {
2996 return "SamplerState";
2997 }
2998}
2999
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003000TString OutputHLSL::arrayString(const TType &type)
3001{
3002 if (!type.isArray())
3003 {
3004 return "";
3005 }
3006
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003007 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003008}
3009
3010TString OutputHLSL::initializer(const TType &type)
3011{
3012 TString string;
3013
Jamie Madill94bf7f22013-07-08 13:31:15 -04003014 size_t size = type.getObjectSize();
3015 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003016 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003017 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003018
Jamie Madill94bf7f22013-07-08 13:31:15 -04003019 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003020 {
3021 string += ", ";
3022 }
3023 }
3024
daniel@transgaming.comead23042010-04-29 03:35:36 +00003025 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003026}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003027
Jamie Madill98493dd2013-07-08 14:39:03 -04003028TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003029{
Jamie Madill98493dd2013-07-08 14:39:03 -04003030 const TFieldList &fields = structure.fields();
3031 const bool isNameless = (structure.name() == "");
3032 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003033 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3034
Jamie Madill98493dd2013-07-08 14:39:03 -04003035 TString string;
3036 string += declareString + "\n"
3037 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003038
Jamie Madillc835df62013-06-21 09:15:32 -04003039 int elementIndex = 0;
3040
Jamie Madill9cf6c072013-06-20 11:55:53 -04003041 for (unsigned int i = 0; i < fields.size(); i++)
3042 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003043 const TField &field = *fields[i];
3044 const TType &fieldType = *field.type();
3045 const TStructure *fieldStruct = fieldType.getStruct();
3046 const TString &fieldTypeString = fieldStruct ? structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) : typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003047
Jamie Madillc835df62013-06-21 09:15:32 -04003048 if (useStd140Packing)
3049 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003050 string += std140PrePaddingString(*field.type(), &elementIndex);
Jamie Madillc835df62013-06-21 09:15:32 -04003051 }
3052
Jamie Madill98493dd2013-07-08 14:39:03 -04003053 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003054
3055 if (useStd140Packing)
3056 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003057 string += std140PostPaddingString(*field.type(), useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003058 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003059 }
3060
3061 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003062 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003063
Jamie Madille4075c92013-06-21 09:15:32 -04003064 // Add remaining element index to the global map, for use with nested structs in standard layouts
3065 if (useStd140Packing)
3066 {
3067 mStd140StructElementIndexes[structName] = elementIndex;
3068 }
3069
Jamie Madill98493dd2013-07-08 14:39:03 -04003070 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003071}
3072
Jamie Madill98493dd2013-07-08 14:39:03 -04003073TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003074{
Jamie Madill98493dd2013-07-08 14:39:03 -04003075 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003076 {
3077 return "";
3078 }
3079
3080 TString prefix = "";
3081
3082 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3083 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003084
3085 if (useStd140Packing)
3086 {
3087 prefix += "std";
3088 }
3089
Jamie Madill9cf6c072013-06-20 11:55:53 -04003090 if (useHLSLRowMajorPacking)
3091 {
Jamie Madillc835df62013-06-21 09:15:32 -04003092 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003093 prefix += "rm";
3094 }
3095
Jamie Madill98493dd2013-07-08 14:39:03 -04003096 return prefix + structLookup(structure.name());
Jamie Madill9cf6c072013-06-20 11:55:53 -04003097}
3098
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003099void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003100{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003101 if (name == "")
3102 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003103 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003104 }
3105
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003106 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
3107 {
3108 return; // Already added
3109 }
3110
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003111 TType ctorType = type;
3112 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003113 ctorType.setPrecision(EbpHigh);
3114 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003115
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003116 TString ctorName = type.getStruct() ? decorate(name) : name;
3117
3118 typedef std::vector<TType> ParameterArray;
3119 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003120
Jamie Madill98493dd2013-07-08 14:39:03 -04003121 const TStructure* structure = type.getStruct();
3122 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003123 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003124 mStructNames.insert(decorate(name));
3125
Jamie Madill98493dd2013-07-08 14:39:03 -04003126 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003127
Jamie Madill98493dd2013-07-08 14:39:03 -04003128 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003129 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003130 // Add row-major packed struct for interface blocks
3131 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003132 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003133 "#pragma pack_matrix(column_major)\n";
3134
Jamie Madillc835df62013-06-21 09:15:32 -04003135 const TString &std140Prefix = "std";
Jamie Madill98493dd2013-07-08 14:39:03 -04003136 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003137
3138 const TString &std140RowMajorPrefix = "std_rm";
3139 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003140 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003141 "#pragma pack_matrix(column_major)\n";
3142
Jamie Madill98493dd2013-07-08 14:39:03 -04003143 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003144 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003145 mStructDeclarations.push_back(std140String);
3146 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003147 }
3148
Jamie Madill98493dd2013-07-08 14:39:03 -04003149 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003150 for (unsigned int i = 0; i < fields.size(); i++)
3151 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003152 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003153 }
3154 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003155 else if (parameters)
3156 {
3157 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3158 {
3159 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3160 }
3161 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003162 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003163
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003164 TString constructor;
3165
3166 if (ctorType.getStruct())
3167 {
3168 constructor += ctorName + " " + ctorName + "_ctor(";
3169 }
3170 else // Built-in type
3171 {
3172 constructor += typeString(ctorType) + " " + ctorName + "(";
3173 }
3174
3175 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3176 {
3177 const TType &type = ctorParameters[parameter];
3178
3179 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3180
3181 if (parameter < ctorParameters.size() - 1)
3182 {
3183 constructor += ", ";
3184 }
3185 }
3186
3187 constructor += ")\n"
3188 "{\n";
3189
3190 if (ctorType.getStruct())
3191 {
3192 constructor += " " + ctorName + " structure = {";
3193 }
3194 else
3195 {
3196 constructor += " return " + typeString(ctorType) + "(";
3197 }
3198
3199 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3200 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003201 int rows = ctorType.getRows();
3202 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003203 const TType &parameter = ctorParameters[0];
3204
3205 if (parameter.isScalar())
3206 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003207 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003208 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003209 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003210 {
3211 constructor += TString((row == col) ? "x0" : "0.0");
3212
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003213 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003214 {
3215 constructor += ", ";
3216 }
3217 }
3218 }
3219 }
3220 else if (parameter.isMatrix())
3221 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003222 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003223 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003224 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003225 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003226 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003227 {
3228 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3229 }
3230 else
3231 {
3232 constructor += TString((row == col) ? "1.0" : "0.0");
3233 }
3234
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003235 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003236 {
3237 constructor += ", ";
3238 }
3239 }
3240 }
3241 }
3242 else UNREACHABLE();
3243 }
3244 else
3245 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003246 size_t remainingComponents = ctorType.getObjectSize();
3247 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003248
3249 while (remainingComponents > 0)
3250 {
3251 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003252 const size_t parameterSize = parameter.getObjectSize();
3253 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003254
3255 constructor += "x" + str(parameterIndex);
3256
3257 if (parameter.isScalar())
3258 {
3259 remainingComponents -= parameter.getObjectSize();
3260 }
3261 else if (parameter.isVector())
3262 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003263 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003264 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003265 ASSERT(parameterSize <= remainingComponents);
3266 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003267 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003268 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003269 {
3270 switch (remainingComponents)
3271 {
3272 case 1: constructor += ".x"; break;
3273 case 2: constructor += ".xy"; break;
3274 case 3: constructor += ".xyz"; break;
3275 case 4: constructor += ".xyzw"; break;
3276 default: UNREACHABLE();
3277 }
3278
3279 remainingComponents = 0;
3280 }
3281 else UNREACHABLE();
3282 }
3283 else if (parameter.isMatrix() || parameter.getStruct())
3284 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003285 ASSERT(remainingComponents == parameterSize || moreParameters);
3286 ASSERT(parameterSize <= remainingComponents);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003287
Jamie Madill94bf7f22013-07-08 13:31:15 -04003288 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003289 }
3290 else UNREACHABLE();
3291
3292 if (moreParameters)
3293 {
3294 parameterIndex++;
3295 }
3296
3297 if (remainingComponents)
3298 {
3299 constructor += ", ";
3300 }
3301 }
3302 }
3303
3304 if (ctorType.getStruct())
3305 {
3306 constructor += "};\n"
3307 " return structure;\n"
3308 "}\n";
3309 }
3310 else
3311 {
3312 constructor += ");\n"
3313 "}\n";
3314 }
3315
daniel@transgaming.com63691862010-04-29 03:32:42 +00003316 mConstructors.insert(constructor);
3317}
3318
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003319const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3320{
3321 TInfoSinkBase &out = mBody;
3322
Jamie Madill98493dd2013-07-08 14:39:03 -04003323 const TStructure* structure = type.getStruct();
3324 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003325 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003326 out << structLookup(structure->name()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003327
Jamie Madill98493dd2013-07-08 14:39:03 -04003328 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003329
Jamie Madill98493dd2013-07-08 14:39:03 -04003330 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003331 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003332 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003333
3334 constUnion = writeConstantUnion(*fieldType, constUnion);
3335
Jamie Madill98493dd2013-07-08 14:39:03 -04003336 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003337 {
3338 out << ", ";
3339 }
3340 }
3341
3342 out << ")";
3343 }
3344 else
3345 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003346 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003347 bool writeType = size > 1;
3348
3349 if (writeType)
3350 {
3351 out << typeString(type) << "(";
3352 }
3353
Jamie Madill94bf7f22013-07-08 13:31:15 -04003354 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003355 {
3356 switch (constUnion->getType())
3357 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003358 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003359 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003360 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003361 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003362 default: UNREACHABLE();
3363 }
3364
3365 if (i != size - 1)
3366 {
3367 out << ", ";
3368 }
3369 }
3370
3371 if (writeType)
3372 {
3373 out << ")";
3374 }
3375 }
3376
3377 return constUnion;
3378}
3379
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003380TString OutputHLSL::scopeString(unsigned int depthLimit)
3381{
3382 TString string;
3383
3384 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3385 {
3386 string += "_" + str(i);
3387 }
3388
3389 return string;
3390}
3391
3392TString OutputHLSL::scopedStruct(const TString &typeName)
3393{
3394 if (typeName == "")
3395 {
3396 return typeName;
3397 }
3398
3399 return typeName + scopeString(mScopeDepth);
3400}
3401
3402TString OutputHLSL::structLookup(const TString &typeName)
3403{
3404 for (int depth = mScopeDepth; depth >= 0; depth--)
3405 {
3406 TString scopedName = decorate(typeName + scopeString(depth));
3407
3408 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3409 {
3410 if (*structName == scopedName)
3411 {
3412 return scopedName;
3413 }
3414 }
3415 }
3416
3417 UNREACHABLE(); // Should have found a matching constructor
3418
3419 return typeName;
3420}
3421
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003422TString OutputHLSL::decorate(const TString &string)
3423{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003424 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003425 {
3426 return "_" + string;
3427 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003428
3429 return string;
3430}
3431
apatrick@chromium.org65756022012-01-17 21:45:38 +00003432TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003433{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003434 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003435 {
3436 return "ex_" + string;
3437 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003438
3439 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003440}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003441
Jamie Madill98493dd2013-07-08 14:39:03 -04003442TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003443{
Jamie Madill98493dd2013-07-08 14:39:03 -04003444 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003445 {
3446 return decorate(string);
3447 }
3448
3449 return string;
3450}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003451
3452TString OutputHLSL::registerString(TIntermSymbol *operand)
3453{
3454 ASSERT(operand->getQualifier() == EvqUniform);
3455
3456 if (IsSampler(operand->getBasicType()))
3457 {
3458 return "s" + str(samplerRegister(operand));
3459 }
3460
3461 return "c" + str(uniformRegister(operand));
3462}
3463
3464int OutputHLSL::samplerRegister(TIntermSymbol *sampler)
3465{
3466 const TType &type = sampler->getType();
3467 ASSERT(IsSampler(type.getBasicType()));
3468
3469 int index = mSamplerRegister;
3470 mSamplerRegister += sampler->totalRegisterCount();
3471
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003472 declareUniform(type, sampler->getSymbol(), index);
3473
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003474 return index;
3475}
3476
3477int OutputHLSL::uniformRegister(TIntermSymbol *uniform)
3478{
3479 const TType &type = uniform->getType();
3480 ASSERT(!IsSampler(type.getBasicType()));
3481
3482 int index = mUniformRegister;
3483 mUniformRegister += uniform->totalRegisterCount();
3484
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003485 declareUniform(type, uniform->getSymbol(), index);
3486
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003487 return index;
3488}
3489
Jamie Madill98493dd2013-07-08 14:39:03 -04003490void OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, ActiveUniforms& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003491{
Jamie Madill98493dd2013-07-08 14:39:03 -04003492 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003493
3494 if (!structure)
3495 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003496 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill98493dd2013-07-08 14:39:03 -04003497 output.push_back(Uniform(glVariableType(type), glVariablePrecision(type), name.c_str(), (unsigned int)type.getArraySize(), (unsigned int)registerIndex, isRowMajorMatrix));
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003498 }
3499 else
3500 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003501 Uniform structUniform(GL_NONE, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), (unsigned int)registerIndex, false);
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003502
Jamie Madill98493dd2013-07-08 14:39:03 -04003503 int fieldRegister = registerIndex;
3504 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003505
Jamie Madill98493dd2013-07-08 14:39:03 -04003506 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003507 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003508 TField *field = fields[fieldIndex];
3509 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003510
Jamie Madill010fffa2013-06-20 11:55:53 -04003511 // make sure to copy matrix packing information
Jamie Madill98493dd2013-07-08 14:39:03 -04003512 fieldType->setLayoutQualifier(type.getLayoutQualifier());
Jamie Madill010fffa2013-06-20 11:55:53 -04003513
Jamie Madill98493dd2013-07-08 14:39:03 -04003514 declareUniformToList(*fieldType, field->name(), fieldRegister, structUniform.fields);
3515 fieldRegister += fieldType->totalRegisterCount();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003516 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003517
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003518 output.push_back(structUniform);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003519 }
3520}
3521
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003522void OutputHLSL::declareUniform(const TType &type, const TString &name, int index)
3523{
3524 declareUniformToList(type, name, index, mActiveUniforms);
3525}
3526
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003527GLenum OutputHLSL::glVariableType(const TType &type)
3528{
3529 if (type.getBasicType() == EbtFloat)
3530 {
3531 if (type.isScalar())
3532 {
3533 return GL_FLOAT;
3534 }
3535 else if (type.isVector())
3536 {
3537 switch(type.getNominalSize())
3538 {
3539 case 2: return GL_FLOAT_VEC2;
3540 case 3: return GL_FLOAT_VEC3;
3541 case 4: return GL_FLOAT_VEC4;
3542 default: UNREACHABLE();
3543 }
3544 }
3545 else if (type.isMatrix())
3546 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003547 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003548 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003549 case 2:
3550 switch(type.getRows())
3551 {
3552 case 2: return GL_FLOAT_MAT2;
3553 case 3: return GL_FLOAT_MAT2x3;
3554 case 4: return GL_FLOAT_MAT2x4;
3555 default: UNREACHABLE();
3556 }
3557
3558 case 3:
3559 switch(type.getRows())
3560 {
3561 case 2: return GL_FLOAT_MAT3x2;
3562 case 3: return GL_FLOAT_MAT3;
3563 case 4: return GL_FLOAT_MAT3x4;
3564 default: UNREACHABLE();
3565 }
3566
3567 case 4:
3568 switch(type.getRows())
3569 {
3570 case 2: return GL_FLOAT_MAT4x2;
3571 case 3: return GL_FLOAT_MAT4x3;
3572 case 4: return GL_FLOAT_MAT4;
3573 default: UNREACHABLE();
3574 }
3575
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003576 default: UNREACHABLE();
3577 }
3578 }
3579 else UNREACHABLE();
3580 }
3581 else if (type.getBasicType() == EbtInt)
3582 {
3583 if (type.isScalar())
3584 {
3585 return GL_INT;
3586 }
3587 else if (type.isVector())
3588 {
3589 switch(type.getNominalSize())
3590 {
3591 case 2: return GL_INT_VEC2;
3592 case 3: return GL_INT_VEC3;
3593 case 4: return GL_INT_VEC4;
3594 default: UNREACHABLE();
3595 }
3596 }
3597 else UNREACHABLE();
3598 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003599 else if (type.getBasicType() == EbtUInt)
3600 {
3601 if (type.isScalar())
3602 {
3603 return GL_UNSIGNED_INT;
3604 }
3605 else if (type.isVector())
3606 {
Jamie Madill22d63da2013-06-07 12:45:12 -04003607 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003608 {
3609 case 2: return GL_UNSIGNED_INT_VEC2;
3610 case 3: return GL_UNSIGNED_INT_VEC3;
3611 case 4: return GL_UNSIGNED_INT_VEC4;
3612 default: UNREACHABLE();
3613 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003614 }
3615 else UNREACHABLE();
3616 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003617 else if (type.getBasicType() == EbtBool)
3618 {
3619 if (type.isScalar())
3620 {
3621 return GL_BOOL;
3622 }
3623 else if (type.isVector())
3624 {
3625 switch(type.getNominalSize())
3626 {
3627 case 2: return GL_BOOL_VEC2;
3628 case 3: return GL_BOOL_VEC3;
3629 case 4: return GL_BOOL_VEC4;
3630 default: UNREACHABLE();
3631 }
3632 }
3633 else UNREACHABLE();
3634 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003635
3636 switch(type.getBasicType())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003637 {
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003638 case EbtSampler2D: return GL_SAMPLER_2D;
3639 case EbtSampler3D: return GL_SAMPLER_3D;
3640 case EbtSamplerCube: return GL_SAMPLER_CUBE;
3641 case EbtSampler2DArray: return GL_SAMPLER_2D_ARRAY;
3642 case EbtISampler2D: return GL_INT_SAMPLER_2D;
3643 case EbtISampler3D: return GL_INT_SAMPLER_3D;
3644 case EbtISamplerCube: return GL_INT_SAMPLER_CUBE;
3645 case EbtISampler2DArray: return GL_INT_SAMPLER_2D_ARRAY;
3646 case EbtUSampler2D: return GL_UNSIGNED_INT_SAMPLER_2D;
3647 case EbtUSampler3D: return GL_UNSIGNED_INT_SAMPLER_3D;
3648 case EbtUSamplerCube: return GL_UNSIGNED_INT_SAMPLER_CUBE;
3649 case EbtUSampler2DArray: return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3650 case EbtSampler2DShadow: return GL_SAMPLER_2D_SHADOW;
3651 case EbtSamplerCubeShadow: return GL_SAMPLER_CUBE_SHADOW;
3652 case EbtSampler2DArrayShadow: return GL_SAMPLER_2D_ARRAY_SHADOW;
3653 default: UNREACHABLE();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003654 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003655
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003656
3657 return GL_NONE;
3658}
3659
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003660GLenum OutputHLSL::glVariablePrecision(const TType &type)
3661{
3662 if (type.getBasicType() == EbtFloat)
3663 {
3664 switch (type.getPrecision())
3665 {
3666 case EbpHigh: return GL_HIGH_FLOAT;
3667 case EbpMedium: return GL_MEDIUM_FLOAT;
3668 case EbpLow: return GL_LOW_FLOAT;
3669 case EbpUndefined:
3670 // Should be defined as the default precision by the parser
3671 default: UNREACHABLE();
3672 }
3673 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003674 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003675 {
3676 switch (type.getPrecision())
3677 {
3678 case EbpHigh: return GL_HIGH_INT;
3679 case EbpMedium: return GL_MEDIUM_INT;
3680 case EbpLow: return GL_LOW_INT;
3681 case EbpUndefined:
3682 // Should be defined as the default precision by the parser
3683 default: UNREACHABLE();
3684 }
3685 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003686
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00003687 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003688 return GL_NONE;
3689}
3690
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003691bool OutputHLSL::isVaryingOut(TQualifier qualifier)
3692{
3693 switch(qualifier)
3694 {
3695 case EvqVaryingOut:
3696 case EvqInvariantVaryingOut:
3697 case EvqSmoothOut:
3698 case EvqFlatOut:
3699 case EvqCentroidOut:
3700 return true;
3701 }
3702
3703 return false;
3704}
3705
3706bool OutputHLSL::isVaryingIn(TQualifier qualifier)
3707{
3708 switch(qualifier)
3709 {
3710 case EvqVaryingIn:
3711 case EvqInvariantVaryingIn:
3712 case EvqSmoothIn:
3713 case EvqFlatIn:
3714 case EvqCentroidIn:
3715 return true;
3716 }
3717
3718 return false;
3719}
3720
3721bool OutputHLSL::isVarying(TQualifier qualifier)
3722{
3723 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
3724}
3725
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003726}