blob: 15cce639cd4b9103fe69ba3a1f2928d92440af87 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +00002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00007#include "compiler/OutputHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
alokp@chromium.org79fb1012012-04-26 21:07:39 +00009#include "common/angleutils.h"
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +000010#include "common/utilities.h"
alokp@chromium.org91b72322010-06-02 15:50:56 +000011#include "compiler/debug.h"
daniel@transgaming.com89431aa2012-05-31 01:20:29 +000012#include "compiler/DetectDiscontinuity.h"
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000013#include "compiler/InfoSink.h"
14#include "compiler/SearchSymbol.h"
15#include "compiler/UnfoldShortCircuit.h"
Jamie Madill440dc742013-06-20 11:55:55 -040016#include "compiler/HLSLLayoutEncoder.h"
Jamie Madill570e04d2013-06-21 09:15:33 -040017#include "compiler/FlagStd140Structs.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000018
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000019#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000020#include <cfloat>
21#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000022
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000023namespace sh
24{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000025// Integer to TString conversion
26TString str(int i)
27{
28 char buffer[20];
kbr@chromium.orgddb6e8e2012-04-25 00:48:13 +000029 snprintf(buffer, sizeof(buffer), "%d", i);
daniel@transgaming.com005c7392010-04-15 20:45:27 +000030 return buffer;
31}
32
Nicolas Capense0ba27a2013-06-24 16:10:52 -040033TString OutputHLSL::TextureFunction::name() const
34{
35 TString name = "gl_texture";
36
Nicolas Capens6d232bb2013-07-08 15:56:38 -040037 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040038 {
39 name += "2D";
40 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040041 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040042 {
43 name += "3D";
44 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040045 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040046 {
47 name += "Cube";
48 }
49 else UNREACHABLE();
50
51 if (proj)
52 {
53 name += "Proj";
54 }
55
Nicolas Capens75fb4752013-07-10 15:14:47 -040056 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040057 {
58 case IMPLICIT: break;
59 case BIAS: break;
60 case LOD: name += "Lod"; break;
61 case LOD0: name += "Lod0"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -040062 case SIZE: name += "Size"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040063 default: UNREACHABLE();
64 }
65
66 return name + "(";
67}
68
69bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
70{
71 if (sampler < rhs.sampler) return true;
72 if (coords < rhs.coords) return true;
73 if (!proj && rhs.proj) return true;
Nicolas Capens75fb4752013-07-10 15:14:47 -040074 if (method < rhs.method) return true;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040075
76 return false;
77}
78
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +000079OutputHLSL::OutputHLSL(TParseContext &context, const ShBuiltInResources& resources, ShShaderOutput outputType)
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +000080 : TIntermTraverser(true, true, true), mContext(context), mOutputType(outputType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000081{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000082 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +000083 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000084
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +000085 mUsesFragColor = false;
86 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +000087 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +000088 mUsesFragCoord = false;
89 mUsesPointCoord = false;
90 mUsesFrontFacing = false;
91 mUsesPointSize = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -040092 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +000093 mUsesXor = false;
94 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +000095 mUsesMod2v = false;
96 mUsesMod2f = false;
97 mUsesMod3v = false;
98 mUsesMod3f = false;
99 mUsesMod4v = false;
100 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000101 mUsesFaceforward1 = false;
102 mUsesFaceforward2 = false;
103 mUsesFaceforward3 = false;
104 mUsesFaceforward4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000105 mUsesAtan2_1 = false;
106 mUsesAtan2_2 = false;
107 mUsesAtan2_3 = false;
108 mUsesAtan2_4 = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000109
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000110 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
111
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000112 mScopeDepth = 0;
113
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000114 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000115
116 mContainsLoopDiscontinuity = false;
117 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000118 mInsideDiscontinuousLoop = false;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000119
120 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000121
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000122 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000123 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000124 if (mContext.shaderType == SH_FRAGMENT_SHADER)
125 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000126 mUniformRegister = 3; // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000127 }
128 else
129 {
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000130 mUniformRegister = 2; // Reserve registers for dx_DepthRange and dx_ViewAdjust
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000131 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000132 }
133 else
134 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000135 mUniformRegister = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000136 }
137
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000138 mSamplerRegister = 0;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000139 mInterfaceBlockRegister = 2; // Reserve registers for the default uniform block and driver constants
Jamie Madill574d9dd2013-06-20 11:55:56 -0400140 mPaddingCounter = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000141}
142
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000143OutputHLSL::~OutputHLSL()
144{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000145 delete mUnfoldShortCircuit;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000146}
147
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000148void OutputHLSL::output()
149{
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +0000150 mContainsLoopDiscontinuity = mContext.shaderType == SH_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400151 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
152 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000153
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000154 mContext.treeRoot->traverse(this); // Output the body first to determine what has to go in the header
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000155 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000156
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000157 mContext.infoSink().obj << mHeader.c_str();
158 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000159}
160
Jamie Madill570e04d2013-06-21 09:15:33 -0400161void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
162{
163 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
164 {
165 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
166
167 // This will mark the necessary block elements as referenced
168 flaggedNode->traverse(this);
169 TString structName(mBody.c_str());
170 mBody.erase();
171
172 mFlaggedStructOriginalNames[flaggedNode] = structName;
173
174 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
175 {
176 structName.erase(pos, 1);
177 }
178
179 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
180 }
181}
182
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000183TInfoSinkBase &OutputHLSL::getBodyStream()
184{
185 return mBody;
186}
187
daniel@transgaming.com043da132012-12-20 21:12:22 +0000188const ActiveUniforms &OutputHLSL::getUniforms()
189{
190 return mActiveUniforms;
191}
192
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000193const ActiveInterfaceBlocks &OutputHLSL::getInterfaceBlocks() const
194{
195 return mActiveInterfaceBlocks;
196}
197
Jamie Madill46131a32013-06-20 11:55:50 -0400198const ActiveShaderVariables &OutputHLSL::getOutputVariables() const
199{
200 return mActiveOutputVariables;
201}
202
Jamie Madilldefb6742013-06-20 11:55:51 -0400203const ActiveShaderVariables &OutputHLSL::getAttributes() const
204{
205 return mActiveAttributes;
206}
207
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000208int OutputHLSL::vectorSize(const TType &type) const
209{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000210 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000211 int arraySize = type.isArray() ? type.getArraySize() : 1;
212
213 return elementSize * arraySize;
214}
215
Jamie Madill98493dd2013-07-08 14:39:03 -0400216TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, const TField &field)
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000217{
Jamie Madill98493dd2013-07-08 14:39:03 -0400218 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000219 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400220 return interfaceBlock.name() + "." + field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000221 }
222 else
223 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400224 return field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000225 }
226}
227
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000228TString OutputHLSL::decoratePrivate(const TString &privateText)
229{
230 return "dx_" + privateText;
231}
232
Jamie Madill98493dd2013-07-08 14:39:03 -0400233TString OutputHLSL::interfaceBlockStructNameString(const TInterfaceBlock &interfaceBlock)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000234{
Jamie Madill98493dd2013-07-08 14:39:03 -0400235 return decoratePrivate(interfaceBlock.name()) + "_type";
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000236}
237
Jamie Madill98493dd2013-07-08 14:39:03 -0400238TString OutputHLSL::interfaceBlockInstanceString(const TInterfaceBlock& interfaceBlock, unsigned int arrayIndex)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000239{
Jamie Madill98493dd2013-07-08 14:39:03 -0400240 if (!interfaceBlock.hasInstanceName())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000241 {
242 return "";
243 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400244 else if (interfaceBlock.isArray())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000245 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400246 return decoratePrivate(interfaceBlock.instanceName()) + "_" + str(arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000247 }
248 else
249 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400250 return decorate(interfaceBlock.instanceName());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000251 }
252}
253
Jamie Madill98493dd2013-07-08 14:39:03 -0400254TString OutputHLSL::interfaceBlockFieldTypeString(const TField &field, TLayoutBlockStorage blockStorage)
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000255{
Jamie Madill98493dd2013-07-08 14:39:03 -0400256 const TType &fieldType = *field.type();
257 const TLayoutMatrixPacking matrixPacking = fieldType.getLayoutQualifier().matrixPacking;
Jamie Madill529077d2013-06-20 11:55:54 -0400258 ASSERT(matrixPacking != EmpUnspecified);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000259
Jamie Madill98493dd2013-07-08 14:39:03 -0400260 if (fieldType.isMatrix())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000261 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400262 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill529077d2013-06-20 11:55:54 -0400263 const TString &matrixPackString = (matrixPacking == EmpRowMajor ? "column_major" : "row_major");
Jamie Madill98493dd2013-07-08 14:39:03 -0400264 return matrixPackString + " " + typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000265 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400266 else if (fieldType.getStruct())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000267 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400268 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill98493dd2013-07-08 14:39:03 -0400269 return structureTypeName(*fieldType.getStruct(), matrixPacking == EmpColumnMajor, blockStorage == EbsStd140);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000270 }
271 else
272 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400273 return typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000274 }
275}
276
Jamie Madill98493dd2013-07-08 14:39:03 -0400277TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, TLayoutBlockStorage blockStorage)
278{
279 TString hlsl;
280
281 int elementIndex = 0;
282
283 for (unsigned int typeIndex = 0; typeIndex < interfaceBlock.fields().size(); typeIndex++)
284 {
285 const TField &field = *interfaceBlock.fields()[typeIndex];
286 const TType &fieldType = *field.type();
287
288 if (blockStorage == EbsStd140)
289 {
290 // 2 and 3 component vector types in some cases need pre-padding
291 hlsl += std140PrePaddingString(fieldType, &elementIndex);
292 }
293
294 hlsl += " " + interfaceBlockFieldTypeString(field, blockStorage) +
295 " " + decorate(field.name()) + arrayString(fieldType) + ";\n";
296
297 // must pad out after matrices and arrays, where HLSL usually allows itself room to pack stuff
298 if (blockStorage == EbsStd140)
299 {
300 const bool useHLSLRowMajorPacking = (fieldType.getLayoutQualifier().matrixPacking == EmpColumnMajor);
301 hlsl += std140PostPaddingString(fieldType, useHLSLRowMajorPacking);
302 }
303 }
304
305 return hlsl;
306}
307
308TString OutputHLSL::interfaceBlockStructString(const TInterfaceBlock &interfaceBlock)
309{
310 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
311
312 return "struct " + interfaceBlockStructNameString(interfaceBlock) + "\n"
313 "{\n" +
314 interfaceBlockFieldString(interfaceBlock, blockStorage) +
315 "};\n\n";
316}
317
318TString OutputHLSL::interfaceBlockString(const TInterfaceBlock &interfaceBlock, unsigned int registerIndex, unsigned int arrayIndex)
319{
320 const TString &arrayIndexString = (arrayIndex != GL_INVALID_INDEX ? decorate(str(arrayIndex)) : "");
321 const TString &blockName = interfaceBlock.name() + arrayIndexString;
322 TString hlsl;
323
324 hlsl += "cbuffer " + blockName + " : register(b" + str(registerIndex) + ")\n"
325 "{\n";
326
327 if (interfaceBlock.hasInstanceName())
328 {
329 hlsl += " " + interfaceBlockStructNameString(interfaceBlock) + " " + interfaceBlockInstanceString(interfaceBlock, arrayIndex) + ";\n";
330 }
331 else
332 {
333 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
334 hlsl += interfaceBlockFieldString(interfaceBlock, blockStorage);
335 }
336
337 hlsl += "};\n\n";
338
339 return hlsl;
340}
341
Jamie Madill574d9dd2013-06-20 11:55:56 -0400342TString OutputHLSL::std140PrePaddingString(const TType &type, int *elementIndex)
343{
344 if (type.getBasicType() == EbtStruct || type.isMatrix() || type.isArray())
345 {
346 // no padding needed, HLSL will align the field to a new register
347 *elementIndex = 0;
348 return "";
349 }
350
351 const GLenum glType = glVariableType(type);
352 const int numComponents = gl::UniformComponentCount(glType);
353
354 if (numComponents >= 4)
355 {
356 // no padding needed, HLSL will align the field to a new register
357 *elementIndex = 0;
358 return "";
359 }
360
361 if (*elementIndex + numComponents > 4)
362 {
363 // no padding needed, HLSL will align the field to a new register
364 *elementIndex = numComponents;
365 return "";
366 }
367
368 TString padding;
369
370 const int alignment = numComponents == 3 ? 4 : numComponents;
371 const int paddingOffset = (*elementIndex % alignment);
372
373 if (paddingOffset != 0)
374 {
375 // padding is neccessary
376 for (int paddingIndex = paddingOffset; paddingIndex < alignment; paddingIndex++)
377 {
378 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
379 }
380
381 *elementIndex += (alignment - paddingOffset);
382 }
383
384 *elementIndex += numComponents;
385 *elementIndex %= 4;
386
387 return padding;
388}
389
Jamie Madille4075c92013-06-21 09:15:32 -0400390TString OutputHLSL::std140PostPaddingString(const TType &type, bool useHLSLRowMajorPacking)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400391{
Jamie Madillc835df62013-06-21 09:15:32 -0400392 if (!type.isMatrix() && !type.isArray() && type.getBasicType() != EbtStruct)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400393 {
394 return "";
395 }
396
Jamie Madill574d9dd2013-06-20 11:55:56 -0400397 int numComponents = 0;
398
399 if (type.isMatrix())
400 {
Jamie Madille4075c92013-06-21 09:15:32 -0400401 // This method can also be called from structureString, which does not use layout qualifiers.
402 // Thus, use the method parameter for determining the matrix packing.
403 //
404 // Note HLSL row major packing corresponds to GL API column-major, and vice-versa, since we
405 // wish to always transpose GL matrices to play well with HLSL's matrix array indexing.
406 //
407 const bool isRowMajorMatrix = !useHLSLRowMajorPacking;
Jamie Madillc835df62013-06-21 09:15:32 -0400408 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400409 numComponents = gl::MatrixComponentCount(glType, isRowMajorMatrix);
410 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400411 else if (type.getStruct())
Jamie Madillc835df62013-06-21 09:15:32 -0400412 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400413 const TString &structName = structureTypeName(*type.getStruct(), useHLSLRowMajorPacking, true);
Jamie Madille4075c92013-06-21 09:15:32 -0400414 numComponents = mStd140StructElementIndexes[structName];
415
416 if (numComponents == 0)
417 {
418 return "";
419 }
Jamie Madillc835df62013-06-21 09:15:32 -0400420 }
Jamie Madill574d9dd2013-06-20 11:55:56 -0400421 else
422 {
Jamie Madillc835df62013-06-21 09:15:32 -0400423 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400424 numComponents = gl::UniformComponentCount(glType);
425 }
426
427 TString padding;
428 for (int paddingOffset = numComponents; paddingOffset < 4; paddingOffset++)
429 {
430 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
431 }
432 return padding;
433}
434
Jamie Madill440dc742013-06-20 11:55:55 -0400435// Use the same layout for packed and shared
436void setBlockLayout(InterfaceBlock *interfaceBlock, BlockLayoutType newLayout)
437{
438 interfaceBlock->layout = newLayout;
439 interfaceBlock->blockInfo.clear();
440
441 switch (newLayout)
442 {
443 case BLOCKLAYOUT_SHARED:
444 case BLOCKLAYOUT_PACKED:
445 {
446 HLSLBlockEncoder hlslEncoder(&interfaceBlock->blockInfo);
447 hlslEncoder.encodeFields(interfaceBlock->activeUniforms);
448 interfaceBlock->dataSize = hlslEncoder.getBlockSize();
449 }
450 break;
451
452 case BLOCKLAYOUT_STANDARD:
453 {
454 Std140BlockEncoder stdEncoder(&interfaceBlock->blockInfo);
455 stdEncoder.encodeFields(interfaceBlock->activeUniforms);
456 interfaceBlock->dataSize = stdEncoder.getBlockSize();
457 }
458 break;
459
460 default:
461 UNREACHABLE();
462 break;
463 }
464}
465
Jamie Madill574d9dd2013-06-20 11:55:56 -0400466BlockLayoutType convertBlockLayoutType(TLayoutBlockStorage blockStorage)
467{
468 switch (blockStorage)
469 {
470 case EbsPacked: return BLOCKLAYOUT_PACKED;
471 case EbsShared: return BLOCKLAYOUT_SHARED;
472 case EbsStd140: return BLOCKLAYOUT_STANDARD;
473 default: UNREACHABLE(); return BLOCKLAYOUT_SHARED;
474 }
475}
476
Jamie Madill98493dd2013-07-08 14:39:03 -0400477TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400478{
479 TString init;
480
481 TString preIndentString;
482 TString fullIndentString;
483
484 for (int spaces = 0; spaces < (indent * 4); spaces++)
485 {
486 preIndentString += ' ';
487 }
488
489 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
490 {
491 fullIndentString += ' ';
492 }
493
494 init += preIndentString + "{\n";
495
Jamie Madill98493dd2013-07-08 14:39:03 -0400496 const TFieldList &fields = structure.fields();
497 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400498 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400499 const TField &field = *fields[fieldIndex];
500 const TString &fieldName = rhsStructName + "." + decorate(field.name());
501 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400502
Jamie Madill98493dd2013-07-08 14:39:03 -0400503 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400504 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400505 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400506 }
507 else
508 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400509 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400510 }
511 }
512
513 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
514
515 return init;
516}
517
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000518void OutputHLSL::header()
519{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000520 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000521
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000522 TString uniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000523 TString interfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000524 TString varyings;
525 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400526 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000527
528 for (ReferencedSymbols::const_iterator uniform = mReferencedUniforms.begin(); uniform != mReferencedUniforms.end(); uniform++)
529 {
530 const TType &type = uniform->second->getType();
531 const TString &name = uniform->second->getSymbol();
532
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000533 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture
534 {
535 int index = samplerRegister(mReferencedUniforms[name]);
536
Nicolas Capenscb127d32013-07-15 17:26:18 -0400537 uniforms += "uniform " + samplerString(type) + " sampler_" + decorateUniform(name, type) + arrayString(type) +
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000538 " : register(s" + str(index) + ");\n";
539
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000540 uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) +
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000541 " : register(t" + str(index) + ");\n";
542 }
543 else
544 {
545 uniforms += "uniform " + typeString(type) + " " + decorateUniform(name, type) + arrayString(type) +
546 " : register(" + registerString(mReferencedUniforms[name]) + ");\n";
547 }
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000548 }
549
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000550 for (ReferencedSymbols::const_iterator interfaceBlockIt = mReferencedInterfaceBlocks.begin(); interfaceBlockIt != mReferencedInterfaceBlocks.end(); interfaceBlockIt++)
551 {
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000552 const TType &nodeType = interfaceBlockIt->second->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -0400553 const TInterfaceBlock &interfaceBlock = *nodeType.getInterfaceBlock();
554 const TFieldList &fieldList = interfaceBlock.fields();
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000555
Jamie Madill98493dd2013-07-08 14:39:03 -0400556 unsigned int arraySize = static_cast<unsigned int>(interfaceBlock.arraySize());
557 sh::InterfaceBlock activeBlock(interfaceBlock.name().c_str(), arraySize, mInterfaceBlockRegister);
558 for (unsigned int typeIndex = 0; typeIndex < fieldList.size(); typeIndex++)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000559 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400560 const TField &field = *fieldList[typeIndex];
561 const TString &fullUniformName = interfaceBlockFieldString(interfaceBlock, field);
562 declareUniformToList(*field.type(), fullUniformName, typeIndex, activeBlock.activeUniforms);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000563 }
564
Jamie Madill98493dd2013-07-08 14:39:03 -0400565 mInterfaceBlockRegister += std::max(1u, arraySize);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000566
Jamie Madill98493dd2013-07-08 14:39:03 -0400567 BlockLayoutType blockLayoutType = convertBlockLayoutType(interfaceBlock.blockStorage());
568 setBlockLayout(&activeBlock, blockLayoutType);
569 mActiveInterfaceBlocks.push_back(activeBlock);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000570
Jamie Madill98493dd2013-07-08 14:39:03 -0400571 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000572 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400573 interfaceBlocks += interfaceBlockStructString(interfaceBlock);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000574 }
575
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000576 if (arraySize > 0)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000577 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000578 for (unsigned int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
579 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400580 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex + arrayIndex, arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000581 }
582 }
583 else
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000584 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400585 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000586 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000587 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000588
Jamie Madill570e04d2013-06-21 09:15:33 -0400589 for (auto flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
590 {
591 TIntermTyped *structNode = flaggedStructIt->first;
592 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400593 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400594 const TString &originalName = mFlaggedStructOriginalNames[structNode];
595
Jamie Madill98493dd2013-07-08 14:39:03 -0400596 flaggedStructs += "static " + decorate(structure.name()) + " " + mappedName + " =\n";
597 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400598 flaggedStructs += "\n";
599 }
600
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000601 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
602 {
603 const TType &type = varying->second->getType();
604 const TString &name = varying->second->getSymbol();
605
606 // Program linking depends on this exact format
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +0000607 varyings += "static " + interpolationString(type.getQualifier()) + " " + typeString(type) + " " +
608 decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000609 }
610
611 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
612 {
613 const TType &type = attribute->second->getType();
614 const TString &name = attribute->second->getSymbol();
615
616 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400617
618 ShaderVariable shaderVar(glVariableType(type), glVariablePrecision(type), name.c_str(),
619 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
620 mActiveAttributes.push_back(shaderVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000621 }
622
Jamie Madill529077d2013-06-20 11:55:54 -0400623 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
624 {
625 out << *structDeclaration;
626 }
627
628 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
629 {
630 out << *constructor;
631 }
632
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400633 if (mContext.shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000634 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000635 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000636 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000637
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000638 out << "// Varyings\n";
639 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400640 out << "\n";
641
642 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000643 {
Jamie Madill46131a32013-06-20 11:55:50 -0400644 for (auto outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000645 {
Jamie Madill46131a32013-06-20 11:55:50 -0400646 const TString &variableName = outputVariableIt->first;
647 const TType &variableType = outputVariableIt->second->getType();
648 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
649
650 out << "static " + typeString(variableType) + " out_" + variableName + arrayString(variableType) +
651 " = " + initializer(variableType) + ";\n";
652
653 ShaderVariable outputVar(glVariableType(variableType), glVariablePrecision(variableType), variableName.c_str(),
654 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
655 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000656 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000657 }
Jamie Madill46131a32013-06-20 11:55:50 -0400658 else
659 {
660 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
661
662 out << "static float4 gl_Color[" << numColorValues << "] =\n"
663 "{\n";
664 for (unsigned int i = 0; i < numColorValues; i++)
665 {
666 out << " float4(0, 0, 0, 0)";
667 if (i + 1 != numColorValues)
668 {
669 out << ",";
670 }
671 out << "\n";
672 }
673
674 out << "};\n";
675 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000676
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400677 if (mUsesFragDepth)
678 {
679 out << "static float gl_Depth = 0.0;\n";
680 }
681
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000682 if (mUsesFragCoord)
683 {
684 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
685 }
686
687 if (mUsesPointCoord)
688 {
689 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
690 }
691
692 if (mUsesFrontFacing)
693 {
694 out << "static bool gl_FrontFacing = false;\n";
695 }
696
697 out << "\n";
698
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000699 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000700 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000701 out << "struct gl_DepthRangeParameters\n"
702 "{\n"
703 " float near;\n"
704 " float far;\n"
705 " float diff;\n"
706 "};\n"
707 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000708 }
709
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000710 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000711 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000712 out << "cbuffer DriverConstants : register(b1)\n"
713 "{\n";
714
715 if (mUsesDepthRange)
716 {
717 out << " float3 dx_DepthRange : packoffset(c0);\n";
718 }
719
720 if (mUsesFragCoord)
721 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000722 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000723 }
724
725 if (mUsesFragCoord || mUsesFrontFacing)
726 {
727 out << " float3 dx_DepthFront : packoffset(c2);\n";
728 }
729
730 out << "};\n";
731 }
732 else
733 {
734 if (mUsesDepthRange)
735 {
736 out << "uniform float3 dx_DepthRange : register(c0);";
737 }
738
739 if (mUsesFragCoord)
740 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000741 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000742 }
743
744 if (mUsesFragCoord || mUsesFrontFacing)
745 {
746 out << "uniform float3 dx_DepthFront : register(c2);\n";
747 }
748 }
749
750 out << "\n";
751
752 if (mUsesDepthRange)
753 {
754 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
755 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000756 }
757
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000758 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000759 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000760
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000761 if (!interfaceBlocks.empty())
762 {
763 out << interfaceBlocks;
764 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400765
766 if (!flaggedStructs.empty())
767 {
768 out << "// Std140 Structures accessed by value\n";
769 out << "\n";
770 out << flaggedStructs;
771 out << "\n";
772 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000773 }
774
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000775 if (usingMRTExtension && mNumRenderTargets > 1)
776 {
777 out << "#define GL_USES_MRT\n";
778 }
779
780 if (mUsesFragColor)
781 {
782 out << "#define GL_USES_FRAG_COLOR\n";
783 }
784
785 if (mUsesFragData)
786 {
787 out << "#define GL_USES_FRAG_DATA\n";
788 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000789 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000790 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000791 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000792 out << "// Attributes\n";
793 out << attributes;
794 out << "\n"
795 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
796
797 if (mUsesPointSize)
798 {
799 out << "static float gl_PointSize = float(1);\n";
800 }
801
802 out << "\n"
803 "// Varyings\n";
804 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000805 out << "\n";
806
807 if (mUsesDepthRange)
808 {
809 out << "struct gl_DepthRangeParameters\n"
810 "{\n"
811 " float near;\n"
812 " float far;\n"
813 " float diff;\n"
814 "};\n"
815 "\n";
816 }
817
818 if (mOutputType == SH_HLSL11_OUTPUT)
819 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000820 if (mUsesDepthRange)
821 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000822 out << "cbuffer DriverConstants : register(b1)\n"
823 "{\n"
824 " float3 dx_DepthRange : packoffset(c0);\n"
825 "};\n"
826 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000827 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000828 }
829 else
830 {
831 if (mUsesDepthRange)
832 {
833 out << "uniform float3 dx_DepthRange : register(c0);\n";
834 }
835
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000836 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000837 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000838 }
839
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000840 if (mUsesDepthRange)
841 {
842 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
843 "\n";
844 }
845
846 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000848
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000849 if (!interfaceBlocks.empty())
850 {
851 out << interfaceBlocks;
852 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400853
854 if (!flaggedStructs.empty())
855 {
856 out << "// Std140 Structures accessed by value\n";
857 out << "\n";
858 out << flaggedStructs;
859 out << "\n";
860 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000861 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400862 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000863
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400864 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
865 {
866 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400867 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000868 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400869 switch(textureFunction->sampler)
870 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400871 case EbtSampler2D: out << "int2 "; break;
872 case EbtSampler3D: out << "int3 "; break;
873 case EbtSamplerCube: out << "int2 "; break;
874 case EbtSampler2DArray: out << "int3 "; break;
875 case EbtISampler2D: out << "int2 "; break;
876 case EbtISampler3D: out << "int3 "; break;
877 case EbtISamplerCube: out << "int2 "; break;
878 case EbtISampler2DArray: out << "int3 "; break;
879 case EbtUSampler2D: out << "int2 "; break;
880 case EbtUSampler3D: out << "int3 "; break;
881 case EbtUSamplerCube: out << "int2 "; break;
882 case EbtUSampler2DArray: out << "int3 "; break;
883 case EbtSampler2DShadow: out << "int2 "; break;
884 case EbtSamplerCubeShadow: out << "int2 "; break;
885 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400886 default: UNREACHABLE();
887 }
888 }
889 else // Sampling function
890 {
891 switch(textureFunction->sampler)
892 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400893 case EbtSampler2D: out << "float4 "; break;
894 case EbtSampler3D: out << "float4 "; break;
895 case EbtSamplerCube: out << "float4 "; break;
896 case EbtSampler2DArray: out << "float4 "; break;
897 case EbtISampler2D: out << "int4 "; break;
898 case EbtISampler3D: out << "int4 "; break;
899 case EbtISamplerCube: out << "int4 "; break;
900 case EbtISampler2DArray: out << "int4 "; break;
901 case EbtUSampler2D: out << "uint4 "; break;
902 case EbtUSampler3D: out << "uint4 "; break;
903 case EbtUSamplerCube: out << "uint4 "; break;
904 case EbtUSampler2DArray: out << "uint4 "; break;
905 case EbtSampler2DShadow: out << "float "; break;
906 case EbtSamplerCubeShadow: out << "float "; break;
907 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400908 default: UNREACHABLE();
909 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000910 }
911
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400912 // Function name
913 out << textureFunction->name();
914
915 // Argument list
916 int hlslCoords = 4;
917
918 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000919 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400920 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000921 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400922 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
923 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
924 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000925 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400926
Nicolas Capens75fb4752013-07-10 15:14:47 -0400927 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000928 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400929 case TextureFunction::IMPLICIT: break;
930 case TextureFunction::BIAS: hlslCoords = 4; break;
931 case TextureFunction::LOD: hlslCoords = 4; break;
932 case TextureFunction::LOD0: hlslCoords = 4; break;
933 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000934 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400935 }
936 else if (mOutputType == SH_HLSL11_OUTPUT)
937 {
938 switch(textureFunction->sampler)
939 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400940 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
941 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
942 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
943 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
944 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
945 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
946 case EbtISamplerCube: out << "TextureCube<int4> x, SamplerState s"; hlslCoords = 3; break;
947 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
948 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
949 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
950 case EbtUSamplerCube: out << "TextureCube<uint4> x, SamplerState s"; hlslCoords = 3; break;
951 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
952 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
953 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
954 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400955 default: UNREACHABLE();
956 }
957 }
958 else UNREACHABLE();
959
960 switch(textureFunction->coords)
961 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400962 case 1: out << ", int lod"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400963 case 2: out << ", float2 t"; break;
964 case 3: out << ", float3 t"; break;
965 case 4: out << ", float4 t"; break;
966 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000967 }
968
Nicolas Capens75fb4752013-07-10 15:14:47 -0400969 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000970 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400971 case TextureFunction::IMPLICIT: break;
972 case TextureFunction::BIAS: out << ", float bias"; break;
973 case TextureFunction::LOD: out << ", float lod"; break;
974 case TextureFunction::LOD0: break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400975 case TextureFunction::SIZE: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400976 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000977 }
978
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400979 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400980 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400981
Nicolas Capens75fb4752013-07-10 15:14:47 -0400982 if (textureFunction->method == TextureFunction::SIZE)
983 {
984 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
985 {
986 if (IsSamplerArray(textureFunction->sampler))
987 {
988 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
989 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
990 }
991 else
992 {
993 out << " uint width; uint height; uint numberOfLevels;\n"
994 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
995 }
996 }
997 else if (IsSampler3D(textureFunction->sampler))
998 {
999 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
1000 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
1001 }
1002 else UNREACHABLE();
1003
1004 switch(textureFunction->sampler)
1005 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04001006 case EbtSampler2D: out << " return int2(width, height);"; break;
1007 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
1008 case EbtSamplerCube: out << " return int2(width, height);"; break;
1009 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
1010 case EbtISampler2D: out << " return int2(width, height);"; break;
1011 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
1012 case EbtISamplerCube: out << " return int2(width, height);"; break;
1013 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
1014 case EbtUSampler2D: out << " return int2(width, height);"; break;
1015 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
1016 case EbtUSamplerCube: out << " return int2(width, height);"; break;
1017 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
1018 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
1019 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
1020 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001021 default: UNREACHABLE();
1022 }
1023 }
1024 else if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
daniel@transgaming.com15795192011-05-11 15:36:20 +00001025 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001026 // Currently unsupported because TextureCube does not support Load
1027 // This will require emulation using a Texture2DArray with 6 faces
1028 if (textureFunction->sampler == EbtISamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001029 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001030 out << " return int4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001031 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001032 else if (textureFunction->sampler == EbtUSamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001033 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001034 out << " return uint4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001035 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001036 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001037 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001038 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001039 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001040 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001041 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001042 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001043 {
Nicolas Capens93e50de2013-07-09 13:46:28 -04001044 if (IsSamplerArray(textureFunction->sampler))
1045 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001046 out << " float width; float height; float layers; float levels;\n";
1047
1048 if (textureFunction->method == TextureFunction::LOD0)
1049 {
1050 out << " uint mip = 0;\n";
1051 }
1052 else
1053 {
1054 if (textureFunction->method == TextureFunction::IMPLICIT ||
1055 textureFunction->method == TextureFunction::BIAS)
1056 {
1057 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1058 " float2 tSized = float2(t.x * width, t.y * height);\n"
1059 " float dx = length(ddx(tSized));\n"
1060 " float dy = length(ddy(tSized));\n"
1061 " float lod = log2(max(sqrt(dx), sqrt(dy)));\n";
1062
1063 if (textureFunction->method == TextureFunction::BIAS)
1064 {
1065 out << " lod += bias;\n";
1066 }
1067 }
1068
1069 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1070 }
1071
1072 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001073 }
1074 else
1075 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001076 out << " float width; float height; float levels;\n";
1077
1078 if (textureFunction->method == TextureFunction::LOD0)
1079 {
1080 out << " uint mip = 0;\n";
1081 }
1082 else
1083 {
1084 if (textureFunction->method == TextureFunction::IMPLICIT ||
1085 textureFunction->method == TextureFunction::BIAS)
1086 {
1087 out << " x.GetDimensions(0, width, height, levels);\n"
1088 " float2 tSized = float2(t.x * width, t.y * height);\n"
1089 " float dx = length(ddx(tSized));\n"
1090 " float dy = length(ddy(tSized));\n"
1091 " float lod = log2(max(sqrt(dx), sqrt(dy)));\n";
1092
1093 if (textureFunction->method == TextureFunction::BIAS)
1094 {
1095 out << " lod += bias;\n";
1096 }
1097 }
1098
1099 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1100 }
1101
1102 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001103 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001104 }
1105 else if (IsSampler3D(textureFunction->sampler))
1106 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001107 out << " float width; float height; float depth; float levels;\n";
1108
1109 if (textureFunction->method == TextureFunction::LOD0)
1110 {
1111 out << " uint mip = 0;\n";
1112 }
1113 else
1114 {
1115 if (textureFunction->method == TextureFunction::IMPLICIT ||
1116 textureFunction->method == TextureFunction::BIAS)
1117 {
1118 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1119 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
1120 " float dx = length(ddx(tSized));\n"
1121 " float dy = length(ddy(tSized));\n"
1122 " float lod = log2(max(sqrt(dx), sqrt(dy)));\n";
1123
1124 if (textureFunction->method == TextureFunction::BIAS)
1125 {
1126 out << " lod += bias;\n";
1127 }
1128 }
1129
1130 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1131 }
1132
1133 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001134 }
1135 else UNREACHABLE();
1136 }
1137
1138 out << " return ";
1139
1140 // HLSL intrinsic
1141 if (mOutputType == SH_HLSL9_OUTPUT)
1142 {
1143 switch(textureFunction->sampler)
1144 {
1145 case EbtSampler2D: out << "tex2D"; break;
1146 case EbtSamplerCube: out << "texCUBE"; break;
1147 default: UNREACHABLE();
1148 }
1149
Nicolas Capens75fb4752013-07-10 15:14:47 -04001150 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001151 {
1152 case TextureFunction::IMPLICIT: out << "(s, "; break;
1153 case TextureFunction::BIAS: out << "bias(s, "; break;
1154 case TextureFunction::LOD: out << "lod(s, "; break;
1155 case TextureFunction::LOD0: out << "lod(s, "; break;
1156 default: UNREACHABLE();
1157 }
1158 }
1159 else if (mOutputType == SH_HLSL11_OUTPUT)
1160 {
1161 if (IsIntegerSampler(textureFunction->sampler))
1162 {
1163 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001164 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001165 else if (IsShadowSampler(textureFunction->sampler))
1166 {
1167 out << "x.SampleCmp(s, ";
1168 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001169 else
1170 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001171 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001172 {
1173 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1174 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1175 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1176 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
1177 default: UNREACHABLE();
1178 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001179 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001180 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001181 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001182
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001183 // Integer sampling requires integer addresses
1184 TString addressx = "";
1185 TString addressy = "";
1186 TString addressz = "";
1187 TString close = "";
1188
1189 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001190 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001191 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001192 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001193 case 2: out << "int3("; break;
1194 case 3: out << "int4("; break;
1195 default: UNREACHABLE();
1196 }
1197
Nicolas Capensc98406a2013-07-10 14:52:44 -04001198 addressx = "int(floor(width * frac((";
1199 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001200
1201 if (IsSamplerArray(textureFunction->sampler))
1202 {
1203 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1204 }
1205 else
1206 {
Nicolas Capensc98406a2013-07-10 14:52:44 -04001207 addressz = "int(floor(depth * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001208 }
1209
1210 close = "))))";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001211 }
1212 else
1213 {
1214 switch(hlslCoords)
1215 {
1216 case 2: out << "float2("; break;
1217 case 3: out << "float3("; break;
1218 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001219 default: UNREACHABLE();
1220 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001221 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001222
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001223 TString proj = ""; // Only used for projected textures
1224
1225 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001226 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001227 switch(textureFunction->coords)
1228 {
1229 case 3: proj = " / t.z"; break;
1230 case 4: proj = " / t.w"; break;
1231 default: UNREACHABLE();
1232 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001233 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001234
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001235 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001236
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001237 if (mOutputType == SH_HLSL9_OUTPUT)
1238 {
1239 if (hlslCoords >= 3)
1240 {
1241 if (textureFunction->coords < 3)
1242 {
1243 out << ", 0";
1244 }
1245 else
1246 {
1247 out << ", t.z" + proj;
1248 }
1249 }
1250
1251 if (hlslCoords == 4)
1252 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001253 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001254 {
1255 case TextureFunction::BIAS: out << ", bias"; break;
1256 case TextureFunction::LOD: out << ", lod"; break;
1257 case TextureFunction::LOD0: out << ", 0"; break;
1258 default: UNREACHABLE();
1259 }
1260 }
1261
1262 out << "));\n";
1263 }
1264 else if (mOutputType == SH_HLSL11_OUTPUT)
1265 {
1266 if (hlslCoords >= 3)
1267 {
1268 out << ", " + addressz + ("t.z" + proj) + close;
1269 }
1270
Nicolas Capenscb127d32013-07-15 17:26:18 -04001271 if (IsIntegerSampler(textureFunction->sampler))
1272 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001273 out << ", mip));";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001274 }
1275 else if (IsShadowSampler(textureFunction->sampler))
1276 {
1277 // Compare value
1278 switch(textureFunction->coords)
1279 {
1280 case 3: out << "), t.z);"; break;
1281 case 4: out << "), t.w);"; break;
1282 default: UNREACHABLE();
1283 }
1284 }
1285 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001286 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001287 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001288 {
1289 case TextureFunction::IMPLICIT: out << "));"; break;
1290 case TextureFunction::BIAS: out << "), bias);"; break;
1291 case TextureFunction::LOD: out << "), lod);"; break;
1292 case TextureFunction::LOD0: out << "), 0);"; break;
1293 default: UNREACHABLE();
1294 }
1295 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001296 }
1297 else UNREACHABLE();
1298 }
1299
1300 out << "\n"
1301 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001302 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001303 }
1304
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001305 if (mUsesFragCoord)
1306 {
1307 out << "#define GL_USES_FRAG_COORD\n";
1308 }
1309
1310 if (mUsesPointCoord)
1311 {
1312 out << "#define GL_USES_POINT_COORD\n";
1313 }
1314
1315 if (mUsesFrontFacing)
1316 {
1317 out << "#define GL_USES_FRONT_FACING\n";
1318 }
1319
1320 if (mUsesPointSize)
1321 {
1322 out << "#define GL_USES_POINT_SIZE\n";
1323 }
1324
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001325 if (mUsesFragDepth)
1326 {
1327 out << "#define GL_USES_FRAG_DEPTH\n";
1328 }
1329
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001330 if (mUsesDepthRange)
1331 {
1332 out << "#define GL_USES_DEPTH_RANGE\n";
1333 }
1334
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001335 if (mUsesXor)
1336 {
1337 out << "bool xor(bool p, bool q)\n"
1338 "{\n"
1339 " return (p || q) && !(p && q);\n"
1340 "}\n"
1341 "\n";
1342 }
1343
1344 if (mUsesMod1)
1345 {
1346 out << "float mod(float x, float y)\n"
1347 "{\n"
1348 " return x - y * floor(x / y);\n"
1349 "}\n"
1350 "\n";
1351 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001352
1353 if (mUsesMod2v)
1354 {
1355 out << "float2 mod(float2 x, float2 y)\n"
1356 "{\n"
1357 " return x - y * floor(x / y);\n"
1358 "}\n"
1359 "\n";
1360 }
1361
1362 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001363 {
1364 out << "float2 mod(float2 x, float y)\n"
1365 "{\n"
1366 " return x - y * floor(x / y);\n"
1367 "}\n"
1368 "\n";
1369 }
1370
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001371 if (mUsesMod3v)
1372 {
1373 out << "float3 mod(float3 x, float3 y)\n"
1374 "{\n"
1375 " return x - y * floor(x / y);\n"
1376 "}\n"
1377 "\n";
1378 }
1379
1380 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001381 {
1382 out << "float3 mod(float3 x, float y)\n"
1383 "{\n"
1384 " return x - y * floor(x / y);\n"
1385 "}\n"
1386 "\n";
1387 }
1388
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001389 if (mUsesMod4v)
1390 {
1391 out << "float4 mod(float4 x, float4 y)\n"
1392 "{\n"
1393 " return x - y * floor(x / y);\n"
1394 "}\n"
1395 "\n";
1396 }
1397
1398 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001399 {
1400 out << "float4 mod(float4 x, float y)\n"
1401 "{\n"
1402 " return x - y * floor(x / y);\n"
1403 "}\n"
1404 "\n";
1405 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001406
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001407 if (mUsesFaceforward1)
1408 {
1409 out << "float faceforward(float N, float I, float Nref)\n"
1410 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001411 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001412 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001413 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001414 " }\n"
1415 " else\n"
1416 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001417 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001418 " }\n"
1419 "}\n"
1420 "\n";
1421 }
1422
1423 if (mUsesFaceforward2)
1424 {
1425 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1426 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001427 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001428 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001429 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001430 " }\n"
1431 " else\n"
1432 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001433 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001434 " }\n"
1435 "}\n"
1436 "\n";
1437 }
1438
1439 if (mUsesFaceforward3)
1440 {
1441 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1442 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001443 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001444 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001445 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001446 " }\n"
1447 " else\n"
1448 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001449 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001450 " }\n"
1451 "}\n"
1452 "\n";
1453 }
1454
1455 if (mUsesFaceforward4)
1456 {
1457 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1458 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001459 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001460 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001461 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001462 " }\n"
1463 " else\n"
1464 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001465 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001466 " }\n"
1467 "}\n"
1468 "\n";
1469 }
1470
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001471 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001472 {
1473 out << "float atanyx(float y, float x)\n"
1474 "{\n"
1475 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1476 " return atan2(y, x);\n"
1477 "}\n";
1478 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001479
1480 if (mUsesAtan2_2)
1481 {
1482 out << "float2 atanyx(float2 y, float2 x)\n"
1483 "{\n"
1484 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1485 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1486 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1487 "}\n";
1488 }
1489
1490 if (mUsesAtan2_3)
1491 {
1492 out << "float3 atanyx(float3 y, float3 x)\n"
1493 "{\n"
1494 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1495 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1496 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1497 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1498 "}\n";
1499 }
1500
1501 if (mUsesAtan2_4)
1502 {
1503 out << "float4 atanyx(float4 y, float4 x)\n"
1504 "{\n"
1505 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1506 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1507 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1508 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1509 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1510 "}\n";
1511 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001512}
1513
1514void OutputHLSL::visitSymbol(TIntermSymbol *node)
1515{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001516 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001517
Jamie Madill570e04d2013-06-21 09:15:33 -04001518 // Handle accessing std140 structs by value
1519 if (mFlaggedStructMappedNames.count(node) > 0)
1520 {
1521 out << mFlaggedStructMappedNames[node];
1522 return;
1523 }
1524
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001525 TString name = node->getSymbol();
1526
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001527 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001528 {
1529 mUsesDepthRange = true;
1530 out << name;
1531 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001532 else
1533 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001534 TQualifier qualifier = node->getQualifier();
1535
1536 if (qualifier == EvqUniform)
1537 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001538 const TType& nodeType = node->getType();
1539 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1540
1541 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001542 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001543 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001544 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001545 else
1546 {
1547 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001548 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001549
1550 out << decorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001551 }
Jamie Madillb120eac2013-06-12 14:08:13 -04001552 else if (qualifier == EvqAttribute || qualifier == EvqVertexInput)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001553 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001554 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001555 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001556 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001557 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001558 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001559 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001560 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001561 }
Jamie Madill46131a32013-06-20 11:55:50 -04001562 else if (qualifier == EvqFragmentOutput)
1563 {
1564 mReferencedOutputVariables[name] = node;
1565 out << "out_" << name;
1566 }
1567 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001568 {
1569 out << "gl_Color[0]";
1570 mUsesFragColor = true;
1571 }
1572 else if (qualifier == EvqFragData)
1573 {
1574 out << "gl_Color";
1575 mUsesFragData = true;
1576 }
1577 else if (qualifier == EvqFragCoord)
1578 {
1579 mUsesFragCoord = true;
1580 out << name;
1581 }
1582 else if (qualifier == EvqPointCoord)
1583 {
1584 mUsesPointCoord = true;
1585 out << name;
1586 }
1587 else if (qualifier == EvqFrontFacing)
1588 {
1589 mUsesFrontFacing = true;
1590 out << name;
1591 }
1592 else if (qualifier == EvqPointSize)
1593 {
1594 mUsesPointSize = true;
1595 out << name;
1596 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001597 else if (name == "gl_FragDepthEXT")
1598 {
1599 mUsesFragDepth = true;
1600 out << "gl_Depth";
1601 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001602 else
1603 {
1604 out << decorate(name);
1605 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001606 }
1607}
1608
1609bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1610{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001611 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001612
Jamie Madill570e04d2013-06-21 09:15:33 -04001613 // Handle accessing std140 structs by value
1614 if (mFlaggedStructMappedNames.count(node) > 0)
1615 {
1616 out << mFlaggedStructMappedNames[node];
1617 return false;
1618 }
1619
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001620 switch (node->getOp())
1621 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001622 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001623 case EOpInitialize:
1624 if (visit == PreVisit)
1625 {
1626 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1627 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1628 // new variable is created before the assignment is evaluated), so we need to convert
1629 // this to "float t = x, x = t;".
1630
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001631 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1632 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001633
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001634 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1635 expression->traverse(&searchSymbol);
1636 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001637
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001638 if (sameSymbol)
1639 {
1640 // Type already printed
1641 out << "t" + str(mUniqueIndex) + " = ";
1642 expression->traverse(this);
1643 out << ", ";
1644 symbolNode->traverse(this);
1645 out << " = t" + str(mUniqueIndex);
1646
1647 mUniqueIndex++;
1648 return false;
1649 }
1650 }
1651 else if (visit == InVisit)
1652 {
1653 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001654 }
1655 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001656 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1657 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1658 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1659 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1660 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1661 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001662 if (visit == PreVisit)
1663 {
1664 out << "(";
1665 }
1666 else if (visit == InVisit)
1667 {
1668 out << " = mul(";
1669 node->getLeft()->traverse(this);
1670 out << ", transpose(";
1671 }
1672 else
1673 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001674 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001675 }
1676 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001677 case EOpMatrixTimesMatrixAssign:
1678 if (visit == PreVisit)
1679 {
1680 out << "(";
1681 }
1682 else if (visit == InVisit)
1683 {
1684 out << " = mul(";
1685 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001686 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001687 }
1688 else
1689 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001690 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001691 }
1692 break;
1693 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001694 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001695 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001696 const TType& leftType = node->getLeft()->getType();
1697 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001698 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001699 if (visit == PreVisit)
1700 {
1701 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1702 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1703
1704 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1705 out << interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1706
1707 return false;
1708 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001709 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001710 else
1711 {
1712 outputTriplet(visit, "", "[", "]");
1713 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001714 }
1715 break;
1716 case EOpIndexIndirect:
1717 // We do not currently support indirect references to interface blocks
1718 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1719 outputTriplet(visit, "", "[", "]");
1720 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001721 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001722 if (visit == InVisit)
1723 {
1724 const TStructure* structure = node->getLeft()->getType().getStruct();
1725 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1726 const TField* field = structure->fields()[index->getIConst(0)];
1727 out << "." + decorateField(field->name(), *structure);
1728
1729 return false;
1730 }
1731 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001732 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001733 if (visit == InVisit)
1734 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001735 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1736 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1737 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
1738 out << "." + decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001739
1740 return false;
1741 }
1742 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001743 case EOpVectorSwizzle:
1744 if (visit == InVisit)
1745 {
1746 out << ".";
1747
1748 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1749
1750 if (swizzle)
1751 {
1752 TIntermSequence &sequence = swizzle->getSequence();
1753
1754 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1755 {
1756 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1757
1758 if (element)
1759 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001760 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001761
1762 switch (i)
1763 {
1764 case 0: out << "x"; break;
1765 case 1: out << "y"; break;
1766 case 2: out << "z"; break;
1767 case 3: out << "w"; break;
1768 default: UNREACHABLE();
1769 }
1770 }
1771 else UNREACHABLE();
1772 }
1773 }
1774 else UNREACHABLE();
1775
1776 return false; // Fully processed
1777 }
1778 break;
1779 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1780 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1781 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1782 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001783 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001784 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001785 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001786 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001787 if (node->getOp() == EOpEqual)
1788 {
1789 outputTriplet(visit, "(", " == ", ")");
1790 }
1791 else
1792 {
1793 outputTriplet(visit, "(", " != ", ")");
1794 }
1795 }
1796 else if (node->getLeft()->getBasicType() == EbtStruct)
1797 {
1798 if (node->getOp() == EOpEqual)
1799 {
1800 out << "(";
1801 }
1802 else
1803 {
1804 out << "!(";
1805 }
1806
Jamie Madill98493dd2013-07-08 14:39:03 -04001807 const TStructure &structure = *node->getLeft()->getType().getStruct();
1808 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001809
Jamie Madill98493dd2013-07-08 14:39:03 -04001810 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001811 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001812 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001813
1814 node->getLeft()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001815 out << "." + decorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001816 node->getRight()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001817 out << "." + decorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001818
Jamie Madill98493dd2013-07-08 14:39:03 -04001819 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001820 {
1821 out << " && ";
1822 }
1823 }
1824
1825 out << ")";
1826
1827 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001828 }
1829 else
1830 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001831 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001832
1833 if (node->getOp() == EOpEqual)
1834 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001835 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001836 }
1837 else
1838 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001839 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001840 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001841 }
1842 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001843 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1844 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1845 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1846 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1847 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001848 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001849 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1850 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001851 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001852 case EOpLogicalOr:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001853 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001854 return false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001855 case EOpLogicalXor:
1856 mUsesXor = true;
1857 outputTriplet(visit, "xor(", ", ", ")");
1858 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001859 case EOpLogicalAnd:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001860 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001861 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001862 default: UNREACHABLE();
1863 }
1864
1865 return true;
1866}
1867
1868bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1869{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001870 switch (node->getOp())
1871 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001872 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1873 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1874 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1875 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1876 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1877 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1878 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001879 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04001880 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001881 case EOpConvFloatToBool:
1882 switch (node->getOperand()->getType().getNominalSize())
1883 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001884 case 1: outputTriplet(visit, "bool(", "", ")"); break;
1885 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
1886 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
1887 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001888 default: UNREACHABLE();
1889 }
1890 break;
1891 case EOpConvBoolToFloat:
1892 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04001893 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001894 switch (node->getOperand()->getType().getNominalSize())
1895 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001896 case 1: outputTriplet(visit, "float(", "", ")"); break;
1897 case 2: outputTriplet(visit, "float2(", "", ")"); break;
1898 case 3: outputTriplet(visit, "float3(", "", ")"); break;
1899 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001900 default: UNREACHABLE();
1901 }
1902 break;
1903 case EOpConvFloatToInt:
1904 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04001905 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001906 switch (node->getOperand()->getType().getNominalSize())
1907 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001908 case 1: outputTriplet(visit, "int(", "", ")"); break;
1909 case 2: outputTriplet(visit, "int2(", "", ")"); break;
1910 case 3: outputTriplet(visit, "int3(", "", ")"); break;
1911 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001912 default: UNREACHABLE();
1913 }
1914 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04001915 case EOpConvFloatToUInt:
1916 case EOpConvBoolToUInt:
1917 case EOpConvIntToUInt:
Jamie Madilla16f1672013-07-03 15:15:17 -04001918 switch (node->getOperand()->getType().getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001919 {
1920 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001921 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
1922 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
1923 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001924 default: UNREACHABLE();
1925 }
1926 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001927 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1928 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1929 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1930 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1931 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1932 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1933 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1934 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1935 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1936 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1937 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1938 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1939 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1940 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1941 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1942 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1943 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1944 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1945 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1946 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1947 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001948 case EOpDFdx:
1949 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1950 {
1951 outputTriplet(visit, "(", "", ", 0.0)");
1952 }
1953 else
1954 {
1955 outputTriplet(visit, "ddx(", "", ")");
1956 }
1957 break;
1958 case EOpDFdy:
1959 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1960 {
1961 outputTriplet(visit, "(", "", ", 0.0)");
1962 }
1963 else
1964 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001965 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001966 }
1967 break;
1968 case EOpFwidth:
1969 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1970 {
1971 outputTriplet(visit, "(", "", ", 0.0)");
1972 }
1973 else
1974 {
1975 outputTriplet(visit, "fwidth(", "", ")");
1976 }
1977 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001978 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1979 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001980 default: UNREACHABLE();
1981 }
1982
1983 return true;
1984}
1985
1986bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1987{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001988 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001989
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001990 switch (node->getOp())
1991 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001992 case EOpSequence:
1993 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001994 if (mInsideFunction)
1995 {
Jamie Madill075edd82013-07-08 13:30:19 -04001996 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001997 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001998
1999 mScopeDepth++;
2000
2001 if (mScopeBracket.size() < mScopeDepth)
2002 {
2003 mScopeBracket.push_back(0); // New scope level
2004 }
2005 else
2006 {
2007 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
2008 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002009 }
2010
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002011 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
2012 {
Jamie Madill075edd82013-07-08 13:30:19 -04002013 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002014
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002015 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002016
2017 out << ";\n";
2018 }
2019
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002020 if (mInsideFunction)
2021 {
Jamie Madill075edd82013-07-08 13:30:19 -04002022 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002023 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002024
2025 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002026 }
2027
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002028 return false;
2029 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002030 case EOpDeclaration:
2031 if (visit == PreVisit)
2032 {
2033 TIntermSequence &sequence = node->getSequence();
2034 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002035
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00002036 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002037 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002038 if (variable->getType().getStruct())
2039 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002040 addConstructor(variable->getType(), scopedStruct(variable->getType().getStruct()->name()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00002041 }
2042
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002043 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002044 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00002045 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002046 {
2047 out << "static ";
2048 }
2049
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002050 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002051
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002052 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002053 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002054 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002055
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002056 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002057 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002058 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002059 out << arrayString(symbol->getType());
daniel@transgaming.com7127f202010-04-15 20:45:22 +00002060 out << " = " + initializer(variable->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002061 }
2062 else
2063 {
2064 (*sit)->traverse(this);
2065 }
2066
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002067 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002068 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002069 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002070 }
2071 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002072 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002073 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2074 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002075 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002076 }
2077 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002078 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002079 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002080 {
2081 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2082 {
2083 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2084
2085 if (symbol)
2086 {
2087 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2088 mReferencedVaryings[symbol->getSymbol()] = symbol;
2089 }
2090 else
2091 {
2092 (*sit)->traverse(this);
2093 }
2094 }
2095 }
2096
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002097 return false;
2098 }
2099 else if (visit == InVisit)
2100 {
2101 out << ", ";
2102 }
2103 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002104 case EOpPrototype:
2105 if (visit == PreVisit)
2106 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002107 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002108
2109 TIntermSequence &arguments = node->getSequence();
2110
2111 for (unsigned int i = 0; i < arguments.size(); i++)
2112 {
2113 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2114
2115 if (symbol)
2116 {
2117 out << argumentString(symbol);
2118
2119 if (i < arguments.size() - 1)
2120 {
2121 out << ", ";
2122 }
2123 }
2124 else UNREACHABLE();
2125 }
2126
2127 out << ");\n";
2128
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002129 // Also prototype the Lod0 variant if needed
2130 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2131 {
2132 mOutputLod0Function = true;
2133 node->traverse(this);
2134 mOutputLod0Function = false;
2135 }
2136
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002137 return false;
2138 }
2139 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002140 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002141 case EOpFunction:
2142 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002143 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002144
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002145 out << typeString(node->getType()) << " ";
2146
2147 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002148 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002149 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002150 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002151 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002152 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002153 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002154 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002155
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002156 TIntermSequence &sequence = node->getSequence();
2157 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2158
2159 for (unsigned int i = 0; i < arguments.size(); i++)
2160 {
2161 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2162
2163 if (symbol)
2164 {
2165 if (symbol->getType().getStruct())
2166 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002167 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getStruct()->name()), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002168 }
2169
2170 out << argumentString(symbol);
2171
2172 if (i < arguments.size() - 1)
2173 {
2174 out << ", ";
2175 }
2176 }
2177 else UNREACHABLE();
2178 }
2179
2180 out << ")\n"
2181 "{\n";
2182
2183 if (sequence.size() > 1)
2184 {
2185 mInsideFunction = true;
2186 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002187 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002188 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002189
2190 out << "}\n";
2191
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002192 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2193 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002194 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002195 {
2196 mOutputLod0Function = true;
2197 node->traverse(this);
2198 mOutputLod0Function = false;
2199 }
2200 }
2201
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002202 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002203 }
2204 break;
2205 case EOpFunctionCall:
2206 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002207 TString name = TFunction::unmangleName(node->getName());
2208 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002209 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002210
2211 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002212 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002213 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002214 }
2215 else
2216 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002217 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2218
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002219 TextureFunction textureFunction;
2220 textureFunction.sampler = samplerType;
2221 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002222 textureFunction.method = TextureFunction::IMPLICIT;
2223 textureFunction.proj = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002224
2225 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002226 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002227 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002228 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002229 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002230 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002231 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002232 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002233 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002234 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002235 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002236 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002237 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002238 else if (name == "texture2DProjLod" || name == "textureProjLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002239 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002240 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002241 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002242 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002243 else if (name == "textureSize")
2244 {
2245 textureFunction.method = TextureFunction::SIZE;
2246 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002247 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002248
Nicolas Capens75fb4752013-07-10 15:14:47 -04002249 if (textureFunction.method != TextureFunction::LOD &&
2250 textureFunction.method != TextureFunction::SIZE)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002251 {
2252 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2253 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002254 textureFunction.method = TextureFunction::LOD0;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002255 }
2256 else if (arguments.size() == 3)
2257 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002258 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002259 }
2260 }
2261
2262 mUsesTexture.insert(textureFunction);
2263
2264 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002265 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002266
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002267 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2268 {
2269 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2270 {
2271 out << "texture_";
2272 (*arg)->traverse(this);
2273 out << ", sampler_";
2274 }
2275
2276 (*arg)->traverse(this);
2277
2278 if (arg < arguments.end() - 1)
2279 {
2280 out << ", ";
2281 }
2282 }
2283
2284 out << ")";
2285
2286 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002287 }
2288 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002289 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002290 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002291 addConstructor(node->getType(), "vec1", &node->getSequence());
2292 outputTriplet(visit, "vec1(", "", ")");
2293 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002294 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002295 addConstructor(node->getType(), "vec2", &node->getSequence());
2296 outputTriplet(visit, "vec2(", ", ", ")");
2297 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002298 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002299 addConstructor(node->getType(), "vec3", &node->getSequence());
2300 outputTriplet(visit, "vec3(", ", ", ")");
2301 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002302 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002303 addConstructor(node->getType(), "vec4", &node->getSequence());
2304 outputTriplet(visit, "vec4(", ", ", ")");
2305 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002306 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002307 addConstructor(node->getType(), "bvec1", &node->getSequence());
2308 outputTriplet(visit, "bvec1(", "", ")");
2309 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002310 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002311 addConstructor(node->getType(), "bvec2", &node->getSequence());
2312 outputTriplet(visit, "bvec2(", ", ", ")");
2313 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002314 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002315 addConstructor(node->getType(), "bvec3", &node->getSequence());
2316 outputTriplet(visit, "bvec3(", ", ", ")");
2317 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002318 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002319 addConstructor(node->getType(), "bvec4", &node->getSequence());
2320 outputTriplet(visit, "bvec4(", ", ", ")");
2321 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002322 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002323 addConstructor(node->getType(), "ivec1", &node->getSequence());
2324 outputTriplet(visit, "ivec1(", "", ")");
2325 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002326 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002327 addConstructor(node->getType(), "ivec2", &node->getSequence());
2328 outputTriplet(visit, "ivec2(", ", ", ")");
2329 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002330 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002331 addConstructor(node->getType(), "ivec3", &node->getSequence());
2332 outputTriplet(visit, "ivec3(", ", ", ")");
2333 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002334 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002335 addConstructor(node->getType(), "ivec4", &node->getSequence());
2336 outputTriplet(visit, "ivec4(", ", ", ")");
2337 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002338 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002339 addConstructor(node->getType(), "uvec1", &node->getSequence());
2340 outputTriplet(visit, "uvec1(", "", ")");
2341 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002342 case EOpConstructUVec2:
2343 addConstructor(node->getType(), "uvec2", &node->getSequence());
2344 outputTriplet(visit, "uvec2(", ", ", ")");
2345 break;
2346 case EOpConstructUVec3:
2347 addConstructor(node->getType(), "uvec3", &node->getSequence());
2348 outputTriplet(visit, "uvec3(", ", ", ")");
2349 break;
2350 case EOpConstructUVec4:
2351 addConstructor(node->getType(), "uvec4", &node->getSequence());
2352 outputTriplet(visit, "uvec4(", ", ", ")");
2353 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002354 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002355 addConstructor(node->getType(), "mat2", &node->getSequence());
2356 outputTriplet(visit, "mat2(", ", ", ")");
2357 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002358 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002359 addConstructor(node->getType(), "mat3", &node->getSequence());
2360 outputTriplet(visit, "mat3(", ", ", ")");
2361 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002362 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002363 addConstructor(node->getType(), "mat4", &node->getSequence());
2364 outputTriplet(visit, "mat4(", ", ", ")");
2365 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002366 case EOpConstructStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04002367 addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence());
2368 outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002369 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002370 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2371 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2372 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2373 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2374 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2375 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002376 case EOpMod:
2377 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002378 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002379 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2380 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2381 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002382 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002383 case 11: mUsesMod1 = true; break;
2384 case 22: mUsesMod2v = true; break;
2385 case 21: mUsesMod2f = true; break;
2386 case 33: mUsesMod3v = true; break;
2387 case 31: mUsesMod3f = true; break;
2388 case 44: mUsesMod4v = true; break;
2389 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002390 default: UNREACHABLE();
2391 }
2392
2393 outputTriplet(visit, "mod(", ", ", ")");
2394 }
2395 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002396 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002397 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002398 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002399 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2400 {
2401 case 1: mUsesAtan2_1 = true; break;
2402 case 2: mUsesAtan2_2 = true; break;
2403 case 3: mUsesAtan2_3 = true; break;
2404 case 4: mUsesAtan2_4 = true; break;
2405 default: UNREACHABLE();
2406 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002407 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002408 break;
2409 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2410 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2411 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2412 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2413 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2414 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2415 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2416 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2417 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002418 case EOpFaceForward:
2419 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002420 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002421 {
2422 case 1: mUsesFaceforward1 = true; break;
2423 case 2: mUsesFaceforward2 = true; break;
2424 case 3: mUsesFaceforward3 = true; break;
2425 case 4: mUsesFaceforward4 = true; break;
2426 default: UNREACHABLE();
2427 }
2428
2429 outputTriplet(visit, "faceforward(", ", ", ")");
2430 }
2431 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002432 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2433 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2434 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002435 default: UNREACHABLE();
2436 }
2437
2438 return true;
2439}
2440
2441bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2442{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002443 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002444
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002445 if (node->usesTernaryOperator())
2446 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002447 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002448 }
2449 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002450 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002451 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002452
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002453 out << "if(";
2454
2455 node->getCondition()->traverse(this);
2456
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002457 out << ")\n";
2458
Jamie Madill075edd82013-07-08 13:30:19 -04002459 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002460 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002461
daniel@transgaming.combb885322010-04-15 20:45:24 +00002462 if (node->getTrueBlock())
2463 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002464 traverseStatements(node->getTrueBlock());
daniel@transgaming.combb885322010-04-15 20:45:24 +00002465 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002466
Jamie Madill075edd82013-07-08 13:30:19 -04002467 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002468 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002469
2470 if (node->getFalseBlock())
2471 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002472 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002473
Jamie Madill075edd82013-07-08 13:30:19 -04002474 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002475 out << "{\n";
2476
Jamie Madill075edd82013-07-08 13:30:19 -04002477 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002478 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002479
Jamie Madill075edd82013-07-08 13:30:19 -04002480 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002481 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002482 }
2483 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002484
2485 return false;
2486}
2487
2488void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2489{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002490 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002491}
2492
2493bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2494{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002495 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2496
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002497 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002498 {
2499 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2500 }
2501
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002502 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002503 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002504 if (handleExcessiveLoop(node))
2505 {
2506 return false;
2507 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002508 }
2509
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002510 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002511
alokp@chromium.org52813552010-11-16 18:36:09 +00002512 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002513 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002514 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002515
Jamie Madill075edd82013-07-08 13:30:19 -04002516 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002517 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002518 }
2519 else
2520 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002521 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002522
2523 if (node->getInit())
2524 {
2525 node->getInit()->traverse(this);
2526 }
2527
2528 out << "; ";
2529
alokp@chromium.org52813552010-11-16 18:36:09 +00002530 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002531 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002532 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002533 }
2534
2535 out << "; ";
2536
alokp@chromium.org52813552010-11-16 18:36:09 +00002537 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002538 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002539 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002540 }
2541
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002542 out << ")\n";
2543
Jamie Madill075edd82013-07-08 13:30:19 -04002544 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002545 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002546 }
2547
2548 if (node->getBody())
2549 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002550 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002551 }
2552
Jamie Madill075edd82013-07-08 13:30:19 -04002553 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002554 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002555
alokp@chromium.org52813552010-11-16 18:36:09 +00002556 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002557 {
Jamie Madill075edd82013-07-08 13:30:19 -04002558 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002559 out << "while(\n";
2560
alokp@chromium.org52813552010-11-16 18:36:09 +00002561 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002562
daniel@transgaming.com73536982012-03-21 20:45:49 +00002563 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002564 }
2565
daniel@transgaming.com73536982012-03-21 20:45:49 +00002566 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002567
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002568 mInsideDiscontinuousLoop = wasDiscontinuous;
2569
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002570 return false;
2571}
2572
2573bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2574{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002575 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002576
2577 switch (node->getFlowOp())
2578 {
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002579 case EOpKill: outputTriplet(visit, "discard;\n", "", ""); break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002580 case EOpBreak:
2581 if (visit == PreVisit)
2582 {
2583 if (mExcessiveLoopIndex)
2584 {
2585 out << "{Break";
2586 mExcessiveLoopIndex->traverse(this);
2587 out << " = true; break;}\n";
2588 }
2589 else
2590 {
2591 out << "break;\n";
2592 }
2593 }
2594 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002595 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002596 case EOpReturn:
2597 if (visit == PreVisit)
2598 {
2599 if (node->getExpression())
2600 {
2601 out << "return ";
2602 }
2603 else
2604 {
2605 out << "return;\n";
2606 }
2607 }
2608 else if (visit == PostVisit)
2609 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002610 if (node->getExpression())
2611 {
2612 out << ";\n";
2613 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002614 }
2615 break;
2616 default: UNREACHABLE();
2617 }
2618
2619 return true;
2620}
2621
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002622void OutputHLSL::traverseStatements(TIntermNode *node)
2623{
2624 if (isSingleStatement(node))
2625 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002626 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002627 }
2628
2629 node->traverse(this);
2630}
2631
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002632bool OutputHLSL::isSingleStatement(TIntermNode *node)
2633{
2634 TIntermAggregate *aggregate = node->getAsAggregate();
2635
2636 if (aggregate)
2637 {
2638 if (aggregate->getOp() == EOpSequence)
2639 {
2640 return false;
2641 }
2642 else
2643 {
2644 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2645 {
2646 if (!isSingleStatement(*sit))
2647 {
2648 return false;
2649 }
2650 }
2651
2652 return true;
2653 }
2654 }
2655
2656 return true;
2657}
2658
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002659// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2660// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002661bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2662{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002663 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002664 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002665
2666 // Parse loops of the form:
2667 // for(int index = initial; index [comparator] limit; index += increment)
2668 TIntermSymbol *index = NULL;
2669 TOperator comparator = EOpNull;
2670 int initial = 0;
2671 int limit = 0;
2672 int increment = 0;
2673
2674 // Parse index name and intial value
2675 if (node->getInit())
2676 {
2677 TIntermAggregate *init = node->getInit()->getAsAggregate();
2678
2679 if (init)
2680 {
2681 TIntermSequence &sequence = init->getSequence();
2682 TIntermTyped *variable = sequence[0]->getAsTyped();
2683
2684 if (variable && variable->getQualifier() == EvqTemporary)
2685 {
2686 TIntermBinary *assign = variable->getAsBinaryNode();
2687
2688 if (assign->getOp() == EOpInitialize)
2689 {
2690 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2691 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2692
2693 if (symbol && constant)
2694 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002695 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002696 {
2697 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002698 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002699 }
2700 }
2701 }
2702 }
2703 }
2704 }
2705
2706 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002707 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002708 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002709 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002710
2711 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2712 {
2713 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2714
2715 if (constant)
2716 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002717 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002718 {
2719 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002720 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002721 }
2722 }
2723 }
2724 }
2725
2726 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002727 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002728 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002729 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2730 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002731
2732 if (binaryTerminal)
2733 {
2734 TOperator op = binaryTerminal->getOp();
2735 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2736
2737 if (constant)
2738 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002739 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002740 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002741 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002742
2743 switch (op)
2744 {
2745 case EOpAddAssign: increment = value; break;
2746 case EOpSubAssign: increment = -value; break;
2747 default: UNIMPLEMENTED();
2748 }
2749 }
2750 }
2751 }
2752 else if (unaryTerminal)
2753 {
2754 TOperator op = unaryTerminal->getOp();
2755
2756 switch (op)
2757 {
2758 case EOpPostIncrement: increment = 1; break;
2759 case EOpPostDecrement: increment = -1; break;
2760 case EOpPreIncrement: increment = 1; break;
2761 case EOpPreDecrement: increment = -1; break;
2762 default: UNIMPLEMENTED();
2763 }
2764 }
2765 }
2766
2767 if (index != NULL && comparator != EOpNull && increment != 0)
2768 {
2769 if (comparator == EOpLessThanEqual)
2770 {
2771 comparator = EOpLessThan;
2772 limit += 1;
2773 }
2774
2775 if (comparator == EOpLessThan)
2776 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002777 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002778
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002779 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002780 {
2781 return false; // Not an excessive loop
2782 }
2783
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002784 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2785 mExcessiveLoopIndex = index;
2786
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002787 out << "{int ";
2788 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002789 out << ";\n"
2790 "bool Break";
2791 index->traverse(this);
2792 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002793
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002794 bool firstLoopFragment = true;
2795
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002796 while (iterations > 0)
2797 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002798 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002799
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002800 if (!firstLoopFragment)
2801 {
2802 out << "if(!Break";
2803 index->traverse(this);
2804 out << ") {\n";
2805 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002806
2807 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2808 {
2809 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2810 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002811
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002812 // for(int index = initial; index < clampedLimit; index += increment)
2813
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002814 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002815 index->traverse(this);
2816 out << " = ";
2817 out << initial;
2818
2819 out << "; ";
2820 index->traverse(this);
2821 out << " < ";
2822 out << clampedLimit;
2823
2824 out << "; ";
2825 index->traverse(this);
2826 out << " += ";
2827 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002828 out << ")\n";
2829
Jamie Madill075edd82013-07-08 13:30:19 -04002830 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002831 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002832
2833 if (node->getBody())
2834 {
2835 node->getBody()->traverse(this);
2836 }
2837
Jamie Madill075edd82013-07-08 13:30:19 -04002838 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002839 out << ";}\n";
2840
2841 if (!firstLoopFragment)
2842 {
2843 out << "}\n";
2844 }
2845
2846 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002847
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002848 initial += MAX_LOOP_ITERATIONS * increment;
2849 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002850 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002851
2852 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002853
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002854 mExcessiveLoopIndex = restoreIndex;
2855
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002856 return true;
2857 }
2858 else UNIMPLEMENTED();
2859 }
2860
2861 return false; // Not handled as an excessive loop
2862}
2863
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002864void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002865{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002866 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002867
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002868 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002869 {
2870 out << preString;
2871 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002872 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002873 {
2874 out << inString;
2875 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002876 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002877 {
2878 out << postString;
2879 }
2880}
2881
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002882void OutputHLSL::outputLineDirective(int line)
2883{
2884 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2885 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002886 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002887 mBody << "#line " << line;
2888
2889 if (mContext.sourcePath)
2890 {
2891 mBody << " \"" << mContext.sourcePath << "\"";
2892 }
2893
2894 mBody << "\n";
2895 }
2896}
2897
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002898TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2899{
2900 TQualifier qualifier = symbol->getQualifier();
2901 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002902 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002903
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002904 if (name.empty()) // HLSL demands named arguments, also for prototypes
2905 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002906 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002907 }
2908 else
2909 {
2910 name = decorate(name);
2911 }
2912
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002913 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2914 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04002915 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
2916 qualifierString(qualifier) + " " + samplerString(type) + " sampler_" + name + arrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002917 }
2918
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002919 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002920}
2921
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00002922TString OutputHLSL::interpolationString(TQualifier qualifier)
2923{
2924 switch(qualifier)
2925 {
2926 case EvqVaryingIn: return "";
2927 case EvqInvariantVaryingIn: return "";
2928 case EvqSmoothIn: return "linear";
2929 case EvqFlatIn: return "nointerpolation";
2930 case EvqCentroidIn: return "centroid";
2931 case EvqVaryingOut: return "";
2932 case EvqInvariantVaryingOut: return "";
2933 case EvqSmoothOut: return "linear";
2934 case EvqFlatOut: return "nointerpolation";
2935 case EvqCentroidOut: return "centroid";
2936 default: UNREACHABLE();
2937 }
2938
2939 return "";
2940}
2941
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002942TString OutputHLSL::qualifierString(TQualifier qualifier)
2943{
2944 switch(qualifier)
2945 {
2946 case EvqIn: return "in";
2947 case EvqOut: return "out";
2948 case EvqInOut: return "inout";
2949 case EvqConstReadOnly: return "const";
2950 default: UNREACHABLE();
2951 }
2952
2953 return "";
2954}
2955
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002956TString OutputHLSL::typeString(const TType &type)
2957{
Jamie Madill98493dd2013-07-08 14:39:03 -04002958 const TStructure* structure = type.getStruct();
2959 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002960 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002961 const TString& typeName = structure->name();
2962 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002963 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002964 return structLookup(typeName);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002965 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00002966 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002967 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002968 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002969 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002970 }
2971 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002972 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00002973 int cols = type.getCols();
2974 int rows = type.getRows();
2975 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002976 }
2977 else
2978 {
2979 switch (type.getBasicType())
2980 {
2981 case EbtFloat:
2982 switch (type.getNominalSize())
2983 {
2984 case 1: return "float";
2985 case 2: return "float2";
2986 case 3: return "float3";
2987 case 4: return "float4";
2988 }
2989 case EbtInt:
2990 switch (type.getNominalSize())
2991 {
2992 case 1: return "int";
2993 case 2: return "int2";
2994 case 3: return "int3";
2995 case 4: return "int4";
2996 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002997 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04002998 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002999 {
3000 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003001 case 2: return "uint2";
3002 case 3: return "uint3";
3003 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003004 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003005 case EbtBool:
3006 switch (type.getNominalSize())
3007 {
3008 case 1: return "bool";
3009 case 2: return "bool2";
3010 case 3: return "bool3";
3011 case 4: return "bool4";
3012 }
3013 case EbtVoid:
3014 return "void";
3015 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003016 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04003017 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003018 case EbtSampler2DArray:
3019 case EbtISampler2DArray:
3020 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003021 return "sampler2D";
3022 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003023 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04003024 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003025 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00003026 case EbtSamplerExternalOES:
3027 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00003028 default:
3029 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003030 }
3031 }
3032
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003033 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003034 return "<unknown type>";
3035}
3036
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003037TString OutputHLSL::textureString(const TType &type)
3038{
3039 switch (type.getBasicType())
3040 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003041 case EbtSampler2D: return "Texture2D";
3042 case EbtSamplerCube: return "TextureCube";
3043 case EbtSamplerExternalOES: return "Texture2D";
3044 case EbtSampler2DArray: return "Texture2DArray";
3045 case EbtSampler3D: return "Texture3D";
3046 case EbtISampler2D: return "Texture2D<int4>";
3047 case EbtISampler3D: return "Texture3D<int4>";
3048 case EbtISamplerCube: return "TextureCube<int4>";
3049 case EbtISampler2DArray: return "Texture2DArray<int4>";
3050 case EbtUSampler2D: return "Texture2D<uint4>";
3051 case EbtUSampler3D: return "Texture3D<uint4>";
3052 case EbtUSamplerCube: return "TextureCube<uint4>";
3053 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
3054 case EbtSampler2DShadow: return "Texture2D";
3055 case EbtSamplerCubeShadow: return "TextureCube";
3056 case EbtSampler2DArrayShadow: return "Texture2DArray";
3057 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003058 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04003059
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003060 return "<unknown texture type>";
3061}
3062
Nicolas Capenscb127d32013-07-15 17:26:18 -04003063TString OutputHLSL::samplerString(const TType &type)
3064{
3065 if (IsShadowSampler(type.getBasicType()))
3066 {
3067 return "SamplerComparisonState";
3068 }
3069 else
3070 {
3071 return "SamplerState";
3072 }
3073}
3074
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003075TString OutputHLSL::arrayString(const TType &type)
3076{
3077 if (!type.isArray())
3078 {
3079 return "";
3080 }
3081
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003082 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003083}
3084
3085TString OutputHLSL::initializer(const TType &type)
3086{
3087 TString string;
3088
Jamie Madill94bf7f22013-07-08 13:31:15 -04003089 size_t size = type.getObjectSize();
3090 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003091 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003092 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003093
Jamie Madill94bf7f22013-07-08 13:31:15 -04003094 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003095 {
3096 string += ", ";
3097 }
3098 }
3099
daniel@transgaming.comead23042010-04-29 03:35:36 +00003100 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003101}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003102
Jamie Madill98493dd2013-07-08 14:39:03 -04003103TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003104{
Jamie Madill98493dd2013-07-08 14:39:03 -04003105 const TFieldList &fields = structure.fields();
3106 const bool isNameless = (structure.name() == "");
3107 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003108 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3109
Jamie Madill98493dd2013-07-08 14:39:03 -04003110 TString string;
3111 string += declareString + "\n"
3112 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003113
Jamie Madillc835df62013-06-21 09:15:32 -04003114 int elementIndex = 0;
3115
Jamie Madill9cf6c072013-06-20 11:55:53 -04003116 for (unsigned int i = 0; i < fields.size(); i++)
3117 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003118 const TField &field = *fields[i];
3119 const TType &fieldType = *field.type();
3120 const TStructure *fieldStruct = fieldType.getStruct();
3121 const TString &fieldTypeString = fieldStruct ? structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) : typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003122
Jamie Madillc835df62013-06-21 09:15:32 -04003123 if (useStd140Packing)
3124 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003125 string += std140PrePaddingString(*field.type(), &elementIndex);
Jamie Madillc835df62013-06-21 09:15:32 -04003126 }
3127
Jamie Madill98493dd2013-07-08 14:39:03 -04003128 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003129
3130 if (useStd140Packing)
3131 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003132 string += std140PostPaddingString(*field.type(), useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003133 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003134 }
3135
3136 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003137 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003138
Jamie Madille4075c92013-06-21 09:15:32 -04003139 // Add remaining element index to the global map, for use with nested structs in standard layouts
3140 if (useStd140Packing)
3141 {
3142 mStd140StructElementIndexes[structName] = elementIndex;
3143 }
3144
Jamie Madill98493dd2013-07-08 14:39:03 -04003145 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003146}
3147
Jamie Madill98493dd2013-07-08 14:39:03 -04003148TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003149{
Jamie Madill98493dd2013-07-08 14:39:03 -04003150 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003151 {
3152 return "";
3153 }
3154
3155 TString prefix = "";
3156
3157 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3158 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003159
3160 if (useStd140Packing)
3161 {
3162 prefix += "std";
3163 }
3164
Jamie Madill9cf6c072013-06-20 11:55:53 -04003165 if (useHLSLRowMajorPacking)
3166 {
Jamie Madillc835df62013-06-21 09:15:32 -04003167 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003168 prefix += "rm";
3169 }
3170
Jamie Madill98493dd2013-07-08 14:39:03 -04003171 return prefix + structLookup(structure.name());
Jamie Madill9cf6c072013-06-20 11:55:53 -04003172}
3173
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003174void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003175{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003176 if (name == "")
3177 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003178 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003179 }
3180
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003181 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
3182 {
3183 return; // Already added
3184 }
3185
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003186 TType ctorType = type;
3187 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003188 ctorType.setPrecision(EbpHigh);
3189 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003190
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003191 TString ctorName = type.getStruct() ? decorate(name) : name;
3192
3193 typedef std::vector<TType> ParameterArray;
3194 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003195
Jamie Madill98493dd2013-07-08 14:39:03 -04003196 const TStructure* structure = type.getStruct();
3197 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003198 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003199 mStructNames.insert(decorate(name));
3200
Jamie Madill98493dd2013-07-08 14:39:03 -04003201 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003202
Jamie Madill98493dd2013-07-08 14:39:03 -04003203 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003204 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003205 // Add row-major packed struct for interface blocks
3206 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003207 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003208 "#pragma pack_matrix(column_major)\n";
3209
Jamie Madillc835df62013-06-21 09:15:32 -04003210 const TString &std140Prefix = "std";
Jamie Madill98493dd2013-07-08 14:39:03 -04003211 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003212
3213 const TString &std140RowMajorPrefix = "std_rm";
3214 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003215 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003216 "#pragma pack_matrix(column_major)\n";
3217
Jamie Madill98493dd2013-07-08 14:39:03 -04003218 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003219 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003220 mStructDeclarations.push_back(std140String);
3221 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003222 }
3223
Jamie Madill98493dd2013-07-08 14:39:03 -04003224 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003225 for (unsigned int i = 0; i < fields.size(); i++)
3226 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003227 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003228 }
3229 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003230 else if (parameters)
3231 {
3232 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3233 {
3234 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3235 }
3236 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003237 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003238
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003239 TString constructor;
3240
3241 if (ctorType.getStruct())
3242 {
3243 constructor += ctorName + " " + ctorName + "_ctor(";
3244 }
3245 else // Built-in type
3246 {
3247 constructor += typeString(ctorType) + " " + ctorName + "(";
3248 }
3249
3250 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3251 {
3252 const TType &type = ctorParameters[parameter];
3253
3254 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3255
3256 if (parameter < ctorParameters.size() - 1)
3257 {
3258 constructor += ", ";
3259 }
3260 }
3261
3262 constructor += ")\n"
3263 "{\n";
3264
3265 if (ctorType.getStruct())
3266 {
3267 constructor += " " + ctorName + " structure = {";
3268 }
3269 else
3270 {
3271 constructor += " return " + typeString(ctorType) + "(";
3272 }
3273
3274 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3275 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003276 int rows = ctorType.getRows();
3277 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003278 const TType &parameter = ctorParameters[0];
3279
3280 if (parameter.isScalar())
3281 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003282 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003283 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003284 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003285 {
3286 constructor += TString((row == col) ? "x0" : "0.0");
3287
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003288 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003289 {
3290 constructor += ", ";
3291 }
3292 }
3293 }
3294 }
3295 else if (parameter.isMatrix())
3296 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003297 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003298 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003299 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003300 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003301 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003302 {
3303 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3304 }
3305 else
3306 {
3307 constructor += TString((row == col) ? "1.0" : "0.0");
3308 }
3309
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003310 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003311 {
3312 constructor += ", ";
3313 }
3314 }
3315 }
3316 }
3317 else UNREACHABLE();
3318 }
3319 else
3320 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003321 size_t remainingComponents = ctorType.getObjectSize();
3322 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003323
3324 while (remainingComponents > 0)
3325 {
3326 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003327 const size_t parameterSize = parameter.getObjectSize();
3328 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003329
3330 constructor += "x" + str(parameterIndex);
3331
3332 if (parameter.isScalar())
3333 {
3334 remainingComponents -= parameter.getObjectSize();
3335 }
3336 else if (parameter.isVector())
3337 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003338 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003339 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003340 ASSERT(parameterSize <= remainingComponents);
3341 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003342 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003343 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003344 {
3345 switch (remainingComponents)
3346 {
3347 case 1: constructor += ".x"; break;
3348 case 2: constructor += ".xy"; break;
3349 case 3: constructor += ".xyz"; break;
3350 case 4: constructor += ".xyzw"; break;
3351 default: UNREACHABLE();
3352 }
3353
3354 remainingComponents = 0;
3355 }
3356 else UNREACHABLE();
3357 }
3358 else if (parameter.isMatrix() || parameter.getStruct())
3359 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003360 ASSERT(remainingComponents == parameterSize || moreParameters);
3361 ASSERT(parameterSize <= remainingComponents);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003362
Jamie Madill94bf7f22013-07-08 13:31:15 -04003363 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003364 }
3365 else UNREACHABLE();
3366
3367 if (moreParameters)
3368 {
3369 parameterIndex++;
3370 }
3371
3372 if (remainingComponents)
3373 {
3374 constructor += ", ";
3375 }
3376 }
3377 }
3378
3379 if (ctorType.getStruct())
3380 {
3381 constructor += "};\n"
3382 " return structure;\n"
3383 "}\n";
3384 }
3385 else
3386 {
3387 constructor += ");\n"
3388 "}\n";
3389 }
3390
daniel@transgaming.com63691862010-04-29 03:32:42 +00003391 mConstructors.insert(constructor);
3392}
3393
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003394const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3395{
3396 TInfoSinkBase &out = mBody;
3397
Jamie Madill98493dd2013-07-08 14:39:03 -04003398 const TStructure* structure = type.getStruct();
3399 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003400 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003401 out << structLookup(structure->name()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003402
Jamie Madill98493dd2013-07-08 14:39:03 -04003403 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003404
Jamie Madill98493dd2013-07-08 14:39:03 -04003405 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003406 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003407 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003408
3409 constUnion = writeConstantUnion(*fieldType, constUnion);
3410
Jamie Madill98493dd2013-07-08 14:39:03 -04003411 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003412 {
3413 out << ", ";
3414 }
3415 }
3416
3417 out << ")";
3418 }
3419 else
3420 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003421 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003422 bool writeType = size > 1;
3423
3424 if (writeType)
3425 {
3426 out << typeString(type) << "(";
3427 }
3428
Jamie Madill94bf7f22013-07-08 13:31:15 -04003429 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003430 {
3431 switch (constUnion->getType())
3432 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003433 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003434 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003435 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003436 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003437 default: UNREACHABLE();
3438 }
3439
3440 if (i != size - 1)
3441 {
3442 out << ", ";
3443 }
3444 }
3445
3446 if (writeType)
3447 {
3448 out << ")";
3449 }
3450 }
3451
3452 return constUnion;
3453}
3454
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003455TString OutputHLSL::scopeString(unsigned int depthLimit)
3456{
3457 TString string;
3458
3459 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3460 {
3461 string += "_" + str(i);
3462 }
3463
3464 return string;
3465}
3466
3467TString OutputHLSL::scopedStruct(const TString &typeName)
3468{
3469 if (typeName == "")
3470 {
3471 return typeName;
3472 }
3473
3474 return typeName + scopeString(mScopeDepth);
3475}
3476
3477TString OutputHLSL::structLookup(const TString &typeName)
3478{
3479 for (int depth = mScopeDepth; depth >= 0; depth--)
3480 {
3481 TString scopedName = decorate(typeName + scopeString(depth));
3482
3483 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3484 {
3485 if (*structName == scopedName)
3486 {
3487 return scopedName;
3488 }
3489 }
3490 }
3491
3492 UNREACHABLE(); // Should have found a matching constructor
3493
3494 return typeName;
3495}
3496
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003497TString OutputHLSL::decorate(const TString &string)
3498{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003499 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003500 {
3501 return "_" + string;
3502 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003503
3504 return string;
3505}
3506
apatrick@chromium.org65756022012-01-17 21:45:38 +00003507TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003508{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003509 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003510 {
3511 return "ex_" + string;
3512 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003513
3514 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003515}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003516
Jamie Madill98493dd2013-07-08 14:39:03 -04003517TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003518{
Jamie Madill98493dd2013-07-08 14:39:03 -04003519 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003520 {
3521 return decorate(string);
3522 }
3523
3524 return string;
3525}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003526
3527TString OutputHLSL::registerString(TIntermSymbol *operand)
3528{
3529 ASSERT(operand->getQualifier() == EvqUniform);
3530
3531 if (IsSampler(operand->getBasicType()))
3532 {
3533 return "s" + str(samplerRegister(operand));
3534 }
3535
3536 return "c" + str(uniformRegister(operand));
3537}
3538
3539int OutputHLSL::samplerRegister(TIntermSymbol *sampler)
3540{
3541 const TType &type = sampler->getType();
3542 ASSERT(IsSampler(type.getBasicType()));
3543
3544 int index = mSamplerRegister;
3545 mSamplerRegister += sampler->totalRegisterCount();
3546
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003547 declareUniform(type, sampler->getSymbol(), index);
3548
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003549 return index;
3550}
3551
3552int OutputHLSL::uniformRegister(TIntermSymbol *uniform)
3553{
3554 const TType &type = uniform->getType();
3555 ASSERT(!IsSampler(type.getBasicType()));
3556
3557 int index = mUniformRegister;
3558 mUniformRegister += uniform->totalRegisterCount();
3559
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003560 declareUniform(type, uniform->getSymbol(), index);
3561
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003562 return index;
3563}
3564
Jamie Madill98493dd2013-07-08 14:39:03 -04003565void OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, ActiveUniforms& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003566{
Jamie Madill98493dd2013-07-08 14:39:03 -04003567 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003568
3569 if (!structure)
3570 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003571 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill98493dd2013-07-08 14:39:03 -04003572 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 +00003573 }
3574 else
3575 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003576 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 +00003577
Jamie Madill98493dd2013-07-08 14:39:03 -04003578 int fieldRegister = registerIndex;
3579 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003580
Jamie Madill98493dd2013-07-08 14:39:03 -04003581 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003582 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003583 TField *field = fields[fieldIndex];
3584 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003585
Jamie Madill010fffa2013-06-20 11:55:53 -04003586 // make sure to copy matrix packing information
Jamie Madill98493dd2013-07-08 14:39:03 -04003587 fieldType->setLayoutQualifier(type.getLayoutQualifier());
Jamie Madill010fffa2013-06-20 11:55:53 -04003588
Jamie Madill98493dd2013-07-08 14:39:03 -04003589 declareUniformToList(*fieldType, field->name(), fieldRegister, structUniform.fields);
3590 fieldRegister += fieldType->totalRegisterCount();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003591 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003592
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003593 output.push_back(structUniform);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003594 }
3595}
3596
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003597void OutputHLSL::declareUniform(const TType &type, const TString &name, int index)
3598{
3599 declareUniformToList(type, name, index, mActiveUniforms);
3600}
3601
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003602GLenum OutputHLSL::glVariableType(const TType &type)
3603{
3604 if (type.getBasicType() == EbtFloat)
3605 {
3606 if (type.isScalar())
3607 {
3608 return GL_FLOAT;
3609 }
3610 else if (type.isVector())
3611 {
3612 switch(type.getNominalSize())
3613 {
3614 case 2: return GL_FLOAT_VEC2;
3615 case 3: return GL_FLOAT_VEC3;
3616 case 4: return GL_FLOAT_VEC4;
3617 default: UNREACHABLE();
3618 }
3619 }
3620 else if (type.isMatrix())
3621 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003622 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003623 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003624 case 2:
3625 switch(type.getRows())
3626 {
3627 case 2: return GL_FLOAT_MAT2;
3628 case 3: return GL_FLOAT_MAT2x3;
3629 case 4: return GL_FLOAT_MAT2x4;
3630 default: UNREACHABLE();
3631 }
3632
3633 case 3:
3634 switch(type.getRows())
3635 {
3636 case 2: return GL_FLOAT_MAT3x2;
3637 case 3: return GL_FLOAT_MAT3;
3638 case 4: return GL_FLOAT_MAT3x4;
3639 default: UNREACHABLE();
3640 }
3641
3642 case 4:
3643 switch(type.getRows())
3644 {
3645 case 2: return GL_FLOAT_MAT4x2;
3646 case 3: return GL_FLOAT_MAT4x3;
3647 case 4: return GL_FLOAT_MAT4;
3648 default: UNREACHABLE();
3649 }
3650
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003651 default: UNREACHABLE();
3652 }
3653 }
3654 else UNREACHABLE();
3655 }
3656 else if (type.getBasicType() == EbtInt)
3657 {
3658 if (type.isScalar())
3659 {
3660 return GL_INT;
3661 }
3662 else if (type.isVector())
3663 {
3664 switch(type.getNominalSize())
3665 {
3666 case 2: return GL_INT_VEC2;
3667 case 3: return GL_INT_VEC3;
3668 case 4: return GL_INT_VEC4;
3669 default: UNREACHABLE();
3670 }
3671 }
3672 else UNREACHABLE();
3673 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003674 else if (type.getBasicType() == EbtUInt)
3675 {
3676 if (type.isScalar())
3677 {
3678 return GL_UNSIGNED_INT;
3679 }
3680 else if (type.isVector())
3681 {
Jamie Madill22d63da2013-06-07 12:45:12 -04003682 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003683 {
3684 case 2: return GL_UNSIGNED_INT_VEC2;
3685 case 3: return GL_UNSIGNED_INT_VEC3;
3686 case 4: return GL_UNSIGNED_INT_VEC4;
3687 default: UNREACHABLE();
3688 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003689 }
3690 else UNREACHABLE();
3691 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003692 else if (type.getBasicType() == EbtBool)
3693 {
3694 if (type.isScalar())
3695 {
3696 return GL_BOOL;
3697 }
3698 else if (type.isVector())
3699 {
3700 switch(type.getNominalSize())
3701 {
3702 case 2: return GL_BOOL_VEC2;
3703 case 3: return GL_BOOL_VEC3;
3704 case 4: return GL_BOOL_VEC4;
3705 default: UNREACHABLE();
3706 }
3707 }
3708 else UNREACHABLE();
3709 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003710
3711 switch(type.getBasicType())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003712 {
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003713 case EbtSampler2D: return GL_SAMPLER_2D;
3714 case EbtSampler3D: return GL_SAMPLER_3D;
3715 case EbtSamplerCube: return GL_SAMPLER_CUBE;
3716 case EbtSampler2DArray: return GL_SAMPLER_2D_ARRAY;
3717 case EbtISampler2D: return GL_INT_SAMPLER_2D;
3718 case EbtISampler3D: return GL_INT_SAMPLER_3D;
3719 case EbtISamplerCube: return GL_INT_SAMPLER_CUBE;
3720 case EbtISampler2DArray: return GL_INT_SAMPLER_2D_ARRAY;
3721 case EbtUSampler2D: return GL_UNSIGNED_INT_SAMPLER_2D;
3722 case EbtUSampler3D: return GL_UNSIGNED_INT_SAMPLER_3D;
3723 case EbtUSamplerCube: return GL_UNSIGNED_INT_SAMPLER_CUBE;
3724 case EbtUSampler2DArray: return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3725 case EbtSampler2DShadow: return GL_SAMPLER_2D_SHADOW;
3726 case EbtSamplerCubeShadow: return GL_SAMPLER_CUBE_SHADOW;
3727 case EbtSampler2DArrayShadow: return GL_SAMPLER_2D_ARRAY_SHADOW;
3728 default: UNREACHABLE();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003729 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003730
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003731
3732 return GL_NONE;
3733}
3734
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003735GLenum OutputHLSL::glVariablePrecision(const TType &type)
3736{
3737 if (type.getBasicType() == EbtFloat)
3738 {
3739 switch (type.getPrecision())
3740 {
3741 case EbpHigh: return GL_HIGH_FLOAT;
3742 case EbpMedium: return GL_MEDIUM_FLOAT;
3743 case EbpLow: return GL_LOW_FLOAT;
3744 case EbpUndefined:
3745 // Should be defined as the default precision by the parser
3746 default: UNREACHABLE();
3747 }
3748 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003749 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003750 {
3751 switch (type.getPrecision())
3752 {
3753 case EbpHigh: return GL_HIGH_INT;
3754 case EbpMedium: return GL_MEDIUM_INT;
3755 case EbpLow: return GL_LOW_INT;
3756 case EbpUndefined:
3757 // Should be defined as the default precision by the parser
3758 default: UNREACHABLE();
3759 }
3760 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003761
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00003762 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003763 return GL_NONE;
3764}
3765
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003766bool OutputHLSL::isVaryingOut(TQualifier qualifier)
3767{
3768 switch(qualifier)
3769 {
3770 case EvqVaryingOut:
3771 case EvqInvariantVaryingOut:
3772 case EvqSmoothOut:
3773 case EvqFlatOut:
3774 case EvqCentroidOut:
3775 return true;
3776 }
3777
3778 return false;
3779}
3780
3781bool OutputHLSL::isVaryingIn(TQualifier qualifier)
3782{
3783 switch(qualifier)
3784 {
3785 case EvqVaryingIn:
3786 case EvqInvariantVaryingIn:
3787 case EvqSmoothIn:
3788 case EvqFlatIn:
3789 case EvqCentroidIn:
3790 return true;
3791 }
3792
3793 return false;
3794}
3795
3796bool OutputHLSL::isVarying(TQualifier qualifier)
3797{
3798 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
3799}
3800
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003801}