blob: 4ec5b2b0be2b4512bca7211f2efc48eca395b29e [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);
Jamie Madill9060a4e2013-08-12 16:22:57 -0700569
570 if (interfaceBlock.matrixPacking() == EmpRowMajor)
571 {
572 activeBlock.isRowMajorLayout = true;
573 }
574
Jamie Madill98493dd2013-07-08 14:39:03 -0400575 mActiveInterfaceBlocks.push_back(activeBlock);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000576
Jamie Madill98493dd2013-07-08 14:39:03 -0400577 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000578 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400579 interfaceBlocks += interfaceBlockStructString(interfaceBlock);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000580 }
581
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000582 if (arraySize > 0)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000583 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000584 for (unsigned int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
585 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400586 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex + arrayIndex, arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000587 }
588 }
589 else
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000590 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400591 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000592 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000593 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000594
Jamie Madill570e04d2013-06-21 09:15:33 -0400595 for (auto flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
596 {
597 TIntermTyped *structNode = flaggedStructIt->first;
598 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400599 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400600 const TString &originalName = mFlaggedStructOriginalNames[structNode];
601
Jamie Madill98493dd2013-07-08 14:39:03 -0400602 flaggedStructs += "static " + decorate(structure.name()) + " " + mappedName + " =\n";
603 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400604 flaggedStructs += "\n";
605 }
606
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000607 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
608 {
609 const TType &type = varying->second->getType();
610 const TString &name = varying->second->getSymbol();
611
612 // Program linking depends on this exact format
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +0000613 varyings += "static " + interpolationString(type.getQualifier()) + " " + typeString(type) + " " +
614 decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000615 }
616
617 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
618 {
619 const TType &type = attribute->second->getType();
620 const TString &name = attribute->second->getSymbol();
621
622 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400623
624 ShaderVariable shaderVar(glVariableType(type), glVariablePrecision(type), name.c_str(),
625 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
626 mActiveAttributes.push_back(shaderVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000627 }
628
Jamie Madill529077d2013-06-20 11:55:54 -0400629 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
630 {
631 out << *structDeclaration;
632 }
633
634 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
635 {
636 out << *constructor;
637 }
638
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400639 if (mContext.shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000640 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000641 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000642 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000643
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000644 out << "// Varyings\n";
645 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400646 out << "\n";
647
648 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000649 {
Jamie Madill46131a32013-06-20 11:55:50 -0400650 for (auto outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000651 {
Jamie Madill46131a32013-06-20 11:55:50 -0400652 const TString &variableName = outputVariableIt->first;
653 const TType &variableType = outputVariableIt->second->getType();
654 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
655
656 out << "static " + typeString(variableType) + " out_" + variableName + arrayString(variableType) +
657 " = " + initializer(variableType) + ";\n";
658
659 ShaderVariable outputVar(glVariableType(variableType), glVariablePrecision(variableType), variableName.c_str(),
660 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
661 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000662 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000663 }
Jamie Madill46131a32013-06-20 11:55:50 -0400664 else
665 {
666 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
667
668 out << "static float4 gl_Color[" << numColorValues << "] =\n"
669 "{\n";
670 for (unsigned int i = 0; i < numColorValues; i++)
671 {
672 out << " float4(0, 0, 0, 0)";
673 if (i + 1 != numColorValues)
674 {
675 out << ",";
676 }
677 out << "\n";
678 }
679
680 out << "};\n";
681 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000682
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400683 if (mUsesFragDepth)
684 {
685 out << "static float gl_Depth = 0.0;\n";
686 }
687
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000688 if (mUsesFragCoord)
689 {
690 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
691 }
692
693 if (mUsesPointCoord)
694 {
695 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
696 }
697
698 if (mUsesFrontFacing)
699 {
700 out << "static bool gl_FrontFacing = false;\n";
701 }
702
703 out << "\n";
704
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000705 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000706 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000707 out << "struct gl_DepthRangeParameters\n"
708 "{\n"
709 " float near;\n"
710 " float far;\n"
711 " float diff;\n"
712 "};\n"
713 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000714 }
715
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000716 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000717 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000718 out << "cbuffer DriverConstants : register(b1)\n"
719 "{\n";
720
721 if (mUsesDepthRange)
722 {
723 out << " float3 dx_DepthRange : packoffset(c0);\n";
724 }
725
726 if (mUsesFragCoord)
727 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000728 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000729 }
730
731 if (mUsesFragCoord || mUsesFrontFacing)
732 {
733 out << " float3 dx_DepthFront : packoffset(c2);\n";
734 }
735
736 out << "};\n";
737 }
738 else
739 {
740 if (mUsesDepthRange)
741 {
742 out << "uniform float3 dx_DepthRange : register(c0);";
743 }
744
745 if (mUsesFragCoord)
746 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000747 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000748 }
749
750 if (mUsesFragCoord || mUsesFrontFacing)
751 {
752 out << "uniform float3 dx_DepthFront : register(c2);\n";
753 }
754 }
755
756 out << "\n";
757
758 if (mUsesDepthRange)
759 {
760 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
761 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000762 }
763
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000764 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000765 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000766
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000767 if (!interfaceBlocks.empty())
768 {
769 out << interfaceBlocks;
770 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400771
772 if (!flaggedStructs.empty())
773 {
774 out << "// Std140 Structures accessed by value\n";
775 out << "\n";
776 out << flaggedStructs;
777 out << "\n";
778 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000779 }
780
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000781 if (usingMRTExtension && mNumRenderTargets > 1)
782 {
783 out << "#define GL_USES_MRT\n";
784 }
785
786 if (mUsesFragColor)
787 {
788 out << "#define GL_USES_FRAG_COLOR\n";
789 }
790
791 if (mUsesFragData)
792 {
793 out << "#define GL_USES_FRAG_DATA\n";
794 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000795 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000796 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000797 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000798 out << "// Attributes\n";
799 out << attributes;
800 out << "\n"
801 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
802
803 if (mUsesPointSize)
804 {
805 out << "static float gl_PointSize = float(1);\n";
806 }
807
808 out << "\n"
809 "// Varyings\n";
810 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000811 out << "\n";
812
813 if (mUsesDepthRange)
814 {
815 out << "struct gl_DepthRangeParameters\n"
816 "{\n"
817 " float near;\n"
818 " float far;\n"
819 " float diff;\n"
820 "};\n"
821 "\n";
822 }
823
824 if (mOutputType == SH_HLSL11_OUTPUT)
825 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000826 if (mUsesDepthRange)
827 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000828 out << "cbuffer DriverConstants : register(b1)\n"
829 "{\n"
830 " float3 dx_DepthRange : packoffset(c0);\n"
831 "};\n"
832 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000833 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000834 }
835 else
836 {
837 if (mUsesDepthRange)
838 {
839 out << "uniform float3 dx_DepthRange : register(c0);\n";
840 }
841
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000842 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000843 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000844 }
845
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000846 if (mUsesDepthRange)
847 {
848 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
849 "\n";
850 }
851
852 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000854
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000855 if (!interfaceBlocks.empty())
856 {
857 out << interfaceBlocks;
858 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400859
860 if (!flaggedStructs.empty())
861 {
862 out << "// Std140 Structures accessed by value\n";
863 out << "\n";
864 out << flaggedStructs;
865 out << "\n";
866 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000867 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400868 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000869
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400870 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
871 {
872 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400873 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000874 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400875 switch(textureFunction->sampler)
876 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400877 case EbtSampler2D: out << "int2 "; break;
878 case EbtSampler3D: out << "int3 "; break;
879 case EbtSamplerCube: out << "int2 "; break;
880 case EbtSampler2DArray: out << "int3 "; break;
881 case EbtISampler2D: out << "int2 "; break;
882 case EbtISampler3D: out << "int3 "; break;
883 case EbtISamplerCube: out << "int2 "; break;
884 case EbtISampler2DArray: out << "int3 "; break;
885 case EbtUSampler2D: out << "int2 "; break;
886 case EbtUSampler3D: out << "int3 "; break;
887 case EbtUSamplerCube: out << "int2 "; break;
888 case EbtUSampler2DArray: out << "int3 "; break;
889 case EbtSampler2DShadow: out << "int2 "; break;
890 case EbtSamplerCubeShadow: out << "int2 "; break;
891 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400892 default: UNREACHABLE();
893 }
894 }
895 else // Sampling function
896 {
897 switch(textureFunction->sampler)
898 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400899 case EbtSampler2D: out << "float4 "; break;
900 case EbtSampler3D: out << "float4 "; break;
901 case EbtSamplerCube: out << "float4 "; break;
902 case EbtSampler2DArray: out << "float4 "; break;
903 case EbtISampler2D: out << "int4 "; break;
904 case EbtISampler3D: out << "int4 "; break;
905 case EbtISamplerCube: out << "int4 "; break;
906 case EbtISampler2DArray: out << "int4 "; break;
907 case EbtUSampler2D: out << "uint4 "; break;
908 case EbtUSampler3D: out << "uint4 "; break;
909 case EbtUSamplerCube: out << "uint4 "; break;
910 case EbtUSampler2DArray: out << "uint4 "; break;
911 case EbtSampler2DShadow: out << "float "; break;
912 case EbtSamplerCubeShadow: out << "float "; break;
913 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400914 default: UNREACHABLE();
915 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000916 }
917
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400918 // Function name
919 out << textureFunction->name();
920
921 // Argument list
922 int hlslCoords = 4;
923
924 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000925 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400926 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000927 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400928 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
929 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
930 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000931 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400932
Nicolas Capens75fb4752013-07-10 15:14:47 -0400933 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000934 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400935 case TextureFunction::IMPLICIT: break;
936 case TextureFunction::BIAS: hlslCoords = 4; break;
937 case TextureFunction::LOD: hlslCoords = 4; break;
938 case TextureFunction::LOD0: hlslCoords = 4; break;
939 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000940 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400941 }
942 else if (mOutputType == SH_HLSL11_OUTPUT)
943 {
944 switch(textureFunction->sampler)
945 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400946 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
947 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
948 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
949 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
950 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
951 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
952 case EbtISamplerCube: out << "TextureCube<int4> x, SamplerState s"; hlslCoords = 3; break;
953 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
954 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
955 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
956 case EbtUSamplerCube: out << "TextureCube<uint4> x, SamplerState s"; hlslCoords = 3; break;
957 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
958 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
959 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
960 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400961 default: UNREACHABLE();
962 }
963 }
964 else UNREACHABLE();
965
966 switch(textureFunction->coords)
967 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400968 case 1: out << ", int lod"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400969 case 2: out << ", float2 t"; break;
970 case 3: out << ", float3 t"; break;
971 case 4: out << ", float4 t"; break;
972 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000973 }
974
Nicolas Capens75fb4752013-07-10 15:14:47 -0400975 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000976 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400977 case TextureFunction::IMPLICIT: break;
978 case TextureFunction::BIAS: out << ", float bias"; break;
979 case TextureFunction::LOD: out << ", float lod"; break;
980 case TextureFunction::LOD0: break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400981 case TextureFunction::SIZE: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400982 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000983 }
984
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400985 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400986 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400987
Nicolas Capens75fb4752013-07-10 15:14:47 -0400988 if (textureFunction->method == TextureFunction::SIZE)
989 {
990 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
991 {
992 if (IsSamplerArray(textureFunction->sampler))
993 {
994 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
995 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
996 }
997 else
998 {
999 out << " uint width; uint height; uint numberOfLevels;\n"
1000 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
1001 }
1002 }
1003 else if (IsSampler3D(textureFunction->sampler))
1004 {
1005 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
1006 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
1007 }
1008 else UNREACHABLE();
1009
1010 switch(textureFunction->sampler)
1011 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04001012 case EbtSampler2D: out << " return int2(width, height);"; break;
1013 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
1014 case EbtSamplerCube: out << " return int2(width, height);"; break;
1015 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
1016 case EbtISampler2D: out << " return int2(width, height);"; break;
1017 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
1018 case EbtISamplerCube: out << " return int2(width, height);"; break;
1019 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
1020 case EbtUSampler2D: out << " return int2(width, height);"; break;
1021 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
1022 case EbtUSamplerCube: out << " return int2(width, height);"; break;
1023 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
1024 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
1025 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
1026 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001027 default: UNREACHABLE();
1028 }
1029 }
1030 else if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
daniel@transgaming.com15795192011-05-11 15:36:20 +00001031 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001032 // Currently unsupported because TextureCube does not support Load
1033 // This will require emulation using a Texture2DArray with 6 faces
1034 if (textureFunction->sampler == EbtISamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001035 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001036 out << " return int4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001037 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001038 else if (textureFunction->sampler == EbtUSamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001039 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001040 out << " return uint4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001041 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001042 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001043 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001044 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001045 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001046 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001047 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001048 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001049 {
Nicolas Capens93e50de2013-07-09 13:46:28 -04001050 if (IsSamplerArray(textureFunction->sampler))
1051 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001052 out << " float width; float height; float layers; float levels;\n";
1053
1054 if (textureFunction->method == TextureFunction::LOD0)
1055 {
1056 out << " uint mip = 0;\n";
1057 }
1058 else
1059 {
1060 if (textureFunction->method == TextureFunction::IMPLICIT ||
1061 textureFunction->method == TextureFunction::BIAS)
1062 {
1063 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1064 " float2 tSized = float2(t.x * width, t.y * height);\n"
1065 " float dx = length(ddx(tSized));\n"
1066 " float dy = length(ddy(tSized));\n"
1067 " float lod = log2(max(sqrt(dx), sqrt(dy)));\n";
1068
1069 if (textureFunction->method == TextureFunction::BIAS)
1070 {
1071 out << " lod += bias;\n";
1072 }
1073 }
1074
1075 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1076 }
1077
1078 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001079 }
1080 else
1081 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001082 out << " float width; float height; float levels;\n";
1083
1084 if (textureFunction->method == TextureFunction::LOD0)
1085 {
1086 out << " uint mip = 0;\n";
1087 }
1088 else
1089 {
1090 if (textureFunction->method == TextureFunction::IMPLICIT ||
1091 textureFunction->method == TextureFunction::BIAS)
1092 {
1093 out << " x.GetDimensions(0, width, height, levels);\n"
1094 " float2 tSized = float2(t.x * width, t.y * height);\n"
1095 " float dx = length(ddx(tSized));\n"
1096 " float dy = length(ddy(tSized));\n"
1097 " float lod = log2(max(sqrt(dx), sqrt(dy)));\n";
1098
1099 if (textureFunction->method == TextureFunction::BIAS)
1100 {
1101 out << " lod += bias;\n";
1102 }
1103 }
1104
1105 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1106 }
1107
1108 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001109 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001110 }
1111 else if (IsSampler3D(textureFunction->sampler))
1112 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001113 out << " float width; float height; float depth; float levels;\n";
1114
1115 if (textureFunction->method == TextureFunction::LOD0)
1116 {
1117 out << " uint mip = 0;\n";
1118 }
1119 else
1120 {
1121 if (textureFunction->method == TextureFunction::IMPLICIT ||
1122 textureFunction->method == TextureFunction::BIAS)
1123 {
1124 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1125 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
1126 " float dx = length(ddx(tSized));\n"
1127 " float dy = length(ddy(tSized));\n"
1128 " float lod = log2(max(sqrt(dx), sqrt(dy)));\n";
1129
1130 if (textureFunction->method == TextureFunction::BIAS)
1131 {
1132 out << " lod += bias;\n";
1133 }
1134 }
1135
1136 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1137 }
1138
1139 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001140 }
1141 else UNREACHABLE();
1142 }
1143
1144 out << " return ";
1145
1146 // HLSL intrinsic
1147 if (mOutputType == SH_HLSL9_OUTPUT)
1148 {
1149 switch(textureFunction->sampler)
1150 {
1151 case EbtSampler2D: out << "tex2D"; break;
1152 case EbtSamplerCube: out << "texCUBE"; break;
1153 default: UNREACHABLE();
1154 }
1155
Nicolas Capens75fb4752013-07-10 15:14:47 -04001156 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001157 {
1158 case TextureFunction::IMPLICIT: out << "(s, "; break;
1159 case TextureFunction::BIAS: out << "bias(s, "; break;
1160 case TextureFunction::LOD: out << "lod(s, "; break;
1161 case TextureFunction::LOD0: out << "lod(s, "; break;
1162 default: UNREACHABLE();
1163 }
1164 }
1165 else if (mOutputType == SH_HLSL11_OUTPUT)
1166 {
1167 if (IsIntegerSampler(textureFunction->sampler))
1168 {
1169 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001170 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001171 else if (IsShadowSampler(textureFunction->sampler))
1172 {
1173 out << "x.SampleCmp(s, ";
1174 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001175 else
1176 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001177 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001178 {
1179 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1180 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1181 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1182 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
1183 default: UNREACHABLE();
1184 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001185 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001186 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001187 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001188
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001189 // Integer sampling requires integer addresses
1190 TString addressx = "";
1191 TString addressy = "";
1192 TString addressz = "";
1193 TString close = "";
1194
1195 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001196 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001197 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001198 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001199 case 2: out << "int3("; break;
1200 case 3: out << "int4("; break;
1201 default: UNREACHABLE();
1202 }
1203
Nicolas Capensc98406a2013-07-10 14:52:44 -04001204 addressx = "int(floor(width * frac((";
1205 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001206
1207 if (IsSamplerArray(textureFunction->sampler))
1208 {
1209 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1210 }
1211 else
1212 {
Nicolas Capensc98406a2013-07-10 14:52:44 -04001213 addressz = "int(floor(depth * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001214 }
1215
1216 close = "))))";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001217 }
1218 else
1219 {
1220 switch(hlslCoords)
1221 {
1222 case 2: out << "float2("; break;
1223 case 3: out << "float3("; break;
1224 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001225 default: UNREACHABLE();
1226 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001227 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001228
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001229 TString proj = ""; // Only used for projected textures
1230
1231 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001232 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001233 switch(textureFunction->coords)
1234 {
1235 case 3: proj = " / t.z"; break;
1236 case 4: proj = " / t.w"; break;
1237 default: UNREACHABLE();
1238 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001239 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001240
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001241 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001242
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001243 if (mOutputType == SH_HLSL9_OUTPUT)
1244 {
1245 if (hlslCoords >= 3)
1246 {
1247 if (textureFunction->coords < 3)
1248 {
1249 out << ", 0";
1250 }
1251 else
1252 {
1253 out << ", t.z" + proj;
1254 }
1255 }
1256
1257 if (hlslCoords == 4)
1258 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001259 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001260 {
1261 case TextureFunction::BIAS: out << ", bias"; break;
1262 case TextureFunction::LOD: out << ", lod"; break;
1263 case TextureFunction::LOD0: out << ", 0"; break;
1264 default: UNREACHABLE();
1265 }
1266 }
1267
1268 out << "));\n";
1269 }
1270 else if (mOutputType == SH_HLSL11_OUTPUT)
1271 {
1272 if (hlslCoords >= 3)
1273 {
1274 out << ", " + addressz + ("t.z" + proj) + close;
1275 }
1276
Nicolas Capenscb127d32013-07-15 17:26:18 -04001277 if (IsIntegerSampler(textureFunction->sampler))
1278 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001279 out << ", mip));";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001280 }
1281 else if (IsShadowSampler(textureFunction->sampler))
1282 {
1283 // Compare value
1284 switch(textureFunction->coords)
1285 {
1286 case 3: out << "), t.z);"; break;
1287 case 4: out << "), t.w);"; break;
1288 default: UNREACHABLE();
1289 }
1290 }
1291 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001292 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001293 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001294 {
1295 case TextureFunction::IMPLICIT: out << "));"; break;
1296 case TextureFunction::BIAS: out << "), bias);"; break;
1297 case TextureFunction::LOD: out << "), lod);"; break;
1298 case TextureFunction::LOD0: out << "), 0);"; break;
1299 default: UNREACHABLE();
1300 }
1301 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001302 }
1303 else UNREACHABLE();
1304 }
1305
1306 out << "\n"
1307 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001308 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001309 }
1310
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001311 if (mUsesFragCoord)
1312 {
1313 out << "#define GL_USES_FRAG_COORD\n";
1314 }
1315
1316 if (mUsesPointCoord)
1317 {
1318 out << "#define GL_USES_POINT_COORD\n";
1319 }
1320
1321 if (mUsesFrontFacing)
1322 {
1323 out << "#define GL_USES_FRONT_FACING\n";
1324 }
1325
1326 if (mUsesPointSize)
1327 {
1328 out << "#define GL_USES_POINT_SIZE\n";
1329 }
1330
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001331 if (mUsesFragDepth)
1332 {
1333 out << "#define GL_USES_FRAG_DEPTH\n";
1334 }
1335
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001336 if (mUsesDepthRange)
1337 {
1338 out << "#define GL_USES_DEPTH_RANGE\n";
1339 }
1340
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001341 if (mUsesXor)
1342 {
1343 out << "bool xor(bool p, bool q)\n"
1344 "{\n"
1345 " return (p || q) && !(p && q);\n"
1346 "}\n"
1347 "\n";
1348 }
1349
1350 if (mUsesMod1)
1351 {
1352 out << "float mod(float x, float y)\n"
1353 "{\n"
1354 " return x - y * floor(x / y);\n"
1355 "}\n"
1356 "\n";
1357 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001358
1359 if (mUsesMod2v)
1360 {
1361 out << "float2 mod(float2 x, float2 y)\n"
1362 "{\n"
1363 " return x - y * floor(x / y);\n"
1364 "}\n"
1365 "\n";
1366 }
1367
1368 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001369 {
1370 out << "float2 mod(float2 x, float y)\n"
1371 "{\n"
1372 " return x - y * floor(x / y);\n"
1373 "}\n"
1374 "\n";
1375 }
1376
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001377 if (mUsesMod3v)
1378 {
1379 out << "float3 mod(float3 x, float3 y)\n"
1380 "{\n"
1381 " return x - y * floor(x / y);\n"
1382 "}\n"
1383 "\n";
1384 }
1385
1386 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001387 {
1388 out << "float3 mod(float3 x, float y)\n"
1389 "{\n"
1390 " return x - y * floor(x / y);\n"
1391 "}\n"
1392 "\n";
1393 }
1394
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001395 if (mUsesMod4v)
1396 {
1397 out << "float4 mod(float4 x, float4 y)\n"
1398 "{\n"
1399 " return x - y * floor(x / y);\n"
1400 "}\n"
1401 "\n";
1402 }
1403
1404 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001405 {
1406 out << "float4 mod(float4 x, float y)\n"
1407 "{\n"
1408 " return x - y * floor(x / y);\n"
1409 "}\n"
1410 "\n";
1411 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001412
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001413 if (mUsesFaceforward1)
1414 {
1415 out << "float faceforward(float N, float I, float Nref)\n"
1416 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001417 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001418 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001419 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001420 " }\n"
1421 " else\n"
1422 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001423 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001424 " }\n"
1425 "}\n"
1426 "\n";
1427 }
1428
1429 if (mUsesFaceforward2)
1430 {
1431 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1432 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001433 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001434 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001435 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001436 " }\n"
1437 " else\n"
1438 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001439 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001440 " }\n"
1441 "}\n"
1442 "\n";
1443 }
1444
1445 if (mUsesFaceforward3)
1446 {
1447 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1448 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001449 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001450 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001451 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001452 " }\n"
1453 " else\n"
1454 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001455 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001456 " }\n"
1457 "}\n"
1458 "\n";
1459 }
1460
1461 if (mUsesFaceforward4)
1462 {
1463 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1464 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001465 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001466 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001467 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001468 " }\n"
1469 " else\n"
1470 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001471 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001472 " }\n"
1473 "}\n"
1474 "\n";
1475 }
1476
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001477 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001478 {
1479 out << "float atanyx(float y, float x)\n"
1480 "{\n"
1481 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1482 " return atan2(y, x);\n"
1483 "}\n";
1484 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001485
1486 if (mUsesAtan2_2)
1487 {
1488 out << "float2 atanyx(float2 y, float2 x)\n"
1489 "{\n"
1490 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1491 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1492 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1493 "}\n";
1494 }
1495
1496 if (mUsesAtan2_3)
1497 {
1498 out << "float3 atanyx(float3 y, float3 x)\n"
1499 "{\n"
1500 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1501 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1502 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1503 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1504 "}\n";
1505 }
1506
1507 if (mUsesAtan2_4)
1508 {
1509 out << "float4 atanyx(float4 y, float4 x)\n"
1510 "{\n"
1511 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1512 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1513 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1514 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1515 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1516 "}\n";
1517 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001518}
1519
1520void OutputHLSL::visitSymbol(TIntermSymbol *node)
1521{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001522 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001523
Jamie Madill570e04d2013-06-21 09:15:33 -04001524 // Handle accessing std140 structs by value
1525 if (mFlaggedStructMappedNames.count(node) > 0)
1526 {
1527 out << mFlaggedStructMappedNames[node];
1528 return;
1529 }
1530
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001531 TString name = node->getSymbol();
1532
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001533 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001534 {
1535 mUsesDepthRange = true;
1536 out << name;
1537 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001538 else
1539 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001540 TQualifier qualifier = node->getQualifier();
1541
1542 if (qualifier == EvqUniform)
1543 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001544 const TType& nodeType = node->getType();
1545 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1546
1547 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001548 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001549 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001550 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001551 else
1552 {
1553 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001554 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001555
1556 out << decorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001557 }
Jamie Madill19571812013-08-12 15:26:34 -07001558 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001559 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001560 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001561 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001562 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001563 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001564 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001565 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001566 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001567 }
Jamie Madill19571812013-08-12 15:26:34 -07001568 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001569 {
1570 mReferencedOutputVariables[name] = node;
1571 out << "out_" << name;
1572 }
1573 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001574 {
1575 out << "gl_Color[0]";
1576 mUsesFragColor = true;
1577 }
1578 else if (qualifier == EvqFragData)
1579 {
1580 out << "gl_Color";
1581 mUsesFragData = true;
1582 }
1583 else if (qualifier == EvqFragCoord)
1584 {
1585 mUsesFragCoord = true;
1586 out << name;
1587 }
1588 else if (qualifier == EvqPointCoord)
1589 {
1590 mUsesPointCoord = true;
1591 out << name;
1592 }
1593 else if (qualifier == EvqFrontFacing)
1594 {
1595 mUsesFrontFacing = true;
1596 out << name;
1597 }
1598 else if (qualifier == EvqPointSize)
1599 {
1600 mUsesPointSize = true;
1601 out << name;
1602 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001603 else if (name == "gl_FragDepthEXT")
1604 {
1605 mUsesFragDepth = true;
1606 out << "gl_Depth";
1607 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001608 else
1609 {
1610 out << decorate(name);
1611 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001612 }
1613}
1614
1615bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1616{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001617 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001618
Jamie Madill570e04d2013-06-21 09:15:33 -04001619 // Handle accessing std140 structs by value
1620 if (mFlaggedStructMappedNames.count(node) > 0)
1621 {
1622 out << mFlaggedStructMappedNames[node];
1623 return false;
1624 }
1625
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001626 switch (node->getOp())
1627 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001628 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001629 case EOpInitialize:
1630 if (visit == PreVisit)
1631 {
1632 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1633 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1634 // new variable is created before the assignment is evaluated), so we need to convert
1635 // this to "float t = x, x = t;".
1636
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001637 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1638 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001639
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001640 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1641 expression->traverse(&searchSymbol);
1642 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001643
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001644 if (sameSymbol)
1645 {
1646 // Type already printed
1647 out << "t" + str(mUniqueIndex) + " = ";
1648 expression->traverse(this);
1649 out << ", ";
1650 symbolNode->traverse(this);
1651 out << " = t" + str(mUniqueIndex);
1652
1653 mUniqueIndex++;
1654 return false;
1655 }
1656 }
1657 else if (visit == InVisit)
1658 {
1659 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001660 }
1661 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001662 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1663 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1664 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1665 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1666 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1667 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001668 if (visit == PreVisit)
1669 {
1670 out << "(";
1671 }
1672 else if (visit == InVisit)
1673 {
1674 out << " = mul(";
1675 node->getLeft()->traverse(this);
1676 out << ", transpose(";
1677 }
1678 else
1679 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001680 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001681 }
1682 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001683 case EOpMatrixTimesMatrixAssign:
1684 if (visit == PreVisit)
1685 {
1686 out << "(";
1687 }
1688 else if (visit == InVisit)
1689 {
1690 out << " = mul(";
1691 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001692 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001693 }
1694 else
1695 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001696 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001697 }
1698 break;
1699 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001700 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001701 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001702 const TType& leftType = node->getLeft()->getType();
1703 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001704 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001705 if (visit == PreVisit)
1706 {
1707 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1708 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1709
1710 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1711 out << interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1712
1713 return false;
1714 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001715 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001716 else
1717 {
1718 outputTriplet(visit, "", "[", "]");
1719 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001720 }
1721 break;
1722 case EOpIndexIndirect:
1723 // We do not currently support indirect references to interface blocks
1724 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1725 outputTriplet(visit, "", "[", "]");
1726 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001727 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001728 if (visit == InVisit)
1729 {
1730 const TStructure* structure = node->getLeft()->getType().getStruct();
1731 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1732 const TField* field = structure->fields()[index->getIConst(0)];
1733 out << "." + decorateField(field->name(), *structure);
1734
1735 return false;
1736 }
1737 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001738 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001739 if (visit == InVisit)
1740 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001741 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1742 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1743 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
1744 out << "." + decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001745
1746 return false;
1747 }
1748 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001749 case EOpVectorSwizzle:
1750 if (visit == InVisit)
1751 {
1752 out << ".";
1753
1754 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1755
1756 if (swizzle)
1757 {
1758 TIntermSequence &sequence = swizzle->getSequence();
1759
1760 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1761 {
1762 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1763
1764 if (element)
1765 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001766 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001767
1768 switch (i)
1769 {
1770 case 0: out << "x"; break;
1771 case 1: out << "y"; break;
1772 case 2: out << "z"; break;
1773 case 3: out << "w"; break;
1774 default: UNREACHABLE();
1775 }
1776 }
1777 else UNREACHABLE();
1778 }
1779 }
1780 else UNREACHABLE();
1781
1782 return false; // Fully processed
1783 }
1784 break;
1785 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1786 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1787 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1788 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001789 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001790 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001791 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001792 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001793 if (node->getOp() == EOpEqual)
1794 {
1795 outputTriplet(visit, "(", " == ", ")");
1796 }
1797 else
1798 {
1799 outputTriplet(visit, "(", " != ", ")");
1800 }
1801 }
1802 else if (node->getLeft()->getBasicType() == EbtStruct)
1803 {
1804 if (node->getOp() == EOpEqual)
1805 {
1806 out << "(";
1807 }
1808 else
1809 {
1810 out << "!(";
1811 }
1812
Jamie Madill98493dd2013-07-08 14:39:03 -04001813 const TStructure &structure = *node->getLeft()->getType().getStruct();
1814 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001815
Jamie Madill98493dd2013-07-08 14:39:03 -04001816 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001817 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001818 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001819
1820 node->getLeft()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001821 out << "." + decorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001822 node->getRight()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001823 out << "." + decorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001824
Jamie Madill98493dd2013-07-08 14:39:03 -04001825 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001826 {
1827 out << " && ";
1828 }
1829 }
1830
1831 out << ")";
1832
1833 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001834 }
1835 else
1836 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001837 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001838
1839 if (node->getOp() == EOpEqual)
1840 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001841 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001842 }
1843 else
1844 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001845 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001846 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001847 }
1848 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001849 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1850 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1851 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1852 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1853 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001854 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001855 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1856 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001857 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001858 case EOpLogicalOr:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001859 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001860 return false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001861 case EOpLogicalXor:
1862 mUsesXor = true;
1863 outputTriplet(visit, "xor(", ", ", ")");
1864 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001865 case EOpLogicalAnd:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001866 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001867 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001868 default: UNREACHABLE();
1869 }
1870
1871 return true;
1872}
1873
1874bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1875{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001876 switch (node->getOp())
1877 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001878 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1879 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1880 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1881 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1882 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1883 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1884 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001885 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04001886 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001887 case EOpConvFloatToBool:
1888 switch (node->getOperand()->getType().getNominalSize())
1889 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001890 case 1: outputTriplet(visit, "bool(", "", ")"); break;
1891 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
1892 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
1893 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001894 default: UNREACHABLE();
1895 }
1896 break;
1897 case EOpConvBoolToFloat:
1898 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04001899 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001900 switch (node->getOperand()->getType().getNominalSize())
1901 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001902 case 1: outputTriplet(visit, "float(", "", ")"); break;
1903 case 2: outputTriplet(visit, "float2(", "", ")"); break;
1904 case 3: outputTriplet(visit, "float3(", "", ")"); break;
1905 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001906 default: UNREACHABLE();
1907 }
1908 break;
1909 case EOpConvFloatToInt:
1910 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04001911 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001912 switch (node->getOperand()->getType().getNominalSize())
1913 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001914 case 1: outputTriplet(visit, "int(", "", ")"); break;
1915 case 2: outputTriplet(visit, "int2(", "", ")"); break;
1916 case 3: outputTriplet(visit, "int3(", "", ")"); break;
1917 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001918 default: UNREACHABLE();
1919 }
1920 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04001921 case EOpConvFloatToUInt:
1922 case EOpConvBoolToUInt:
1923 case EOpConvIntToUInt:
Jamie Madilla16f1672013-07-03 15:15:17 -04001924 switch (node->getOperand()->getType().getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001925 {
1926 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001927 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
1928 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
1929 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001930 default: UNREACHABLE();
1931 }
1932 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001933 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1934 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1935 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1936 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1937 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1938 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1939 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1940 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1941 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1942 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1943 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1944 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1945 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1946 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1947 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1948 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1949 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1950 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1951 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1952 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1953 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001954 case EOpDFdx:
1955 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1956 {
1957 outputTriplet(visit, "(", "", ", 0.0)");
1958 }
1959 else
1960 {
1961 outputTriplet(visit, "ddx(", "", ")");
1962 }
1963 break;
1964 case EOpDFdy:
1965 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1966 {
1967 outputTriplet(visit, "(", "", ", 0.0)");
1968 }
1969 else
1970 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001971 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001972 }
1973 break;
1974 case EOpFwidth:
1975 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1976 {
1977 outputTriplet(visit, "(", "", ", 0.0)");
1978 }
1979 else
1980 {
1981 outputTriplet(visit, "fwidth(", "", ")");
1982 }
1983 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001984 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1985 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001986 default: UNREACHABLE();
1987 }
1988
1989 return true;
1990}
1991
1992bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1993{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001994 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001995
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001996 switch (node->getOp())
1997 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001998 case EOpSequence:
1999 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002000 if (mInsideFunction)
2001 {
Jamie Madill075edd82013-07-08 13:30:19 -04002002 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002003 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002004
2005 mScopeDepth++;
2006
2007 if (mScopeBracket.size() < mScopeDepth)
2008 {
2009 mScopeBracket.push_back(0); // New scope level
2010 }
2011 else
2012 {
2013 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
2014 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002015 }
2016
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002017 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
2018 {
Jamie Madill075edd82013-07-08 13:30:19 -04002019 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002020
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002021 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002022
2023 out << ";\n";
2024 }
2025
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002026 if (mInsideFunction)
2027 {
Jamie Madill075edd82013-07-08 13:30:19 -04002028 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002029 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002030
2031 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002032 }
2033
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002034 return false;
2035 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002036 case EOpDeclaration:
2037 if (visit == PreVisit)
2038 {
2039 TIntermSequence &sequence = node->getSequence();
2040 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002041
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00002042 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002043 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002044 if (variable->getType().getStruct())
2045 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002046 addConstructor(variable->getType(), scopedStruct(variable->getType().getStruct()->name()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00002047 }
2048
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002049 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002050 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00002051 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002052 {
2053 out << "static ";
2054 }
2055
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002056 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002057
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002058 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002059 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002060 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002061
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002062 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002063 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002064 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002065 out << arrayString(symbol->getType());
daniel@transgaming.com7127f202010-04-15 20:45:22 +00002066 out << " = " + initializer(variable->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002067 }
2068 else
2069 {
2070 (*sit)->traverse(this);
2071 }
2072
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002073 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002074 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002075 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002076 }
2077 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002078 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002079 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2080 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002081 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002082 }
2083 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002084 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002085 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002086 {
2087 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2088 {
2089 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2090
2091 if (symbol)
2092 {
2093 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2094 mReferencedVaryings[symbol->getSymbol()] = symbol;
2095 }
2096 else
2097 {
2098 (*sit)->traverse(this);
2099 }
2100 }
2101 }
2102
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002103 return false;
2104 }
2105 else if (visit == InVisit)
2106 {
2107 out << ", ";
2108 }
2109 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002110 case EOpPrototype:
2111 if (visit == PreVisit)
2112 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002113 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002114
2115 TIntermSequence &arguments = node->getSequence();
2116
2117 for (unsigned int i = 0; i < arguments.size(); i++)
2118 {
2119 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2120
2121 if (symbol)
2122 {
2123 out << argumentString(symbol);
2124
2125 if (i < arguments.size() - 1)
2126 {
2127 out << ", ";
2128 }
2129 }
2130 else UNREACHABLE();
2131 }
2132
2133 out << ");\n";
2134
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002135 // Also prototype the Lod0 variant if needed
2136 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2137 {
2138 mOutputLod0Function = true;
2139 node->traverse(this);
2140 mOutputLod0Function = false;
2141 }
2142
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002143 return false;
2144 }
2145 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002146 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002147 case EOpFunction:
2148 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002149 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002150
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002151 out << typeString(node->getType()) << " ";
2152
2153 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002154 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002155 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002156 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002157 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002158 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002159 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002160 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002161
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002162 TIntermSequence &sequence = node->getSequence();
2163 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2164
2165 for (unsigned int i = 0; i < arguments.size(); i++)
2166 {
2167 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2168
2169 if (symbol)
2170 {
2171 if (symbol->getType().getStruct())
2172 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002173 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getStruct()->name()), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002174 }
2175
2176 out << argumentString(symbol);
2177
2178 if (i < arguments.size() - 1)
2179 {
2180 out << ", ";
2181 }
2182 }
2183 else UNREACHABLE();
2184 }
2185
2186 out << ")\n"
2187 "{\n";
2188
2189 if (sequence.size() > 1)
2190 {
2191 mInsideFunction = true;
2192 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002193 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002194 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002195
2196 out << "}\n";
2197
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002198 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2199 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002200 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002201 {
2202 mOutputLod0Function = true;
2203 node->traverse(this);
2204 mOutputLod0Function = false;
2205 }
2206 }
2207
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002208 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002209 }
2210 break;
2211 case EOpFunctionCall:
2212 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002213 TString name = TFunction::unmangleName(node->getName());
2214 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002215 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002216
2217 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002218 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002219 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002220 }
2221 else
2222 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002223 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2224
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002225 TextureFunction textureFunction;
2226 textureFunction.sampler = samplerType;
2227 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002228 textureFunction.method = TextureFunction::IMPLICIT;
2229 textureFunction.proj = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002230
2231 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002232 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002233 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002234 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002235 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002236 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002237 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002238 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002239 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002240 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002241 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002242 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002243 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002244 else if (name == "texture2DProjLod" || name == "textureProjLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002245 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002246 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002247 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002248 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002249 else if (name == "textureSize")
2250 {
2251 textureFunction.method = TextureFunction::SIZE;
2252 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002253 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002254
Nicolas Capens75fb4752013-07-10 15:14:47 -04002255 if (textureFunction.method != TextureFunction::LOD &&
2256 textureFunction.method != TextureFunction::SIZE)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002257 {
2258 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2259 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002260 textureFunction.method = TextureFunction::LOD0;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002261 }
2262 else if (arguments.size() == 3)
2263 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002264 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002265 }
2266 }
2267
2268 mUsesTexture.insert(textureFunction);
2269
2270 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002271 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002272
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002273 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2274 {
2275 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2276 {
2277 out << "texture_";
2278 (*arg)->traverse(this);
2279 out << ", sampler_";
2280 }
2281
2282 (*arg)->traverse(this);
2283
2284 if (arg < arguments.end() - 1)
2285 {
2286 out << ", ";
2287 }
2288 }
2289
2290 out << ")";
2291
2292 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002293 }
2294 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002295 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002296 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002297 addConstructor(node->getType(), "vec1", &node->getSequence());
2298 outputTriplet(visit, "vec1(", "", ")");
2299 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002300 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002301 addConstructor(node->getType(), "vec2", &node->getSequence());
2302 outputTriplet(visit, "vec2(", ", ", ")");
2303 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002304 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002305 addConstructor(node->getType(), "vec3", &node->getSequence());
2306 outputTriplet(visit, "vec3(", ", ", ")");
2307 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002308 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002309 addConstructor(node->getType(), "vec4", &node->getSequence());
2310 outputTriplet(visit, "vec4(", ", ", ")");
2311 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002312 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002313 addConstructor(node->getType(), "bvec1", &node->getSequence());
2314 outputTriplet(visit, "bvec1(", "", ")");
2315 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002316 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002317 addConstructor(node->getType(), "bvec2", &node->getSequence());
2318 outputTriplet(visit, "bvec2(", ", ", ")");
2319 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002320 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002321 addConstructor(node->getType(), "bvec3", &node->getSequence());
2322 outputTriplet(visit, "bvec3(", ", ", ")");
2323 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002324 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002325 addConstructor(node->getType(), "bvec4", &node->getSequence());
2326 outputTriplet(visit, "bvec4(", ", ", ")");
2327 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002328 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002329 addConstructor(node->getType(), "ivec1", &node->getSequence());
2330 outputTriplet(visit, "ivec1(", "", ")");
2331 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002332 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002333 addConstructor(node->getType(), "ivec2", &node->getSequence());
2334 outputTriplet(visit, "ivec2(", ", ", ")");
2335 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002336 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002337 addConstructor(node->getType(), "ivec3", &node->getSequence());
2338 outputTriplet(visit, "ivec3(", ", ", ")");
2339 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002340 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002341 addConstructor(node->getType(), "ivec4", &node->getSequence());
2342 outputTriplet(visit, "ivec4(", ", ", ")");
2343 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002344 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002345 addConstructor(node->getType(), "uvec1", &node->getSequence());
2346 outputTriplet(visit, "uvec1(", "", ")");
2347 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002348 case EOpConstructUVec2:
2349 addConstructor(node->getType(), "uvec2", &node->getSequence());
2350 outputTriplet(visit, "uvec2(", ", ", ")");
2351 break;
2352 case EOpConstructUVec3:
2353 addConstructor(node->getType(), "uvec3", &node->getSequence());
2354 outputTriplet(visit, "uvec3(", ", ", ")");
2355 break;
2356 case EOpConstructUVec4:
2357 addConstructor(node->getType(), "uvec4", &node->getSequence());
2358 outputTriplet(visit, "uvec4(", ", ", ")");
2359 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002360 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002361 addConstructor(node->getType(), "mat2", &node->getSequence());
2362 outputTriplet(visit, "mat2(", ", ", ")");
2363 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002364 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002365 addConstructor(node->getType(), "mat3", &node->getSequence());
2366 outputTriplet(visit, "mat3(", ", ", ")");
2367 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002368 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002369 addConstructor(node->getType(), "mat4", &node->getSequence());
2370 outputTriplet(visit, "mat4(", ", ", ")");
2371 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002372 case EOpConstructStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04002373 addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence());
2374 outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002375 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002376 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2377 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2378 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2379 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2380 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2381 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002382 case EOpMod:
2383 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002384 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002385 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2386 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2387 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002388 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002389 case 11: mUsesMod1 = true; break;
2390 case 22: mUsesMod2v = true; break;
2391 case 21: mUsesMod2f = true; break;
2392 case 33: mUsesMod3v = true; break;
2393 case 31: mUsesMod3f = true; break;
2394 case 44: mUsesMod4v = true; break;
2395 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002396 default: UNREACHABLE();
2397 }
2398
2399 outputTriplet(visit, "mod(", ", ", ")");
2400 }
2401 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002402 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002403 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002404 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002405 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2406 {
2407 case 1: mUsesAtan2_1 = true; break;
2408 case 2: mUsesAtan2_2 = true; break;
2409 case 3: mUsesAtan2_3 = true; break;
2410 case 4: mUsesAtan2_4 = true; break;
2411 default: UNREACHABLE();
2412 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002413 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002414 break;
2415 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2416 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2417 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2418 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2419 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2420 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2421 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2422 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2423 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002424 case EOpFaceForward:
2425 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002426 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002427 {
2428 case 1: mUsesFaceforward1 = true; break;
2429 case 2: mUsesFaceforward2 = true; break;
2430 case 3: mUsesFaceforward3 = true; break;
2431 case 4: mUsesFaceforward4 = true; break;
2432 default: UNREACHABLE();
2433 }
2434
2435 outputTriplet(visit, "faceforward(", ", ", ")");
2436 }
2437 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002438 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2439 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2440 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002441 default: UNREACHABLE();
2442 }
2443
2444 return true;
2445}
2446
2447bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2448{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002449 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002450
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002451 if (node->usesTernaryOperator())
2452 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002453 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002454 }
2455 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002456 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002457 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002458
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002459 out << "if(";
2460
2461 node->getCondition()->traverse(this);
2462
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002463 out << ")\n";
2464
Jamie Madill075edd82013-07-08 13:30:19 -04002465 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002466 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002467
daniel@transgaming.combb885322010-04-15 20:45:24 +00002468 if (node->getTrueBlock())
2469 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002470 traverseStatements(node->getTrueBlock());
daniel@transgaming.combb885322010-04-15 20:45:24 +00002471 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002472
Jamie Madill075edd82013-07-08 13:30:19 -04002473 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002474 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002475
2476 if (node->getFalseBlock())
2477 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002478 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002479
Jamie Madill075edd82013-07-08 13:30:19 -04002480 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002481 out << "{\n";
2482
Jamie Madill075edd82013-07-08 13:30:19 -04002483 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002484 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002485
Jamie Madill075edd82013-07-08 13:30:19 -04002486 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002487 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002488 }
2489 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002490
2491 return false;
2492}
2493
2494void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2495{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002496 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002497}
2498
2499bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2500{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002501 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2502
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002503 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002504 {
2505 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2506 }
2507
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002508 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002509 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002510 if (handleExcessiveLoop(node))
2511 {
2512 return false;
2513 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002514 }
2515
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002516 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002517
alokp@chromium.org52813552010-11-16 18:36:09 +00002518 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002519 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002520 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002521
Jamie Madill075edd82013-07-08 13:30:19 -04002522 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002523 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002524 }
2525 else
2526 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002527 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002528
2529 if (node->getInit())
2530 {
2531 node->getInit()->traverse(this);
2532 }
2533
2534 out << "; ";
2535
alokp@chromium.org52813552010-11-16 18:36:09 +00002536 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002537 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002538 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002539 }
2540
2541 out << "; ";
2542
alokp@chromium.org52813552010-11-16 18:36:09 +00002543 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002544 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002545 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002546 }
2547
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002548 out << ")\n";
2549
Jamie Madill075edd82013-07-08 13:30:19 -04002550 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002551 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002552 }
2553
2554 if (node->getBody())
2555 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002556 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002557 }
2558
Jamie Madill075edd82013-07-08 13:30:19 -04002559 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002560 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002561
alokp@chromium.org52813552010-11-16 18:36:09 +00002562 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002563 {
Jamie Madill075edd82013-07-08 13:30:19 -04002564 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002565 out << "while(\n";
2566
alokp@chromium.org52813552010-11-16 18:36:09 +00002567 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002568
daniel@transgaming.com73536982012-03-21 20:45:49 +00002569 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002570 }
2571
daniel@transgaming.com73536982012-03-21 20:45:49 +00002572 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002573
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002574 mInsideDiscontinuousLoop = wasDiscontinuous;
2575
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002576 return false;
2577}
2578
2579bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2580{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002581 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002582
2583 switch (node->getFlowOp())
2584 {
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002585 case EOpKill: outputTriplet(visit, "discard;\n", "", ""); break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002586 case EOpBreak:
2587 if (visit == PreVisit)
2588 {
2589 if (mExcessiveLoopIndex)
2590 {
2591 out << "{Break";
2592 mExcessiveLoopIndex->traverse(this);
2593 out << " = true; break;}\n";
2594 }
2595 else
2596 {
2597 out << "break;\n";
2598 }
2599 }
2600 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002601 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002602 case EOpReturn:
2603 if (visit == PreVisit)
2604 {
2605 if (node->getExpression())
2606 {
2607 out << "return ";
2608 }
2609 else
2610 {
2611 out << "return;\n";
2612 }
2613 }
2614 else if (visit == PostVisit)
2615 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002616 if (node->getExpression())
2617 {
2618 out << ";\n";
2619 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002620 }
2621 break;
2622 default: UNREACHABLE();
2623 }
2624
2625 return true;
2626}
2627
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002628void OutputHLSL::traverseStatements(TIntermNode *node)
2629{
2630 if (isSingleStatement(node))
2631 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002632 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002633 }
2634
2635 node->traverse(this);
2636}
2637
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002638bool OutputHLSL::isSingleStatement(TIntermNode *node)
2639{
2640 TIntermAggregate *aggregate = node->getAsAggregate();
2641
2642 if (aggregate)
2643 {
2644 if (aggregate->getOp() == EOpSequence)
2645 {
2646 return false;
2647 }
2648 else
2649 {
2650 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2651 {
2652 if (!isSingleStatement(*sit))
2653 {
2654 return false;
2655 }
2656 }
2657
2658 return true;
2659 }
2660 }
2661
2662 return true;
2663}
2664
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002665// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2666// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002667bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2668{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002669 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002670 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002671
2672 // Parse loops of the form:
2673 // for(int index = initial; index [comparator] limit; index += increment)
2674 TIntermSymbol *index = NULL;
2675 TOperator comparator = EOpNull;
2676 int initial = 0;
2677 int limit = 0;
2678 int increment = 0;
2679
2680 // Parse index name and intial value
2681 if (node->getInit())
2682 {
2683 TIntermAggregate *init = node->getInit()->getAsAggregate();
2684
2685 if (init)
2686 {
2687 TIntermSequence &sequence = init->getSequence();
2688 TIntermTyped *variable = sequence[0]->getAsTyped();
2689
2690 if (variable && variable->getQualifier() == EvqTemporary)
2691 {
2692 TIntermBinary *assign = variable->getAsBinaryNode();
2693
2694 if (assign->getOp() == EOpInitialize)
2695 {
2696 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2697 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2698
2699 if (symbol && constant)
2700 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002701 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002702 {
2703 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002704 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002705 }
2706 }
2707 }
2708 }
2709 }
2710 }
2711
2712 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002713 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002714 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002715 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002716
2717 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2718 {
2719 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2720
2721 if (constant)
2722 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002723 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002724 {
2725 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002726 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002727 }
2728 }
2729 }
2730 }
2731
2732 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002733 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002734 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002735 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2736 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002737
2738 if (binaryTerminal)
2739 {
2740 TOperator op = binaryTerminal->getOp();
2741 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2742
2743 if (constant)
2744 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002745 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002746 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002747 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002748
2749 switch (op)
2750 {
2751 case EOpAddAssign: increment = value; break;
2752 case EOpSubAssign: increment = -value; break;
2753 default: UNIMPLEMENTED();
2754 }
2755 }
2756 }
2757 }
2758 else if (unaryTerminal)
2759 {
2760 TOperator op = unaryTerminal->getOp();
2761
2762 switch (op)
2763 {
2764 case EOpPostIncrement: increment = 1; break;
2765 case EOpPostDecrement: increment = -1; break;
2766 case EOpPreIncrement: increment = 1; break;
2767 case EOpPreDecrement: increment = -1; break;
2768 default: UNIMPLEMENTED();
2769 }
2770 }
2771 }
2772
2773 if (index != NULL && comparator != EOpNull && increment != 0)
2774 {
2775 if (comparator == EOpLessThanEqual)
2776 {
2777 comparator = EOpLessThan;
2778 limit += 1;
2779 }
2780
2781 if (comparator == EOpLessThan)
2782 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002783 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002784
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002785 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002786 {
2787 return false; // Not an excessive loop
2788 }
2789
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002790 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2791 mExcessiveLoopIndex = index;
2792
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002793 out << "{int ";
2794 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002795 out << ";\n"
2796 "bool Break";
2797 index->traverse(this);
2798 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002799
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002800 bool firstLoopFragment = true;
2801
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002802 while (iterations > 0)
2803 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002804 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002805
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002806 if (!firstLoopFragment)
2807 {
2808 out << "if(!Break";
2809 index->traverse(this);
2810 out << ") {\n";
2811 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002812
2813 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2814 {
2815 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2816 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002817
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002818 // for(int index = initial; index < clampedLimit; index += increment)
2819
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002820 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002821 index->traverse(this);
2822 out << " = ";
2823 out << initial;
2824
2825 out << "; ";
2826 index->traverse(this);
2827 out << " < ";
2828 out << clampedLimit;
2829
2830 out << "; ";
2831 index->traverse(this);
2832 out << " += ";
2833 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002834 out << ")\n";
2835
Jamie Madill075edd82013-07-08 13:30:19 -04002836 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002837 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002838
2839 if (node->getBody())
2840 {
2841 node->getBody()->traverse(this);
2842 }
2843
Jamie Madill075edd82013-07-08 13:30:19 -04002844 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002845 out << ";}\n";
2846
2847 if (!firstLoopFragment)
2848 {
2849 out << "}\n";
2850 }
2851
2852 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002853
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002854 initial += MAX_LOOP_ITERATIONS * increment;
2855 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002856 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002857
2858 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002859
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002860 mExcessiveLoopIndex = restoreIndex;
2861
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002862 return true;
2863 }
2864 else UNIMPLEMENTED();
2865 }
2866
2867 return false; // Not handled as an excessive loop
2868}
2869
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002870void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002871{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002872 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002873
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002874 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002875 {
2876 out << preString;
2877 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002878 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002879 {
2880 out << inString;
2881 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002882 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002883 {
2884 out << postString;
2885 }
2886}
2887
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002888void OutputHLSL::outputLineDirective(int line)
2889{
2890 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2891 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002892 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002893 mBody << "#line " << line;
2894
2895 if (mContext.sourcePath)
2896 {
2897 mBody << " \"" << mContext.sourcePath << "\"";
2898 }
2899
2900 mBody << "\n";
2901 }
2902}
2903
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002904TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2905{
2906 TQualifier qualifier = symbol->getQualifier();
2907 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002908 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002909
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002910 if (name.empty()) // HLSL demands named arguments, also for prototypes
2911 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002912 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002913 }
2914 else
2915 {
2916 name = decorate(name);
2917 }
2918
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002919 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2920 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04002921 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
2922 qualifierString(qualifier) + " " + samplerString(type) + " sampler_" + name + arrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002923 }
2924
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002925 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002926}
2927
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00002928TString OutputHLSL::interpolationString(TQualifier qualifier)
2929{
2930 switch(qualifier)
2931 {
2932 case EvqVaryingIn: return "";
Jamie Madill19571812013-08-12 15:26:34 -07002933 case EvqFragmentIn: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00002934 case EvqInvariantVaryingIn: return "";
2935 case EvqSmoothIn: return "linear";
2936 case EvqFlatIn: return "nointerpolation";
2937 case EvqCentroidIn: return "centroid";
2938 case EvqVaryingOut: return "";
Jamie Madill19571812013-08-12 15:26:34 -07002939 case EvqVertexOut: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00002940 case EvqInvariantVaryingOut: return "";
2941 case EvqSmoothOut: return "linear";
2942 case EvqFlatOut: return "nointerpolation";
2943 case EvqCentroidOut: return "centroid";
2944 default: UNREACHABLE();
2945 }
2946
2947 return "";
2948}
2949
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002950TString OutputHLSL::qualifierString(TQualifier qualifier)
2951{
2952 switch(qualifier)
2953 {
2954 case EvqIn: return "in";
2955 case EvqOut: return "out";
2956 case EvqInOut: return "inout";
2957 case EvqConstReadOnly: return "const";
2958 default: UNREACHABLE();
2959 }
2960
2961 return "";
2962}
2963
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002964TString OutputHLSL::typeString(const TType &type)
2965{
Jamie Madill98493dd2013-07-08 14:39:03 -04002966 const TStructure* structure = type.getStruct();
2967 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002968 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002969 const TString& typeName = structure->name();
2970 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002971 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002972 return structLookup(typeName);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002973 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00002974 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002975 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002976 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002977 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002978 }
2979 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002980 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00002981 int cols = type.getCols();
2982 int rows = type.getRows();
2983 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002984 }
2985 else
2986 {
2987 switch (type.getBasicType())
2988 {
2989 case EbtFloat:
2990 switch (type.getNominalSize())
2991 {
2992 case 1: return "float";
2993 case 2: return "float2";
2994 case 3: return "float3";
2995 case 4: return "float4";
2996 }
2997 case EbtInt:
2998 switch (type.getNominalSize())
2999 {
3000 case 1: return "int";
3001 case 2: return "int2";
3002 case 3: return "int3";
3003 case 4: return "int4";
3004 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003005 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04003006 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003007 {
3008 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003009 case 2: return "uint2";
3010 case 3: return "uint3";
3011 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003012 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003013 case EbtBool:
3014 switch (type.getNominalSize())
3015 {
3016 case 1: return "bool";
3017 case 2: return "bool2";
3018 case 3: return "bool3";
3019 case 4: return "bool4";
3020 }
3021 case EbtVoid:
3022 return "void";
3023 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003024 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04003025 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003026 case EbtSampler2DArray:
3027 case EbtISampler2DArray:
3028 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003029 return "sampler2D";
3030 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003031 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04003032 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003033 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00003034 case EbtSamplerExternalOES:
3035 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00003036 default:
3037 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003038 }
3039 }
3040
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003041 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003042 return "<unknown type>";
3043}
3044
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003045TString OutputHLSL::textureString(const TType &type)
3046{
3047 switch (type.getBasicType())
3048 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003049 case EbtSampler2D: return "Texture2D";
3050 case EbtSamplerCube: return "TextureCube";
3051 case EbtSamplerExternalOES: return "Texture2D";
3052 case EbtSampler2DArray: return "Texture2DArray";
3053 case EbtSampler3D: return "Texture3D";
3054 case EbtISampler2D: return "Texture2D<int4>";
3055 case EbtISampler3D: return "Texture3D<int4>";
3056 case EbtISamplerCube: return "TextureCube<int4>";
3057 case EbtISampler2DArray: return "Texture2DArray<int4>";
3058 case EbtUSampler2D: return "Texture2D<uint4>";
3059 case EbtUSampler3D: return "Texture3D<uint4>";
3060 case EbtUSamplerCube: return "TextureCube<uint4>";
3061 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
3062 case EbtSampler2DShadow: return "Texture2D";
3063 case EbtSamplerCubeShadow: return "TextureCube";
3064 case EbtSampler2DArrayShadow: return "Texture2DArray";
3065 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003066 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04003067
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003068 return "<unknown texture type>";
3069}
3070
Nicolas Capenscb127d32013-07-15 17:26:18 -04003071TString OutputHLSL::samplerString(const TType &type)
3072{
3073 if (IsShadowSampler(type.getBasicType()))
3074 {
3075 return "SamplerComparisonState";
3076 }
3077 else
3078 {
3079 return "SamplerState";
3080 }
3081}
3082
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003083TString OutputHLSL::arrayString(const TType &type)
3084{
3085 if (!type.isArray())
3086 {
3087 return "";
3088 }
3089
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003090 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003091}
3092
3093TString OutputHLSL::initializer(const TType &type)
3094{
3095 TString string;
3096
Jamie Madill94bf7f22013-07-08 13:31:15 -04003097 size_t size = type.getObjectSize();
3098 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003099 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003100 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003101
Jamie Madill94bf7f22013-07-08 13:31:15 -04003102 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003103 {
3104 string += ", ";
3105 }
3106 }
3107
daniel@transgaming.comead23042010-04-29 03:35:36 +00003108 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003109}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003110
Jamie Madill98493dd2013-07-08 14:39:03 -04003111TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003112{
Jamie Madill98493dd2013-07-08 14:39:03 -04003113 const TFieldList &fields = structure.fields();
3114 const bool isNameless = (structure.name() == "");
3115 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003116 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3117
Jamie Madill98493dd2013-07-08 14:39:03 -04003118 TString string;
3119 string += declareString + "\n"
3120 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003121
Jamie Madillc835df62013-06-21 09:15:32 -04003122 int elementIndex = 0;
3123
Jamie Madill9cf6c072013-06-20 11:55:53 -04003124 for (unsigned int i = 0; i < fields.size(); i++)
3125 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003126 const TField &field = *fields[i];
3127 const TType &fieldType = *field.type();
3128 const TStructure *fieldStruct = fieldType.getStruct();
3129 const TString &fieldTypeString = fieldStruct ? structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) : typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003130
Jamie Madillc835df62013-06-21 09:15:32 -04003131 if (useStd140Packing)
3132 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003133 string += std140PrePaddingString(*field.type(), &elementIndex);
Jamie Madillc835df62013-06-21 09:15:32 -04003134 }
3135
Jamie Madill98493dd2013-07-08 14:39:03 -04003136 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003137
3138 if (useStd140Packing)
3139 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003140 string += std140PostPaddingString(*field.type(), useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003141 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003142 }
3143
3144 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003145 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003146
Jamie Madille4075c92013-06-21 09:15:32 -04003147 // Add remaining element index to the global map, for use with nested structs in standard layouts
3148 if (useStd140Packing)
3149 {
3150 mStd140StructElementIndexes[structName] = elementIndex;
3151 }
3152
Jamie Madill98493dd2013-07-08 14:39:03 -04003153 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003154}
3155
Jamie Madill98493dd2013-07-08 14:39:03 -04003156TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003157{
Jamie Madill98493dd2013-07-08 14:39:03 -04003158 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003159 {
3160 return "";
3161 }
3162
3163 TString prefix = "";
3164
3165 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3166 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003167
3168 if (useStd140Packing)
3169 {
3170 prefix += "std";
3171 }
3172
Jamie Madill9cf6c072013-06-20 11:55:53 -04003173 if (useHLSLRowMajorPacking)
3174 {
Jamie Madillc835df62013-06-21 09:15:32 -04003175 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003176 prefix += "rm";
3177 }
3178
Jamie Madill98493dd2013-07-08 14:39:03 -04003179 return prefix + structLookup(structure.name());
Jamie Madill9cf6c072013-06-20 11:55:53 -04003180}
3181
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003182void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003183{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003184 if (name == "")
3185 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003186 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003187 }
3188
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003189 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
3190 {
3191 return; // Already added
3192 }
3193
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003194 TType ctorType = type;
3195 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003196 ctorType.setPrecision(EbpHigh);
3197 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003198
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003199 TString ctorName = type.getStruct() ? decorate(name) : name;
3200
3201 typedef std::vector<TType> ParameterArray;
3202 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003203
Jamie Madill98493dd2013-07-08 14:39:03 -04003204 const TStructure* structure = type.getStruct();
3205 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003206 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003207 mStructNames.insert(decorate(name));
3208
Jamie Madill98493dd2013-07-08 14:39:03 -04003209 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003210
Jamie Madill98493dd2013-07-08 14:39:03 -04003211 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003212 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003213 // Add row-major packed struct for interface blocks
3214 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003215 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003216 "#pragma pack_matrix(column_major)\n";
3217
Jamie Madillc835df62013-06-21 09:15:32 -04003218 const TString &std140Prefix = "std";
Jamie Madill98493dd2013-07-08 14:39:03 -04003219 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003220
3221 const TString &std140RowMajorPrefix = "std_rm";
3222 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003223 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003224 "#pragma pack_matrix(column_major)\n";
3225
Jamie Madill98493dd2013-07-08 14:39:03 -04003226 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003227 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003228 mStructDeclarations.push_back(std140String);
3229 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003230 }
3231
Jamie Madill98493dd2013-07-08 14:39:03 -04003232 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003233 for (unsigned int i = 0; i < fields.size(); i++)
3234 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003235 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003236 }
3237 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003238 else if (parameters)
3239 {
3240 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3241 {
3242 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3243 }
3244 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003245 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003246
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003247 TString constructor;
3248
3249 if (ctorType.getStruct())
3250 {
3251 constructor += ctorName + " " + ctorName + "_ctor(";
3252 }
3253 else // Built-in type
3254 {
3255 constructor += typeString(ctorType) + " " + ctorName + "(";
3256 }
3257
3258 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3259 {
3260 const TType &type = ctorParameters[parameter];
3261
3262 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3263
3264 if (parameter < ctorParameters.size() - 1)
3265 {
3266 constructor += ", ";
3267 }
3268 }
3269
3270 constructor += ")\n"
3271 "{\n";
3272
3273 if (ctorType.getStruct())
3274 {
3275 constructor += " " + ctorName + " structure = {";
3276 }
3277 else
3278 {
3279 constructor += " return " + typeString(ctorType) + "(";
3280 }
3281
3282 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3283 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003284 int rows = ctorType.getRows();
3285 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003286 const TType &parameter = ctorParameters[0];
3287
3288 if (parameter.isScalar())
3289 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003290 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003291 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003292 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003293 {
3294 constructor += TString((row == col) ? "x0" : "0.0");
3295
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003296 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003297 {
3298 constructor += ", ";
3299 }
3300 }
3301 }
3302 }
3303 else if (parameter.isMatrix())
3304 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003305 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003306 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003307 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003308 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003309 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003310 {
3311 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3312 }
3313 else
3314 {
3315 constructor += TString((row == col) ? "1.0" : "0.0");
3316 }
3317
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003318 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003319 {
3320 constructor += ", ";
3321 }
3322 }
3323 }
3324 }
3325 else UNREACHABLE();
3326 }
3327 else
3328 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003329 size_t remainingComponents = ctorType.getObjectSize();
3330 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003331
3332 while (remainingComponents > 0)
3333 {
3334 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003335 const size_t parameterSize = parameter.getObjectSize();
3336 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003337
3338 constructor += "x" + str(parameterIndex);
3339
3340 if (parameter.isScalar())
3341 {
3342 remainingComponents -= parameter.getObjectSize();
3343 }
3344 else if (parameter.isVector())
3345 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003346 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003347 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003348 ASSERT(parameterSize <= remainingComponents);
3349 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003350 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003351 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003352 {
3353 switch (remainingComponents)
3354 {
3355 case 1: constructor += ".x"; break;
3356 case 2: constructor += ".xy"; break;
3357 case 3: constructor += ".xyz"; break;
3358 case 4: constructor += ".xyzw"; break;
3359 default: UNREACHABLE();
3360 }
3361
3362 remainingComponents = 0;
3363 }
3364 else UNREACHABLE();
3365 }
3366 else if (parameter.isMatrix() || parameter.getStruct())
3367 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003368 ASSERT(remainingComponents == parameterSize || moreParameters);
3369 ASSERT(parameterSize <= remainingComponents);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003370
Jamie Madill94bf7f22013-07-08 13:31:15 -04003371 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003372 }
3373 else UNREACHABLE();
3374
3375 if (moreParameters)
3376 {
3377 parameterIndex++;
3378 }
3379
3380 if (remainingComponents)
3381 {
3382 constructor += ", ";
3383 }
3384 }
3385 }
3386
3387 if (ctorType.getStruct())
3388 {
3389 constructor += "};\n"
3390 " return structure;\n"
3391 "}\n";
3392 }
3393 else
3394 {
3395 constructor += ");\n"
3396 "}\n";
3397 }
3398
daniel@transgaming.com63691862010-04-29 03:32:42 +00003399 mConstructors.insert(constructor);
3400}
3401
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003402const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3403{
3404 TInfoSinkBase &out = mBody;
3405
Jamie Madill98493dd2013-07-08 14:39:03 -04003406 const TStructure* structure = type.getStruct();
3407 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003408 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003409 out << structLookup(structure->name()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003410
Jamie Madill98493dd2013-07-08 14:39:03 -04003411 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003412
Jamie Madill98493dd2013-07-08 14:39:03 -04003413 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003414 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003415 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003416
3417 constUnion = writeConstantUnion(*fieldType, constUnion);
3418
Jamie Madill98493dd2013-07-08 14:39:03 -04003419 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003420 {
3421 out << ", ";
3422 }
3423 }
3424
3425 out << ")";
3426 }
3427 else
3428 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003429 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003430 bool writeType = size > 1;
3431
3432 if (writeType)
3433 {
3434 out << typeString(type) << "(";
3435 }
3436
Jamie Madill94bf7f22013-07-08 13:31:15 -04003437 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003438 {
3439 switch (constUnion->getType())
3440 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003441 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003442 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003443 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003444 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003445 default: UNREACHABLE();
3446 }
3447
3448 if (i != size - 1)
3449 {
3450 out << ", ";
3451 }
3452 }
3453
3454 if (writeType)
3455 {
3456 out << ")";
3457 }
3458 }
3459
3460 return constUnion;
3461}
3462
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003463TString OutputHLSL::scopeString(unsigned int depthLimit)
3464{
3465 TString string;
3466
3467 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3468 {
3469 string += "_" + str(i);
3470 }
3471
3472 return string;
3473}
3474
3475TString OutputHLSL::scopedStruct(const TString &typeName)
3476{
3477 if (typeName == "")
3478 {
3479 return typeName;
3480 }
3481
3482 return typeName + scopeString(mScopeDepth);
3483}
3484
3485TString OutputHLSL::structLookup(const TString &typeName)
3486{
3487 for (int depth = mScopeDepth; depth >= 0; depth--)
3488 {
3489 TString scopedName = decorate(typeName + scopeString(depth));
3490
3491 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3492 {
3493 if (*structName == scopedName)
3494 {
3495 return scopedName;
3496 }
3497 }
3498 }
3499
3500 UNREACHABLE(); // Should have found a matching constructor
3501
3502 return typeName;
3503}
3504
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003505TString OutputHLSL::decorate(const TString &string)
3506{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003507 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003508 {
3509 return "_" + string;
3510 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003511
3512 return string;
3513}
3514
apatrick@chromium.org65756022012-01-17 21:45:38 +00003515TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003516{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003517 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003518 {
3519 return "ex_" + string;
3520 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003521
3522 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003523}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003524
Jamie Madill98493dd2013-07-08 14:39:03 -04003525TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003526{
Jamie Madill98493dd2013-07-08 14:39:03 -04003527 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003528 {
3529 return decorate(string);
3530 }
3531
3532 return string;
3533}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003534
3535TString OutputHLSL::registerString(TIntermSymbol *operand)
3536{
3537 ASSERT(operand->getQualifier() == EvqUniform);
3538
3539 if (IsSampler(operand->getBasicType()))
3540 {
3541 return "s" + str(samplerRegister(operand));
3542 }
3543
3544 return "c" + str(uniformRegister(operand));
3545}
3546
3547int OutputHLSL::samplerRegister(TIntermSymbol *sampler)
3548{
3549 const TType &type = sampler->getType();
3550 ASSERT(IsSampler(type.getBasicType()));
3551
3552 int index = mSamplerRegister;
3553 mSamplerRegister += sampler->totalRegisterCount();
3554
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003555 declareUniform(type, sampler->getSymbol(), index);
3556
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003557 return index;
3558}
3559
3560int OutputHLSL::uniformRegister(TIntermSymbol *uniform)
3561{
3562 const TType &type = uniform->getType();
3563 ASSERT(!IsSampler(type.getBasicType()));
3564
3565 int index = mUniformRegister;
3566 mUniformRegister += uniform->totalRegisterCount();
3567
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003568 declareUniform(type, uniform->getSymbol(), index);
3569
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003570 return index;
3571}
3572
Jamie Madill98493dd2013-07-08 14:39:03 -04003573void OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, ActiveUniforms& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003574{
Jamie Madill98493dd2013-07-08 14:39:03 -04003575 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003576
3577 if (!structure)
3578 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003579 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill98493dd2013-07-08 14:39:03 -04003580 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 +00003581 }
3582 else
3583 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003584 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 +00003585
Jamie Madill98493dd2013-07-08 14:39:03 -04003586 int fieldRegister = registerIndex;
3587 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003588
Jamie Madill98493dd2013-07-08 14:39:03 -04003589 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003590 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003591 TField *field = fields[fieldIndex];
3592 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003593
Jamie Madill010fffa2013-06-20 11:55:53 -04003594 // make sure to copy matrix packing information
Jamie Madill98493dd2013-07-08 14:39:03 -04003595 fieldType->setLayoutQualifier(type.getLayoutQualifier());
Jamie Madill010fffa2013-06-20 11:55:53 -04003596
Jamie Madill98493dd2013-07-08 14:39:03 -04003597 declareUniformToList(*fieldType, field->name(), fieldRegister, structUniform.fields);
3598 fieldRegister += fieldType->totalRegisterCount();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003599 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003600
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003601 output.push_back(structUniform);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003602 }
3603}
3604
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003605void OutputHLSL::declareUniform(const TType &type, const TString &name, int index)
3606{
3607 declareUniformToList(type, name, index, mActiveUniforms);
3608}
3609
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003610GLenum OutputHLSL::glVariableType(const TType &type)
3611{
3612 if (type.getBasicType() == EbtFloat)
3613 {
3614 if (type.isScalar())
3615 {
3616 return GL_FLOAT;
3617 }
3618 else if (type.isVector())
3619 {
3620 switch(type.getNominalSize())
3621 {
3622 case 2: return GL_FLOAT_VEC2;
3623 case 3: return GL_FLOAT_VEC3;
3624 case 4: return GL_FLOAT_VEC4;
3625 default: UNREACHABLE();
3626 }
3627 }
3628 else if (type.isMatrix())
3629 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003630 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003631 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003632 case 2:
3633 switch(type.getRows())
3634 {
3635 case 2: return GL_FLOAT_MAT2;
3636 case 3: return GL_FLOAT_MAT2x3;
3637 case 4: return GL_FLOAT_MAT2x4;
3638 default: UNREACHABLE();
3639 }
3640
3641 case 3:
3642 switch(type.getRows())
3643 {
3644 case 2: return GL_FLOAT_MAT3x2;
3645 case 3: return GL_FLOAT_MAT3;
3646 case 4: return GL_FLOAT_MAT3x4;
3647 default: UNREACHABLE();
3648 }
3649
3650 case 4:
3651 switch(type.getRows())
3652 {
3653 case 2: return GL_FLOAT_MAT4x2;
3654 case 3: return GL_FLOAT_MAT4x3;
3655 case 4: return GL_FLOAT_MAT4;
3656 default: UNREACHABLE();
3657 }
3658
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003659 default: UNREACHABLE();
3660 }
3661 }
3662 else UNREACHABLE();
3663 }
3664 else if (type.getBasicType() == EbtInt)
3665 {
3666 if (type.isScalar())
3667 {
3668 return GL_INT;
3669 }
3670 else if (type.isVector())
3671 {
3672 switch(type.getNominalSize())
3673 {
3674 case 2: return GL_INT_VEC2;
3675 case 3: return GL_INT_VEC3;
3676 case 4: return GL_INT_VEC4;
3677 default: UNREACHABLE();
3678 }
3679 }
3680 else UNREACHABLE();
3681 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003682 else if (type.getBasicType() == EbtUInt)
3683 {
3684 if (type.isScalar())
3685 {
3686 return GL_UNSIGNED_INT;
3687 }
3688 else if (type.isVector())
3689 {
Jamie Madill22d63da2013-06-07 12:45:12 -04003690 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003691 {
3692 case 2: return GL_UNSIGNED_INT_VEC2;
3693 case 3: return GL_UNSIGNED_INT_VEC3;
3694 case 4: return GL_UNSIGNED_INT_VEC4;
3695 default: UNREACHABLE();
3696 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003697 }
3698 else UNREACHABLE();
3699 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003700 else if (type.getBasicType() == EbtBool)
3701 {
3702 if (type.isScalar())
3703 {
3704 return GL_BOOL;
3705 }
3706 else if (type.isVector())
3707 {
3708 switch(type.getNominalSize())
3709 {
3710 case 2: return GL_BOOL_VEC2;
3711 case 3: return GL_BOOL_VEC3;
3712 case 4: return GL_BOOL_VEC4;
3713 default: UNREACHABLE();
3714 }
3715 }
3716 else UNREACHABLE();
3717 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003718
3719 switch(type.getBasicType())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003720 {
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003721 case EbtSampler2D: return GL_SAMPLER_2D;
3722 case EbtSampler3D: return GL_SAMPLER_3D;
3723 case EbtSamplerCube: return GL_SAMPLER_CUBE;
3724 case EbtSampler2DArray: return GL_SAMPLER_2D_ARRAY;
3725 case EbtISampler2D: return GL_INT_SAMPLER_2D;
3726 case EbtISampler3D: return GL_INT_SAMPLER_3D;
3727 case EbtISamplerCube: return GL_INT_SAMPLER_CUBE;
3728 case EbtISampler2DArray: return GL_INT_SAMPLER_2D_ARRAY;
3729 case EbtUSampler2D: return GL_UNSIGNED_INT_SAMPLER_2D;
3730 case EbtUSampler3D: return GL_UNSIGNED_INT_SAMPLER_3D;
3731 case EbtUSamplerCube: return GL_UNSIGNED_INT_SAMPLER_CUBE;
3732 case EbtUSampler2DArray: return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3733 case EbtSampler2DShadow: return GL_SAMPLER_2D_SHADOW;
3734 case EbtSamplerCubeShadow: return GL_SAMPLER_CUBE_SHADOW;
3735 case EbtSampler2DArrayShadow: return GL_SAMPLER_2D_ARRAY_SHADOW;
3736 default: UNREACHABLE();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003737 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003738
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003739
3740 return GL_NONE;
3741}
3742
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003743GLenum OutputHLSL::glVariablePrecision(const TType &type)
3744{
3745 if (type.getBasicType() == EbtFloat)
3746 {
3747 switch (type.getPrecision())
3748 {
3749 case EbpHigh: return GL_HIGH_FLOAT;
3750 case EbpMedium: return GL_MEDIUM_FLOAT;
3751 case EbpLow: return GL_LOW_FLOAT;
3752 case EbpUndefined:
3753 // Should be defined as the default precision by the parser
3754 default: UNREACHABLE();
3755 }
3756 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003757 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003758 {
3759 switch (type.getPrecision())
3760 {
3761 case EbpHigh: return GL_HIGH_INT;
3762 case EbpMedium: return GL_MEDIUM_INT;
3763 case EbpLow: return GL_LOW_INT;
3764 case EbpUndefined:
3765 // Should be defined as the default precision by the parser
3766 default: UNREACHABLE();
3767 }
3768 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003769
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00003770 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003771 return GL_NONE;
3772}
3773
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003774bool OutputHLSL::isVaryingOut(TQualifier qualifier)
3775{
3776 switch(qualifier)
3777 {
3778 case EvqVaryingOut:
3779 case EvqInvariantVaryingOut:
3780 case EvqSmoothOut:
3781 case EvqFlatOut:
3782 case EvqCentroidOut:
Jamie Madill19571812013-08-12 15:26:34 -07003783 case EvqVertexOut:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003784 return true;
3785 }
3786
3787 return false;
3788}
3789
3790bool OutputHLSL::isVaryingIn(TQualifier qualifier)
3791{
3792 switch(qualifier)
3793 {
3794 case EvqVaryingIn:
3795 case EvqInvariantVaryingIn:
3796 case EvqSmoothIn:
3797 case EvqFlatIn:
3798 case EvqCentroidIn:
Jamie Madill19571812013-08-12 15:26:34 -07003799 case EvqFragmentIn:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003800 return true;
3801 }
3802
3803 return false;
3804}
3805
3806bool OutputHLSL::isVarying(TQualifier qualifier)
3807{
3808 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
3809}
3810
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003811}