blob: 972d10abfafa1af12971100d66805b337f412fbe [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
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400188const std::vector<Uniform> &OutputHLSL::getUniforms()
daniel@transgaming.com043da132012-12-20 21:12:22 +0000189{
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 Madill9d2ffb12013-08-30 13:21:04 -0400198const std::vector<Attribute> &OutputHLSL::getOutputVariables() const
Jamie Madill46131a32013-06-20 11:55:50 -0400199{
200 return mActiveOutputVariables;
201}
202
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400203const std::vector<Attribute> &OutputHLSL::getAttributes() const
Jamie Madilldefb6742013-06-20 11:55:51 -0400204{
205 return mActiveAttributes;
206}
207
Jamie Madill47fdd132013-08-30 13:21:04 -0400208const std::vector<Varying> &OutputHLSL::getVaryings() const
209{
210 return mActiveVaryings;
211}
212
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000213int OutputHLSL::vectorSize(const TType &type) const
214{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000215 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000216 int arraySize = type.isArray() ? type.getArraySize() : 1;
217
218 return elementSize * arraySize;
219}
220
Jamie Madill98493dd2013-07-08 14:39:03 -0400221TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, const TField &field)
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000222{
Jamie Madill98493dd2013-07-08 14:39:03 -0400223 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000224 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400225 return interfaceBlock.name() + "." + field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000226 }
227 else
228 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400229 return field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000230 }
231}
232
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000233TString OutputHLSL::decoratePrivate(const TString &privateText)
234{
235 return "dx_" + privateText;
236}
237
Jamie Madill98493dd2013-07-08 14:39:03 -0400238TString OutputHLSL::interfaceBlockStructNameString(const TInterfaceBlock &interfaceBlock)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000239{
Jamie Madill98493dd2013-07-08 14:39:03 -0400240 return decoratePrivate(interfaceBlock.name()) + "_type";
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000241}
242
Jamie Madill98493dd2013-07-08 14:39:03 -0400243TString OutputHLSL::interfaceBlockInstanceString(const TInterfaceBlock& interfaceBlock, unsigned int arrayIndex)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000244{
Jamie Madill98493dd2013-07-08 14:39:03 -0400245 if (!interfaceBlock.hasInstanceName())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000246 {
247 return "";
248 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400249 else if (interfaceBlock.isArray())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000250 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400251 return decoratePrivate(interfaceBlock.instanceName()) + "_" + str(arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000252 }
253 else
254 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400255 return decorate(interfaceBlock.instanceName());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000256 }
257}
258
Jamie Madill98493dd2013-07-08 14:39:03 -0400259TString OutputHLSL::interfaceBlockFieldTypeString(const TField &field, TLayoutBlockStorage blockStorage)
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000260{
Jamie Madill98493dd2013-07-08 14:39:03 -0400261 const TType &fieldType = *field.type();
262 const TLayoutMatrixPacking matrixPacking = fieldType.getLayoutQualifier().matrixPacking;
Jamie Madill529077d2013-06-20 11:55:54 -0400263 ASSERT(matrixPacking != EmpUnspecified);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000264
Jamie Madill98493dd2013-07-08 14:39:03 -0400265 if (fieldType.isMatrix())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000266 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400267 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill529077d2013-06-20 11:55:54 -0400268 const TString &matrixPackString = (matrixPacking == EmpRowMajor ? "column_major" : "row_major");
Jamie Madill98493dd2013-07-08 14:39:03 -0400269 return matrixPackString + " " + typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000270 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400271 else if (fieldType.getStruct())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000272 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400273 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill98493dd2013-07-08 14:39:03 -0400274 return structureTypeName(*fieldType.getStruct(), matrixPacking == EmpColumnMajor, blockStorage == EbsStd140);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000275 }
276 else
277 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400278 return typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000279 }
280}
281
Jamie Madill98493dd2013-07-08 14:39:03 -0400282TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, TLayoutBlockStorage blockStorage)
283{
284 TString hlsl;
285
286 int elementIndex = 0;
287
288 for (unsigned int typeIndex = 0; typeIndex < interfaceBlock.fields().size(); typeIndex++)
289 {
290 const TField &field = *interfaceBlock.fields()[typeIndex];
291 const TType &fieldType = *field.type();
292
293 if (blockStorage == EbsStd140)
294 {
295 // 2 and 3 component vector types in some cases need pre-padding
296 hlsl += std140PrePaddingString(fieldType, &elementIndex);
297 }
298
299 hlsl += " " + interfaceBlockFieldTypeString(field, blockStorage) +
300 " " + decorate(field.name()) + arrayString(fieldType) + ";\n";
301
302 // must pad out after matrices and arrays, where HLSL usually allows itself room to pack stuff
303 if (blockStorage == EbsStd140)
304 {
305 const bool useHLSLRowMajorPacking = (fieldType.getLayoutQualifier().matrixPacking == EmpColumnMajor);
306 hlsl += std140PostPaddingString(fieldType, useHLSLRowMajorPacking);
307 }
308 }
309
310 return hlsl;
311}
312
313TString OutputHLSL::interfaceBlockStructString(const TInterfaceBlock &interfaceBlock)
314{
315 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
316
317 return "struct " + interfaceBlockStructNameString(interfaceBlock) + "\n"
318 "{\n" +
319 interfaceBlockFieldString(interfaceBlock, blockStorage) +
320 "};\n\n";
321}
322
323TString OutputHLSL::interfaceBlockString(const TInterfaceBlock &interfaceBlock, unsigned int registerIndex, unsigned int arrayIndex)
324{
325 const TString &arrayIndexString = (arrayIndex != GL_INVALID_INDEX ? decorate(str(arrayIndex)) : "");
326 const TString &blockName = interfaceBlock.name() + arrayIndexString;
327 TString hlsl;
328
329 hlsl += "cbuffer " + blockName + " : register(b" + str(registerIndex) + ")\n"
330 "{\n";
331
332 if (interfaceBlock.hasInstanceName())
333 {
334 hlsl += " " + interfaceBlockStructNameString(interfaceBlock) + " " + interfaceBlockInstanceString(interfaceBlock, arrayIndex) + ";\n";
335 }
336 else
337 {
338 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
339 hlsl += interfaceBlockFieldString(interfaceBlock, blockStorage);
340 }
341
342 hlsl += "};\n\n";
343
344 return hlsl;
345}
346
Jamie Madill574d9dd2013-06-20 11:55:56 -0400347TString OutputHLSL::std140PrePaddingString(const TType &type, int *elementIndex)
348{
349 if (type.getBasicType() == EbtStruct || type.isMatrix() || type.isArray())
350 {
351 // no padding needed, HLSL will align the field to a new register
352 *elementIndex = 0;
353 return "";
354 }
355
356 const GLenum glType = glVariableType(type);
357 const int numComponents = gl::UniformComponentCount(glType);
358
359 if (numComponents >= 4)
360 {
361 // no padding needed, HLSL will align the field to a new register
362 *elementIndex = 0;
363 return "";
364 }
365
366 if (*elementIndex + numComponents > 4)
367 {
368 // no padding needed, HLSL will align the field to a new register
369 *elementIndex = numComponents;
370 return "";
371 }
372
373 TString padding;
374
375 const int alignment = numComponents == 3 ? 4 : numComponents;
376 const int paddingOffset = (*elementIndex % alignment);
377
378 if (paddingOffset != 0)
379 {
380 // padding is neccessary
381 for (int paddingIndex = paddingOffset; paddingIndex < alignment; paddingIndex++)
382 {
383 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
384 }
385
386 *elementIndex += (alignment - paddingOffset);
387 }
388
389 *elementIndex += numComponents;
390 *elementIndex %= 4;
391
392 return padding;
393}
394
Jamie Madille4075c92013-06-21 09:15:32 -0400395TString OutputHLSL::std140PostPaddingString(const TType &type, bool useHLSLRowMajorPacking)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400396{
Jamie Madillc835df62013-06-21 09:15:32 -0400397 if (!type.isMatrix() && !type.isArray() && type.getBasicType() != EbtStruct)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400398 {
399 return "";
400 }
401
Jamie Madill574d9dd2013-06-20 11:55:56 -0400402 int numComponents = 0;
403
404 if (type.isMatrix())
405 {
Jamie Madille4075c92013-06-21 09:15:32 -0400406 // This method can also be called from structureString, which does not use layout qualifiers.
407 // Thus, use the method parameter for determining the matrix packing.
408 //
409 // Note HLSL row major packing corresponds to GL API column-major, and vice-versa, since we
410 // wish to always transpose GL matrices to play well with HLSL's matrix array indexing.
411 //
412 const bool isRowMajorMatrix = !useHLSLRowMajorPacking;
Jamie Madillc835df62013-06-21 09:15:32 -0400413 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400414 numComponents = gl::MatrixComponentCount(glType, isRowMajorMatrix);
415 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400416 else if (type.getStruct())
Jamie Madillc835df62013-06-21 09:15:32 -0400417 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400418 const TString &structName = structureTypeName(*type.getStruct(), useHLSLRowMajorPacking, true);
Jamie Madille4075c92013-06-21 09:15:32 -0400419 numComponents = mStd140StructElementIndexes[structName];
420
421 if (numComponents == 0)
422 {
423 return "";
424 }
Jamie Madillc835df62013-06-21 09:15:32 -0400425 }
Jamie Madill574d9dd2013-06-20 11:55:56 -0400426 else
427 {
Jamie Madillc835df62013-06-21 09:15:32 -0400428 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400429 numComponents = gl::UniformComponentCount(glType);
430 }
431
432 TString padding;
433 for (int paddingOffset = numComponents; paddingOffset < 4; paddingOffset++)
434 {
435 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
436 }
437 return padding;
438}
439
Jamie Madill440dc742013-06-20 11:55:55 -0400440// Use the same layout for packed and shared
441void setBlockLayout(InterfaceBlock *interfaceBlock, BlockLayoutType newLayout)
442{
443 interfaceBlock->layout = newLayout;
444 interfaceBlock->blockInfo.clear();
445
446 switch (newLayout)
447 {
448 case BLOCKLAYOUT_SHARED:
449 case BLOCKLAYOUT_PACKED:
450 {
451 HLSLBlockEncoder hlslEncoder(&interfaceBlock->blockInfo);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400452 hlslEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400453 interfaceBlock->dataSize = hlslEncoder.getBlockSize();
454 }
455 break;
456
457 case BLOCKLAYOUT_STANDARD:
458 {
459 Std140BlockEncoder stdEncoder(&interfaceBlock->blockInfo);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400460 stdEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400461 interfaceBlock->dataSize = stdEncoder.getBlockSize();
462 }
463 break;
464
465 default:
466 UNREACHABLE();
467 break;
468 }
469}
470
Jamie Madill574d9dd2013-06-20 11:55:56 -0400471BlockLayoutType convertBlockLayoutType(TLayoutBlockStorage blockStorage)
472{
473 switch (blockStorage)
474 {
475 case EbsPacked: return BLOCKLAYOUT_PACKED;
476 case EbsShared: return BLOCKLAYOUT_SHARED;
477 case EbsStd140: return BLOCKLAYOUT_STANDARD;
478 default: UNREACHABLE(); return BLOCKLAYOUT_SHARED;
479 }
480}
481
Jamie Madill98493dd2013-07-08 14:39:03 -0400482TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400483{
484 TString init;
485
486 TString preIndentString;
487 TString fullIndentString;
488
489 for (int spaces = 0; spaces < (indent * 4); spaces++)
490 {
491 preIndentString += ' ';
492 }
493
494 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
495 {
496 fullIndentString += ' ';
497 }
498
499 init += preIndentString + "{\n";
500
Jamie Madill98493dd2013-07-08 14:39:03 -0400501 const TFieldList &fields = structure.fields();
502 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400503 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400504 const TField &field = *fields[fieldIndex];
505 const TString &fieldName = rhsStructName + "." + decorate(field.name());
506 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400507
Jamie Madill98493dd2013-07-08 14:39:03 -0400508 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400509 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400510 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400511 }
512 else
513 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400514 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400515 }
516 }
517
518 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
519
520 return init;
521}
522
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000523void OutputHLSL::header()
524{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000525 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000526
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000527 TString uniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000528 TString interfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000529 TString varyings;
530 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400531 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000532
533 for (ReferencedSymbols::const_iterator uniform = mReferencedUniforms.begin(); uniform != mReferencedUniforms.end(); uniform++)
534 {
535 const TType &type = uniform->second->getType();
536 const TString &name = uniform->second->getSymbol();
537
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000538 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture
539 {
540 int index = samplerRegister(mReferencedUniforms[name]);
541
Nicolas Capenscb127d32013-07-15 17:26:18 -0400542 uniforms += "uniform " + samplerString(type) + " sampler_" + decorateUniform(name, type) + arrayString(type) +
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000543 " : register(s" + str(index) + ");\n";
544
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000545 uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) +
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000546 " : register(t" + str(index) + ");\n";
547 }
548 else
549 {
550 uniforms += "uniform " + typeString(type) + " " + decorateUniform(name, type) + arrayString(type) +
551 " : register(" + registerString(mReferencedUniforms[name]) + ");\n";
552 }
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000553 }
554
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000555 for (ReferencedSymbols::const_iterator interfaceBlockIt = mReferencedInterfaceBlocks.begin(); interfaceBlockIt != mReferencedInterfaceBlocks.end(); interfaceBlockIt++)
556 {
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000557 const TType &nodeType = interfaceBlockIt->second->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -0400558 const TInterfaceBlock &interfaceBlock = *nodeType.getInterfaceBlock();
559 const TFieldList &fieldList = interfaceBlock.fields();
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000560
Jamie Madill98493dd2013-07-08 14:39:03 -0400561 unsigned int arraySize = static_cast<unsigned int>(interfaceBlock.arraySize());
562 sh::InterfaceBlock activeBlock(interfaceBlock.name().c_str(), arraySize, mInterfaceBlockRegister);
563 for (unsigned int typeIndex = 0; typeIndex < fieldList.size(); typeIndex++)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000564 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400565 const TField &field = *fieldList[typeIndex];
566 const TString &fullUniformName = interfaceBlockFieldString(interfaceBlock, field);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400567 declareInterfaceBlockField(*field.type(), fullUniformName, activeBlock.fields);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000568 }
569
Jamie Madill98493dd2013-07-08 14:39:03 -0400570 mInterfaceBlockRegister += std::max(1u, arraySize);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000571
Jamie Madill98493dd2013-07-08 14:39:03 -0400572 BlockLayoutType blockLayoutType = convertBlockLayoutType(interfaceBlock.blockStorage());
573 setBlockLayout(&activeBlock, blockLayoutType);
Jamie Madill9060a4e2013-08-12 16:22:57 -0700574
575 if (interfaceBlock.matrixPacking() == EmpRowMajor)
576 {
577 activeBlock.isRowMajorLayout = true;
578 }
579
Jamie Madill98493dd2013-07-08 14:39:03 -0400580 mActiveInterfaceBlocks.push_back(activeBlock);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000581
Jamie Madill98493dd2013-07-08 14:39:03 -0400582 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000583 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400584 interfaceBlocks += interfaceBlockStructString(interfaceBlock);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000585 }
586
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000587 if (arraySize > 0)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000588 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000589 for (unsigned int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
590 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400591 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex + arrayIndex, arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000592 }
593 }
594 else
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000595 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400596 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000597 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000598 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000599
Jamie Madill570e04d2013-06-21 09:15:33 -0400600 for (auto flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
601 {
602 TIntermTyped *structNode = flaggedStructIt->first;
603 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400604 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400605 const TString &originalName = mFlaggedStructOriginalNames[structNode];
606
Jamie Madill98493dd2013-07-08 14:39:03 -0400607 flaggedStructs += "static " + decorate(structure.name()) + " " + mappedName + " =\n";
608 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400609 flaggedStructs += "\n";
610 }
611
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000612 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
613 {
614 const TType &type = varying->second->getType();
615 const TString &name = varying->second->getSymbol();
616
617 // Program linking depends on this exact format
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +0000618 varyings += "static " + interpolationString(type.getQualifier()) + " " + typeString(type) + " " +
619 decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madill47fdd132013-08-30 13:21:04 -0400620
621 declareVaryingToList(type, name, mActiveVaryings);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000622 }
623
624 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
625 {
626 const TType &type = attribute->second->getType();
627 const TString &name = attribute->second->getSymbol();
628
629 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400630
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400631 Attribute attributeVar(glVariableType(type), glVariablePrecision(type), name.c_str(),
632 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
633 mActiveAttributes.push_back(attributeVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000634 }
635
Jamie Madill529077d2013-06-20 11:55:54 -0400636 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
637 {
638 out << *structDeclaration;
639 }
640
641 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
642 {
643 out << *constructor;
644 }
645
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400646 if (mContext.shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000648 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000649 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000650
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000651 out << "// Varyings\n";
652 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400653 out << "\n";
654
655 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000656 {
Jamie Madill46131a32013-06-20 11:55:50 -0400657 for (auto outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000658 {
Jamie Madill46131a32013-06-20 11:55:50 -0400659 const TString &variableName = outputVariableIt->first;
660 const TType &variableType = outputVariableIt->second->getType();
661 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
662
663 out << "static " + typeString(variableType) + " out_" + variableName + arrayString(variableType) +
664 " = " + initializer(variableType) + ";\n";
665
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400666 Attribute outputVar(glVariableType(variableType), glVariablePrecision(variableType), variableName.c_str(),
667 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
Jamie Madill46131a32013-06-20 11:55:50 -0400668 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000669 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000670 }
Jamie Madill46131a32013-06-20 11:55:50 -0400671 else
672 {
673 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
674
675 out << "static float4 gl_Color[" << numColorValues << "] =\n"
676 "{\n";
677 for (unsigned int i = 0; i < numColorValues; i++)
678 {
679 out << " float4(0, 0, 0, 0)";
680 if (i + 1 != numColorValues)
681 {
682 out << ",";
683 }
684 out << "\n";
685 }
686
687 out << "};\n";
688 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000689
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400690 if (mUsesFragDepth)
691 {
692 out << "static float gl_Depth = 0.0;\n";
693 }
694
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000695 if (mUsesFragCoord)
696 {
697 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
698 }
699
700 if (mUsesPointCoord)
701 {
702 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
703 }
704
705 if (mUsesFrontFacing)
706 {
707 out << "static bool gl_FrontFacing = false;\n";
708 }
709
710 out << "\n";
711
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000712 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000713 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000714 out << "struct gl_DepthRangeParameters\n"
715 "{\n"
716 " float near;\n"
717 " float far;\n"
718 " float diff;\n"
719 "};\n"
720 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000721 }
722
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000723 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000724 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000725 out << "cbuffer DriverConstants : register(b1)\n"
726 "{\n";
727
728 if (mUsesDepthRange)
729 {
730 out << " float3 dx_DepthRange : packoffset(c0);\n";
731 }
732
733 if (mUsesFragCoord)
734 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000735 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000736 }
737
738 if (mUsesFragCoord || mUsesFrontFacing)
739 {
740 out << " float3 dx_DepthFront : packoffset(c2);\n";
741 }
742
743 out << "};\n";
744 }
745 else
746 {
747 if (mUsesDepthRange)
748 {
749 out << "uniform float3 dx_DepthRange : register(c0);";
750 }
751
752 if (mUsesFragCoord)
753 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000754 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000755 }
756
757 if (mUsesFragCoord || mUsesFrontFacing)
758 {
759 out << "uniform float3 dx_DepthFront : register(c2);\n";
760 }
761 }
762
763 out << "\n";
764
765 if (mUsesDepthRange)
766 {
767 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
768 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000769 }
770
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000771 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000772 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000773
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000774 if (!interfaceBlocks.empty())
775 {
776 out << interfaceBlocks;
777 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400778
779 if (!flaggedStructs.empty())
780 {
781 out << "// Std140 Structures accessed by value\n";
782 out << "\n";
783 out << flaggedStructs;
784 out << "\n";
785 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000786 }
787
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000788 if (usingMRTExtension && mNumRenderTargets > 1)
789 {
790 out << "#define GL_USES_MRT\n";
791 }
792
793 if (mUsesFragColor)
794 {
795 out << "#define GL_USES_FRAG_COLOR\n";
796 }
797
798 if (mUsesFragData)
799 {
800 out << "#define GL_USES_FRAG_DATA\n";
801 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000802 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000803 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000804 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000805 out << "// Attributes\n";
806 out << attributes;
807 out << "\n"
808 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
809
810 if (mUsesPointSize)
811 {
812 out << "static float gl_PointSize = float(1);\n";
813 }
814
815 out << "\n"
816 "// Varyings\n";
817 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000818 out << "\n";
819
820 if (mUsesDepthRange)
821 {
822 out << "struct gl_DepthRangeParameters\n"
823 "{\n"
824 " float near;\n"
825 " float far;\n"
826 " float diff;\n"
827 "};\n"
828 "\n";
829 }
830
831 if (mOutputType == SH_HLSL11_OUTPUT)
832 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000833 if (mUsesDepthRange)
834 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000835 out << "cbuffer DriverConstants : register(b1)\n"
836 "{\n"
837 " float3 dx_DepthRange : packoffset(c0);\n"
838 "};\n"
839 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000840 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000841 }
842 else
843 {
844 if (mUsesDepthRange)
845 {
846 out << "uniform float3 dx_DepthRange : register(c0);\n";
847 }
848
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000849 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000850 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000851 }
852
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000853 if (mUsesDepthRange)
854 {
855 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
856 "\n";
857 }
858
859 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000861
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000862 if (!interfaceBlocks.empty())
863 {
864 out << interfaceBlocks;
865 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400866
867 if (!flaggedStructs.empty())
868 {
869 out << "// Std140 Structures accessed by value\n";
870 out << "\n";
871 out << flaggedStructs;
872 out << "\n";
873 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000874 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400875 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000876
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400877 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
878 {
879 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400880 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000881 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400882 switch(textureFunction->sampler)
883 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400884 case EbtSampler2D: out << "int2 "; break;
885 case EbtSampler3D: out << "int3 "; break;
886 case EbtSamplerCube: out << "int2 "; break;
887 case EbtSampler2DArray: out << "int3 "; break;
888 case EbtISampler2D: out << "int2 "; break;
889 case EbtISampler3D: out << "int3 "; break;
890 case EbtISamplerCube: out << "int2 "; break;
891 case EbtISampler2DArray: out << "int3 "; break;
892 case EbtUSampler2D: out << "int2 "; break;
893 case EbtUSampler3D: out << "int3 "; break;
894 case EbtUSamplerCube: out << "int2 "; break;
895 case EbtUSampler2DArray: out << "int3 "; break;
896 case EbtSampler2DShadow: out << "int2 "; break;
897 case EbtSamplerCubeShadow: out << "int2 "; break;
898 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400899 default: UNREACHABLE();
900 }
901 }
902 else // Sampling function
903 {
904 switch(textureFunction->sampler)
905 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400906 case EbtSampler2D: out << "float4 "; break;
907 case EbtSampler3D: out << "float4 "; break;
908 case EbtSamplerCube: out << "float4 "; break;
909 case EbtSampler2DArray: out << "float4 "; break;
910 case EbtISampler2D: out << "int4 "; break;
911 case EbtISampler3D: out << "int4 "; break;
912 case EbtISamplerCube: out << "int4 "; break;
913 case EbtISampler2DArray: out << "int4 "; break;
914 case EbtUSampler2D: out << "uint4 "; break;
915 case EbtUSampler3D: out << "uint4 "; break;
916 case EbtUSamplerCube: out << "uint4 "; break;
917 case EbtUSampler2DArray: out << "uint4 "; break;
918 case EbtSampler2DShadow: out << "float "; break;
919 case EbtSamplerCubeShadow: out << "float "; break;
920 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400921 default: UNREACHABLE();
922 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000923 }
924
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400925 // Function name
926 out << textureFunction->name();
927
928 // Argument list
929 int hlslCoords = 4;
930
931 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000932 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400933 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000934 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400935 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
936 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
937 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000938 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400939
Nicolas Capens75fb4752013-07-10 15:14:47 -0400940 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000941 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400942 case TextureFunction::IMPLICIT: break;
943 case TextureFunction::BIAS: hlslCoords = 4; break;
944 case TextureFunction::LOD: hlslCoords = 4; break;
945 case TextureFunction::LOD0: hlslCoords = 4; break;
946 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000947 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400948 }
949 else if (mOutputType == SH_HLSL11_OUTPUT)
950 {
951 switch(textureFunction->sampler)
952 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400953 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
954 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
955 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
956 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
957 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
958 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
959 case EbtISamplerCube: out << "TextureCube<int4> x, SamplerState s"; hlslCoords = 3; break;
960 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
961 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
962 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
963 case EbtUSamplerCube: out << "TextureCube<uint4> x, SamplerState s"; hlslCoords = 3; break;
964 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
965 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
966 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
967 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400968 default: UNREACHABLE();
969 }
970 }
971 else UNREACHABLE();
972
973 switch(textureFunction->coords)
974 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400975 case 1: out << ", int lod"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400976 case 2: out << ", float2 t"; break;
977 case 3: out << ", float3 t"; break;
978 case 4: out << ", float4 t"; break;
979 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000980 }
981
Nicolas Capens75fb4752013-07-10 15:14:47 -0400982 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000983 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400984 case TextureFunction::IMPLICIT: break;
985 case TextureFunction::BIAS: out << ", float bias"; break;
986 case TextureFunction::LOD: out << ", float lod"; break;
987 case TextureFunction::LOD0: break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400988 case TextureFunction::SIZE: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400989 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000990 }
991
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400992 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400993 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400994
Nicolas Capens75fb4752013-07-10 15:14:47 -0400995 if (textureFunction->method == TextureFunction::SIZE)
996 {
997 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
998 {
999 if (IsSamplerArray(textureFunction->sampler))
1000 {
1001 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
1002 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
1003 }
1004 else
1005 {
1006 out << " uint width; uint height; uint numberOfLevels;\n"
1007 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
1008 }
1009 }
1010 else if (IsSampler3D(textureFunction->sampler))
1011 {
1012 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
1013 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
1014 }
1015 else UNREACHABLE();
1016
1017 switch(textureFunction->sampler)
1018 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04001019 case EbtSampler2D: out << " return int2(width, height);"; break;
1020 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
1021 case EbtSamplerCube: out << " return int2(width, height);"; break;
1022 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
1023 case EbtISampler2D: out << " return int2(width, height);"; break;
1024 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
1025 case EbtISamplerCube: out << " return int2(width, height);"; break;
1026 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
1027 case EbtUSampler2D: out << " return int2(width, height);"; break;
1028 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
1029 case EbtUSamplerCube: out << " return int2(width, height);"; break;
1030 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
1031 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
1032 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
1033 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001034 default: UNREACHABLE();
1035 }
1036 }
1037 else if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
daniel@transgaming.com15795192011-05-11 15:36:20 +00001038 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001039 // Currently unsupported because TextureCube does not support Load
1040 // This will require emulation using a Texture2DArray with 6 faces
1041 if (textureFunction->sampler == EbtISamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001042 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001043 out << " return int4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001044 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001045 else if (textureFunction->sampler == EbtUSamplerCube)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001046 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001047 out << " return uint4(0, 0, 0, 0);";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001048 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001049 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001050 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001051 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001052 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001053 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001054 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001055 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001056 {
Nicolas Capens93e50de2013-07-09 13:46:28 -04001057 if (IsSamplerArray(textureFunction->sampler))
1058 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001059 out << " float width; float height; float layers; float levels;\n";
1060
1061 if (textureFunction->method == TextureFunction::LOD0)
1062 {
1063 out << " uint mip = 0;\n";
1064 }
1065 else
1066 {
1067 if (textureFunction->method == TextureFunction::IMPLICIT ||
1068 textureFunction->method == TextureFunction::BIAS)
1069 {
1070 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1071 " float2 tSized = float2(t.x * width, t.y * height);\n"
1072 " float dx = length(ddx(tSized));\n"
1073 " float dy = length(ddy(tSized));\n"
1074 " float lod = log2(max(sqrt(dx), sqrt(dy)));\n";
1075
1076 if (textureFunction->method == TextureFunction::BIAS)
1077 {
1078 out << " lod += bias;\n";
1079 }
1080 }
1081
1082 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1083 }
1084
1085 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001086 }
1087 else
1088 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001089 out << " float width; float height; float levels;\n";
1090
1091 if (textureFunction->method == TextureFunction::LOD0)
1092 {
1093 out << " uint mip = 0;\n";
1094 }
1095 else
1096 {
1097 if (textureFunction->method == TextureFunction::IMPLICIT ||
1098 textureFunction->method == TextureFunction::BIAS)
1099 {
1100 out << " x.GetDimensions(0, width, height, levels);\n"
1101 " float2 tSized = float2(t.x * width, t.y * height);\n"
1102 " float dx = length(ddx(tSized));\n"
1103 " float dy = length(ddy(tSized));\n"
1104 " float lod = log2(max(sqrt(dx), sqrt(dy)));\n";
1105
1106 if (textureFunction->method == TextureFunction::BIAS)
1107 {
1108 out << " lod += bias;\n";
1109 }
1110 }
1111
1112 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1113 }
1114
1115 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001116 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001117 }
1118 else if (IsSampler3D(textureFunction->sampler))
1119 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001120 out << " float width; float height; float depth; float levels;\n";
1121
1122 if (textureFunction->method == TextureFunction::LOD0)
1123 {
1124 out << " uint mip = 0;\n";
1125 }
1126 else
1127 {
1128 if (textureFunction->method == TextureFunction::IMPLICIT ||
1129 textureFunction->method == TextureFunction::BIAS)
1130 {
1131 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1132 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
1133 " float dx = length(ddx(tSized));\n"
1134 " float dy = length(ddy(tSized));\n"
1135 " float lod = log2(max(sqrt(dx), sqrt(dy)));\n";
1136
1137 if (textureFunction->method == TextureFunction::BIAS)
1138 {
1139 out << " lod += bias;\n";
1140 }
1141 }
1142
1143 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1144 }
1145
1146 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001147 }
1148 else UNREACHABLE();
1149 }
1150
1151 out << " return ";
1152
1153 // HLSL intrinsic
1154 if (mOutputType == SH_HLSL9_OUTPUT)
1155 {
1156 switch(textureFunction->sampler)
1157 {
1158 case EbtSampler2D: out << "tex2D"; break;
1159 case EbtSamplerCube: out << "texCUBE"; break;
1160 default: UNREACHABLE();
1161 }
1162
Nicolas Capens75fb4752013-07-10 15:14:47 -04001163 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001164 {
1165 case TextureFunction::IMPLICIT: out << "(s, "; break;
1166 case TextureFunction::BIAS: out << "bias(s, "; break;
1167 case TextureFunction::LOD: out << "lod(s, "; break;
1168 case TextureFunction::LOD0: out << "lod(s, "; break;
1169 default: UNREACHABLE();
1170 }
1171 }
1172 else if (mOutputType == SH_HLSL11_OUTPUT)
1173 {
1174 if (IsIntegerSampler(textureFunction->sampler))
1175 {
1176 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001177 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001178 else if (IsShadowSampler(textureFunction->sampler))
1179 {
1180 out << "x.SampleCmp(s, ";
1181 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001182 else
1183 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001184 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001185 {
1186 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1187 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1188 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1189 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
1190 default: UNREACHABLE();
1191 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001192 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001193 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001194 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001195
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001196 // Integer sampling requires integer addresses
1197 TString addressx = "";
1198 TString addressy = "";
1199 TString addressz = "";
1200 TString close = "";
1201
1202 if (IsIntegerSampler(textureFunction->sampler))
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001203 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001204 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001205 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001206 case 2: out << "int3("; break;
1207 case 3: out << "int4("; break;
1208 default: UNREACHABLE();
1209 }
1210
Nicolas Capensc98406a2013-07-10 14:52:44 -04001211 addressx = "int(floor(width * frac((";
1212 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001213
1214 if (IsSamplerArray(textureFunction->sampler))
1215 {
1216 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1217 }
1218 else
1219 {
Nicolas Capensc98406a2013-07-10 14:52:44 -04001220 addressz = "int(floor(depth * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001221 }
1222
1223 close = "))))";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001224 }
1225 else
1226 {
1227 switch(hlslCoords)
1228 {
1229 case 2: out << "float2("; break;
1230 case 3: out << "float3("; break;
1231 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001232 default: UNREACHABLE();
1233 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001234 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001235
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001236 TString proj = ""; // Only used for projected textures
1237
1238 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001239 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001240 switch(textureFunction->coords)
1241 {
1242 case 3: proj = " / t.z"; break;
1243 case 4: proj = " / t.w"; break;
1244 default: UNREACHABLE();
1245 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001246 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001247
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001248 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001249
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001250 if (mOutputType == SH_HLSL9_OUTPUT)
1251 {
1252 if (hlslCoords >= 3)
1253 {
1254 if (textureFunction->coords < 3)
1255 {
1256 out << ", 0";
1257 }
1258 else
1259 {
1260 out << ", t.z" + proj;
1261 }
1262 }
1263
1264 if (hlslCoords == 4)
1265 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001266 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001267 {
1268 case TextureFunction::BIAS: out << ", bias"; break;
1269 case TextureFunction::LOD: out << ", lod"; break;
1270 case TextureFunction::LOD0: out << ", 0"; break;
1271 default: UNREACHABLE();
1272 }
1273 }
1274
1275 out << "));\n";
1276 }
1277 else if (mOutputType == SH_HLSL11_OUTPUT)
1278 {
1279 if (hlslCoords >= 3)
1280 {
1281 out << ", " + addressz + ("t.z" + proj) + close;
1282 }
1283
Nicolas Capenscb127d32013-07-15 17:26:18 -04001284 if (IsIntegerSampler(textureFunction->sampler))
1285 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001286 out << ", mip));";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001287 }
1288 else if (IsShadowSampler(textureFunction->sampler))
1289 {
1290 // Compare value
1291 switch(textureFunction->coords)
1292 {
1293 case 3: out << "), t.z);"; break;
1294 case 4: out << "), t.w);"; break;
1295 default: UNREACHABLE();
1296 }
1297 }
1298 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001299 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001300 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001301 {
1302 case TextureFunction::IMPLICIT: out << "));"; break;
1303 case TextureFunction::BIAS: out << "), bias);"; break;
1304 case TextureFunction::LOD: out << "), lod);"; break;
1305 case TextureFunction::LOD0: out << "), 0);"; break;
1306 default: UNREACHABLE();
1307 }
1308 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001309 }
1310 else UNREACHABLE();
1311 }
1312
1313 out << "\n"
1314 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001315 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001316 }
1317
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001318 if (mUsesFragCoord)
1319 {
1320 out << "#define GL_USES_FRAG_COORD\n";
1321 }
1322
1323 if (mUsesPointCoord)
1324 {
1325 out << "#define GL_USES_POINT_COORD\n";
1326 }
1327
1328 if (mUsesFrontFacing)
1329 {
1330 out << "#define GL_USES_FRONT_FACING\n";
1331 }
1332
1333 if (mUsesPointSize)
1334 {
1335 out << "#define GL_USES_POINT_SIZE\n";
1336 }
1337
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001338 if (mUsesFragDepth)
1339 {
1340 out << "#define GL_USES_FRAG_DEPTH\n";
1341 }
1342
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001343 if (mUsesDepthRange)
1344 {
1345 out << "#define GL_USES_DEPTH_RANGE\n";
1346 }
1347
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001348 if (mUsesXor)
1349 {
1350 out << "bool xor(bool p, bool q)\n"
1351 "{\n"
1352 " return (p || q) && !(p && q);\n"
1353 "}\n"
1354 "\n";
1355 }
1356
1357 if (mUsesMod1)
1358 {
1359 out << "float mod(float x, float y)\n"
1360 "{\n"
1361 " return x - y * floor(x / y);\n"
1362 "}\n"
1363 "\n";
1364 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001365
1366 if (mUsesMod2v)
1367 {
1368 out << "float2 mod(float2 x, float2 y)\n"
1369 "{\n"
1370 " return x - y * floor(x / y);\n"
1371 "}\n"
1372 "\n";
1373 }
1374
1375 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001376 {
1377 out << "float2 mod(float2 x, float y)\n"
1378 "{\n"
1379 " return x - y * floor(x / y);\n"
1380 "}\n"
1381 "\n";
1382 }
1383
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001384 if (mUsesMod3v)
1385 {
1386 out << "float3 mod(float3 x, float3 y)\n"
1387 "{\n"
1388 " return x - y * floor(x / y);\n"
1389 "}\n"
1390 "\n";
1391 }
1392
1393 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001394 {
1395 out << "float3 mod(float3 x, float y)\n"
1396 "{\n"
1397 " return x - y * floor(x / y);\n"
1398 "}\n"
1399 "\n";
1400 }
1401
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001402 if (mUsesMod4v)
1403 {
1404 out << "float4 mod(float4 x, float4 y)\n"
1405 "{\n"
1406 " return x - y * floor(x / y);\n"
1407 "}\n"
1408 "\n";
1409 }
1410
1411 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001412 {
1413 out << "float4 mod(float4 x, float y)\n"
1414 "{\n"
1415 " return x - y * floor(x / y);\n"
1416 "}\n"
1417 "\n";
1418 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001419
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001420 if (mUsesFaceforward1)
1421 {
1422 out << "float faceforward(float N, float I, float Nref)\n"
1423 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001424 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001425 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001426 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001427 " }\n"
1428 " else\n"
1429 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001430 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001431 " }\n"
1432 "}\n"
1433 "\n";
1434 }
1435
1436 if (mUsesFaceforward2)
1437 {
1438 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1439 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001440 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001441 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001442 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001443 " }\n"
1444 " else\n"
1445 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001446 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001447 " }\n"
1448 "}\n"
1449 "\n";
1450 }
1451
1452 if (mUsesFaceforward3)
1453 {
1454 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1455 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001456 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001457 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001458 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001459 " }\n"
1460 " else\n"
1461 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001462 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001463 " }\n"
1464 "}\n"
1465 "\n";
1466 }
1467
1468 if (mUsesFaceforward4)
1469 {
1470 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1471 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001472 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001473 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001474 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001475 " }\n"
1476 " else\n"
1477 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001478 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001479 " }\n"
1480 "}\n"
1481 "\n";
1482 }
1483
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001484 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001485 {
1486 out << "float atanyx(float y, float x)\n"
1487 "{\n"
1488 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1489 " return atan2(y, x);\n"
1490 "}\n";
1491 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001492
1493 if (mUsesAtan2_2)
1494 {
1495 out << "float2 atanyx(float2 y, float2 x)\n"
1496 "{\n"
1497 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1498 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1499 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1500 "}\n";
1501 }
1502
1503 if (mUsesAtan2_3)
1504 {
1505 out << "float3 atanyx(float3 y, float3 x)\n"
1506 "{\n"
1507 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1508 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1509 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1510 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1511 "}\n";
1512 }
1513
1514 if (mUsesAtan2_4)
1515 {
1516 out << "float4 atanyx(float4 y, float4 x)\n"
1517 "{\n"
1518 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1519 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1520 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1521 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1522 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1523 "}\n";
1524 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001525}
1526
1527void OutputHLSL::visitSymbol(TIntermSymbol *node)
1528{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001529 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001530
Jamie Madill570e04d2013-06-21 09:15:33 -04001531 // Handle accessing std140 structs by value
1532 if (mFlaggedStructMappedNames.count(node) > 0)
1533 {
1534 out << mFlaggedStructMappedNames[node];
1535 return;
1536 }
1537
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001538 TString name = node->getSymbol();
1539
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001540 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001541 {
1542 mUsesDepthRange = true;
1543 out << name;
1544 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001545 else
1546 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001547 TQualifier qualifier = node->getQualifier();
1548
1549 if (qualifier == EvqUniform)
1550 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001551 const TType& nodeType = node->getType();
1552 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1553
1554 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001555 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001556 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001557 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001558 else
1559 {
1560 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001561 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001562
1563 out << decorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001564 }
Jamie Madill19571812013-08-12 15:26:34 -07001565 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001566 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001567 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001568 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001569 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001570 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001571 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001572 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001573 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001574 }
Jamie Madill19571812013-08-12 15:26:34 -07001575 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001576 {
1577 mReferencedOutputVariables[name] = node;
1578 out << "out_" << name;
1579 }
1580 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001581 {
1582 out << "gl_Color[0]";
1583 mUsesFragColor = true;
1584 }
1585 else if (qualifier == EvqFragData)
1586 {
1587 out << "gl_Color";
1588 mUsesFragData = true;
1589 }
1590 else if (qualifier == EvqFragCoord)
1591 {
1592 mUsesFragCoord = true;
1593 out << name;
1594 }
1595 else if (qualifier == EvqPointCoord)
1596 {
1597 mUsesPointCoord = true;
1598 out << name;
1599 }
1600 else if (qualifier == EvqFrontFacing)
1601 {
1602 mUsesFrontFacing = true;
1603 out << name;
1604 }
1605 else if (qualifier == EvqPointSize)
1606 {
1607 mUsesPointSize = true;
1608 out << name;
1609 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001610 else if (name == "gl_FragDepthEXT")
1611 {
1612 mUsesFragDepth = true;
1613 out << "gl_Depth";
1614 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001615 else
1616 {
1617 out << decorate(name);
1618 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001619 }
1620}
1621
1622bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1623{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001624 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001625
Jamie Madill570e04d2013-06-21 09:15:33 -04001626 // Handle accessing std140 structs by value
1627 if (mFlaggedStructMappedNames.count(node) > 0)
1628 {
1629 out << mFlaggedStructMappedNames[node];
1630 return false;
1631 }
1632
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001633 switch (node->getOp())
1634 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001635 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001636 case EOpInitialize:
1637 if (visit == PreVisit)
1638 {
1639 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1640 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1641 // new variable is created before the assignment is evaluated), so we need to convert
1642 // this to "float t = x, x = t;".
1643
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001644 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1645 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001646
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001647 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1648 expression->traverse(&searchSymbol);
1649 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001650
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001651 if (sameSymbol)
1652 {
1653 // Type already printed
1654 out << "t" + str(mUniqueIndex) + " = ";
1655 expression->traverse(this);
1656 out << ", ";
1657 symbolNode->traverse(this);
1658 out << " = t" + str(mUniqueIndex);
1659
1660 mUniqueIndex++;
1661 return false;
1662 }
1663 }
1664 else if (visit == InVisit)
1665 {
1666 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001667 }
1668 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001669 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1670 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1671 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1672 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1673 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1674 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001675 if (visit == PreVisit)
1676 {
1677 out << "(";
1678 }
1679 else if (visit == InVisit)
1680 {
1681 out << " = mul(";
1682 node->getLeft()->traverse(this);
1683 out << ", transpose(";
1684 }
1685 else
1686 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001687 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001688 }
1689 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001690 case EOpMatrixTimesMatrixAssign:
1691 if (visit == PreVisit)
1692 {
1693 out << "(";
1694 }
1695 else if (visit == InVisit)
1696 {
1697 out << " = mul(";
1698 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001699 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001700 }
1701 else
1702 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001703 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001704 }
1705 break;
1706 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001707 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001708 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001709 const TType& leftType = node->getLeft()->getType();
1710 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001711 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001712 if (visit == PreVisit)
1713 {
1714 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1715 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1716
1717 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1718 out << interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1719
1720 return false;
1721 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001722 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001723 else
1724 {
1725 outputTriplet(visit, "", "[", "]");
1726 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001727 }
1728 break;
1729 case EOpIndexIndirect:
1730 // We do not currently support indirect references to interface blocks
1731 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1732 outputTriplet(visit, "", "[", "]");
1733 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001734 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001735 if (visit == InVisit)
1736 {
1737 const TStructure* structure = node->getLeft()->getType().getStruct();
1738 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1739 const TField* field = structure->fields()[index->getIConst(0)];
1740 out << "." + decorateField(field->name(), *structure);
1741
1742 return false;
1743 }
1744 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001745 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001746 if (visit == InVisit)
1747 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001748 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1749 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1750 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
1751 out << "." + decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001752
1753 return false;
1754 }
1755 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001756 case EOpVectorSwizzle:
1757 if (visit == InVisit)
1758 {
1759 out << ".";
1760
1761 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1762
1763 if (swizzle)
1764 {
1765 TIntermSequence &sequence = swizzle->getSequence();
1766
1767 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1768 {
1769 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1770
1771 if (element)
1772 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001773 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001774
1775 switch (i)
1776 {
1777 case 0: out << "x"; break;
1778 case 1: out << "y"; break;
1779 case 2: out << "z"; break;
1780 case 3: out << "w"; break;
1781 default: UNREACHABLE();
1782 }
1783 }
1784 else UNREACHABLE();
1785 }
1786 }
1787 else UNREACHABLE();
1788
1789 return false; // Fully processed
1790 }
1791 break;
1792 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1793 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1794 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1795 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001796 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001797 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001798 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001799 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001800 if (node->getOp() == EOpEqual)
1801 {
1802 outputTriplet(visit, "(", " == ", ")");
1803 }
1804 else
1805 {
1806 outputTriplet(visit, "(", " != ", ")");
1807 }
1808 }
1809 else if (node->getLeft()->getBasicType() == EbtStruct)
1810 {
1811 if (node->getOp() == EOpEqual)
1812 {
1813 out << "(";
1814 }
1815 else
1816 {
1817 out << "!(";
1818 }
1819
Jamie Madill98493dd2013-07-08 14:39:03 -04001820 const TStructure &structure = *node->getLeft()->getType().getStruct();
1821 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001822
Jamie Madill98493dd2013-07-08 14:39:03 -04001823 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001824 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001825 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001826
1827 node->getLeft()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001828 out << "." + decorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001829 node->getRight()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04001830 out << "." + decorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001831
Jamie Madill98493dd2013-07-08 14:39:03 -04001832 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001833 {
1834 out << " && ";
1835 }
1836 }
1837
1838 out << ")";
1839
1840 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001841 }
1842 else
1843 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001844 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001845
1846 if (node->getOp() == EOpEqual)
1847 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001848 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001849 }
1850 else
1851 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001852 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001853 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001854 }
1855 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001856 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1857 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1858 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1859 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1860 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001861 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001862 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1863 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001864 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001865 case EOpLogicalOr:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001866 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001867 return false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001868 case EOpLogicalXor:
1869 mUsesXor = true;
1870 outputTriplet(visit, "xor(", ", ", ")");
1871 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001872 case EOpLogicalAnd:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001873 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001874 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001875 default: UNREACHABLE();
1876 }
1877
1878 return true;
1879}
1880
1881bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1882{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001883 switch (node->getOp())
1884 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001885 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1886 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1887 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1888 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1889 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1890 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1891 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001892 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04001893 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001894 case EOpConvFloatToBool:
1895 switch (node->getOperand()->getType().getNominalSize())
1896 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001897 case 1: outputTriplet(visit, "bool(", "", ")"); break;
1898 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
1899 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
1900 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001901 default: UNREACHABLE();
1902 }
1903 break;
1904 case EOpConvBoolToFloat:
1905 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04001906 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001907 switch (node->getOperand()->getType().getNominalSize())
1908 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001909 case 1: outputTriplet(visit, "float(", "", ")"); break;
1910 case 2: outputTriplet(visit, "float2(", "", ")"); break;
1911 case 3: outputTriplet(visit, "float3(", "", ")"); break;
1912 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001913 default: UNREACHABLE();
1914 }
1915 break;
1916 case EOpConvFloatToInt:
1917 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04001918 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001919 switch (node->getOperand()->getType().getNominalSize())
1920 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001921 case 1: outputTriplet(visit, "int(", "", ")"); break;
1922 case 2: outputTriplet(visit, "int2(", "", ")"); break;
1923 case 3: outputTriplet(visit, "int3(", "", ")"); break;
1924 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001925 default: UNREACHABLE();
1926 }
1927 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04001928 case EOpConvFloatToUInt:
1929 case EOpConvBoolToUInt:
1930 case EOpConvIntToUInt:
Jamie Madilla16f1672013-07-03 15:15:17 -04001931 switch (node->getOperand()->getType().getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001932 {
1933 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00001934 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
1935 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
1936 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001937 default: UNREACHABLE();
1938 }
1939 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001940 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1941 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1942 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1943 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1944 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1945 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1946 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1947 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1948 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1949 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1950 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1951 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1952 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1953 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1954 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1955 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1956 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1957 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1958 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1959 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1960 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001961 case EOpDFdx:
1962 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1963 {
1964 outputTriplet(visit, "(", "", ", 0.0)");
1965 }
1966 else
1967 {
1968 outputTriplet(visit, "ddx(", "", ")");
1969 }
1970 break;
1971 case EOpDFdy:
1972 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1973 {
1974 outputTriplet(visit, "(", "", ", 0.0)");
1975 }
1976 else
1977 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00001978 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001979 }
1980 break;
1981 case EOpFwidth:
1982 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1983 {
1984 outputTriplet(visit, "(", "", ", 0.0)");
1985 }
1986 else
1987 {
1988 outputTriplet(visit, "fwidth(", "", ")");
1989 }
1990 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001991 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1992 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001993 default: UNREACHABLE();
1994 }
1995
1996 return true;
1997}
1998
1999bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2000{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002001 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002002
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002003 switch (node->getOp())
2004 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002005 case EOpSequence:
2006 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002007 if (mInsideFunction)
2008 {
Jamie Madill075edd82013-07-08 13:30:19 -04002009 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002010 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002011
2012 mScopeDepth++;
2013
2014 if (mScopeBracket.size() < mScopeDepth)
2015 {
2016 mScopeBracket.push_back(0); // New scope level
2017 }
2018 else
2019 {
2020 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
2021 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002022 }
2023
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002024 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
2025 {
Jamie Madill075edd82013-07-08 13:30:19 -04002026 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002027
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002028 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002029
2030 out << ";\n";
2031 }
2032
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002033 if (mInsideFunction)
2034 {
Jamie Madill075edd82013-07-08 13:30:19 -04002035 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002036 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002037
2038 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002039 }
2040
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002041 return false;
2042 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002043 case EOpDeclaration:
2044 if (visit == PreVisit)
2045 {
2046 TIntermSequence &sequence = node->getSequence();
2047 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002048
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00002049 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002050 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002051 if (variable->getType().getStruct())
2052 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002053 addConstructor(variable->getType(), scopedStruct(variable->getType().getStruct()->name()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00002054 }
2055
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002056 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002057 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00002058 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002059 {
2060 out << "static ";
2061 }
2062
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002063 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002064
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002065 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002066 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002067 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002068
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002069 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002070 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002071 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002072 out << arrayString(symbol->getType());
daniel@transgaming.com7127f202010-04-15 20:45:22 +00002073 out << " = " + initializer(variable->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002074 }
2075 else
2076 {
2077 (*sit)->traverse(this);
2078 }
2079
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002080 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002081 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002082 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002083 }
2084 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002085 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002086 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2087 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002088 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002089 }
2090 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002091 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002092 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002093 {
2094 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2095 {
2096 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2097
2098 if (symbol)
2099 {
2100 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2101 mReferencedVaryings[symbol->getSymbol()] = symbol;
2102 }
2103 else
2104 {
2105 (*sit)->traverse(this);
2106 }
2107 }
2108 }
2109
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002110 return false;
2111 }
2112 else if (visit == InVisit)
2113 {
2114 out << ", ";
2115 }
2116 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002117 case EOpPrototype:
2118 if (visit == PreVisit)
2119 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002120 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002121
2122 TIntermSequence &arguments = node->getSequence();
2123
2124 for (unsigned int i = 0; i < arguments.size(); i++)
2125 {
2126 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2127
2128 if (symbol)
2129 {
2130 out << argumentString(symbol);
2131
2132 if (i < arguments.size() - 1)
2133 {
2134 out << ", ";
2135 }
2136 }
2137 else UNREACHABLE();
2138 }
2139
2140 out << ");\n";
2141
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002142 // Also prototype the Lod0 variant if needed
2143 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2144 {
2145 mOutputLod0Function = true;
2146 node->traverse(this);
2147 mOutputLod0Function = false;
2148 }
2149
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002150 return false;
2151 }
2152 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002153 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002154 case EOpFunction:
2155 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002156 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002157
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002158 out << typeString(node->getType()) << " ";
2159
2160 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002161 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002162 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002163 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002164 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002165 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002166 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002167 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002168
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002169 TIntermSequence &sequence = node->getSequence();
2170 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2171
2172 for (unsigned int i = 0; i < arguments.size(); i++)
2173 {
2174 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2175
2176 if (symbol)
2177 {
2178 if (symbol->getType().getStruct())
2179 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002180 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getStruct()->name()), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002181 }
2182
2183 out << argumentString(symbol);
2184
2185 if (i < arguments.size() - 1)
2186 {
2187 out << ", ";
2188 }
2189 }
2190 else UNREACHABLE();
2191 }
2192
2193 out << ")\n"
2194 "{\n";
2195
2196 if (sequence.size() > 1)
2197 {
2198 mInsideFunction = true;
2199 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002200 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002201 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002202
2203 out << "}\n";
2204
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002205 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2206 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002207 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002208 {
2209 mOutputLod0Function = true;
2210 node->traverse(this);
2211 mOutputLod0Function = false;
2212 }
2213 }
2214
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002215 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002216 }
2217 break;
2218 case EOpFunctionCall:
2219 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002220 TString name = TFunction::unmangleName(node->getName());
2221 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002222 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002223
2224 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002225 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002226 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002227 }
2228 else
2229 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002230 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2231
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002232 TextureFunction textureFunction;
2233 textureFunction.sampler = samplerType;
2234 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002235 textureFunction.method = TextureFunction::IMPLICIT;
2236 textureFunction.proj = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002237
2238 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002239 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002240 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002241 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002242 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002243 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002244 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002245 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002246 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002247 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002248 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002249 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002250 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002251 else if (name == "texture2DProjLod" || name == "textureProjLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002252 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002253 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002254 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002255 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002256 else if (name == "textureSize")
2257 {
2258 textureFunction.method = TextureFunction::SIZE;
2259 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002260 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002261
Nicolas Capens75fb4752013-07-10 15:14:47 -04002262 if (textureFunction.method != TextureFunction::LOD &&
2263 textureFunction.method != TextureFunction::SIZE)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002264 {
2265 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2266 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002267 textureFunction.method = TextureFunction::LOD0;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002268 }
2269 else if (arguments.size() == 3)
2270 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002271 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002272 }
2273 }
2274
2275 mUsesTexture.insert(textureFunction);
2276
2277 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002278 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002279
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002280 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2281 {
2282 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2283 {
2284 out << "texture_";
2285 (*arg)->traverse(this);
2286 out << ", sampler_";
2287 }
2288
2289 (*arg)->traverse(this);
2290
2291 if (arg < arguments.end() - 1)
2292 {
2293 out << ", ";
2294 }
2295 }
2296
2297 out << ")";
2298
2299 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002300 }
2301 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002302 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002303 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002304 addConstructor(node->getType(), "vec1", &node->getSequence());
2305 outputTriplet(visit, "vec1(", "", ")");
2306 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002307 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002308 addConstructor(node->getType(), "vec2", &node->getSequence());
2309 outputTriplet(visit, "vec2(", ", ", ")");
2310 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002311 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002312 addConstructor(node->getType(), "vec3", &node->getSequence());
2313 outputTriplet(visit, "vec3(", ", ", ")");
2314 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002315 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002316 addConstructor(node->getType(), "vec4", &node->getSequence());
2317 outputTriplet(visit, "vec4(", ", ", ")");
2318 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002319 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002320 addConstructor(node->getType(), "bvec1", &node->getSequence());
2321 outputTriplet(visit, "bvec1(", "", ")");
2322 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002323 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002324 addConstructor(node->getType(), "bvec2", &node->getSequence());
2325 outputTriplet(visit, "bvec2(", ", ", ")");
2326 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002327 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002328 addConstructor(node->getType(), "bvec3", &node->getSequence());
2329 outputTriplet(visit, "bvec3(", ", ", ")");
2330 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002331 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002332 addConstructor(node->getType(), "bvec4", &node->getSequence());
2333 outputTriplet(visit, "bvec4(", ", ", ")");
2334 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002335 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002336 addConstructor(node->getType(), "ivec1", &node->getSequence());
2337 outputTriplet(visit, "ivec1(", "", ")");
2338 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002339 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002340 addConstructor(node->getType(), "ivec2", &node->getSequence());
2341 outputTriplet(visit, "ivec2(", ", ", ")");
2342 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002343 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002344 addConstructor(node->getType(), "ivec3", &node->getSequence());
2345 outputTriplet(visit, "ivec3(", ", ", ")");
2346 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002347 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002348 addConstructor(node->getType(), "ivec4", &node->getSequence());
2349 outputTriplet(visit, "ivec4(", ", ", ")");
2350 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002351 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002352 addConstructor(node->getType(), "uvec1", &node->getSequence());
2353 outputTriplet(visit, "uvec1(", "", ")");
2354 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002355 case EOpConstructUVec2:
2356 addConstructor(node->getType(), "uvec2", &node->getSequence());
2357 outputTriplet(visit, "uvec2(", ", ", ")");
2358 break;
2359 case EOpConstructUVec3:
2360 addConstructor(node->getType(), "uvec3", &node->getSequence());
2361 outputTriplet(visit, "uvec3(", ", ", ")");
2362 break;
2363 case EOpConstructUVec4:
2364 addConstructor(node->getType(), "uvec4", &node->getSequence());
2365 outputTriplet(visit, "uvec4(", ", ", ")");
2366 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002367 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002368 addConstructor(node->getType(), "mat2", &node->getSequence());
2369 outputTriplet(visit, "mat2(", ", ", ")");
2370 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002371 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002372 addConstructor(node->getType(), "mat3", &node->getSequence());
2373 outputTriplet(visit, "mat3(", ", ", ")");
2374 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002375 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002376 addConstructor(node->getType(), "mat4", &node->getSequence());
2377 outputTriplet(visit, "mat4(", ", ", ")");
2378 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002379 case EOpConstructStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04002380 addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence());
2381 outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002382 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002383 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2384 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2385 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2386 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2387 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2388 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002389 case EOpMod:
2390 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002391 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002392 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2393 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2394 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002395 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002396 case 11: mUsesMod1 = true; break;
2397 case 22: mUsesMod2v = true; break;
2398 case 21: mUsesMod2f = true; break;
2399 case 33: mUsesMod3v = true; break;
2400 case 31: mUsesMod3f = true; break;
2401 case 44: mUsesMod4v = true; break;
2402 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002403 default: UNREACHABLE();
2404 }
2405
2406 outputTriplet(visit, "mod(", ", ", ")");
2407 }
2408 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002409 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002410 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002411 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002412 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2413 {
2414 case 1: mUsesAtan2_1 = true; break;
2415 case 2: mUsesAtan2_2 = true; break;
2416 case 3: mUsesAtan2_3 = true; break;
2417 case 4: mUsesAtan2_4 = true; break;
2418 default: UNREACHABLE();
2419 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002420 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002421 break;
2422 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2423 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2424 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2425 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2426 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2427 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2428 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2429 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2430 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002431 case EOpFaceForward:
2432 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002433 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002434 {
2435 case 1: mUsesFaceforward1 = true; break;
2436 case 2: mUsesFaceforward2 = true; break;
2437 case 3: mUsesFaceforward3 = true; break;
2438 case 4: mUsesFaceforward4 = true; break;
2439 default: UNREACHABLE();
2440 }
2441
2442 outputTriplet(visit, "faceforward(", ", ", ")");
2443 }
2444 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002445 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2446 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2447 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002448 default: UNREACHABLE();
2449 }
2450
2451 return true;
2452}
2453
2454bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2455{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002456 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002457
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002458 if (node->usesTernaryOperator())
2459 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002460 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002461 }
2462 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002463 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002464 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002465
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002466 out << "if(";
2467
2468 node->getCondition()->traverse(this);
2469
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002470 out << ")\n";
2471
Jamie Madill075edd82013-07-08 13:30:19 -04002472 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002473 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002474
daniel@transgaming.combb885322010-04-15 20:45:24 +00002475 if (node->getTrueBlock())
2476 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002477 traverseStatements(node->getTrueBlock());
daniel@transgaming.combb885322010-04-15 20:45:24 +00002478 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002479
Jamie Madill075edd82013-07-08 13:30:19 -04002480 outputLineDirective(node->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 if (node->getFalseBlock())
2484 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002485 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002486
Jamie Madill075edd82013-07-08 13:30:19 -04002487 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002488 out << "{\n";
2489
Jamie Madill075edd82013-07-08 13:30:19 -04002490 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002491 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002492
Jamie Madill075edd82013-07-08 13:30:19 -04002493 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002494 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002495 }
2496 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002497
2498 return false;
2499}
2500
2501void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2502{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002503 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002504}
2505
2506bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2507{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002508 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2509
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002510 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002511 {
2512 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2513 }
2514
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002515 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002516 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002517 if (handleExcessiveLoop(node))
2518 {
2519 return false;
2520 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002521 }
2522
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002523 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002524
alokp@chromium.org52813552010-11-16 18:36:09 +00002525 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002526 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002527 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002528
Jamie Madill075edd82013-07-08 13:30:19 -04002529 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002530 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002531 }
2532 else
2533 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002534 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002535
2536 if (node->getInit())
2537 {
2538 node->getInit()->traverse(this);
2539 }
2540
2541 out << "; ";
2542
alokp@chromium.org52813552010-11-16 18:36:09 +00002543 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002544 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002545 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002546 }
2547
2548 out << "; ";
2549
alokp@chromium.org52813552010-11-16 18:36:09 +00002550 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002551 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002552 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002553 }
2554
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002555 out << ")\n";
2556
Jamie Madill075edd82013-07-08 13:30:19 -04002557 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002558 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002559 }
2560
2561 if (node->getBody())
2562 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002563 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002564 }
2565
Jamie Madill075edd82013-07-08 13:30:19 -04002566 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002567 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002568
alokp@chromium.org52813552010-11-16 18:36:09 +00002569 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002570 {
Jamie Madill075edd82013-07-08 13:30:19 -04002571 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002572 out << "while(\n";
2573
alokp@chromium.org52813552010-11-16 18:36:09 +00002574 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002575
daniel@transgaming.com73536982012-03-21 20:45:49 +00002576 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002577 }
2578
daniel@transgaming.com73536982012-03-21 20:45:49 +00002579 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002580
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002581 mInsideDiscontinuousLoop = wasDiscontinuous;
2582
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002583 return false;
2584}
2585
2586bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2587{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002588 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002589
2590 switch (node->getFlowOp())
2591 {
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002592 case EOpKill: outputTriplet(visit, "discard;\n", "", ""); break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002593 case EOpBreak:
2594 if (visit == PreVisit)
2595 {
2596 if (mExcessiveLoopIndex)
2597 {
2598 out << "{Break";
2599 mExcessiveLoopIndex->traverse(this);
2600 out << " = true; break;}\n";
2601 }
2602 else
2603 {
2604 out << "break;\n";
2605 }
2606 }
2607 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002608 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002609 case EOpReturn:
2610 if (visit == PreVisit)
2611 {
2612 if (node->getExpression())
2613 {
2614 out << "return ";
2615 }
2616 else
2617 {
2618 out << "return;\n";
2619 }
2620 }
2621 else if (visit == PostVisit)
2622 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002623 if (node->getExpression())
2624 {
2625 out << ";\n";
2626 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002627 }
2628 break;
2629 default: UNREACHABLE();
2630 }
2631
2632 return true;
2633}
2634
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002635void OutputHLSL::traverseStatements(TIntermNode *node)
2636{
2637 if (isSingleStatement(node))
2638 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002639 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002640 }
2641
2642 node->traverse(this);
2643}
2644
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002645bool OutputHLSL::isSingleStatement(TIntermNode *node)
2646{
2647 TIntermAggregate *aggregate = node->getAsAggregate();
2648
2649 if (aggregate)
2650 {
2651 if (aggregate->getOp() == EOpSequence)
2652 {
2653 return false;
2654 }
2655 else
2656 {
2657 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2658 {
2659 if (!isSingleStatement(*sit))
2660 {
2661 return false;
2662 }
2663 }
2664
2665 return true;
2666 }
2667 }
2668
2669 return true;
2670}
2671
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002672// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2673// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002674bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2675{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002676 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002677 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002678
2679 // Parse loops of the form:
2680 // for(int index = initial; index [comparator] limit; index += increment)
2681 TIntermSymbol *index = NULL;
2682 TOperator comparator = EOpNull;
2683 int initial = 0;
2684 int limit = 0;
2685 int increment = 0;
2686
2687 // Parse index name and intial value
2688 if (node->getInit())
2689 {
2690 TIntermAggregate *init = node->getInit()->getAsAggregate();
2691
2692 if (init)
2693 {
2694 TIntermSequence &sequence = init->getSequence();
2695 TIntermTyped *variable = sequence[0]->getAsTyped();
2696
2697 if (variable && variable->getQualifier() == EvqTemporary)
2698 {
2699 TIntermBinary *assign = variable->getAsBinaryNode();
2700
2701 if (assign->getOp() == EOpInitialize)
2702 {
2703 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2704 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2705
2706 if (symbol && constant)
2707 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002708 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002709 {
2710 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002711 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002712 }
2713 }
2714 }
2715 }
2716 }
2717 }
2718
2719 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002720 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002721 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002722 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002723
2724 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2725 {
2726 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2727
2728 if (constant)
2729 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002730 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002731 {
2732 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002733 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002734 }
2735 }
2736 }
2737 }
2738
2739 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002740 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002741 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002742 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2743 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002744
2745 if (binaryTerminal)
2746 {
2747 TOperator op = binaryTerminal->getOp();
2748 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2749
2750 if (constant)
2751 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002752 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002753 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002754 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002755
2756 switch (op)
2757 {
2758 case EOpAddAssign: increment = value; break;
2759 case EOpSubAssign: increment = -value; break;
2760 default: UNIMPLEMENTED();
2761 }
2762 }
2763 }
2764 }
2765 else if (unaryTerminal)
2766 {
2767 TOperator op = unaryTerminal->getOp();
2768
2769 switch (op)
2770 {
2771 case EOpPostIncrement: increment = 1; break;
2772 case EOpPostDecrement: increment = -1; break;
2773 case EOpPreIncrement: increment = 1; break;
2774 case EOpPreDecrement: increment = -1; break;
2775 default: UNIMPLEMENTED();
2776 }
2777 }
2778 }
2779
2780 if (index != NULL && comparator != EOpNull && increment != 0)
2781 {
2782 if (comparator == EOpLessThanEqual)
2783 {
2784 comparator = EOpLessThan;
2785 limit += 1;
2786 }
2787
2788 if (comparator == EOpLessThan)
2789 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002790 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002791
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002792 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002793 {
2794 return false; // Not an excessive loop
2795 }
2796
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002797 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2798 mExcessiveLoopIndex = index;
2799
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002800 out << "{int ";
2801 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002802 out << ";\n"
2803 "bool Break";
2804 index->traverse(this);
2805 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002806
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002807 bool firstLoopFragment = true;
2808
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002809 while (iterations > 0)
2810 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002811 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002812
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002813 if (!firstLoopFragment)
2814 {
2815 out << "if(!Break";
2816 index->traverse(this);
2817 out << ") {\n";
2818 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002819
2820 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2821 {
2822 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2823 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002824
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002825 // for(int index = initial; index < clampedLimit; index += increment)
2826
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002827 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002828 index->traverse(this);
2829 out << " = ";
2830 out << initial;
2831
2832 out << "; ";
2833 index->traverse(this);
2834 out << " < ";
2835 out << clampedLimit;
2836
2837 out << "; ";
2838 index->traverse(this);
2839 out << " += ";
2840 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002841 out << ")\n";
2842
Jamie Madill075edd82013-07-08 13:30:19 -04002843 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002844 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002845
2846 if (node->getBody())
2847 {
2848 node->getBody()->traverse(this);
2849 }
2850
Jamie Madill075edd82013-07-08 13:30:19 -04002851 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002852 out << ";}\n";
2853
2854 if (!firstLoopFragment)
2855 {
2856 out << "}\n";
2857 }
2858
2859 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002860
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002861 initial += MAX_LOOP_ITERATIONS * increment;
2862 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002863 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002864
2865 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002866
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002867 mExcessiveLoopIndex = restoreIndex;
2868
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002869 return true;
2870 }
2871 else UNIMPLEMENTED();
2872 }
2873
2874 return false; // Not handled as an excessive loop
2875}
2876
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002877void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002878{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002879 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002880
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002881 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002882 {
2883 out << preString;
2884 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002885 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002886 {
2887 out << inString;
2888 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002889 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002890 {
2891 out << postString;
2892 }
2893}
2894
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002895void OutputHLSL::outputLineDirective(int line)
2896{
2897 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2898 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002899 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002900 mBody << "#line " << line;
2901
2902 if (mContext.sourcePath)
2903 {
2904 mBody << " \"" << mContext.sourcePath << "\"";
2905 }
2906
2907 mBody << "\n";
2908 }
2909}
2910
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002911TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2912{
2913 TQualifier qualifier = symbol->getQualifier();
2914 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002915 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002916
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002917 if (name.empty()) // HLSL demands named arguments, also for prototypes
2918 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002919 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002920 }
2921 else
2922 {
2923 name = decorate(name);
2924 }
2925
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002926 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
2927 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04002928 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
2929 qualifierString(qualifier) + " " + samplerString(type) + " sampler_" + name + arrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002930 }
2931
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002932 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002933}
2934
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00002935TString OutputHLSL::interpolationString(TQualifier qualifier)
2936{
2937 switch(qualifier)
2938 {
2939 case EvqVaryingIn: return "";
Jamie Madill19571812013-08-12 15:26:34 -07002940 case EvqFragmentIn: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00002941 case EvqInvariantVaryingIn: return "";
2942 case EvqSmoothIn: return "linear";
2943 case EvqFlatIn: return "nointerpolation";
2944 case EvqCentroidIn: return "centroid";
2945 case EvqVaryingOut: return "";
Jamie Madill19571812013-08-12 15:26:34 -07002946 case EvqVertexOut: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00002947 case EvqInvariantVaryingOut: return "";
2948 case EvqSmoothOut: return "linear";
2949 case EvqFlatOut: return "nointerpolation";
2950 case EvqCentroidOut: return "centroid";
2951 default: UNREACHABLE();
2952 }
2953
2954 return "";
2955}
2956
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002957TString OutputHLSL::qualifierString(TQualifier qualifier)
2958{
2959 switch(qualifier)
2960 {
2961 case EvqIn: return "in";
2962 case EvqOut: return "out";
2963 case EvqInOut: return "inout";
2964 case EvqConstReadOnly: return "const";
2965 default: UNREACHABLE();
2966 }
2967
2968 return "";
2969}
2970
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002971TString OutputHLSL::typeString(const TType &type)
2972{
Jamie Madill98493dd2013-07-08 14:39:03 -04002973 const TStructure* structure = type.getStruct();
2974 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002975 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002976 const TString& typeName = structure->name();
2977 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002978 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002979 return structLookup(typeName);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002980 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00002981 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002982 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002983 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002984 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002985 }
2986 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002987 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00002988 int cols = type.getCols();
2989 int rows = type.getRows();
2990 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002991 }
2992 else
2993 {
2994 switch (type.getBasicType())
2995 {
2996 case EbtFloat:
2997 switch (type.getNominalSize())
2998 {
2999 case 1: return "float";
3000 case 2: return "float2";
3001 case 3: return "float3";
3002 case 4: return "float4";
3003 }
3004 case EbtInt:
3005 switch (type.getNominalSize())
3006 {
3007 case 1: return "int";
3008 case 2: return "int2";
3009 case 3: return "int3";
3010 case 4: return "int4";
3011 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003012 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04003013 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003014 {
3015 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003016 case 2: return "uint2";
3017 case 3: return "uint3";
3018 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003019 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003020 case EbtBool:
3021 switch (type.getNominalSize())
3022 {
3023 case 1: return "bool";
3024 case 2: return "bool2";
3025 case 3: return "bool3";
3026 case 4: return "bool4";
3027 }
3028 case EbtVoid:
3029 return "void";
3030 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003031 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04003032 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003033 case EbtSampler2DArray:
3034 case EbtISampler2DArray:
3035 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003036 return "sampler2D";
3037 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003038 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04003039 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003040 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00003041 case EbtSamplerExternalOES:
3042 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00003043 default:
3044 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003045 }
3046 }
3047
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003048 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003049 return "<unknown type>";
3050}
3051
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003052TString OutputHLSL::textureString(const TType &type)
3053{
3054 switch (type.getBasicType())
3055 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003056 case EbtSampler2D: return "Texture2D";
3057 case EbtSamplerCube: return "TextureCube";
3058 case EbtSamplerExternalOES: return "Texture2D";
3059 case EbtSampler2DArray: return "Texture2DArray";
3060 case EbtSampler3D: return "Texture3D";
3061 case EbtISampler2D: return "Texture2D<int4>";
3062 case EbtISampler3D: return "Texture3D<int4>";
3063 case EbtISamplerCube: return "TextureCube<int4>";
3064 case EbtISampler2DArray: return "Texture2DArray<int4>";
3065 case EbtUSampler2D: return "Texture2D<uint4>";
3066 case EbtUSampler3D: return "Texture3D<uint4>";
3067 case EbtUSamplerCube: return "TextureCube<uint4>";
3068 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
3069 case EbtSampler2DShadow: return "Texture2D";
3070 case EbtSamplerCubeShadow: return "TextureCube";
3071 case EbtSampler2DArrayShadow: return "Texture2DArray";
3072 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003073 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04003074
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003075 return "<unknown texture type>";
3076}
3077
Nicolas Capenscb127d32013-07-15 17:26:18 -04003078TString OutputHLSL::samplerString(const TType &type)
3079{
3080 if (IsShadowSampler(type.getBasicType()))
3081 {
3082 return "SamplerComparisonState";
3083 }
3084 else
3085 {
3086 return "SamplerState";
3087 }
3088}
3089
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003090TString OutputHLSL::arrayString(const TType &type)
3091{
3092 if (!type.isArray())
3093 {
3094 return "";
3095 }
3096
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003097 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003098}
3099
3100TString OutputHLSL::initializer(const TType &type)
3101{
3102 TString string;
3103
Jamie Madill94bf7f22013-07-08 13:31:15 -04003104 size_t size = type.getObjectSize();
3105 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003106 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003107 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003108
Jamie Madill94bf7f22013-07-08 13:31:15 -04003109 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003110 {
3111 string += ", ";
3112 }
3113 }
3114
daniel@transgaming.comead23042010-04-29 03:35:36 +00003115 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003116}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003117
Jamie Madill98493dd2013-07-08 14:39:03 -04003118TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003119{
Jamie Madill98493dd2013-07-08 14:39:03 -04003120 const TFieldList &fields = structure.fields();
3121 const bool isNameless = (structure.name() == "");
3122 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003123 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3124
Jamie Madill98493dd2013-07-08 14:39:03 -04003125 TString string;
3126 string += declareString + "\n"
3127 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003128
Jamie Madillc835df62013-06-21 09:15:32 -04003129 int elementIndex = 0;
3130
Jamie Madill9cf6c072013-06-20 11:55:53 -04003131 for (unsigned int i = 0; i < fields.size(); i++)
3132 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003133 const TField &field = *fields[i];
3134 const TType &fieldType = *field.type();
3135 const TStructure *fieldStruct = fieldType.getStruct();
3136 const TString &fieldTypeString = fieldStruct ? structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) : typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003137
Jamie Madillc835df62013-06-21 09:15:32 -04003138 if (useStd140Packing)
3139 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003140 string += std140PrePaddingString(*field.type(), &elementIndex);
Jamie Madillc835df62013-06-21 09:15:32 -04003141 }
3142
Jamie Madill98493dd2013-07-08 14:39:03 -04003143 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003144
3145 if (useStd140Packing)
3146 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003147 string += std140PostPaddingString(*field.type(), useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003148 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003149 }
3150
3151 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003152 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003153
Jamie Madille4075c92013-06-21 09:15:32 -04003154 // Add remaining element index to the global map, for use with nested structs in standard layouts
3155 if (useStd140Packing)
3156 {
3157 mStd140StructElementIndexes[structName] = elementIndex;
3158 }
3159
Jamie Madill98493dd2013-07-08 14:39:03 -04003160 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003161}
3162
Jamie Madill98493dd2013-07-08 14:39:03 -04003163TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003164{
Jamie Madill98493dd2013-07-08 14:39:03 -04003165 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003166 {
3167 return "";
3168 }
3169
3170 TString prefix = "";
3171
3172 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3173 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003174
3175 if (useStd140Packing)
3176 {
3177 prefix += "std";
3178 }
3179
Jamie Madill9cf6c072013-06-20 11:55:53 -04003180 if (useHLSLRowMajorPacking)
3181 {
Jamie Madillc835df62013-06-21 09:15:32 -04003182 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003183 prefix += "rm";
3184 }
3185
Jamie Madill98493dd2013-07-08 14:39:03 -04003186 return prefix + structLookup(structure.name());
Jamie Madill9cf6c072013-06-20 11:55:53 -04003187}
3188
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003189void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003190{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003191 if (name == "")
3192 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003193 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003194 }
3195
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003196 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
3197 {
3198 return; // Already added
3199 }
3200
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003201 TType ctorType = type;
3202 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003203 ctorType.setPrecision(EbpHigh);
3204 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003205
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003206 TString ctorName = type.getStruct() ? decorate(name) : name;
3207
3208 typedef std::vector<TType> ParameterArray;
3209 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003210
Jamie Madill98493dd2013-07-08 14:39:03 -04003211 const TStructure* structure = type.getStruct();
3212 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003213 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003214 mStructNames.insert(decorate(name));
3215
Jamie Madill98493dd2013-07-08 14:39:03 -04003216 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003217
Jamie Madill98493dd2013-07-08 14:39:03 -04003218 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003219 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003220 // Add row-major packed struct for interface blocks
3221 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003222 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003223 "#pragma pack_matrix(column_major)\n";
3224
Jamie Madillc835df62013-06-21 09:15:32 -04003225 const TString &std140Prefix = "std";
Jamie Madill98493dd2013-07-08 14:39:03 -04003226 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003227
3228 const TString &std140RowMajorPrefix = "std_rm";
3229 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003230 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003231 "#pragma pack_matrix(column_major)\n";
3232
Jamie Madill98493dd2013-07-08 14:39:03 -04003233 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003234 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003235 mStructDeclarations.push_back(std140String);
3236 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003237 }
3238
Jamie Madill98493dd2013-07-08 14:39:03 -04003239 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003240 for (unsigned int i = 0; i < fields.size(); i++)
3241 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003242 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003243 }
3244 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003245 else if (parameters)
3246 {
3247 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3248 {
3249 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3250 }
3251 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003252 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003253
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003254 TString constructor;
3255
3256 if (ctorType.getStruct())
3257 {
3258 constructor += ctorName + " " + ctorName + "_ctor(";
3259 }
3260 else // Built-in type
3261 {
3262 constructor += typeString(ctorType) + " " + ctorName + "(";
3263 }
3264
3265 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3266 {
3267 const TType &type = ctorParameters[parameter];
3268
3269 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3270
3271 if (parameter < ctorParameters.size() - 1)
3272 {
3273 constructor += ", ";
3274 }
3275 }
3276
3277 constructor += ")\n"
3278 "{\n";
3279
3280 if (ctorType.getStruct())
3281 {
3282 constructor += " " + ctorName + " structure = {";
3283 }
3284 else
3285 {
3286 constructor += " return " + typeString(ctorType) + "(";
3287 }
3288
3289 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3290 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003291 int rows = ctorType.getRows();
3292 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003293 const TType &parameter = ctorParameters[0];
3294
3295 if (parameter.isScalar())
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 {
3301 constructor += TString((row == col) ? "x0" : "0.0");
3302
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003303 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003304 {
3305 constructor += ", ";
3306 }
3307 }
3308 }
3309 }
3310 else if (parameter.isMatrix())
3311 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003312 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003313 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003314 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003315 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003316 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003317 {
3318 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3319 }
3320 else
3321 {
3322 constructor += TString((row == col) ? "1.0" : "0.0");
3323 }
3324
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003325 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003326 {
3327 constructor += ", ";
3328 }
3329 }
3330 }
3331 }
3332 else UNREACHABLE();
3333 }
3334 else
3335 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003336 size_t remainingComponents = ctorType.getObjectSize();
3337 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003338
3339 while (remainingComponents > 0)
3340 {
3341 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003342 const size_t parameterSize = parameter.getObjectSize();
3343 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003344
3345 constructor += "x" + str(parameterIndex);
3346
3347 if (parameter.isScalar())
3348 {
3349 remainingComponents -= parameter.getObjectSize();
3350 }
3351 else if (parameter.isVector())
3352 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003353 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003354 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003355 ASSERT(parameterSize <= remainingComponents);
3356 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003357 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003358 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003359 {
3360 switch (remainingComponents)
3361 {
3362 case 1: constructor += ".x"; break;
3363 case 2: constructor += ".xy"; break;
3364 case 3: constructor += ".xyz"; break;
3365 case 4: constructor += ".xyzw"; break;
3366 default: UNREACHABLE();
3367 }
3368
3369 remainingComponents = 0;
3370 }
3371 else UNREACHABLE();
3372 }
3373 else if (parameter.isMatrix() || parameter.getStruct())
3374 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003375 ASSERT(remainingComponents == parameterSize || moreParameters);
3376 ASSERT(parameterSize <= remainingComponents);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003377
Jamie Madill94bf7f22013-07-08 13:31:15 -04003378 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003379 }
3380 else UNREACHABLE();
3381
3382 if (moreParameters)
3383 {
3384 parameterIndex++;
3385 }
3386
3387 if (remainingComponents)
3388 {
3389 constructor += ", ";
3390 }
3391 }
3392 }
3393
3394 if (ctorType.getStruct())
3395 {
3396 constructor += "};\n"
3397 " return structure;\n"
3398 "}\n";
3399 }
3400 else
3401 {
3402 constructor += ");\n"
3403 "}\n";
3404 }
3405
daniel@transgaming.com63691862010-04-29 03:32:42 +00003406 mConstructors.insert(constructor);
3407}
3408
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003409const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3410{
3411 TInfoSinkBase &out = mBody;
3412
Jamie Madill98493dd2013-07-08 14:39:03 -04003413 const TStructure* structure = type.getStruct();
3414 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003415 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003416 out << structLookup(structure->name()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003417
Jamie Madill98493dd2013-07-08 14:39:03 -04003418 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003419
Jamie Madill98493dd2013-07-08 14:39:03 -04003420 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003421 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003422 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003423
3424 constUnion = writeConstantUnion(*fieldType, constUnion);
3425
Jamie Madill98493dd2013-07-08 14:39:03 -04003426 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003427 {
3428 out << ", ";
3429 }
3430 }
3431
3432 out << ")";
3433 }
3434 else
3435 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003436 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003437 bool writeType = size > 1;
3438
3439 if (writeType)
3440 {
3441 out << typeString(type) << "(";
3442 }
3443
Jamie Madill94bf7f22013-07-08 13:31:15 -04003444 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003445 {
3446 switch (constUnion->getType())
3447 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003448 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003449 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003450 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003451 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003452 default: UNREACHABLE();
3453 }
3454
3455 if (i != size - 1)
3456 {
3457 out << ", ";
3458 }
3459 }
3460
3461 if (writeType)
3462 {
3463 out << ")";
3464 }
3465 }
3466
3467 return constUnion;
3468}
3469
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003470TString OutputHLSL::scopeString(unsigned int depthLimit)
3471{
3472 TString string;
3473
3474 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3475 {
3476 string += "_" + str(i);
3477 }
3478
3479 return string;
3480}
3481
3482TString OutputHLSL::scopedStruct(const TString &typeName)
3483{
3484 if (typeName == "")
3485 {
3486 return typeName;
3487 }
3488
3489 return typeName + scopeString(mScopeDepth);
3490}
3491
3492TString OutputHLSL::structLookup(const TString &typeName)
3493{
3494 for (int depth = mScopeDepth; depth >= 0; depth--)
3495 {
3496 TString scopedName = decorate(typeName + scopeString(depth));
3497
3498 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3499 {
3500 if (*structName == scopedName)
3501 {
3502 return scopedName;
3503 }
3504 }
3505 }
3506
3507 UNREACHABLE(); // Should have found a matching constructor
3508
3509 return typeName;
3510}
3511
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003512TString OutputHLSL::decorate(const TString &string)
3513{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003514 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003515 {
3516 return "_" + string;
3517 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003518
3519 return string;
3520}
3521
apatrick@chromium.org65756022012-01-17 21:45:38 +00003522TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003523{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003524 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003525 {
3526 return "ex_" + string;
3527 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003528
3529 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003530}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003531
Jamie Madill98493dd2013-07-08 14:39:03 -04003532TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003533{
Jamie Madill98493dd2013-07-08 14:39:03 -04003534 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003535 {
3536 return decorate(string);
3537 }
3538
3539 return string;
3540}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003541
3542TString OutputHLSL::registerString(TIntermSymbol *operand)
3543{
3544 ASSERT(operand->getQualifier() == EvqUniform);
3545
3546 if (IsSampler(operand->getBasicType()))
3547 {
3548 return "s" + str(samplerRegister(operand));
3549 }
3550
3551 return "c" + str(uniformRegister(operand));
3552}
3553
3554int OutputHLSL::samplerRegister(TIntermSymbol *sampler)
3555{
3556 const TType &type = sampler->getType();
3557 ASSERT(IsSampler(type.getBasicType()));
3558
3559 int index = mSamplerRegister;
3560 mSamplerRegister += sampler->totalRegisterCount();
3561
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003562 declareUniform(type, sampler->getSymbol(), index);
3563
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003564 return index;
3565}
3566
3567int OutputHLSL::uniformRegister(TIntermSymbol *uniform)
3568{
3569 const TType &type = uniform->getType();
3570 ASSERT(!IsSampler(type.getBasicType()));
3571
3572 int index = mUniformRegister;
3573 mUniformRegister += uniform->totalRegisterCount();
3574
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003575 declareUniform(type, uniform->getSymbol(), index);
3576
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003577 return index;
3578}
3579
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003580void OutputHLSL::declareInterfaceBlockField(const TType &type, const TString &name, std::vector<InterfaceBlockField>& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003581{
Jamie Madill98493dd2013-07-08 14:39:03 -04003582 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003583
3584 if (!structure)
3585 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003586 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003587 InterfaceBlockField field(glVariableType(type), glVariablePrecision(type), name.c_str(),
3588 (unsigned int)type.getArraySize(), isRowMajorMatrix);
3589 output.push_back(field);
3590 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003591 else
3592 {
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003593 InterfaceBlockField structField(GL_NONE, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), false);
3594
3595 const TFieldList &fields = structure->fields();
3596
3597 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3598 {
3599 TField *field = fields[fieldIndex];
3600 TType *fieldType = field->type();
3601
3602 // make sure to copy matrix packing information
3603 fieldType->setLayoutQualifier(type.getLayoutQualifier());
3604
3605 declareInterfaceBlockField(*fieldType, field->name(), structField.fields);
3606 }
3607
3608 output.push_back(structField);
3609 }
3610}
3611
3612void OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, std::vector<Uniform>& output)
3613{
3614 const TStructure *structure = type.getStruct();
3615
3616 if (!structure)
3617 {
3618 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
3619 Uniform uniform(glVariableType(type), glVariablePrecision(type), name.c_str(),
3620 (unsigned int)type.getArraySize(), (unsigned int)registerIndex);
3621 output.push_back(uniform);
3622 }
3623 else
3624 {
3625 Uniform structUniform(GL_NONE, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), (unsigned int)registerIndex);
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003626
Jamie Madill98493dd2013-07-08 14:39:03 -04003627 int fieldRegister = registerIndex;
3628 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003629
Jamie Madill98493dd2013-07-08 14:39:03 -04003630 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003631 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003632 TField *field = fields[fieldIndex];
3633 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003634
Jamie Madill010fffa2013-06-20 11:55:53 -04003635 // make sure to copy matrix packing information
Jamie Madill98493dd2013-07-08 14:39:03 -04003636 fieldType->setLayoutQualifier(type.getLayoutQualifier());
Jamie Madill010fffa2013-06-20 11:55:53 -04003637
Jamie Madill98493dd2013-07-08 14:39:03 -04003638 declareUniformToList(*fieldType, field->name(), fieldRegister, structUniform.fields);
3639 fieldRegister += fieldType->totalRegisterCount();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003640 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003641
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003642 output.push_back(structUniform);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003643 }
3644}
3645
Jamie Madill47fdd132013-08-30 13:21:04 -04003646void OutputHLSL::declareVaryingToList(const TType &type, const TString &name, std::vector<Varying>& fieldsOut)
3647{
3648 const TStructure *structure = type.getStruct();
3649
3650 if (!structure)
3651 {
3652 Varying varying(glVariableType(type), glVariablePrecision(type), name.c_str(), (unsigned int)type.getArraySize());
3653 fieldsOut.push_back(varying);
3654 }
3655 else
3656 {
3657 Varying structVarying(GL_NONE, GL_NONE, name.c_str(), (unsigned int)type.getArraySize());
3658 const TFieldList &fields = structure->fields();
3659
3660 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3661 {
3662 const TField &field = *fields[fieldIndex];
3663 declareVaryingToList(*field.type(), field.name(), structVarying.fields);
3664 }
3665
3666 fieldsOut.push_back(structVarying);
3667 }
3668}
3669
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003670void OutputHLSL::declareUniform(const TType &type, const TString &name, int index)
3671{
3672 declareUniformToList(type, name, index, mActiveUniforms);
3673}
3674
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003675GLenum OutputHLSL::glVariableType(const TType &type)
3676{
3677 if (type.getBasicType() == EbtFloat)
3678 {
3679 if (type.isScalar())
3680 {
3681 return GL_FLOAT;
3682 }
3683 else if (type.isVector())
3684 {
3685 switch(type.getNominalSize())
3686 {
3687 case 2: return GL_FLOAT_VEC2;
3688 case 3: return GL_FLOAT_VEC3;
3689 case 4: return GL_FLOAT_VEC4;
3690 default: UNREACHABLE();
3691 }
3692 }
3693 else if (type.isMatrix())
3694 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003695 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003696 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003697 case 2:
3698 switch(type.getRows())
3699 {
3700 case 2: return GL_FLOAT_MAT2;
3701 case 3: return GL_FLOAT_MAT2x3;
3702 case 4: return GL_FLOAT_MAT2x4;
3703 default: UNREACHABLE();
3704 }
3705
3706 case 3:
3707 switch(type.getRows())
3708 {
3709 case 2: return GL_FLOAT_MAT3x2;
3710 case 3: return GL_FLOAT_MAT3;
3711 case 4: return GL_FLOAT_MAT3x4;
3712 default: UNREACHABLE();
3713 }
3714
3715 case 4:
3716 switch(type.getRows())
3717 {
3718 case 2: return GL_FLOAT_MAT4x2;
3719 case 3: return GL_FLOAT_MAT4x3;
3720 case 4: return GL_FLOAT_MAT4;
3721 default: UNREACHABLE();
3722 }
3723
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003724 default: UNREACHABLE();
3725 }
3726 }
3727 else UNREACHABLE();
3728 }
3729 else if (type.getBasicType() == EbtInt)
3730 {
3731 if (type.isScalar())
3732 {
3733 return GL_INT;
3734 }
3735 else if (type.isVector())
3736 {
3737 switch(type.getNominalSize())
3738 {
3739 case 2: return GL_INT_VEC2;
3740 case 3: return GL_INT_VEC3;
3741 case 4: return GL_INT_VEC4;
3742 default: UNREACHABLE();
3743 }
3744 }
3745 else UNREACHABLE();
3746 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003747 else if (type.getBasicType() == EbtUInt)
3748 {
3749 if (type.isScalar())
3750 {
3751 return GL_UNSIGNED_INT;
3752 }
3753 else if (type.isVector())
3754 {
Jamie Madill22d63da2013-06-07 12:45:12 -04003755 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003756 {
3757 case 2: return GL_UNSIGNED_INT_VEC2;
3758 case 3: return GL_UNSIGNED_INT_VEC3;
3759 case 4: return GL_UNSIGNED_INT_VEC4;
3760 default: UNREACHABLE();
3761 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003762 }
3763 else UNREACHABLE();
3764 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003765 else if (type.getBasicType() == EbtBool)
3766 {
3767 if (type.isScalar())
3768 {
3769 return GL_BOOL;
3770 }
3771 else if (type.isVector())
3772 {
3773 switch(type.getNominalSize())
3774 {
3775 case 2: return GL_BOOL_VEC2;
3776 case 3: return GL_BOOL_VEC3;
3777 case 4: return GL_BOOL_VEC4;
3778 default: UNREACHABLE();
3779 }
3780 }
3781 else UNREACHABLE();
3782 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003783
3784 switch(type.getBasicType())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003785 {
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003786 case EbtSampler2D: return GL_SAMPLER_2D;
3787 case EbtSampler3D: return GL_SAMPLER_3D;
3788 case EbtSamplerCube: return GL_SAMPLER_CUBE;
3789 case EbtSampler2DArray: return GL_SAMPLER_2D_ARRAY;
3790 case EbtISampler2D: return GL_INT_SAMPLER_2D;
3791 case EbtISampler3D: return GL_INT_SAMPLER_3D;
3792 case EbtISamplerCube: return GL_INT_SAMPLER_CUBE;
3793 case EbtISampler2DArray: return GL_INT_SAMPLER_2D_ARRAY;
3794 case EbtUSampler2D: return GL_UNSIGNED_INT_SAMPLER_2D;
3795 case EbtUSampler3D: return GL_UNSIGNED_INT_SAMPLER_3D;
3796 case EbtUSamplerCube: return GL_UNSIGNED_INT_SAMPLER_CUBE;
3797 case EbtUSampler2DArray: return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
3798 case EbtSampler2DShadow: return GL_SAMPLER_2D_SHADOW;
3799 case EbtSamplerCubeShadow: return GL_SAMPLER_CUBE_SHADOW;
3800 case EbtSampler2DArrayShadow: return GL_SAMPLER_2D_ARRAY_SHADOW;
3801 default: UNREACHABLE();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003802 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04003803
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003804
3805 return GL_NONE;
3806}
3807
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003808GLenum OutputHLSL::glVariablePrecision(const TType &type)
3809{
3810 if (type.getBasicType() == EbtFloat)
3811 {
3812 switch (type.getPrecision())
3813 {
3814 case EbpHigh: return GL_HIGH_FLOAT;
3815 case EbpMedium: return GL_MEDIUM_FLOAT;
3816 case EbpLow: return GL_LOW_FLOAT;
3817 case EbpUndefined:
3818 // Should be defined as the default precision by the parser
3819 default: UNREACHABLE();
3820 }
3821 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003822 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003823 {
3824 switch (type.getPrecision())
3825 {
3826 case EbpHigh: return GL_HIGH_INT;
3827 case EbpMedium: return GL_MEDIUM_INT;
3828 case EbpLow: return GL_LOW_INT;
3829 case EbpUndefined:
3830 // Should be defined as the default precision by the parser
3831 default: UNREACHABLE();
3832 }
3833 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003834
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00003835 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00003836 return GL_NONE;
3837}
3838
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003839bool OutputHLSL::isVaryingOut(TQualifier qualifier)
3840{
3841 switch(qualifier)
3842 {
3843 case EvqVaryingOut:
3844 case EvqInvariantVaryingOut:
3845 case EvqSmoothOut:
3846 case EvqFlatOut:
3847 case EvqCentroidOut:
Jamie Madill19571812013-08-12 15:26:34 -07003848 case EvqVertexOut:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003849 return true;
3850 }
3851
3852 return false;
3853}
3854
3855bool OutputHLSL::isVaryingIn(TQualifier qualifier)
3856{
3857 switch(qualifier)
3858 {
3859 case EvqVaryingIn:
3860 case EvqInvariantVaryingIn:
3861 case EvqSmoothIn:
3862 case EvqFlatIn:
3863 case EvqCentroidIn:
Jamie Madill19571812013-08-12 15:26:34 -07003864 case EvqFragmentIn:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00003865 return true;
3866 }
3867
3868 return false;
3869}
3870
3871bool OutputHLSL::isVarying(TQualifier qualifier)
3872{
3873 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
3874}
3875
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003876}