blob: 8fc2bcefe8d1a2788a215a1c050133cf81b581a0 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/OutputHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
alokp@chromium.org79fb1012012-04-26 21:07:39 +00009#include "common/angleutils.h"
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +000010#include "common/utilities.h"
Geoff Lang17732822013-08-29 13:46:49 -040011#include "compiler/translator/compilerdebug.h"
12#include "compiler/translator/InfoSink.h"
13#include "compiler/translator/DetectDiscontinuity.h"
14#include "compiler/translator/SearchSymbol.h"
15#include "compiler/translator/UnfoldShortCircuit.h"
16#include "compiler/translator/HLSLLayoutEncoder.h"
17#include "compiler/translator/FlagStd140Structs.h"
Jamie Madill3c9eeb92013-11-04 11:09:26 -050018#include "compiler/translator/NodeSearch.h"
Jamie Madille53c98b2014-02-03 11:57:13 -050019#include "compiler/translator/RewriteElseBlocks.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000020
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000021#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000022#include <cfloat>
23#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000024
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000025namespace sh
26{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000027
Nicolas Capense0ba27a2013-06-24 16:10:52 -040028TString OutputHLSL::TextureFunction::name() const
29{
30 TString name = "gl_texture";
31
Nicolas Capens6d232bb2013-07-08 15:56:38 -040032 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040033 {
34 name += "2D";
35 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040036 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040037 {
38 name += "3D";
39 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040040 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040041 {
42 name += "Cube";
43 }
44 else UNREACHABLE();
45
46 if (proj)
47 {
48 name += "Proj";
49 }
50
Nicolas Capensb1f45b72013-12-19 17:37:19 -050051 if (offset)
52 {
53 name += "Offset";
54 }
55
Nicolas Capens75fb4752013-07-10 15:14:47 -040056 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040057 {
Nicolas Capensfc014542014-02-18 14:47:13 -050058 case IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040059 case BIAS: break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050060 case LOD: name += "Lod"; break;
61 case LOD0: name += "Lod0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040062 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050063 case SIZE: name += "Size"; break;
64 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -050065 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040066 default: UNREACHABLE();
67 }
68
69 return name + "(";
70}
71
Jamie Madillc2141fb2013-08-30 13:21:08 -040072const char *RegisterPrefix(const TType &type)
73{
74 if (IsSampler(type.getBasicType()))
75 {
76 return "s";
77 }
78 else
79 {
80 return "c";
81 }
82}
83
Nicolas Capense0ba27a2013-06-24 16:10:52 -040084bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
85{
86 if (sampler < rhs.sampler) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040087 if (sampler > rhs.sampler) return false;
88
Nicolas Capense0ba27a2013-06-24 16:10:52 -040089 if (coords < rhs.coords) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040090 if (coords > rhs.coords) return false;
91
Nicolas Capense0ba27a2013-06-24 16:10:52 -040092 if (!proj && rhs.proj) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040093 if (proj && !rhs.proj) return false;
94
95 if (!offset && rhs.offset) return true;
96 if (offset && !rhs.offset) return false;
97
Nicolas Capens75fb4752013-07-10 15:14:47 -040098 if (method < rhs.method) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040099 if (method > rhs.method) return false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400100
101 return false;
102}
103
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000104OutputHLSL::OutputHLSL(TParseContext &context, const ShBuiltInResources& resources, ShShaderOutput outputType)
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +0000105 : TIntermTraverser(true, true, true), mContext(context), mOutputType(outputType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000107 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000108 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000109
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000110 mUsesFragColor = false;
111 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000112 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000113 mUsesFragCoord = false;
114 mUsesPointCoord = false;
115 mUsesFrontFacing = false;
116 mUsesPointSize = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400117 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000118 mUsesXor = false;
119 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000120 mUsesMod2v = false;
121 mUsesMod2f = false;
122 mUsesMod3v = false;
123 mUsesMod3f = false;
124 mUsesMod4v = false;
125 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000126 mUsesFaceforward1 = false;
127 mUsesFaceforward2 = false;
128 mUsesFaceforward3 = false;
129 mUsesFaceforward4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000130 mUsesAtan2_1 = false;
131 mUsesAtan2_2 = false;
132 mUsesAtan2_3 = false;
133 mUsesAtan2_4 = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500134 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400135 mUsesNestedBreak = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000136
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000137 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
138
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000139 mScopeDepth = 0;
140
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000141 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000142
143 mContainsLoopDiscontinuity = false;
144 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000145 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400146 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000147
148 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000149
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000150 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000151 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000152 if (mContext.shaderType == SH_FRAGMENT_SHADER)
153 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000154 mUniformRegister = 3; // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000155 }
156 else
157 {
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000158 mUniformRegister = 2; // Reserve registers for dx_DepthRange and dx_ViewAdjust
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000159 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000160 }
161 else
162 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000163 mUniformRegister = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000164 }
165
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000166 mSamplerRegister = 0;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000167 mInterfaceBlockRegister = 2; // Reserve registers for the default uniform block and driver constants
Jamie Madill574d9dd2013-06-20 11:55:56 -0400168 mPaddingCounter = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000169}
170
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000171OutputHLSL::~OutputHLSL()
172{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000173 delete mUnfoldShortCircuit;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000174}
175
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000176void OutputHLSL::output()
177{
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +0000178 mContainsLoopDiscontinuity = mContext.shaderType == SH_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400179 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
180 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000181
Jamie Madille53c98b2014-02-03 11:57:13 -0500182 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
183 // use a vertex attribute as a condition, and some related computation in the else block.
184 if (mOutputType == SH_HLSL9_OUTPUT && mContext.shaderType == SH_VERTEX_SHADER)
185 {
186 RewriteElseBlocks(mContext.treeRoot);
187 }
188
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000189 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 +0000190 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000191
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000192 mContext.infoSink().obj << mHeader.c_str();
193 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000194}
195
Jamie Madill570e04d2013-06-21 09:15:33 -0400196void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
197{
198 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
199 {
200 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
201
202 // This will mark the necessary block elements as referenced
203 flaggedNode->traverse(this);
204 TString structName(mBody.c_str());
205 mBody.erase();
206
207 mFlaggedStructOriginalNames[flaggedNode] = structName;
208
209 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
210 {
211 structName.erase(pos, 1);
212 }
213
214 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
215 }
216}
217
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000218TInfoSinkBase &OutputHLSL::getBodyStream()
219{
220 return mBody;
221}
222
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400223const std::vector<Uniform> &OutputHLSL::getUniforms()
daniel@transgaming.com043da132012-12-20 21:12:22 +0000224{
225 return mActiveUniforms;
226}
227
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000228const ActiveInterfaceBlocks &OutputHLSL::getInterfaceBlocks() const
229{
230 return mActiveInterfaceBlocks;
231}
232
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400233const std::vector<Attribute> &OutputHLSL::getOutputVariables() const
Jamie Madill46131a32013-06-20 11:55:50 -0400234{
235 return mActiveOutputVariables;
236}
237
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400238const std::vector<Attribute> &OutputHLSL::getAttributes() const
Jamie Madilldefb6742013-06-20 11:55:51 -0400239{
240 return mActiveAttributes;
241}
242
Jamie Madill47fdd132013-08-30 13:21:04 -0400243const std::vector<Varying> &OutputHLSL::getVaryings() const
244{
245 return mActiveVaryings;
246}
247
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000248int OutputHLSL::vectorSize(const TType &type) const
249{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000250 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000251 int arraySize = type.isArray() ? type.getArraySize() : 1;
252
253 return elementSize * arraySize;
254}
255
Jamie Madill98493dd2013-07-08 14:39:03 -0400256TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, const TField &field)
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000257{
Jamie Madill98493dd2013-07-08 14:39:03 -0400258 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000259 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400260 return interfaceBlock.name() + "." + field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000261 }
262 else
263 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400264 return field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000265 }
266}
267
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000268TString OutputHLSL::decoratePrivate(const TString &privateText)
269{
270 return "dx_" + privateText;
271}
272
Jamie Madill98493dd2013-07-08 14:39:03 -0400273TString OutputHLSL::interfaceBlockStructNameString(const TInterfaceBlock &interfaceBlock)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000274{
Jamie Madill98493dd2013-07-08 14:39:03 -0400275 return decoratePrivate(interfaceBlock.name()) + "_type";
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000276}
277
Jamie Madill98493dd2013-07-08 14:39:03 -0400278TString OutputHLSL::interfaceBlockInstanceString(const TInterfaceBlock& interfaceBlock, unsigned int arrayIndex)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000279{
Jamie Madill98493dd2013-07-08 14:39:03 -0400280 if (!interfaceBlock.hasInstanceName())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000281 {
282 return "";
283 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400284 else if (interfaceBlock.isArray())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000285 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400286 return decoratePrivate(interfaceBlock.instanceName()) + "_" + str(arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000287 }
288 else
289 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400290 return decorate(interfaceBlock.instanceName());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000291 }
292}
293
Jamie Madill98493dd2013-07-08 14:39:03 -0400294TString OutputHLSL::interfaceBlockFieldTypeString(const TField &field, TLayoutBlockStorage blockStorage)
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000295{
Jamie Madill98493dd2013-07-08 14:39:03 -0400296 const TType &fieldType = *field.type();
297 const TLayoutMatrixPacking matrixPacking = fieldType.getLayoutQualifier().matrixPacking;
Jamie Madill529077d2013-06-20 11:55:54 -0400298 ASSERT(matrixPacking != EmpUnspecified);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000299
Jamie Madill98493dd2013-07-08 14:39:03 -0400300 if (fieldType.isMatrix())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000301 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400302 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill529077d2013-06-20 11:55:54 -0400303 const TString &matrixPackString = (matrixPacking == EmpRowMajor ? "column_major" : "row_major");
Jamie Madill98493dd2013-07-08 14:39:03 -0400304 return matrixPackString + " " + typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000305 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400306 else if (fieldType.getStruct())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000307 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400308 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill98493dd2013-07-08 14:39:03 -0400309 return structureTypeName(*fieldType.getStruct(), matrixPacking == EmpColumnMajor, blockStorage == EbsStd140);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000310 }
311 else
312 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400313 return typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000314 }
315}
316
Jamie Madill98493dd2013-07-08 14:39:03 -0400317TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, TLayoutBlockStorage blockStorage)
318{
319 TString hlsl;
320
321 int elementIndex = 0;
322
323 for (unsigned int typeIndex = 0; typeIndex < interfaceBlock.fields().size(); typeIndex++)
324 {
325 const TField &field = *interfaceBlock.fields()[typeIndex];
326 const TType &fieldType = *field.type();
327
328 if (blockStorage == EbsStd140)
329 {
330 // 2 and 3 component vector types in some cases need pre-padding
331 hlsl += std140PrePaddingString(fieldType, &elementIndex);
332 }
333
334 hlsl += " " + interfaceBlockFieldTypeString(field, blockStorage) +
335 " " + decorate(field.name()) + arrayString(fieldType) + ";\n";
336
337 // must pad out after matrices and arrays, where HLSL usually allows itself room to pack stuff
338 if (blockStorage == EbsStd140)
339 {
340 const bool useHLSLRowMajorPacking = (fieldType.getLayoutQualifier().matrixPacking == EmpColumnMajor);
341 hlsl += std140PostPaddingString(fieldType, useHLSLRowMajorPacking);
342 }
343 }
344
345 return hlsl;
346}
347
348TString OutputHLSL::interfaceBlockStructString(const TInterfaceBlock &interfaceBlock)
349{
350 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
351
352 return "struct " + interfaceBlockStructNameString(interfaceBlock) + "\n"
353 "{\n" +
354 interfaceBlockFieldString(interfaceBlock, blockStorage) +
355 "};\n\n";
356}
357
358TString OutputHLSL::interfaceBlockString(const TInterfaceBlock &interfaceBlock, unsigned int registerIndex, unsigned int arrayIndex)
359{
360 const TString &arrayIndexString = (arrayIndex != GL_INVALID_INDEX ? decorate(str(arrayIndex)) : "");
361 const TString &blockName = interfaceBlock.name() + arrayIndexString;
362 TString hlsl;
363
364 hlsl += "cbuffer " + blockName + " : register(b" + str(registerIndex) + ")\n"
365 "{\n";
366
367 if (interfaceBlock.hasInstanceName())
368 {
369 hlsl += " " + interfaceBlockStructNameString(interfaceBlock) + " " + interfaceBlockInstanceString(interfaceBlock, arrayIndex) + ";\n";
370 }
371 else
372 {
373 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
374 hlsl += interfaceBlockFieldString(interfaceBlock, blockStorage);
375 }
376
377 hlsl += "};\n\n";
378
379 return hlsl;
380}
381
Jamie Madill574d9dd2013-06-20 11:55:56 -0400382TString OutputHLSL::std140PrePaddingString(const TType &type, int *elementIndex)
383{
384 if (type.getBasicType() == EbtStruct || type.isMatrix() || type.isArray())
385 {
386 // no padding needed, HLSL will align the field to a new register
387 *elementIndex = 0;
388 return "";
389 }
390
391 const GLenum glType = glVariableType(type);
392 const int numComponents = gl::UniformComponentCount(glType);
393
394 if (numComponents >= 4)
395 {
396 // no padding needed, HLSL will align the field to a new register
397 *elementIndex = 0;
398 return "";
399 }
400
401 if (*elementIndex + numComponents > 4)
402 {
403 // no padding needed, HLSL will align the field to a new register
404 *elementIndex = numComponents;
405 return "";
406 }
407
408 TString padding;
409
410 const int alignment = numComponents == 3 ? 4 : numComponents;
411 const int paddingOffset = (*elementIndex % alignment);
412
413 if (paddingOffset != 0)
414 {
415 // padding is neccessary
416 for (int paddingIndex = paddingOffset; paddingIndex < alignment; paddingIndex++)
417 {
418 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
419 }
420
421 *elementIndex += (alignment - paddingOffset);
422 }
423
424 *elementIndex += numComponents;
425 *elementIndex %= 4;
426
427 return padding;
428}
429
Jamie Madille4075c92013-06-21 09:15:32 -0400430TString OutputHLSL::std140PostPaddingString(const TType &type, bool useHLSLRowMajorPacking)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400431{
Jamie Madillc835df62013-06-21 09:15:32 -0400432 if (!type.isMatrix() && !type.isArray() && type.getBasicType() != EbtStruct)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400433 {
434 return "";
435 }
436
Jamie Madill574d9dd2013-06-20 11:55:56 -0400437 int numComponents = 0;
438
439 if (type.isMatrix())
440 {
Jamie Madille4075c92013-06-21 09:15:32 -0400441 // This method can also be called from structureString, which does not use layout qualifiers.
442 // Thus, use the method parameter for determining the matrix packing.
443 //
444 // Note HLSL row major packing corresponds to GL API column-major, and vice-versa, since we
445 // wish to always transpose GL matrices to play well with HLSL's matrix array indexing.
446 //
447 const bool isRowMajorMatrix = !useHLSLRowMajorPacking;
Jamie Madillc835df62013-06-21 09:15:32 -0400448 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400449 numComponents = gl::MatrixComponentCount(glType, isRowMajorMatrix);
450 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400451 else if (type.getStruct())
Jamie Madillc835df62013-06-21 09:15:32 -0400452 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400453 const TString &structName = structureTypeName(*type.getStruct(), useHLSLRowMajorPacking, true);
Jamie Madille4075c92013-06-21 09:15:32 -0400454 numComponents = mStd140StructElementIndexes[structName];
455
456 if (numComponents == 0)
457 {
458 return "";
459 }
Jamie Madillc835df62013-06-21 09:15:32 -0400460 }
Jamie Madill574d9dd2013-06-20 11:55:56 -0400461 else
462 {
Jamie Madillc835df62013-06-21 09:15:32 -0400463 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400464 numComponents = gl::UniformComponentCount(glType);
465 }
466
467 TString padding;
468 for (int paddingOffset = numComponents; paddingOffset < 4; paddingOffset++)
469 {
470 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
471 }
472 return padding;
473}
474
Jamie Madill440dc742013-06-20 11:55:55 -0400475// Use the same layout for packed and shared
476void setBlockLayout(InterfaceBlock *interfaceBlock, BlockLayoutType newLayout)
477{
478 interfaceBlock->layout = newLayout;
479 interfaceBlock->blockInfo.clear();
480
481 switch (newLayout)
482 {
483 case BLOCKLAYOUT_SHARED:
484 case BLOCKLAYOUT_PACKED:
485 {
486 HLSLBlockEncoder hlslEncoder(&interfaceBlock->blockInfo);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400487 hlslEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400488 interfaceBlock->dataSize = hlslEncoder.getBlockSize();
489 }
490 break;
491
492 case BLOCKLAYOUT_STANDARD:
493 {
494 Std140BlockEncoder stdEncoder(&interfaceBlock->blockInfo);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400495 stdEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400496 interfaceBlock->dataSize = stdEncoder.getBlockSize();
497 }
498 break;
499
500 default:
501 UNREACHABLE();
502 break;
503 }
504}
505
Jamie Madill574d9dd2013-06-20 11:55:56 -0400506BlockLayoutType convertBlockLayoutType(TLayoutBlockStorage blockStorage)
507{
508 switch (blockStorage)
509 {
510 case EbsPacked: return BLOCKLAYOUT_PACKED;
511 case EbsShared: return BLOCKLAYOUT_SHARED;
512 case EbsStd140: return BLOCKLAYOUT_STANDARD;
513 default: UNREACHABLE(); return BLOCKLAYOUT_SHARED;
514 }
515}
516
Jamie Madill98493dd2013-07-08 14:39:03 -0400517TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400518{
519 TString init;
520
521 TString preIndentString;
522 TString fullIndentString;
523
524 for (int spaces = 0; spaces < (indent * 4); spaces++)
525 {
526 preIndentString += ' ';
527 }
528
529 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
530 {
531 fullIndentString += ' ';
532 }
533
534 init += preIndentString + "{\n";
535
Jamie Madill98493dd2013-07-08 14:39:03 -0400536 const TFieldList &fields = structure.fields();
537 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400538 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400539 const TField &field = *fields[fieldIndex];
540 const TString &fieldName = rhsStructName + "." + decorate(field.name());
541 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400542
Jamie Madill98493dd2013-07-08 14:39:03 -0400543 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400544 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400545 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400546 }
547 else
548 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400549 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400550 }
551 }
552
553 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
554
555 return init;
556}
557
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000558void OutputHLSL::header()
559{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000560 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000561
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000562 TString uniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000563 TString interfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000564 TString varyings;
565 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400566 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000567
Jamie Madillc2141fb2013-08-30 13:21:08 -0400568 for (ReferencedSymbols::const_iterator uniformIt = mReferencedUniforms.begin(); uniformIt != mReferencedUniforms.end(); uniformIt++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000569 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400570 const TIntermSymbol &uniform = *uniformIt->second;
571 const TType &type = uniform.getType();
572 const TString &name = uniform.getSymbol();
573
574 int registerIndex = declareUniformAndAssignRegister(type, name);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000575
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000576 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture
577 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400578 uniforms += "uniform " + samplerString(type) + " sampler_" + decorateUniform(name, type) + arrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400579 " : register(s" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000580
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000581 uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400582 " : register(t" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000583 }
584 else
585 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400586 const TStructure *structure = type.getStruct();
587 const TString &typeName = (structure ? structureTypeName(*structure, false, false) : typeString(type));
588
589 const TString &registerString = TString("register(") + RegisterPrefix(type) + str(registerIndex) + ")";
590
591 uniforms += "uniform " + typeName + " " + decorateUniform(name, type) + arrayString(type) + " : " + registerString + ";\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000592 }
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000593 }
594
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000595 for (ReferencedSymbols::const_iterator interfaceBlockIt = mReferencedInterfaceBlocks.begin(); interfaceBlockIt != mReferencedInterfaceBlocks.end(); interfaceBlockIt++)
596 {
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000597 const TType &nodeType = interfaceBlockIt->second->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -0400598 const TInterfaceBlock &interfaceBlock = *nodeType.getInterfaceBlock();
599 const TFieldList &fieldList = interfaceBlock.fields();
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000600
Jamie Madill98493dd2013-07-08 14:39:03 -0400601 unsigned int arraySize = static_cast<unsigned int>(interfaceBlock.arraySize());
602 sh::InterfaceBlock activeBlock(interfaceBlock.name().c_str(), arraySize, mInterfaceBlockRegister);
603 for (unsigned int typeIndex = 0; typeIndex < fieldList.size(); typeIndex++)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000604 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400605 const TField &field = *fieldList[typeIndex];
606 const TString &fullUniformName = interfaceBlockFieldString(interfaceBlock, field);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400607 declareInterfaceBlockField(*field.type(), fullUniformName, activeBlock.fields);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000608 }
609
Jamie Madill98493dd2013-07-08 14:39:03 -0400610 mInterfaceBlockRegister += std::max(1u, arraySize);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000611
Jamie Madill98493dd2013-07-08 14:39:03 -0400612 BlockLayoutType blockLayoutType = convertBlockLayoutType(interfaceBlock.blockStorage());
613 setBlockLayout(&activeBlock, blockLayoutType);
Jamie Madill9060a4e2013-08-12 16:22:57 -0700614
615 if (interfaceBlock.matrixPacking() == EmpRowMajor)
616 {
617 activeBlock.isRowMajorLayout = true;
618 }
619
Jamie Madill98493dd2013-07-08 14:39:03 -0400620 mActiveInterfaceBlocks.push_back(activeBlock);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000621
Jamie Madill98493dd2013-07-08 14:39:03 -0400622 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000623 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400624 interfaceBlocks += interfaceBlockStructString(interfaceBlock);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000625 }
626
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000627 if (arraySize > 0)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000628 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000629 for (unsigned int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
630 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400631 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex + arrayIndex, arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000632 }
633 }
634 else
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000635 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400636 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000637 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000638 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000639
Jamie Madill829f59e2013-11-13 19:40:54 -0500640 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400641 {
642 TIntermTyped *structNode = flaggedStructIt->first;
643 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400644 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400645 const TString &originalName = mFlaggedStructOriginalNames[structNode];
646
Jamie Madill98493dd2013-07-08 14:39:03 -0400647 flaggedStructs += "static " + decorate(structure.name()) + " " + mappedName + " =\n";
648 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400649 flaggedStructs += "\n";
650 }
651
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000652 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
653 {
654 const TType &type = varying->second->getType();
655 const TString &name = varying->second->getSymbol();
656
657 // Program linking depends on this exact format
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +0000658 varyings += "static " + interpolationString(type.getQualifier()) + " " + typeString(type) + " " +
659 decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madill47fdd132013-08-30 13:21:04 -0400660
Jamie Madill94599662013-08-30 13:21:10 -0400661 declareVaryingToList(type, type.getQualifier(), name, mActiveVaryings);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000662 }
663
664 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
665 {
666 const TType &type = attribute->second->getType();
667 const TString &name = attribute->second->getSymbol();
668
669 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400670
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400671 Attribute attributeVar(glVariableType(type), glVariablePrecision(type), name.c_str(),
672 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
673 mActiveAttributes.push_back(attributeVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000674 }
675
Jamie Madill529077d2013-06-20 11:55:54 -0400676 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
677 {
678 out << *structDeclaration;
679 }
680
681 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
682 {
683 out << *constructor;
684 }
685
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500686 if (mUsesDiscardRewriting)
687 {
688 out << "#define ANGLE_USES_DISCARD_REWRITING" << "\n";
689 }
690
Nicolas Capens655fe362014-04-11 13:12:34 -0400691 if (mUsesNestedBreak)
692 {
693 out << "#define ANGLE_USES_NESTED_BREAK" << "\n";
694 }
695
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400696 if (mContext.shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000697 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000698 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000699 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000700
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000701 out << "// Varyings\n";
702 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400703 out << "\n";
704
705 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000706 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500707 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000708 {
Jamie Madill46131a32013-06-20 11:55:50 -0400709 const TString &variableName = outputVariableIt->first;
710 const TType &variableType = outputVariableIt->second->getType();
711 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
712
713 out << "static " + typeString(variableType) + " out_" + variableName + arrayString(variableType) +
714 " = " + initializer(variableType) + ";\n";
715
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400716 Attribute outputVar(glVariableType(variableType), glVariablePrecision(variableType), variableName.c_str(),
717 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
Jamie Madill46131a32013-06-20 11:55:50 -0400718 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000719 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000720 }
Jamie Madill46131a32013-06-20 11:55:50 -0400721 else
722 {
723 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
724
725 out << "static float4 gl_Color[" << numColorValues << "] =\n"
726 "{\n";
727 for (unsigned int i = 0; i < numColorValues; i++)
728 {
729 out << " float4(0, 0, 0, 0)";
730 if (i + 1 != numColorValues)
731 {
732 out << ",";
733 }
734 out << "\n";
735 }
736
737 out << "};\n";
738 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000739
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400740 if (mUsesFragDepth)
741 {
742 out << "static float gl_Depth = 0.0;\n";
743 }
744
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000745 if (mUsesFragCoord)
746 {
747 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
748 }
749
750 if (mUsesPointCoord)
751 {
752 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
753 }
754
755 if (mUsesFrontFacing)
756 {
757 out << "static bool gl_FrontFacing = false;\n";
758 }
759
760 out << "\n";
761
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000762 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000763 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000764 out << "struct gl_DepthRangeParameters\n"
765 "{\n"
766 " float near;\n"
767 " float far;\n"
768 " float diff;\n"
769 "};\n"
770 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000771 }
772
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000773 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000774 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000775 out << "cbuffer DriverConstants : register(b1)\n"
776 "{\n";
777
778 if (mUsesDepthRange)
779 {
780 out << " float3 dx_DepthRange : packoffset(c0);\n";
781 }
782
783 if (mUsesFragCoord)
784 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000785 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000786 }
787
788 if (mUsesFragCoord || mUsesFrontFacing)
789 {
790 out << " float3 dx_DepthFront : packoffset(c2);\n";
791 }
792
793 out << "};\n";
794 }
795 else
796 {
797 if (mUsesDepthRange)
798 {
799 out << "uniform float3 dx_DepthRange : register(c0);";
800 }
801
802 if (mUsesFragCoord)
803 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000804 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000805 }
806
807 if (mUsesFragCoord || mUsesFrontFacing)
808 {
809 out << "uniform float3 dx_DepthFront : register(c2);\n";
810 }
811 }
812
813 out << "\n";
814
815 if (mUsesDepthRange)
816 {
817 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
818 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000819 }
820
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000821 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000822 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000823
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000824 if (!interfaceBlocks.empty())
825 {
826 out << interfaceBlocks;
827 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400828
829 if (!flaggedStructs.empty())
830 {
831 out << "// Std140 Structures accessed by value\n";
832 out << "\n";
833 out << flaggedStructs;
834 out << "\n";
835 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000836 }
837
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000838 if (usingMRTExtension && mNumRenderTargets > 1)
839 {
840 out << "#define GL_USES_MRT\n";
841 }
842
843 if (mUsesFragColor)
844 {
845 out << "#define GL_USES_FRAG_COLOR\n";
846 }
847
848 if (mUsesFragData)
849 {
850 out << "#define GL_USES_FRAG_DATA\n";
851 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000853 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000855 out << "// Attributes\n";
856 out << attributes;
857 out << "\n"
858 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
859
860 if (mUsesPointSize)
861 {
862 out << "static float gl_PointSize = float(1);\n";
863 }
864
865 out << "\n"
866 "// Varyings\n";
867 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000868 out << "\n";
869
870 if (mUsesDepthRange)
871 {
872 out << "struct gl_DepthRangeParameters\n"
873 "{\n"
874 " float near;\n"
875 " float far;\n"
876 " float diff;\n"
877 "};\n"
878 "\n";
879 }
880
881 if (mOutputType == SH_HLSL11_OUTPUT)
882 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000883 if (mUsesDepthRange)
884 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000885 out << "cbuffer DriverConstants : register(b1)\n"
886 "{\n"
887 " float3 dx_DepthRange : packoffset(c0);\n"
888 "};\n"
889 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000890 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000891 }
892 else
893 {
894 if (mUsesDepthRange)
895 {
896 out << "uniform float3 dx_DepthRange : register(c0);\n";
897 }
898
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000899 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000900 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000901 }
902
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000903 if (mUsesDepthRange)
904 {
905 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
906 "\n";
907 }
908
909 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000911
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000912 if (!interfaceBlocks.empty())
913 {
914 out << interfaceBlocks;
915 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400916
917 if (!flaggedStructs.empty())
918 {
919 out << "// Std140 Structures accessed by value\n";
920 out << "\n";
921 out << flaggedStructs;
922 out << "\n";
923 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000924 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400925 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000926
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400927 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
928 {
929 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400930 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000931 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400932 switch(textureFunction->sampler)
933 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400934 case EbtSampler2D: out << "int2 "; break;
935 case EbtSampler3D: out << "int3 "; break;
936 case EbtSamplerCube: out << "int2 "; break;
937 case EbtSampler2DArray: out << "int3 "; break;
938 case EbtISampler2D: out << "int2 "; break;
939 case EbtISampler3D: out << "int3 "; break;
940 case EbtISamplerCube: out << "int2 "; break;
941 case EbtISampler2DArray: out << "int3 "; break;
942 case EbtUSampler2D: out << "int2 "; break;
943 case EbtUSampler3D: out << "int3 "; break;
944 case EbtUSamplerCube: out << "int2 "; break;
945 case EbtUSampler2DArray: out << "int3 "; break;
946 case EbtSampler2DShadow: out << "int2 "; break;
947 case EbtSamplerCubeShadow: out << "int2 "; break;
948 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400949 default: UNREACHABLE();
950 }
951 }
952 else // Sampling function
953 {
954 switch(textureFunction->sampler)
955 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400956 case EbtSampler2D: out << "float4 "; break;
957 case EbtSampler3D: out << "float4 "; break;
958 case EbtSamplerCube: out << "float4 "; break;
959 case EbtSampler2DArray: out << "float4 "; break;
960 case EbtISampler2D: out << "int4 "; break;
961 case EbtISampler3D: out << "int4 "; break;
962 case EbtISamplerCube: out << "int4 "; break;
963 case EbtISampler2DArray: out << "int4 "; break;
964 case EbtUSampler2D: out << "uint4 "; break;
965 case EbtUSampler3D: out << "uint4 "; break;
966 case EbtUSamplerCube: out << "uint4 "; break;
967 case EbtUSampler2DArray: out << "uint4 "; break;
968 case EbtSampler2DShadow: out << "float "; break;
969 case EbtSamplerCubeShadow: out << "float "; break;
970 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400971 default: UNREACHABLE();
972 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000973 }
974
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400975 // Function name
976 out << textureFunction->name();
977
978 // Argument list
979 int hlslCoords = 4;
980
981 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000982 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400983 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000984 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400985 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
986 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
987 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000988 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400989
Nicolas Capens75fb4752013-07-10 15:14:47 -0400990 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000991 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400992 case TextureFunction::IMPLICIT: break;
993 case TextureFunction::BIAS: hlslCoords = 4; break;
994 case TextureFunction::LOD: hlslCoords = 4; break;
995 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400996 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400997 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000998 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400999 }
1000 else if (mOutputType == SH_HLSL11_OUTPUT)
1001 {
1002 switch(textureFunction->sampler)
1003 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04001004 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
1005 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
1006 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
1007 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
1008 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
1009 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -05001010 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001011 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
1012 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
1013 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -05001014 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001015 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
1016 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
1017 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
1018 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001019 default: UNREACHABLE();
1020 }
1021 }
1022 else UNREACHABLE();
1023
Nicolas Capensfc014542014-02-18 14:47:13 -05001024 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001025 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001026 switch(textureFunction->coords)
1027 {
1028 case 2: out << ", int2 t"; break;
1029 case 3: out << ", int3 t"; break;
1030 default: UNREACHABLE();
1031 }
1032 }
1033 else // Floating-point coordinates (except textureSize)
1034 {
1035 switch(textureFunction->coords)
1036 {
1037 case 1: out << ", int lod"; break; // textureSize()
1038 case 2: out << ", float2 t"; break;
1039 case 3: out << ", float3 t"; break;
1040 case 4: out << ", float4 t"; break;
1041 default: UNREACHABLE();
1042 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001043 }
1044
Nicolas Capensd11d5492014-02-19 17:06:10 -05001045 if (textureFunction->method == TextureFunction::GRAD)
1046 {
1047 switch(textureFunction->sampler)
1048 {
1049 case EbtSampler2D:
1050 case EbtISampler2D:
1051 case EbtUSampler2D:
1052 case EbtSampler2DArray:
1053 case EbtISampler2DArray:
1054 case EbtUSampler2DArray:
1055 case EbtSampler2DShadow:
1056 case EbtSampler2DArrayShadow:
1057 out << ", float2 ddx, float2 ddy";
1058 break;
1059 case EbtSampler3D:
1060 case EbtISampler3D:
1061 case EbtUSampler3D:
1062 case EbtSamplerCube:
1063 case EbtISamplerCube:
1064 case EbtUSamplerCube:
1065 case EbtSamplerCubeShadow:
1066 out << ", float3 ddx, float3 ddy";
1067 break;
1068 default: UNREACHABLE();
1069 }
1070 }
1071
Nicolas Capens75fb4752013-07-10 15:14:47 -04001072 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +00001073 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001074 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001075 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001076 case TextureFunction::LOD: out << ", float lod"; break;
1077 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001078 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -04001079 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -05001080 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -05001081 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001082 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +00001083 }
1084
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001085 if (textureFunction->offset)
1086 {
1087 switch(textureFunction->sampler)
1088 {
1089 case EbtSampler2D: out << ", int2 offset"; break;
1090 case EbtSampler3D: out << ", int3 offset"; break;
1091 case EbtSampler2DArray: out << ", int2 offset"; break;
1092 case EbtISampler2D: out << ", int2 offset"; break;
1093 case EbtISampler3D: out << ", int3 offset"; break;
1094 case EbtISampler2DArray: out << ", int2 offset"; break;
1095 case EbtUSampler2D: out << ", int2 offset"; break;
1096 case EbtUSampler3D: out << ", int3 offset"; break;
1097 case EbtUSampler2DArray: out << ", int2 offset"; break;
1098 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -05001099 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001100 default: UNREACHABLE();
1101 }
1102 }
1103
Nicolas Capens84cfa122014-04-14 13:48:45 -04001104 if (textureFunction->method == TextureFunction::BIAS ||
1105 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001106 {
1107 out << ", float bias";
1108 }
1109
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001110 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001111 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001112
Nicolas Capens75fb4752013-07-10 15:14:47 -04001113 if (textureFunction->method == TextureFunction::SIZE)
1114 {
1115 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
1116 {
1117 if (IsSamplerArray(textureFunction->sampler))
1118 {
1119 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
1120 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
1121 }
1122 else
1123 {
1124 out << " uint width; uint height; uint numberOfLevels;\n"
1125 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
1126 }
1127 }
1128 else if (IsSampler3D(textureFunction->sampler))
1129 {
1130 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
1131 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
1132 }
1133 else UNREACHABLE();
1134
1135 switch(textureFunction->sampler)
1136 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04001137 case EbtSampler2D: out << " return int2(width, height);"; break;
1138 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
1139 case EbtSamplerCube: out << " return int2(width, height);"; break;
1140 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
1141 case EbtISampler2D: out << " return int2(width, height);"; break;
1142 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
1143 case EbtISamplerCube: out << " return int2(width, height);"; break;
1144 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
1145 case EbtUSampler2D: out << " return int2(width, height);"; break;
1146 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
1147 case EbtUSamplerCube: out << " return int2(width, height);"; break;
1148 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
1149 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
1150 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
1151 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001152 default: UNREACHABLE();
1153 }
1154 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001155 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001156 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001157 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1158 {
1159 out << " float width; float height; float layers; float levels;\n";
1160
1161 out << " uint mip = 0;\n";
1162
1163 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
1164
1165 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
1166 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
1167 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
1168 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
1169
1170 // FACE_POSITIVE_X = 000b
1171 // FACE_NEGATIVE_X = 001b
1172 // FACE_POSITIVE_Y = 010b
1173 // FACE_NEGATIVE_Y = 011b
1174 // FACE_POSITIVE_Z = 100b
1175 // FACE_NEGATIVE_Z = 101b
1176 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
1177
1178 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
1179 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
1180 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
1181
1182 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
1183 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
1184 }
1185 else if (IsIntegerSampler(textureFunction->sampler) &&
1186 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001187 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001188 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001189 {
Nicolas Capens93e50de2013-07-09 13:46:28 -04001190 if (IsSamplerArray(textureFunction->sampler))
1191 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001192 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001193
Nicolas Capens9edebd62013-08-06 10:59:10 -04001194 if (textureFunction->method == TextureFunction::LOD0)
1195 {
1196 out << " uint mip = 0;\n";
1197 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001198 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1199 {
1200 out << " uint mip = bias;\n";
1201 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001202 else
1203 {
1204 if (textureFunction->method == TextureFunction::IMPLICIT ||
1205 textureFunction->method == TextureFunction::BIAS)
1206 {
1207 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1208 " float2 tSized = float2(t.x * width, t.y * height);\n"
1209 " float dx = length(ddx(tSized));\n"
1210 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001211 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001212
1213 if (textureFunction->method == TextureFunction::BIAS)
1214 {
1215 out << " lod += bias;\n";
1216 }
1217 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001218 else if (textureFunction->method == TextureFunction::GRAD)
1219 {
1220 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1221 " float lod = log2(max(length(ddx), length(ddy)));\n";
1222 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001223
1224 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1225 }
1226
1227 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001228 }
1229 else
1230 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001231 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001232
Nicolas Capens9edebd62013-08-06 10:59:10 -04001233 if (textureFunction->method == TextureFunction::LOD0)
1234 {
1235 out << " uint mip = 0;\n";
1236 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001237 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1238 {
1239 out << " uint mip = bias;\n";
1240 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001241 else
1242 {
1243 if (textureFunction->method == TextureFunction::IMPLICIT ||
1244 textureFunction->method == TextureFunction::BIAS)
1245 {
1246 out << " x.GetDimensions(0, width, height, levels);\n"
1247 " float2 tSized = float2(t.x * width, t.y * height);\n"
1248 " float dx = length(ddx(tSized));\n"
1249 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001250 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001251
1252 if (textureFunction->method == TextureFunction::BIAS)
1253 {
1254 out << " lod += bias;\n";
1255 }
1256 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05001257 else if (textureFunction->method == TextureFunction::LOD)
1258 {
1259 out << " x.GetDimensions(0, width, height, levels);\n";
1260 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001261 else if (textureFunction->method == TextureFunction::GRAD)
1262 {
1263 out << " x.GetDimensions(0, width, height, levels);\n"
1264 " float lod = log2(max(length(ddx), length(ddy)));\n";
1265 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001266
1267 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1268 }
1269
1270 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001271 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001272 }
1273 else if (IsSampler3D(textureFunction->sampler))
1274 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001275 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001276
Nicolas Capens9edebd62013-08-06 10:59:10 -04001277 if (textureFunction->method == TextureFunction::LOD0)
1278 {
1279 out << " uint mip = 0;\n";
1280 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001281 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1282 {
1283 out << " uint mip = bias;\n";
1284 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001285 else
1286 {
1287 if (textureFunction->method == TextureFunction::IMPLICIT ||
1288 textureFunction->method == TextureFunction::BIAS)
1289 {
1290 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1291 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
1292 " float dx = length(ddx(tSized));\n"
1293 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001294 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001295
1296 if (textureFunction->method == TextureFunction::BIAS)
1297 {
1298 out << " lod += bias;\n";
1299 }
1300 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001301 else if (textureFunction->method == TextureFunction::GRAD)
1302 {
1303 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1304 " float lod = log2(max(length(ddx), length(ddy)));\n";
1305 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001306
1307 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1308 }
1309
1310 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001311 }
1312 else UNREACHABLE();
1313 }
1314
1315 out << " return ";
1316
1317 // HLSL intrinsic
1318 if (mOutputType == SH_HLSL9_OUTPUT)
1319 {
1320 switch(textureFunction->sampler)
1321 {
1322 case EbtSampler2D: out << "tex2D"; break;
1323 case EbtSamplerCube: out << "texCUBE"; break;
1324 default: UNREACHABLE();
1325 }
1326
Nicolas Capens75fb4752013-07-10 15:14:47 -04001327 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001328 {
1329 case TextureFunction::IMPLICIT: out << "(s, "; break;
1330 case TextureFunction::BIAS: out << "bias(s, "; break;
1331 case TextureFunction::LOD: out << "lod(s, "; break;
1332 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001333 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001334 default: UNREACHABLE();
1335 }
1336 }
1337 else if (mOutputType == SH_HLSL11_OUTPUT)
1338 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001339 if (textureFunction->method == TextureFunction::GRAD)
1340 {
1341 if (IsIntegerSampler(textureFunction->sampler))
1342 {
1343 out << "x.Load(";
1344 }
1345 else if (IsShadowSampler(textureFunction->sampler))
1346 {
1347 out << "x.SampleCmpLevelZero(s, ";
1348 }
1349 else
1350 {
1351 out << "x.SampleGrad(s, ";
1352 }
1353 }
1354 else if (IsIntegerSampler(textureFunction->sampler) ||
1355 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001356 {
1357 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001358 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001359 else if (IsShadowSampler(textureFunction->sampler))
1360 {
1361 out << "x.SampleCmp(s, ";
1362 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001363 else
1364 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001365 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001366 {
1367 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1368 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1369 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1370 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001371 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001372 default: UNREACHABLE();
1373 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001374 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001375 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001376 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001377
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001378 // Integer sampling requires integer addresses
1379 TString addressx = "";
1380 TString addressy = "";
1381 TString addressz = "";
1382 TString close = "";
1383
Nicolas Capensfc014542014-02-18 14:47:13 -05001384 if (IsIntegerSampler(textureFunction->sampler) ||
1385 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001386 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001387 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001388 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001389 case 2: out << "int3("; break;
1390 case 3: out << "int4("; break;
1391 default: UNREACHABLE();
1392 }
1393
Nicolas Capensfc014542014-02-18 14:47:13 -05001394 // Convert from normalized floating-point to integer
1395 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001396 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001397 addressx = "int(floor(width * frac((";
1398 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001399
Nicolas Capensfc014542014-02-18 14:47:13 -05001400 if (IsSamplerArray(textureFunction->sampler))
1401 {
1402 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1403 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001404 else if (IsSamplerCube(textureFunction->sampler))
1405 {
1406 addressz = "((((";
1407 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001408 else
1409 {
1410 addressz = "int(floor(depth * frac((";
1411 }
1412
1413 close = "))))";
1414 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001415 }
1416 else
1417 {
1418 switch(hlslCoords)
1419 {
1420 case 2: out << "float2("; break;
1421 case 3: out << "float3("; break;
1422 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001423 default: UNREACHABLE();
1424 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001425 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001426
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001427 TString proj = ""; // Only used for projected textures
1428
1429 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001430 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001431 switch(textureFunction->coords)
1432 {
1433 case 3: proj = " / t.z"; break;
1434 case 4: proj = " / t.w"; break;
1435 default: UNREACHABLE();
1436 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001437 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001438
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001439 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001440
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001441 if (mOutputType == SH_HLSL9_OUTPUT)
1442 {
1443 if (hlslCoords >= 3)
1444 {
1445 if (textureFunction->coords < 3)
1446 {
1447 out << ", 0";
1448 }
1449 else
1450 {
1451 out << ", t.z" + proj;
1452 }
1453 }
1454
1455 if (hlslCoords == 4)
1456 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001457 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001458 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001459 case TextureFunction::BIAS: out << ", bias"; break;
1460 case TextureFunction::LOD: out << ", lod"; break;
1461 case TextureFunction::LOD0: out << ", 0"; break;
1462 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001463 default: UNREACHABLE();
1464 }
1465 }
1466
1467 out << "));\n";
1468 }
1469 else if (mOutputType == SH_HLSL11_OUTPUT)
1470 {
1471 if (hlslCoords >= 3)
1472 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001473 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1474 {
1475 out << ", face";
1476 }
1477 else
1478 {
1479 out << ", " + addressz + ("t.z" + proj) + close;
1480 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001481 }
1482
Nicolas Capensd11d5492014-02-19 17:06:10 -05001483 if (textureFunction->method == TextureFunction::GRAD)
1484 {
1485 if (IsIntegerSampler(textureFunction->sampler))
1486 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001487 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001488 }
1489 else if (IsShadowSampler(textureFunction->sampler))
1490 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001491 // Compare value
Nicolas Capensd11d5492014-02-19 17:06:10 -05001492 switch(textureFunction->coords)
1493 {
1494 case 3: out << "), t.z"; break;
1495 case 4: out << "), t.w"; break;
1496 default: UNREACHABLE();
1497 }
1498 }
1499 else
1500 {
1501 out << "), ddx, ddy";
1502 }
1503 }
1504 else if (IsIntegerSampler(textureFunction->sampler) ||
1505 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001506 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001507 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001508 }
1509 else if (IsShadowSampler(textureFunction->sampler))
1510 {
1511 // Compare value
1512 switch(textureFunction->coords)
1513 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001514 case 3: out << "), t.z"; break;
1515 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001516 default: UNREACHABLE();
1517 }
1518 }
1519 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001520 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001521 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001522 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001523 case TextureFunction::IMPLICIT: out << ")"; break;
1524 case TextureFunction::BIAS: out << "), bias"; break;
1525 case TextureFunction::LOD: out << "), lod"; break;
1526 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001527 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001528 default: UNREACHABLE();
1529 }
1530 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001531
1532 if (textureFunction->offset)
1533 {
1534 out << ", offset";
1535 }
1536
1537 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001538 }
1539 else UNREACHABLE();
1540 }
1541
1542 out << "\n"
1543 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001544 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001545 }
1546
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001547 if (mUsesFragCoord)
1548 {
1549 out << "#define GL_USES_FRAG_COORD\n";
1550 }
1551
1552 if (mUsesPointCoord)
1553 {
1554 out << "#define GL_USES_POINT_COORD\n";
1555 }
1556
1557 if (mUsesFrontFacing)
1558 {
1559 out << "#define GL_USES_FRONT_FACING\n";
1560 }
1561
1562 if (mUsesPointSize)
1563 {
1564 out << "#define GL_USES_POINT_SIZE\n";
1565 }
1566
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001567 if (mUsesFragDepth)
1568 {
1569 out << "#define GL_USES_FRAG_DEPTH\n";
1570 }
1571
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001572 if (mUsesDepthRange)
1573 {
1574 out << "#define GL_USES_DEPTH_RANGE\n";
1575 }
1576
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001577 if (mUsesXor)
1578 {
1579 out << "bool xor(bool p, bool q)\n"
1580 "{\n"
1581 " return (p || q) && !(p && q);\n"
1582 "}\n"
1583 "\n";
1584 }
1585
1586 if (mUsesMod1)
1587 {
1588 out << "float mod(float x, float y)\n"
1589 "{\n"
1590 " return x - y * floor(x / y);\n"
1591 "}\n"
1592 "\n";
1593 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001594
1595 if (mUsesMod2v)
1596 {
1597 out << "float2 mod(float2 x, float2 y)\n"
1598 "{\n"
1599 " return x - y * floor(x / y);\n"
1600 "}\n"
1601 "\n";
1602 }
1603
1604 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001605 {
1606 out << "float2 mod(float2 x, float y)\n"
1607 "{\n"
1608 " return x - y * floor(x / y);\n"
1609 "}\n"
1610 "\n";
1611 }
1612
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001613 if (mUsesMod3v)
1614 {
1615 out << "float3 mod(float3 x, float3 y)\n"
1616 "{\n"
1617 " return x - y * floor(x / y);\n"
1618 "}\n"
1619 "\n";
1620 }
1621
1622 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001623 {
1624 out << "float3 mod(float3 x, float y)\n"
1625 "{\n"
1626 " return x - y * floor(x / y);\n"
1627 "}\n"
1628 "\n";
1629 }
1630
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001631 if (mUsesMod4v)
1632 {
1633 out << "float4 mod(float4 x, float4 y)\n"
1634 "{\n"
1635 " return x - y * floor(x / y);\n"
1636 "}\n"
1637 "\n";
1638 }
1639
1640 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001641 {
1642 out << "float4 mod(float4 x, float y)\n"
1643 "{\n"
1644 " return x - y * floor(x / y);\n"
1645 "}\n"
1646 "\n";
1647 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001648
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001649 if (mUsesFaceforward1)
1650 {
1651 out << "float faceforward(float N, float I, float Nref)\n"
1652 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001653 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001654 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001655 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001656 " }\n"
1657 " else\n"
1658 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001659 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001660 " }\n"
1661 "}\n"
1662 "\n";
1663 }
1664
1665 if (mUsesFaceforward2)
1666 {
1667 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1668 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001669 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001670 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001671 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001672 " }\n"
1673 " else\n"
1674 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001675 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001676 " }\n"
1677 "}\n"
1678 "\n";
1679 }
1680
1681 if (mUsesFaceforward3)
1682 {
1683 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1684 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001685 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001686 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001687 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001688 " }\n"
1689 " else\n"
1690 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001691 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001692 " }\n"
1693 "}\n"
1694 "\n";
1695 }
1696
1697 if (mUsesFaceforward4)
1698 {
1699 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1700 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001701 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001702 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001703 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001704 " }\n"
1705 " else\n"
1706 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001707 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001708 " }\n"
1709 "}\n"
1710 "\n";
1711 }
1712
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001713 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001714 {
1715 out << "float atanyx(float y, float x)\n"
1716 "{\n"
1717 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1718 " return atan2(y, x);\n"
1719 "}\n";
1720 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001721
1722 if (mUsesAtan2_2)
1723 {
1724 out << "float2 atanyx(float2 y, float2 x)\n"
1725 "{\n"
1726 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1727 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1728 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1729 "}\n";
1730 }
1731
1732 if (mUsesAtan2_3)
1733 {
1734 out << "float3 atanyx(float3 y, float3 x)\n"
1735 "{\n"
1736 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1737 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1738 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1739 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1740 "}\n";
1741 }
1742
1743 if (mUsesAtan2_4)
1744 {
1745 out << "float4 atanyx(float4 y, float4 x)\n"
1746 "{\n"
1747 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1748 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1749 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1750 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1751 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1752 "}\n";
1753 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001754}
1755
1756void OutputHLSL::visitSymbol(TIntermSymbol *node)
1757{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001758 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001759
Jamie Madill570e04d2013-06-21 09:15:33 -04001760 // Handle accessing std140 structs by value
1761 if (mFlaggedStructMappedNames.count(node) > 0)
1762 {
1763 out << mFlaggedStructMappedNames[node];
1764 return;
1765 }
1766
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001767 TString name = node->getSymbol();
1768
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001769 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001770 {
1771 mUsesDepthRange = true;
1772 out << name;
1773 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001774 else
1775 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001776 TQualifier qualifier = node->getQualifier();
1777
1778 if (qualifier == EvqUniform)
1779 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001780 const TType& nodeType = node->getType();
1781 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1782
1783 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001784 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001785 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001786 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001787 else
1788 {
1789 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001790 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001791
1792 out << decorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001793 }
Jamie Madill19571812013-08-12 15:26:34 -07001794 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001795 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001796 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001797 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001798 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001799 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001800 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001801 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001802 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001803 }
Jamie Madill19571812013-08-12 15:26:34 -07001804 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001805 {
1806 mReferencedOutputVariables[name] = node;
1807 out << "out_" << name;
1808 }
1809 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001810 {
1811 out << "gl_Color[0]";
1812 mUsesFragColor = true;
1813 }
1814 else if (qualifier == EvqFragData)
1815 {
1816 out << "gl_Color";
1817 mUsesFragData = true;
1818 }
1819 else if (qualifier == EvqFragCoord)
1820 {
1821 mUsesFragCoord = true;
1822 out << name;
1823 }
1824 else if (qualifier == EvqPointCoord)
1825 {
1826 mUsesPointCoord = true;
1827 out << name;
1828 }
1829 else if (qualifier == EvqFrontFacing)
1830 {
1831 mUsesFrontFacing = true;
1832 out << name;
1833 }
1834 else if (qualifier == EvqPointSize)
1835 {
1836 mUsesPointSize = true;
1837 out << name;
1838 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001839 else if (name == "gl_FragDepthEXT")
1840 {
1841 mUsesFragDepth = true;
1842 out << "gl_Depth";
1843 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001844 else if (qualifier == EvqInternal)
1845 {
1846 out << name;
1847 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001848 else
1849 {
1850 out << decorate(name);
1851 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001852 }
1853}
1854
1855bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1856{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001857 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001858
Jamie Madill570e04d2013-06-21 09:15:33 -04001859 // Handle accessing std140 structs by value
1860 if (mFlaggedStructMappedNames.count(node) > 0)
1861 {
1862 out << mFlaggedStructMappedNames[node];
1863 return false;
1864 }
1865
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001866 switch (node->getOp())
1867 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001868 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001869 case EOpInitialize:
1870 if (visit == PreVisit)
1871 {
1872 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1873 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1874 // new variable is created before the assignment is evaluated), so we need to convert
1875 // this to "float t = x, x = t;".
1876
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001877 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1878 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001879
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001880 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1881 expression->traverse(&searchSymbol);
1882 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001883
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001884 if (sameSymbol)
1885 {
1886 // Type already printed
1887 out << "t" + str(mUniqueIndex) + " = ";
1888 expression->traverse(this);
1889 out << ", ";
1890 symbolNode->traverse(this);
1891 out << " = t" + str(mUniqueIndex);
1892
1893 mUniqueIndex++;
1894 return false;
1895 }
1896 }
1897 else if (visit == InVisit)
1898 {
1899 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001900 }
1901 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001902 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1903 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1904 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1905 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1906 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1907 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001908 if (visit == PreVisit)
1909 {
1910 out << "(";
1911 }
1912 else if (visit == InVisit)
1913 {
1914 out << " = mul(";
1915 node->getLeft()->traverse(this);
1916 out << ", transpose(";
1917 }
1918 else
1919 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001920 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001921 }
1922 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001923 case EOpMatrixTimesMatrixAssign:
1924 if (visit == PreVisit)
1925 {
1926 out << "(";
1927 }
1928 else if (visit == InVisit)
1929 {
1930 out << " = mul(";
1931 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001932 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001933 }
1934 else
1935 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001936 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001937 }
1938 break;
1939 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001940 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001941 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001942 const TType& leftType = node->getLeft()->getType();
1943 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001944 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001945 if (visit == PreVisit)
1946 {
1947 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1948 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1949
1950 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1951 out << interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1952
1953 return false;
1954 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001955 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001956 else
1957 {
1958 outputTriplet(visit, "", "[", "]");
1959 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001960 }
1961 break;
1962 case EOpIndexIndirect:
1963 // We do not currently support indirect references to interface blocks
1964 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1965 outputTriplet(visit, "", "[", "]");
1966 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001967 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001968 if (visit == InVisit)
1969 {
1970 const TStructure* structure = node->getLeft()->getType().getStruct();
1971 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1972 const TField* field = structure->fields()[index->getIConst(0)];
1973 out << "." + decorateField(field->name(), *structure);
1974
1975 return false;
1976 }
1977 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001978 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001979 if (visit == InVisit)
1980 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001981 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1982 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1983 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
1984 out << "." + decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001985
1986 return false;
1987 }
1988 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001989 case EOpVectorSwizzle:
1990 if (visit == InVisit)
1991 {
1992 out << ".";
1993
1994 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1995
1996 if (swizzle)
1997 {
1998 TIntermSequence &sequence = swizzle->getSequence();
1999
2000 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2001 {
2002 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
2003
2004 if (element)
2005 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002006 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002007
2008 switch (i)
2009 {
2010 case 0: out << "x"; break;
2011 case 1: out << "y"; break;
2012 case 2: out << "z"; break;
2013 case 3: out << "w"; break;
2014 default: UNREACHABLE();
2015 }
2016 }
2017 else UNREACHABLE();
2018 }
2019 }
2020 else UNREACHABLE();
2021
2022 return false; // Fully processed
2023 }
2024 break;
2025 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
2026 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
2027 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
2028 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002029 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002030 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002031 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002032 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002033 if (node->getOp() == EOpEqual)
2034 {
2035 outputTriplet(visit, "(", " == ", ")");
2036 }
2037 else
2038 {
2039 outputTriplet(visit, "(", " != ", ")");
2040 }
2041 }
2042 else if (node->getLeft()->getBasicType() == EbtStruct)
2043 {
2044 if (node->getOp() == EOpEqual)
2045 {
2046 out << "(";
2047 }
2048 else
2049 {
2050 out << "!(";
2051 }
2052
Jamie Madill98493dd2013-07-08 14:39:03 -04002053 const TStructure &structure = *node->getLeft()->getType().getStruct();
2054 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002055
Jamie Madill98493dd2013-07-08 14:39:03 -04002056 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002057 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002058 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002059
2060 node->getLeft()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04002061 out << "." + decorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002062 node->getRight()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04002063 out << "." + decorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002064
Jamie Madill98493dd2013-07-08 14:39:03 -04002065 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002066 {
2067 out << " && ";
2068 }
2069 }
2070
2071 out << ")";
2072
2073 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002074 }
2075 else
2076 {
Jamie Madill0b20c942013-07-19 16:36:56 -04002077 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002078
2079 if (node->getOp() == EOpEqual)
2080 {
Jamie Madill0b20c942013-07-19 16:36:56 -04002081 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002082 }
2083 else
2084 {
Jamie Madill0b20c942013-07-19 16:36:56 -04002085 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002086 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002087 }
2088 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002089 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2090 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2091 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2092 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2093 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00002094 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00002095 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
2096 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00002097 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00002098 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002099 if (node->getRight()->hasSideEffects())
2100 {
2101 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
2102 return false;
2103 }
2104 else
2105 {
2106 outputTriplet(visit, "(", " || ", ")");
2107 return true;
2108 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002109 case EOpLogicalXor:
2110 mUsesXor = true;
2111 outputTriplet(visit, "xor(", ", ", ")");
2112 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00002113 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002114 if (node->getRight()->hasSideEffects())
2115 {
2116 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
2117 return false;
2118 }
2119 else
2120 {
2121 outputTriplet(visit, "(", " && ", ")");
2122 return true;
2123 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002124 default: UNREACHABLE();
2125 }
2126
2127 return true;
2128}
2129
2130bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
2131{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002132 switch (node->getOp())
2133 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002134 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
2135 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
2136 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
2137 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
2138 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
2139 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
2140 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002141 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04002142 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002143 case EOpConvFloatToBool:
2144 switch (node->getOperand()->getType().getNominalSize())
2145 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002146 case 1: outputTriplet(visit, "bool(", "", ")"); break;
2147 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
2148 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
2149 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002150 default: UNREACHABLE();
2151 }
2152 break;
2153 case EOpConvBoolToFloat:
2154 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04002155 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002156 switch (node->getOperand()->getType().getNominalSize())
2157 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002158 case 1: outputTriplet(visit, "float(", "", ")"); break;
2159 case 2: outputTriplet(visit, "float2(", "", ")"); break;
2160 case 3: outputTriplet(visit, "float3(", "", ")"); break;
2161 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002162 default: UNREACHABLE();
2163 }
2164 break;
2165 case EOpConvFloatToInt:
2166 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04002167 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002168 switch (node->getOperand()->getType().getNominalSize())
2169 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002170 case 1: outputTriplet(visit, "int(", "", ")"); break;
2171 case 2: outputTriplet(visit, "int2(", "", ")"); break;
2172 case 3: outputTriplet(visit, "int3(", "", ")"); break;
2173 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002174 default: UNREACHABLE();
2175 }
2176 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002177 case EOpConvFloatToUInt:
2178 case EOpConvBoolToUInt:
2179 case EOpConvIntToUInt:
Jamie Madilla16f1672013-07-03 15:15:17 -04002180 switch (node->getOperand()->getType().getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002181 {
2182 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002183 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
2184 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
2185 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002186 default: UNREACHABLE();
2187 }
2188 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002189 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
2190 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
2191 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
2192 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
2193 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
2194 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
2195 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
2196 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
2197 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
2198 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
2199 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
2200 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
2201 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
2202 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
2203 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
2204 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
2205 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
2206 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
2207 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
2208 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
2209 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002210 case EOpDFdx:
2211 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2212 {
2213 outputTriplet(visit, "(", "", ", 0.0)");
2214 }
2215 else
2216 {
2217 outputTriplet(visit, "ddx(", "", ")");
2218 }
2219 break;
2220 case EOpDFdy:
2221 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2222 {
2223 outputTriplet(visit, "(", "", ", 0.0)");
2224 }
2225 else
2226 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00002227 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002228 }
2229 break;
2230 case EOpFwidth:
2231 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2232 {
2233 outputTriplet(visit, "(", "", ", 0.0)");
2234 }
2235 else
2236 {
2237 outputTriplet(visit, "fwidth(", "", ")");
2238 }
2239 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002240 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
2241 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002242 default: UNREACHABLE();
2243 }
2244
2245 return true;
2246}
2247
2248bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2249{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002250 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002251
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002252 switch (node->getOp())
2253 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002254 case EOpSequence:
2255 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002256 if (mInsideFunction)
2257 {
Jamie Madill075edd82013-07-08 13:30:19 -04002258 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002259 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002260
2261 mScopeDepth++;
2262
2263 if (mScopeBracket.size() < mScopeDepth)
2264 {
2265 mScopeBracket.push_back(0); // New scope level
2266 }
2267 else
2268 {
2269 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
2270 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002271 }
2272
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002273 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
2274 {
Jamie Madill075edd82013-07-08 13:30:19 -04002275 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002276
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002277 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002278
2279 out << ";\n";
2280 }
2281
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002282 if (mInsideFunction)
2283 {
Jamie Madill075edd82013-07-08 13:30:19 -04002284 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002285 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002286
2287 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002288 }
2289
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002290 return false;
2291 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002292 case EOpDeclaration:
2293 if (visit == PreVisit)
2294 {
2295 TIntermSequence &sequence = node->getSequence();
2296 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002297
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00002298 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002299 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002300 if (variable->getType().getStruct())
2301 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002302 addConstructor(variable->getType(), scopedStruct(variable->getType().getStruct()->name()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00002303 }
2304
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002305 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002306 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00002307 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002308 {
2309 out << "static ";
2310 }
2311
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002312 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002313
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002314 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002315 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002316 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002317
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002318 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002319 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002320 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002321 out << arrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05002322 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002323 }
2324 else
2325 {
2326 (*sit)->traverse(this);
2327 }
2328
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002329 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002330 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002331 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002332 }
2333 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002334 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002335 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2336 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002337 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002338 }
2339 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002340 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002341 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002342 {
2343 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2344 {
2345 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2346
2347 if (symbol)
2348 {
2349 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2350 mReferencedVaryings[symbol->getSymbol()] = symbol;
2351 }
2352 else
2353 {
2354 (*sit)->traverse(this);
2355 }
2356 }
2357 }
2358
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002359 return false;
2360 }
2361 else if (visit == InVisit)
2362 {
2363 out << ", ";
2364 }
2365 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002366 case EOpPrototype:
2367 if (visit == PreVisit)
2368 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002369 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002370
2371 TIntermSequence &arguments = node->getSequence();
2372
2373 for (unsigned int i = 0; i < arguments.size(); i++)
2374 {
2375 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2376
2377 if (symbol)
2378 {
2379 out << argumentString(symbol);
2380
2381 if (i < arguments.size() - 1)
2382 {
2383 out << ", ";
2384 }
2385 }
2386 else UNREACHABLE();
2387 }
2388
2389 out << ");\n";
2390
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002391 // Also prototype the Lod0 variant if needed
2392 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2393 {
2394 mOutputLod0Function = true;
2395 node->traverse(this);
2396 mOutputLod0Function = false;
2397 }
2398
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002399 return false;
2400 }
2401 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002402 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002403 case EOpFunction:
2404 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002405 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002406
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002407 out << typeString(node->getType()) << " ";
2408
2409 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002410 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002411 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002412 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002413 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002414 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002415 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002416 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002417
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002418 TIntermSequence &sequence = node->getSequence();
2419 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2420
2421 for (unsigned int i = 0; i < arguments.size(); i++)
2422 {
2423 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2424
2425 if (symbol)
2426 {
2427 if (symbol->getType().getStruct())
2428 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002429 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getStruct()->name()), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002430 }
2431
2432 out << argumentString(symbol);
2433
2434 if (i < arguments.size() - 1)
2435 {
2436 out << ", ";
2437 }
2438 }
2439 else UNREACHABLE();
2440 }
2441
2442 out << ")\n"
2443 "{\n";
2444
2445 if (sequence.size() > 1)
2446 {
2447 mInsideFunction = true;
2448 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002449 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002450 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002451
2452 out << "}\n";
2453
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002454 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2455 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002456 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002457 {
2458 mOutputLod0Function = true;
2459 node->traverse(this);
2460 mOutputLod0Function = false;
2461 }
2462 }
2463
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002464 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002465 }
2466 break;
2467 case EOpFunctionCall:
2468 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002469 TString name = TFunction::unmangleName(node->getName());
2470 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002471 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002472
2473 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002474 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002475 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002476 }
2477 else
2478 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002479 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2480
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002481 TextureFunction textureFunction;
2482 textureFunction.sampler = samplerType;
2483 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002484 textureFunction.method = TextureFunction::IMPLICIT;
2485 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002486 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002487
2488 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002489 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002490 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002491 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002492 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002493 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002494 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002495 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002496 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002497 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002498 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002499 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002500 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002501 else if (name == "texture2DProjLod" || name == "textureProjLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002502 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002503 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002504 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002505 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002506 else if (name == "textureSize")
2507 {
2508 textureFunction.method = TextureFunction::SIZE;
2509 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002510 else if (name == "textureOffset")
2511 {
2512 textureFunction.method = TextureFunction::IMPLICIT;
2513 textureFunction.offset = true;
2514 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002515 else if (name == "textureProjOffset")
2516 {
2517 textureFunction.method = TextureFunction::IMPLICIT;
2518 textureFunction.offset = true;
2519 textureFunction.proj = true;
2520 }
2521 else if (name == "textureLodOffset")
2522 {
2523 textureFunction.method = TextureFunction::LOD;
2524 textureFunction.offset = true;
2525 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002526 else if (name == "textureProjLod")
2527 {
2528 textureFunction.method = TextureFunction::LOD;
2529 textureFunction.proj = true;
2530 }
2531 else if (name == "textureProjLodOffset")
2532 {
2533 textureFunction.method = TextureFunction::LOD;
2534 textureFunction.proj = true;
2535 textureFunction.offset = true;
2536 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002537 else if (name == "texelFetch")
2538 {
2539 textureFunction.method = TextureFunction::FETCH;
2540 }
2541 else if (name == "texelFetchOffset")
2542 {
2543 textureFunction.method = TextureFunction::FETCH;
2544 textureFunction.offset = true;
2545 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05002546 else if (name == "textureGrad")
2547 {
2548 textureFunction.method = TextureFunction::GRAD;
2549 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002550 else if (name == "textureGradOffset")
2551 {
2552 textureFunction.method = TextureFunction::GRAD;
2553 textureFunction.offset = true;
2554 }
Nicolas Capensf7378e32014-02-19 17:29:32 -05002555 else if (name == "textureProjGrad")
2556 {
2557 textureFunction.method = TextureFunction::GRAD;
2558 textureFunction.proj = true;
2559 }
2560 else if (name == "textureProjGradOffset")
2561 {
2562 textureFunction.method = TextureFunction::GRAD;
2563 textureFunction.proj = true;
2564 textureFunction.offset = true;
2565 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002566 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002567
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002568 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002569 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002570 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2571
2572 if (textureFunction.offset)
2573 {
2574 mandatoryArgumentCount++;
2575 }
2576
2577 bool bias = (arguments.size() > mandatoryArgumentCount); // Bias argument is optional
2578
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002579 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2580 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002581 if (bias)
2582 {
2583 textureFunction.method = TextureFunction::LOD0BIAS;
2584 }
2585 else
2586 {
2587 textureFunction.method = TextureFunction::LOD0;
2588 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002589 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002590 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002591 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002592 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002593 }
2594 }
2595
2596 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002597
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002598 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002599 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002600
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002601 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2602 {
2603 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2604 {
2605 out << "texture_";
2606 (*arg)->traverse(this);
2607 out << ", sampler_";
2608 }
2609
2610 (*arg)->traverse(this);
2611
2612 if (arg < arguments.end() - 1)
2613 {
2614 out << ", ";
2615 }
2616 }
2617
2618 out << ")";
2619
2620 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002621 }
2622 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002623 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002624 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002625 addConstructor(node->getType(), "vec1", &node->getSequence());
2626 outputTriplet(visit, "vec1(", "", ")");
2627 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002628 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002629 addConstructor(node->getType(), "vec2", &node->getSequence());
2630 outputTriplet(visit, "vec2(", ", ", ")");
2631 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002632 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002633 addConstructor(node->getType(), "vec3", &node->getSequence());
2634 outputTriplet(visit, "vec3(", ", ", ")");
2635 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002636 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002637 addConstructor(node->getType(), "vec4", &node->getSequence());
2638 outputTriplet(visit, "vec4(", ", ", ")");
2639 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002640 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002641 addConstructor(node->getType(), "bvec1", &node->getSequence());
2642 outputTriplet(visit, "bvec1(", "", ")");
2643 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002644 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002645 addConstructor(node->getType(), "bvec2", &node->getSequence());
2646 outputTriplet(visit, "bvec2(", ", ", ")");
2647 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002648 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002649 addConstructor(node->getType(), "bvec3", &node->getSequence());
2650 outputTriplet(visit, "bvec3(", ", ", ")");
2651 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002652 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002653 addConstructor(node->getType(), "bvec4", &node->getSequence());
2654 outputTriplet(visit, "bvec4(", ", ", ")");
2655 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002656 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002657 addConstructor(node->getType(), "ivec1", &node->getSequence());
2658 outputTriplet(visit, "ivec1(", "", ")");
2659 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002660 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002661 addConstructor(node->getType(), "ivec2", &node->getSequence());
2662 outputTriplet(visit, "ivec2(", ", ", ")");
2663 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002664 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002665 addConstructor(node->getType(), "ivec3", &node->getSequence());
2666 outputTriplet(visit, "ivec3(", ", ", ")");
2667 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002668 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002669 addConstructor(node->getType(), "ivec4", &node->getSequence());
2670 outputTriplet(visit, "ivec4(", ", ", ")");
2671 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002672 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002673 addConstructor(node->getType(), "uvec1", &node->getSequence());
2674 outputTriplet(visit, "uvec1(", "", ")");
2675 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002676 case EOpConstructUVec2:
2677 addConstructor(node->getType(), "uvec2", &node->getSequence());
2678 outputTriplet(visit, "uvec2(", ", ", ")");
2679 break;
2680 case EOpConstructUVec3:
2681 addConstructor(node->getType(), "uvec3", &node->getSequence());
2682 outputTriplet(visit, "uvec3(", ", ", ")");
2683 break;
2684 case EOpConstructUVec4:
2685 addConstructor(node->getType(), "uvec4", &node->getSequence());
2686 outputTriplet(visit, "uvec4(", ", ", ")");
2687 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002688 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002689 addConstructor(node->getType(), "mat2", &node->getSequence());
2690 outputTriplet(visit, "mat2(", ", ", ")");
2691 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002692 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002693 addConstructor(node->getType(), "mat3", &node->getSequence());
2694 outputTriplet(visit, "mat3(", ", ", ")");
2695 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002696 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002697 addConstructor(node->getType(), "mat4", &node->getSequence());
2698 outputTriplet(visit, "mat4(", ", ", ")");
2699 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002700 case EOpConstructStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04002701 addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence());
2702 outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002703 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002704 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2705 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2706 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2707 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2708 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2709 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002710 case EOpMod:
2711 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002712 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002713 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2714 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2715 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002716 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002717 case 11: mUsesMod1 = true; break;
2718 case 22: mUsesMod2v = true; break;
2719 case 21: mUsesMod2f = true; break;
2720 case 33: mUsesMod3v = true; break;
2721 case 31: mUsesMod3f = true; break;
2722 case 44: mUsesMod4v = true; break;
2723 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002724 default: UNREACHABLE();
2725 }
2726
2727 outputTriplet(visit, "mod(", ", ", ")");
2728 }
2729 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002730 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002731 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002732 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002733 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2734 {
2735 case 1: mUsesAtan2_1 = true; break;
2736 case 2: mUsesAtan2_2 = true; break;
2737 case 3: mUsesAtan2_3 = true; break;
2738 case 4: mUsesAtan2_4 = true; break;
2739 default: UNREACHABLE();
2740 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002741 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002742 break;
2743 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2744 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2745 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2746 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2747 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2748 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2749 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2750 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2751 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002752 case EOpFaceForward:
2753 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002754 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002755 {
2756 case 1: mUsesFaceforward1 = true; break;
2757 case 2: mUsesFaceforward2 = true; break;
2758 case 3: mUsesFaceforward3 = true; break;
2759 case 4: mUsesFaceforward4 = true; break;
2760 default: UNREACHABLE();
2761 }
2762
2763 outputTriplet(visit, "faceforward(", ", ", ")");
2764 }
2765 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002766 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2767 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2768 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002769 default: UNREACHABLE();
2770 }
2771
2772 return true;
2773}
2774
2775bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2776{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002777 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002778
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002779 if (node->usesTernaryOperator())
2780 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002781 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002782 }
2783 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002784 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002785 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002786
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002787 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002788
2789 node->getCondition()->traverse(this);
2790
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002791 out << ")\n";
2792
Jamie Madill075edd82013-07-08 13:30:19 -04002793 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002794 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002795
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002796 bool discard = false;
2797
daniel@transgaming.combb885322010-04-15 20:45:24 +00002798 if (node->getTrueBlock())
2799 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002800 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002801
2802 // Detect true discard
2803 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002804 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002805
Jamie Madill075edd82013-07-08 13:30:19 -04002806 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002807 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002808
2809 if (node->getFalseBlock())
2810 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002811 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002812
Jamie Madill075edd82013-07-08 13:30:19 -04002813 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002814 out << "{\n";
2815
Jamie Madill075edd82013-07-08 13:30:19 -04002816 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002817 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002818
Jamie Madill075edd82013-07-08 13:30:19 -04002819 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002820 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002821
2822 // Detect false discard
2823 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2824 }
2825
2826 // ANGLE issue 486: Detect problematic conditional discard
2827 if (discard && FindSideEffectRewriting::search(node))
2828 {
2829 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002830 }
2831 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002832
2833 return false;
2834}
2835
2836void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2837{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002838 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002839}
2840
2841bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2842{
Nicolas Capens655fe362014-04-11 13:12:34 -04002843 mNestedLoopDepth++;
2844
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002845 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2846
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002847 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002848 {
2849 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2850 }
2851
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002852 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002853 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002854 if (handleExcessiveLoop(node))
2855 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002856 mInsideDiscontinuousLoop = wasDiscontinuous;
2857 mNestedLoopDepth--;
2858
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002859 return false;
2860 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002861 }
2862
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002863 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002864
alokp@chromium.org52813552010-11-16 18:36:09 +00002865 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002866 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002867 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002868
Jamie Madill075edd82013-07-08 13:30:19 -04002869 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002870 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002871 }
2872 else
2873 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002874 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002875
2876 if (node->getInit())
2877 {
2878 node->getInit()->traverse(this);
2879 }
2880
2881 out << "; ";
2882
alokp@chromium.org52813552010-11-16 18:36:09 +00002883 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002884 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002885 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002886 }
2887
2888 out << "; ";
2889
alokp@chromium.org52813552010-11-16 18:36:09 +00002890 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002891 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002892 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002893 }
2894
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002895 out << ")\n";
2896
Jamie Madill075edd82013-07-08 13:30:19 -04002897 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002898 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002899 }
2900
2901 if (node->getBody())
2902 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002903 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002904 }
2905
Jamie Madill075edd82013-07-08 13:30:19 -04002906 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002907 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002908
alokp@chromium.org52813552010-11-16 18:36:09 +00002909 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002910 {
Jamie Madill075edd82013-07-08 13:30:19 -04002911 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002912 out << "while(\n";
2913
alokp@chromium.org52813552010-11-16 18:36:09 +00002914 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002915
daniel@transgaming.com73536982012-03-21 20:45:49 +00002916 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002917 }
2918
daniel@transgaming.com73536982012-03-21 20:45:49 +00002919 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002920
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002921 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002922 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002923
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002924 return false;
2925}
2926
2927bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2928{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002929 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002930
2931 switch (node->getFlowOp())
2932 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002933 case EOpKill:
2934 outputTriplet(visit, "discard;\n", "", "");
2935 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002936 case EOpBreak:
2937 if (visit == PreVisit)
2938 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002939 if (mNestedLoopDepth > 1)
2940 {
2941 mUsesNestedBreak = true;
2942 }
2943
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002944 if (mExcessiveLoopIndex)
2945 {
2946 out << "{Break";
2947 mExcessiveLoopIndex->traverse(this);
2948 out << " = true; break;}\n";
2949 }
2950 else
2951 {
2952 out << "break;\n";
2953 }
2954 }
2955 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002956 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002957 case EOpReturn:
2958 if (visit == PreVisit)
2959 {
2960 if (node->getExpression())
2961 {
2962 out << "return ";
2963 }
2964 else
2965 {
2966 out << "return;\n";
2967 }
2968 }
2969 else if (visit == PostVisit)
2970 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002971 if (node->getExpression())
2972 {
2973 out << ";\n";
2974 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002975 }
2976 break;
2977 default: UNREACHABLE();
2978 }
2979
2980 return true;
2981}
2982
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002983void OutputHLSL::traverseStatements(TIntermNode *node)
2984{
2985 if (isSingleStatement(node))
2986 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002987 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002988 }
2989
2990 node->traverse(this);
2991}
2992
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002993bool OutputHLSL::isSingleStatement(TIntermNode *node)
2994{
2995 TIntermAggregate *aggregate = node->getAsAggregate();
2996
2997 if (aggregate)
2998 {
2999 if (aggregate->getOp() == EOpSequence)
3000 {
3001 return false;
3002 }
3003 else
3004 {
3005 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
3006 {
3007 if (!isSingleStatement(*sit))
3008 {
3009 return false;
3010 }
3011 }
3012
3013 return true;
3014 }
3015 }
3016
3017 return true;
3018}
3019
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003020// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
3021// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003022bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
3023{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003024 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00003025 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003026
3027 // Parse loops of the form:
3028 // for(int index = initial; index [comparator] limit; index += increment)
3029 TIntermSymbol *index = NULL;
3030 TOperator comparator = EOpNull;
3031 int initial = 0;
3032 int limit = 0;
3033 int increment = 0;
3034
3035 // Parse index name and intial value
3036 if (node->getInit())
3037 {
3038 TIntermAggregate *init = node->getInit()->getAsAggregate();
3039
3040 if (init)
3041 {
3042 TIntermSequence &sequence = init->getSequence();
3043 TIntermTyped *variable = sequence[0]->getAsTyped();
3044
3045 if (variable && variable->getQualifier() == EvqTemporary)
3046 {
3047 TIntermBinary *assign = variable->getAsBinaryNode();
3048
3049 if (assign->getOp() == EOpInitialize)
3050 {
3051 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3052 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3053
3054 if (symbol && constant)
3055 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003056 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003057 {
3058 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003059 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003060 }
3061 }
3062 }
3063 }
3064 }
3065 }
3066
3067 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00003068 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003069 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003070 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003071
3072 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
3073 {
3074 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3075
3076 if (constant)
3077 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003078 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003079 {
3080 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003081 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003082 }
3083 }
3084 }
3085 }
3086
3087 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00003088 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003089 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003090 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3091 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003092
3093 if (binaryTerminal)
3094 {
3095 TOperator op = binaryTerminal->getOp();
3096 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3097
3098 if (constant)
3099 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003100 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003101 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003102 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003103
3104 switch (op)
3105 {
3106 case EOpAddAssign: increment = value; break;
3107 case EOpSubAssign: increment = -value; break;
3108 default: UNIMPLEMENTED();
3109 }
3110 }
3111 }
3112 }
3113 else if (unaryTerminal)
3114 {
3115 TOperator op = unaryTerminal->getOp();
3116
3117 switch (op)
3118 {
3119 case EOpPostIncrement: increment = 1; break;
3120 case EOpPostDecrement: increment = -1; break;
3121 case EOpPreIncrement: increment = 1; break;
3122 case EOpPreDecrement: increment = -1; break;
3123 default: UNIMPLEMENTED();
3124 }
3125 }
3126 }
3127
3128 if (index != NULL && comparator != EOpNull && increment != 0)
3129 {
3130 if (comparator == EOpLessThanEqual)
3131 {
3132 comparator = EOpLessThan;
3133 limit += 1;
3134 }
3135
3136 if (comparator == EOpLessThan)
3137 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00003138 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003139
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003140 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003141 {
3142 return false; // Not an excessive loop
3143 }
3144
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003145 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
3146 mExcessiveLoopIndex = index;
3147
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003148 out << "{int ";
3149 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003150 out << ";\n"
3151 "bool Break";
3152 index->traverse(this);
3153 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003154
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003155 bool firstLoopFragment = true;
3156
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003157 while (iterations > 0)
3158 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003159 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003160
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003161 if (!firstLoopFragment)
3162 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003163 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003164 index->traverse(this);
3165 out << ") {\n";
3166 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00003167
3168 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
3169 {
3170 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
3171 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003172
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003173 // for(int index = initial; index < clampedLimit; index += increment)
3174
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003175 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003176 index->traverse(this);
3177 out << " = ";
3178 out << initial;
3179
3180 out << "; ";
3181 index->traverse(this);
3182 out << " < ";
3183 out << clampedLimit;
3184
3185 out << "; ";
3186 index->traverse(this);
3187 out << " += ";
3188 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003189 out << ")\n";
3190
Jamie Madill075edd82013-07-08 13:30:19 -04003191 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003192 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003193
3194 if (node->getBody())
3195 {
3196 node->getBody()->traverse(this);
3197 }
3198
Jamie Madill075edd82013-07-08 13:30:19 -04003199 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003200 out << ";}\n";
3201
3202 if (!firstLoopFragment)
3203 {
3204 out << "}\n";
3205 }
3206
3207 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003208
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003209 initial += MAX_LOOP_ITERATIONS * increment;
3210 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003211 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003212
3213 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003214
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003215 mExcessiveLoopIndex = restoreIndex;
3216
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003217 return true;
3218 }
3219 else UNIMPLEMENTED();
3220 }
3221
3222 return false; // Not handled as an excessive loop
3223}
3224
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003225void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003226{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00003227 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003228
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003229 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003230 {
3231 out << preString;
3232 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003233 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003234 {
3235 out << inString;
3236 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003237 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003238 {
3239 out << postString;
3240 }
3241}
3242
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003243void OutputHLSL::outputLineDirective(int line)
3244{
3245 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
3246 {
baustin@google.com8ab69842011-06-02 21:53:45 +00003247 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003248 mBody << "#line " << line;
3249
3250 if (mContext.sourcePath)
3251 {
3252 mBody << " \"" << mContext.sourcePath << "\"";
3253 }
3254
3255 mBody << "\n";
3256 }
3257}
3258
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003259TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3260{
3261 TQualifier qualifier = symbol->getQualifier();
3262 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003263 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003264
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003265 if (name.empty()) // HLSL demands named arguments, also for prototypes
3266 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00003267 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003268 }
3269 else
3270 {
3271 name = decorate(name);
3272 }
3273
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003274 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
3275 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003276 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
3277 qualifierString(qualifier) + " " + samplerString(type) + " sampler_" + name + arrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003278 }
3279
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003280 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003281}
3282
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003283TString OutputHLSL::interpolationString(TQualifier qualifier)
3284{
3285 switch(qualifier)
3286 {
3287 case EvqVaryingIn: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003288 case EvqFragmentIn: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003289 case EvqInvariantVaryingIn: return "";
3290 case EvqSmoothIn: return "linear";
3291 case EvqFlatIn: return "nointerpolation";
3292 case EvqCentroidIn: return "centroid";
3293 case EvqVaryingOut: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003294 case EvqVertexOut: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003295 case EvqInvariantVaryingOut: return "";
3296 case EvqSmoothOut: return "linear";
3297 case EvqFlatOut: return "nointerpolation";
3298 case EvqCentroidOut: return "centroid";
3299 default: UNREACHABLE();
3300 }
3301
3302 return "";
3303}
3304
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003305TString OutputHLSL::qualifierString(TQualifier qualifier)
3306{
3307 switch(qualifier)
3308 {
3309 case EvqIn: return "in";
3310 case EvqOut: return "out";
3311 case EvqInOut: return "inout";
3312 case EvqConstReadOnly: return "const";
3313 default: UNREACHABLE();
3314 }
3315
3316 return "";
3317}
3318
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003319TString OutputHLSL::typeString(const TType &type)
3320{
Jamie Madill98493dd2013-07-08 14:39:03 -04003321 const TStructure* structure = type.getStruct();
3322 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003323 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003324 const TString& typeName = structure->name();
3325 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003326 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003327 return structLookup(typeName);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003328 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003329 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003330 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003331 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003332 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003333 }
3334 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003335 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003336 int cols = type.getCols();
3337 int rows = type.getRows();
3338 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003339 }
3340 else
3341 {
3342 switch (type.getBasicType())
3343 {
3344 case EbtFloat:
3345 switch (type.getNominalSize())
3346 {
3347 case 1: return "float";
3348 case 2: return "float2";
3349 case 3: return "float3";
3350 case 4: return "float4";
3351 }
3352 case EbtInt:
3353 switch (type.getNominalSize())
3354 {
3355 case 1: return "int";
3356 case 2: return "int2";
3357 case 3: return "int3";
3358 case 4: return "int4";
3359 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003360 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04003361 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003362 {
3363 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003364 case 2: return "uint2";
3365 case 3: return "uint3";
3366 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003367 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003368 case EbtBool:
3369 switch (type.getNominalSize())
3370 {
3371 case 1: return "bool";
3372 case 2: return "bool2";
3373 case 3: return "bool3";
3374 case 4: return "bool4";
3375 }
3376 case EbtVoid:
3377 return "void";
3378 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003379 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04003380 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003381 case EbtSampler2DArray:
3382 case EbtISampler2DArray:
3383 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003384 return "sampler2D";
3385 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003386 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04003387 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003388 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00003389 case EbtSamplerExternalOES:
3390 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00003391 default:
3392 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003393 }
3394 }
3395
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003396 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003397 return "<unknown type>";
3398}
3399
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003400TString OutputHLSL::textureString(const TType &type)
3401{
3402 switch (type.getBasicType())
3403 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003404 case EbtSampler2D: return "Texture2D";
3405 case EbtSamplerCube: return "TextureCube";
3406 case EbtSamplerExternalOES: return "Texture2D";
3407 case EbtSampler2DArray: return "Texture2DArray";
3408 case EbtSampler3D: return "Texture3D";
3409 case EbtISampler2D: return "Texture2D<int4>";
3410 case EbtISampler3D: return "Texture3D<int4>";
Nicolas Capens0027fa92014-02-20 14:26:42 -05003411 case EbtISamplerCube: return "Texture2DArray<int4>";
Nicolas Capenscb127d32013-07-15 17:26:18 -04003412 case EbtISampler2DArray: return "Texture2DArray<int4>";
3413 case EbtUSampler2D: return "Texture2D<uint4>";
3414 case EbtUSampler3D: return "Texture3D<uint4>";
Nicolas Capens0027fa92014-02-20 14:26:42 -05003415 case EbtUSamplerCube: return "Texture2DArray<uint4>";
Nicolas Capenscb127d32013-07-15 17:26:18 -04003416 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
3417 case EbtSampler2DShadow: return "Texture2D";
3418 case EbtSamplerCubeShadow: return "TextureCube";
3419 case EbtSampler2DArrayShadow: return "Texture2DArray";
3420 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003421 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04003422
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003423 return "<unknown texture type>";
3424}
3425
Nicolas Capenscb127d32013-07-15 17:26:18 -04003426TString OutputHLSL::samplerString(const TType &type)
3427{
3428 if (IsShadowSampler(type.getBasicType()))
3429 {
3430 return "SamplerComparisonState";
3431 }
3432 else
3433 {
3434 return "SamplerState";
3435 }
3436}
3437
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003438TString OutputHLSL::arrayString(const TType &type)
3439{
3440 if (!type.isArray())
3441 {
3442 return "";
3443 }
3444
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003445 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003446}
3447
3448TString OutputHLSL::initializer(const TType &type)
3449{
3450 TString string;
3451
Jamie Madill94bf7f22013-07-08 13:31:15 -04003452 size_t size = type.getObjectSize();
3453 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003454 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003455 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003456
Jamie Madill94bf7f22013-07-08 13:31:15 -04003457 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003458 {
3459 string += ", ";
3460 }
3461 }
3462
daniel@transgaming.comead23042010-04-29 03:35:36 +00003463 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003464}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003465
Jamie Madill98493dd2013-07-08 14:39:03 -04003466TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003467{
Jamie Madill98493dd2013-07-08 14:39:03 -04003468 const TFieldList &fields = structure.fields();
3469 const bool isNameless = (structure.name() == "");
3470 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003471 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3472
Jamie Madill98493dd2013-07-08 14:39:03 -04003473 TString string;
3474 string += declareString + "\n"
3475 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003476
Jamie Madillc835df62013-06-21 09:15:32 -04003477 int elementIndex = 0;
3478
Jamie Madill9cf6c072013-06-20 11:55:53 -04003479 for (unsigned int i = 0; i < fields.size(); i++)
3480 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003481 const TField &field = *fields[i];
3482 const TType &fieldType = *field.type();
3483 const TStructure *fieldStruct = fieldType.getStruct();
3484 const TString &fieldTypeString = fieldStruct ? structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) : typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003485
Jamie Madillc835df62013-06-21 09:15:32 -04003486 if (useStd140Packing)
3487 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003488 string += std140PrePaddingString(*field.type(), &elementIndex);
Jamie Madillc835df62013-06-21 09:15:32 -04003489 }
3490
Jamie Madill98493dd2013-07-08 14:39:03 -04003491 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003492
3493 if (useStd140Packing)
3494 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003495 string += std140PostPaddingString(*field.type(), useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003496 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003497 }
3498
3499 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003500 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003501
Jamie Madille4075c92013-06-21 09:15:32 -04003502 // Add remaining element index to the global map, for use with nested structs in standard layouts
3503 if (useStd140Packing)
3504 {
3505 mStd140StructElementIndexes[structName] = elementIndex;
3506 }
3507
Jamie Madill98493dd2013-07-08 14:39:03 -04003508 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003509}
3510
Jamie Madill98493dd2013-07-08 14:39:03 -04003511TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003512{
Jamie Madill98493dd2013-07-08 14:39:03 -04003513 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003514 {
3515 return "";
3516 }
3517
3518 TString prefix = "";
3519
3520 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3521 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003522
3523 if (useStd140Packing)
3524 {
3525 prefix += "std";
3526 }
3527
Jamie Madill9cf6c072013-06-20 11:55:53 -04003528 if (useHLSLRowMajorPacking)
3529 {
Jamie Madillc835df62013-06-21 09:15:32 -04003530 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003531 prefix += "rm";
3532 }
3533
Jamie Madill98493dd2013-07-08 14:39:03 -04003534 return prefix + structLookup(structure.name());
Jamie Madill9cf6c072013-06-20 11:55:53 -04003535}
3536
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003537void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003538{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003539 if (name == "")
3540 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003541 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003542 }
3543
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003544 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
3545 {
3546 return; // Already added
3547 }
3548
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003549 TType ctorType = type;
3550 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003551 ctorType.setPrecision(EbpHigh);
3552 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003553
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003554 TString ctorName = type.getStruct() ? decorate(name) : name;
3555
3556 typedef std::vector<TType> ParameterArray;
3557 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003558
Jamie Madill98493dd2013-07-08 14:39:03 -04003559 const TStructure* structure = type.getStruct();
3560 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003561 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003562 mStructNames.insert(decorate(name));
3563
Jamie Madill98493dd2013-07-08 14:39:03 -04003564 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003565
Jamie Madill98493dd2013-07-08 14:39:03 -04003566 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003567 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003568 // Add row-major packed struct for interface blocks
3569 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003570 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003571 "#pragma pack_matrix(column_major)\n";
3572
Jamie Madill98493dd2013-07-08 14:39:03 -04003573 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003574 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003575 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003576 "#pragma pack_matrix(column_major)\n";
3577
Jamie Madill98493dd2013-07-08 14:39:03 -04003578 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003579 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003580 mStructDeclarations.push_back(std140String);
3581 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003582 }
3583
Jamie Madill98493dd2013-07-08 14:39:03 -04003584 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003585 for (unsigned int i = 0; i < fields.size(); i++)
3586 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003587 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003588 }
3589 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003590 else if (parameters)
3591 {
3592 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3593 {
3594 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3595 }
3596 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003597 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003598
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003599 TString constructor;
3600
3601 if (ctorType.getStruct())
3602 {
3603 constructor += ctorName + " " + ctorName + "_ctor(";
3604 }
3605 else // Built-in type
3606 {
3607 constructor += typeString(ctorType) + " " + ctorName + "(";
3608 }
3609
3610 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3611 {
3612 const TType &type = ctorParameters[parameter];
3613
3614 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3615
3616 if (parameter < ctorParameters.size() - 1)
3617 {
3618 constructor += ", ";
3619 }
3620 }
3621
3622 constructor += ")\n"
3623 "{\n";
3624
3625 if (ctorType.getStruct())
3626 {
3627 constructor += " " + ctorName + " structure = {";
3628 }
3629 else
3630 {
3631 constructor += " return " + typeString(ctorType) + "(";
3632 }
3633
3634 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3635 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003636 int rows = ctorType.getRows();
3637 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003638 const TType &parameter = ctorParameters[0];
3639
3640 if (parameter.isScalar())
3641 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003642 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003643 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003644 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003645 {
3646 constructor += TString((row == col) ? "x0" : "0.0");
3647
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003648 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003649 {
3650 constructor += ", ";
3651 }
3652 }
3653 }
3654 }
3655 else if (parameter.isMatrix())
3656 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003657 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003658 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003659 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003660 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003661 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003662 {
3663 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3664 }
3665 else
3666 {
3667 constructor += TString((row == col) ? "1.0" : "0.0");
3668 }
3669
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003670 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003671 {
3672 constructor += ", ";
3673 }
3674 }
3675 }
3676 }
3677 else UNREACHABLE();
3678 }
3679 else
3680 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003681 size_t remainingComponents = ctorType.getObjectSize();
3682 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003683
3684 while (remainingComponents > 0)
3685 {
3686 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003687 const size_t parameterSize = parameter.getObjectSize();
3688 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003689
3690 constructor += "x" + str(parameterIndex);
3691
3692 if (parameter.isScalar())
3693 {
3694 remainingComponents -= parameter.getObjectSize();
3695 }
3696 else if (parameter.isVector())
3697 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003698 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003699 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003700 ASSERT(parameterSize <= remainingComponents);
3701 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003702 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003703 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003704 {
3705 switch (remainingComponents)
3706 {
3707 case 1: constructor += ".x"; break;
3708 case 2: constructor += ".xy"; break;
3709 case 3: constructor += ".xyz"; break;
3710 case 4: constructor += ".xyzw"; break;
3711 default: UNREACHABLE();
3712 }
3713
3714 remainingComponents = 0;
3715 }
3716 else UNREACHABLE();
3717 }
3718 else if (parameter.isMatrix() || parameter.getStruct())
3719 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003720 ASSERT(remainingComponents == parameterSize || moreParameters);
3721 ASSERT(parameterSize <= remainingComponents);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003722
Jamie Madill94bf7f22013-07-08 13:31:15 -04003723 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003724 }
3725 else UNREACHABLE();
3726
3727 if (moreParameters)
3728 {
3729 parameterIndex++;
3730 }
3731
3732 if (remainingComponents)
3733 {
3734 constructor += ", ";
3735 }
3736 }
3737 }
3738
3739 if (ctorType.getStruct())
3740 {
3741 constructor += "};\n"
3742 " return structure;\n"
3743 "}\n";
3744 }
3745 else
3746 {
3747 constructor += ");\n"
3748 "}\n";
3749 }
3750
daniel@transgaming.com63691862010-04-29 03:32:42 +00003751 mConstructors.insert(constructor);
3752}
3753
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003754const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3755{
3756 TInfoSinkBase &out = mBody;
3757
Jamie Madill98493dd2013-07-08 14:39:03 -04003758 const TStructure* structure = type.getStruct();
3759 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003760 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003761 out << structLookup(structure->name()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003762
Jamie Madill98493dd2013-07-08 14:39:03 -04003763 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003764
Jamie Madill98493dd2013-07-08 14:39:03 -04003765 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003766 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003767 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003768
3769 constUnion = writeConstantUnion(*fieldType, constUnion);
3770
Jamie Madill98493dd2013-07-08 14:39:03 -04003771 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003772 {
3773 out << ", ";
3774 }
3775 }
3776
3777 out << ")";
3778 }
3779 else
3780 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003781 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003782 bool writeType = size > 1;
3783
3784 if (writeType)
3785 {
3786 out << typeString(type) << "(";
3787 }
3788
Jamie Madill94bf7f22013-07-08 13:31:15 -04003789 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003790 {
3791 switch (constUnion->getType())
3792 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003793 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003794 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003795 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003796 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003797 default: UNREACHABLE();
3798 }
3799
3800 if (i != size - 1)
3801 {
3802 out << ", ";
3803 }
3804 }
3805
3806 if (writeType)
3807 {
3808 out << ")";
3809 }
3810 }
3811
3812 return constUnion;
3813}
3814
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003815TString OutputHLSL::scopeString(unsigned int depthLimit)
3816{
3817 TString string;
3818
3819 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3820 {
3821 string += "_" + str(i);
3822 }
3823
3824 return string;
3825}
3826
3827TString OutputHLSL::scopedStruct(const TString &typeName)
3828{
3829 if (typeName == "")
3830 {
3831 return typeName;
3832 }
3833
3834 return typeName + scopeString(mScopeDepth);
3835}
3836
3837TString OutputHLSL::structLookup(const TString &typeName)
3838{
3839 for (int depth = mScopeDepth; depth >= 0; depth--)
3840 {
3841 TString scopedName = decorate(typeName + scopeString(depth));
3842
3843 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3844 {
3845 if (*structName == scopedName)
3846 {
3847 return scopedName;
3848 }
3849 }
3850 }
3851
3852 UNREACHABLE(); // Should have found a matching constructor
3853
3854 return typeName;
3855}
3856
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003857TString OutputHLSL::decorate(const TString &string)
3858{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003859 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003860 {
3861 return "_" + string;
3862 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003863
3864 return string;
3865}
3866
apatrick@chromium.org65756022012-01-17 21:45:38 +00003867TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003868{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003869 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003870 {
3871 return "ex_" + string;
3872 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003873
3874 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003875}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003876
Jamie Madill98493dd2013-07-08 14:39:03 -04003877TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003878{
Jamie Madill98493dd2013-07-08 14:39:03 -04003879 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003880 {
3881 return decorate(string);
3882 }
3883
3884 return string;
3885}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003886
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003887void OutputHLSL::declareInterfaceBlockField(const TType &type, const TString &name, std::vector<InterfaceBlockField>& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003888{
Jamie Madill98493dd2013-07-08 14:39:03 -04003889 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003890
3891 if (!structure)
3892 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003893 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003894 InterfaceBlockField field(glVariableType(type), glVariablePrecision(type), name.c_str(),
3895 (unsigned int)type.getArraySize(), isRowMajorMatrix);
3896 output.push_back(field);
3897 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003898 else
3899 {
Jamie Madill28167c62013-08-30 13:21:10 -04003900 InterfaceBlockField structField(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), false);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003901
3902 const TFieldList &fields = structure->fields();
3903
3904 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3905 {
3906 TField *field = fields[fieldIndex];
3907 TType *fieldType = field->type();
3908
3909 // make sure to copy matrix packing information
3910 fieldType->setLayoutQualifier(type.getLayoutQualifier());
3911
3912 declareInterfaceBlockField(*fieldType, field->name(), structField.fields);
3913 }
3914
3915 output.push_back(structField);
3916 }
3917}
3918
Jamie Madillc2141fb2013-08-30 13:21:08 -04003919Uniform OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, std::vector<Uniform>& output)
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003920{
3921 const TStructure *structure = type.getStruct();
3922
3923 if (!structure)
3924 {
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003925 Uniform uniform(glVariableType(type), glVariablePrecision(type), name.c_str(),
Jamie Madill56093782013-08-30 13:21:11 -04003926 (unsigned int)type.getArraySize(), (unsigned int)registerIndex, 0);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003927 output.push_back(uniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003928
3929 return uniform;
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003930 }
3931 else
3932 {
Jamie Madill56093782013-08-30 13:21:11 -04003933 Uniform structUniform(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(),
3934 (unsigned int)registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003935
Jamie Madill98493dd2013-07-08 14:39:03 -04003936 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003937
Jamie Madill98493dd2013-07-08 14:39:03 -04003938 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003939 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003940 TField *field = fields[fieldIndex];
3941 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003942
Jamie Madill56093782013-08-30 13:21:11 -04003943 declareUniformToList(*fieldType, field->name(), GL_INVALID_INDEX, structUniform.fields);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003944 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003945
Jamie Madill56093782013-08-30 13:21:11 -04003946 // assign register offset information -- this will override the information in any sub-structures.
3947 HLSLVariableGetRegisterInfo(registerIndex, &structUniform);
3948
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003949 output.push_back(structUniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003950
3951 return structUniform;
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003952 }
3953}
3954
Jamie Madill139b9092013-08-30 13:21:06 -04003955InterpolationType getInterpolationType(TQualifier qualifier)
3956{
3957 switch (qualifier)
3958 {
3959 case EvqFlatIn:
3960 case EvqFlatOut:
3961 return INTERPOLATION_FLAT;
3962
3963 case EvqSmoothIn:
3964 case EvqSmoothOut:
3965 case EvqVertexOut:
3966 case EvqFragmentIn:
Jamie Madill384b6042013-09-13 10:06:24 -04003967 case EvqVaryingIn:
3968 case EvqVaryingOut:
Jamie Madill139b9092013-08-30 13:21:06 -04003969 return INTERPOLATION_SMOOTH;
3970
3971 case EvqCentroidIn:
3972 case EvqCentroidOut:
3973 return INTERPOLATION_CENTROID;
3974
3975 default: UNREACHABLE();
3976 return INTERPOLATION_SMOOTH;
3977 }
3978}
3979
Jamie Madill94599662013-08-30 13:21:10 -04003980void OutputHLSL::declareVaryingToList(const TType &type, TQualifier baseTypeQualifier, const TString &name, std::vector<Varying>& fieldsOut)
Jamie Madill47fdd132013-08-30 13:21:04 -04003981{
3982 const TStructure *structure = type.getStruct();
3983
Jamie Madill94599662013-08-30 13:21:10 -04003984 InterpolationType interpolation = getInterpolationType(baseTypeQualifier);
Jamie Madill47fdd132013-08-30 13:21:04 -04003985 if (!structure)
3986 {
Jamie Madill139b9092013-08-30 13:21:06 -04003987 Varying varying(glVariableType(type), glVariablePrecision(type), name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003988 fieldsOut.push_back(varying);
3989 }
3990 else
3991 {
Jamie Madill28167c62013-08-30 13:21:10 -04003992 Varying structVarying(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003993 const TFieldList &fields = structure->fields();
3994
Jamie Madill28167c62013-08-30 13:21:10 -04003995 structVarying.structName = structure->name().c_str();
3996
Jamie Madill47fdd132013-08-30 13:21:04 -04003997 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3998 {
3999 const TField &field = *fields[fieldIndex];
Jamie Madill94599662013-08-30 13:21:10 -04004000 declareVaryingToList(*field.type(), baseTypeQualifier, field.name(), structVarying.fields);
Jamie Madill47fdd132013-08-30 13:21:04 -04004001 }
4002
4003 fieldsOut.push_back(structVarying);
4004 }
4005}
4006
Jamie Madillc2141fb2013-08-30 13:21:08 -04004007int OutputHLSL::declareUniformAndAssignRegister(const TType &type, const TString &name)
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00004008{
Jamie Madillc2141fb2013-08-30 13:21:08 -04004009 int registerIndex = (IsSampler(type.getBasicType()) ? mSamplerRegister : mUniformRegister);
4010
4011 const Uniform &uniform = declareUniformToList(type, name, registerIndex, mActiveUniforms);
4012
4013 if (IsSampler(type.getBasicType()))
4014 {
4015 mSamplerRegister += HLSLVariableRegisterCount(uniform);
4016 }
4017 else
4018 {
4019 mUniformRegister += HLSLVariableRegisterCount(uniform);
4020 }
4021
4022 return registerIndex;
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00004023}
4024
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004025GLenum OutputHLSL::glVariableType(const TType &type)
4026{
4027 if (type.getBasicType() == EbtFloat)
4028 {
4029 if (type.isScalar())
4030 {
4031 return GL_FLOAT;
4032 }
4033 else if (type.isVector())
4034 {
4035 switch(type.getNominalSize())
4036 {
4037 case 2: return GL_FLOAT_VEC2;
4038 case 3: return GL_FLOAT_VEC3;
4039 case 4: return GL_FLOAT_VEC4;
4040 default: UNREACHABLE();
4041 }
4042 }
4043 else if (type.isMatrix())
4044 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00004045 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004046 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00004047 case 2:
4048 switch(type.getRows())
4049 {
4050 case 2: return GL_FLOAT_MAT2;
4051 case 3: return GL_FLOAT_MAT2x3;
4052 case 4: return GL_FLOAT_MAT2x4;
4053 default: UNREACHABLE();
4054 }
4055
4056 case 3:
4057 switch(type.getRows())
4058 {
4059 case 2: return GL_FLOAT_MAT3x2;
4060 case 3: return GL_FLOAT_MAT3;
4061 case 4: return GL_FLOAT_MAT3x4;
4062 default: UNREACHABLE();
4063 }
4064
4065 case 4:
4066 switch(type.getRows())
4067 {
4068 case 2: return GL_FLOAT_MAT4x2;
4069 case 3: return GL_FLOAT_MAT4x3;
4070 case 4: return GL_FLOAT_MAT4;
4071 default: UNREACHABLE();
4072 }
4073
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004074 default: UNREACHABLE();
4075 }
4076 }
4077 else UNREACHABLE();
4078 }
4079 else if (type.getBasicType() == EbtInt)
4080 {
4081 if (type.isScalar())
4082 {
4083 return GL_INT;
4084 }
4085 else if (type.isVector())
4086 {
4087 switch(type.getNominalSize())
4088 {
4089 case 2: return GL_INT_VEC2;
4090 case 3: return GL_INT_VEC3;
4091 case 4: return GL_INT_VEC4;
4092 default: UNREACHABLE();
4093 }
4094 }
4095 else UNREACHABLE();
4096 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004097 else if (type.getBasicType() == EbtUInt)
4098 {
4099 if (type.isScalar())
4100 {
4101 return GL_UNSIGNED_INT;
4102 }
4103 else if (type.isVector())
4104 {
Jamie Madill22d63da2013-06-07 12:45:12 -04004105 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00004106 {
4107 case 2: return GL_UNSIGNED_INT_VEC2;
4108 case 3: return GL_UNSIGNED_INT_VEC3;
4109 case 4: return GL_UNSIGNED_INT_VEC4;
4110 default: UNREACHABLE();
4111 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004112 }
4113 else UNREACHABLE();
4114 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004115 else if (type.getBasicType() == EbtBool)
4116 {
4117 if (type.isScalar())
4118 {
4119 return GL_BOOL;
4120 }
4121 else if (type.isVector())
4122 {
4123 switch(type.getNominalSize())
4124 {
4125 case 2: return GL_BOOL_VEC2;
4126 case 3: return GL_BOOL_VEC3;
4127 case 4: return GL_BOOL_VEC4;
4128 default: UNREACHABLE();
4129 }
4130 }
4131 else UNREACHABLE();
4132 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004133
4134 switch(type.getBasicType())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004135 {
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004136 case EbtSampler2D: return GL_SAMPLER_2D;
4137 case EbtSampler3D: return GL_SAMPLER_3D;
4138 case EbtSamplerCube: return GL_SAMPLER_CUBE;
4139 case EbtSampler2DArray: return GL_SAMPLER_2D_ARRAY;
4140 case EbtISampler2D: return GL_INT_SAMPLER_2D;
4141 case EbtISampler3D: return GL_INT_SAMPLER_3D;
4142 case EbtISamplerCube: return GL_INT_SAMPLER_CUBE;
4143 case EbtISampler2DArray: return GL_INT_SAMPLER_2D_ARRAY;
4144 case EbtUSampler2D: return GL_UNSIGNED_INT_SAMPLER_2D;
4145 case EbtUSampler3D: return GL_UNSIGNED_INT_SAMPLER_3D;
4146 case EbtUSamplerCube: return GL_UNSIGNED_INT_SAMPLER_CUBE;
4147 case EbtUSampler2DArray: return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
4148 case EbtSampler2DShadow: return GL_SAMPLER_2D_SHADOW;
4149 case EbtSamplerCubeShadow: return GL_SAMPLER_CUBE_SHADOW;
4150 case EbtSampler2DArrayShadow: return GL_SAMPLER_2D_ARRAY_SHADOW;
4151 default: UNREACHABLE();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004152 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004153
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004154 return GL_NONE;
4155}
4156
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004157GLenum OutputHLSL::glVariablePrecision(const TType &type)
4158{
4159 if (type.getBasicType() == EbtFloat)
4160 {
4161 switch (type.getPrecision())
4162 {
4163 case EbpHigh: return GL_HIGH_FLOAT;
4164 case EbpMedium: return GL_MEDIUM_FLOAT;
4165 case EbpLow: return GL_LOW_FLOAT;
4166 case EbpUndefined:
4167 // Should be defined as the default precision by the parser
4168 default: UNREACHABLE();
4169 }
4170 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004171 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004172 {
4173 switch (type.getPrecision())
4174 {
4175 case EbpHigh: return GL_HIGH_INT;
4176 case EbpMedium: return GL_MEDIUM_INT;
4177 case EbpLow: return GL_LOW_INT;
4178 case EbpUndefined:
4179 // Should be defined as the default precision by the parser
4180 default: UNREACHABLE();
4181 }
4182 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004183
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00004184 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004185 return GL_NONE;
4186}
4187
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004188bool OutputHLSL::isVaryingOut(TQualifier qualifier)
4189{
4190 switch(qualifier)
4191 {
4192 case EvqVaryingOut:
4193 case EvqInvariantVaryingOut:
4194 case EvqSmoothOut:
4195 case EvqFlatOut:
4196 case EvqCentroidOut:
Jamie Madill19571812013-08-12 15:26:34 -07004197 case EvqVertexOut:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004198 return true;
4199 }
4200
4201 return false;
4202}
4203
4204bool OutputHLSL::isVaryingIn(TQualifier qualifier)
4205{
4206 switch(qualifier)
4207 {
4208 case EvqVaryingIn:
4209 case EvqInvariantVaryingIn:
4210 case EvqSmoothIn:
4211 case EvqFlatIn:
4212 case EvqCentroidIn:
Jamie Madill19571812013-08-12 15:26:34 -07004213 case EvqFragmentIn:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004214 return true;
4215 }
4216
4217 return false;
4218}
4219
4220bool OutputHLSL::isVarying(TQualifier qualifier)
4221{
4222 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
4223}
4224
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004225}