blob: f552ec1abd8dab2c2db7414bec41636daf5d3828 [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"
Jamie Madill834e8b72014-04-11 13:33:58 -040011#include "common/blocklayout.h"
Geoff Lang17732822013-08-29 13:46:49 -040012#include "compiler/translator/compilerdebug.h"
13#include "compiler/translator/InfoSink.h"
14#include "compiler/translator/DetectDiscontinuity.h"
15#include "compiler/translator/SearchSymbol.h"
16#include "compiler/translator/UnfoldShortCircuit.h"
Geoff Lang17732822013-08-29 13:46:49 -040017#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
Jamie Madill3891fd22014-06-13 10:04:30 -040028class OutputHLSL::Std140PaddingHelper
29{
30 public:
31 explicit Std140PaddingHelper(const std::map<TString, int> &structElementIndexes)
32 : mPaddingCounter(0),
33 mElementIndex(0),
34 mStructElementIndexes(structElementIndexes)
35 {}
36
37 int elementIndex() const { return mElementIndex; }
38
39 int prePadding(const TType &type)
40 {
41 if (type.getBasicType() == EbtStruct || type.isMatrix() || type.isArray())
42 {
43 // no padding needed, HLSL will align the field to a new register
44 mElementIndex = 0;
45 return 0;
46 }
47
48 const GLenum glType = glVariableType(type);
49 const int numComponents = gl::UniformComponentCount(glType);
50
51 if (numComponents >= 4)
52 {
53 // no padding needed, HLSL will align the field to a new register
54 mElementIndex = 0;
55 return 0;
56 }
57
58 if (mElementIndex + numComponents > 4)
59 {
60 // no padding needed, HLSL will align the field to a new register
61 mElementIndex = numComponents;
62 return 0;
63 }
64
65 const int alignment = numComponents == 3 ? 4 : numComponents;
66 const int paddingOffset = (mElementIndex % alignment);
67 const int paddingCount = (paddingOffset != 0 ? (alignment - paddingOffset) : 0);
68
69 mElementIndex += paddingCount;
70 mElementIndex += numComponents;
71 mElementIndex %= 4;
72
73 return paddingCount;
74 }
75
76 TString prePaddingString(const TType &type)
77 {
78 int paddingCount = prePadding(type);
79
80 TString padding;
81
82 for (int paddingIndex = 0; paddingIndex < paddingCount; paddingIndex++)
83 {
84 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
85 }
86
87 return padding;
88 }
89
90 TString postPaddingString(const TType &type, bool useHLSLRowMajorPacking)
91 {
92 if (!type.isMatrix() && !type.isArray() && type.getBasicType() != EbtStruct)
93 {
94 return "";
95 }
96
97 int numComponents = 0;
98
99 if (type.isMatrix())
100 {
101 // This method can also be called from structureString, which does not use layout qualifiers.
102 // Thus, use the method parameter for determining the matrix packing.
103 //
104 // Note HLSL row major packing corresponds to GL API column-major, and vice-versa, since we
105 // wish to always transpose GL matrices to play well with HLSL's matrix array indexing.
106 //
107 const bool isRowMajorMatrix = !useHLSLRowMajorPacking;
108 const GLenum glType = glVariableType(type);
109 numComponents = gl::MatrixComponentCount(glType, isRowMajorMatrix);
110 }
111 else if (type.getStruct())
112 {
113 const TString &structName = structureTypeName(*type.getStruct(), useHLSLRowMajorPacking, true);
114 numComponents = mStructElementIndexes.find(structName)->second;
115
116 if (numComponents == 0)
117 {
118 return "";
119 }
120 }
121 else
122 {
123 const GLenum glType = glVariableType(type);
124 numComponents = gl::UniformComponentCount(glType);
125 }
126
127 TString padding;
128 for (int paddingOffset = numComponents; paddingOffset < 4; paddingOffset++)
129 {
130 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
131 }
132 return padding;
133 }
134
135 private:
136 int mPaddingCounter;
137 int mElementIndex;
138 const std::map<TString, int> &mStructElementIndexes;
139};
140
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400141TString OutputHLSL::TextureFunction::name() const
142{
143 TString name = "gl_texture";
144
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400145 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400146 {
147 name += "2D";
148 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400149 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400150 {
151 name += "3D";
152 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400153 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400154 {
155 name += "Cube";
156 }
157 else UNREACHABLE();
158
159 if (proj)
160 {
161 name += "Proj";
162 }
163
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500164 if (offset)
165 {
166 name += "Offset";
167 }
168
Nicolas Capens75fb4752013-07-10 15:14:47 -0400169 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400170 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500171 case IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400172 case BIAS: break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -0500173 case LOD: name += "Lod"; break;
174 case LOD0: name += "Lod0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400175 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -0500176 case SIZE: name += "Size"; break;
177 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500178 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400179 default: UNREACHABLE();
180 }
181
182 return name + "(";
183}
184
Jamie Madillc2141fb2013-08-30 13:21:08 -0400185const char *RegisterPrefix(const TType &type)
186{
187 if (IsSampler(type.getBasicType()))
188 {
189 return "s";
190 }
191 else
192 {
193 return "c";
194 }
195}
196
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400197bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
198{
199 if (sampler < rhs.sampler) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400200 if (sampler > rhs.sampler) return false;
201
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400202 if (coords < rhs.coords) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400203 if (coords > rhs.coords) return false;
204
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400205 if (!proj && rhs.proj) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400206 if (proj && !rhs.proj) return false;
207
208 if (!offset && rhs.offset) return true;
209 if (offset && !rhs.offset) return false;
210
Nicolas Capens75fb4752013-07-10 15:14:47 -0400211 if (method < rhs.method) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400212 if (method > rhs.method) return false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400213
214 return false;
215}
216
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000217OutputHLSL::OutputHLSL(TParseContext &context, const ShBuiltInResources& resources, ShShaderOutput outputType)
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +0000218 : TIntermTraverser(true, true, true), mContext(context), mOutputType(outputType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000219{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000220 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000221 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000222
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000223 mUsesFragColor = false;
224 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000225 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000226 mUsesFragCoord = false;
227 mUsesPointCoord = false;
228 mUsesFrontFacing = false;
229 mUsesPointSize = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400230 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000231 mUsesXor = false;
232 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000233 mUsesMod2v = false;
234 mUsesMod2f = false;
235 mUsesMod3v = false;
236 mUsesMod3f = false;
237 mUsesMod4v = false;
238 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000239 mUsesFaceforward1 = false;
240 mUsesFaceforward2 = false;
241 mUsesFaceforward3 = false;
242 mUsesFaceforward4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000243 mUsesAtan2_1 = false;
244 mUsesAtan2_2 = false;
245 mUsesAtan2_3 = false;
246 mUsesAtan2_4 = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500247 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400248 mUsesNestedBreak = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000249
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000250 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
251
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000252 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000253
254 mContainsLoopDiscontinuity = false;
255 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000256 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400257 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000258
259 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000260
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000261 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000262 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000263 if (mContext.shaderType == SH_FRAGMENT_SHADER)
264 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000265 mUniformRegister = 3; // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000266 }
267 else
268 {
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000269 mUniformRegister = 2; // Reserve registers for dx_DepthRange and dx_ViewAdjust
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000270 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000271 }
272 else
273 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000274 mUniformRegister = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000275 }
276
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000277 mSamplerRegister = 0;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000278 mInterfaceBlockRegister = 2; // Reserve registers for the default uniform block and driver constants
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000279}
280
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000281OutputHLSL::~OutputHLSL()
282{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000283 delete mUnfoldShortCircuit;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000284}
285
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000286void OutputHLSL::output()
287{
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +0000288 mContainsLoopDiscontinuity = mContext.shaderType == SH_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400289 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
290 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000291
Jamie Madille53c98b2014-02-03 11:57:13 -0500292 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
293 // use a vertex attribute as a condition, and some related computation in the else block.
294 if (mOutputType == SH_HLSL9_OUTPUT && mContext.shaderType == SH_VERTEX_SHADER)
295 {
296 RewriteElseBlocks(mContext.treeRoot);
297 }
298
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000299 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 +0000300 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000301
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000302 mContext.infoSink().obj << mHeader.c_str();
303 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000304}
305
Jamie Madill570e04d2013-06-21 09:15:33 -0400306void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
307{
308 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
309 {
310 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
311
312 // This will mark the necessary block elements as referenced
313 flaggedNode->traverse(this);
314 TString structName(mBody.c_str());
315 mBody.erase();
316
317 mFlaggedStructOriginalNames[flaggedNode] = structName;
318
319 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
320 {
321 structName.erase(pos, 1);
322 }
323
324 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
325 }
326}
327
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000328TInfoSinkBase &OutputHLSL::getBodyStream()
329{
330 return mBody;
331}
332
Jamie Madill834e8b72014-04-11 13:33:58 -0400333const std::vector<gl::Uniform> &OutputHLSL::getUniforms()
daniel@transgaming.com043da132012-12-20 21:12:22 +0000334{
335 return mActiveUniforms;
336}
337
Jamie Madill834e8b72014-04-11 13:33:58 -0400338const std::vector<gl::InterfaceBlock> &OutputHLSL::getInterfaceBlocks() const
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000339{
340 return mActiveInterfaceBlocks;
341}
342
Jamie Madill834e8b72014-04-11 13:33:58 -0400343const std::vector<gl::Attribute> &OutputHLSL::getOutputVariables() const
Jamie Madill46131a32013-06-20 11:55:50 -0400344{
345 return mActiveOutputVariables;
346}
347
Jamie Madill834e8b72014-04-11 13:33:58 -0400348const std::vector<gl::Attribute> &OutputHLSL::getAttributes() const
Jamie Madilldefb6742013-06-20 11:55:51 -0400349{
350 return mActiveAttributes;
351}
352
Jamie Madill834e8b72014-04-11 13:33:58 -0400353const std::vector<gl::Varying> &OutputHLSL::getVaryings() const
Jamie Madill47fdd132013-08-30 13:21:04 -0400354{
355 return mActiveVaryings;
356}
357
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000358int OutputHLSL::vectorSize(const TType &type) const
359{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000360 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000361 int arraySize = type.isArray() ? type.getArraySize() : 1;
362
363 return elementSize * arraySize;
364}
365
Jamie Madill98493dd2013-07-08 14:39:03 -0400366TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, const TField &field)
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000367{
Jamie Madill98493dd2013-07-08 14:39:03 -0400368 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000369 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400370 return interfaceBlock.name() + "." + field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000371 }
372 else
373 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400374 return field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000375 }
376}
377
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000378TString OutputHLSL::decoratePrivate(const TString &privateText)
379{
380 return "dx_" + privateText;
381}
382
Jamie Madill98493dd2013-07-08 14:39:03 -0400383TString OutputHLSL::interfaceBlockStructNameString(const TInterfaceBlock &interfaceBlock)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000384{
Jamie Madill98493dd2013-07-08 14:39:03 -0400385 return decoratePrivate(interfaceBlock.name()) + "_type";
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000386}
387
Jamie Madill98493dd2013-07-08 14:39:03 -0400388TString OutputHLSL::interfaceBlockInstanceString(const TInterfaceBlock& interfaceBlock, unsigned int arrayIndex)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000389{
Jamie Madill98493dd2013-07-08 14:39:03 -0400390 if (!interfaceBlock.hasInstanceName())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000391 {
392 return "";
393 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400394 else if (interfaceBlock.isArray())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000395 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400396 return decoratePrivate(interfaceBlock.instanceName()) + "_" + str(arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000397 }
398 else
399 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400400 return decorate(interfaceBlock.instanceName());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000401 }
402}
403
Jamie Madill98493dd2013-07-08 14:39:03 -0400404TString OutputHLSL::interfaceBlockFieldTypeString(const TField &field, TLayoutBlockStorage blockStorage)
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000405{
Jamie Madill98493dd2013-07-08 14:39:03 -0400406 const TType &fieldType = *field.type();
407 const TLayoutMatrixPacking matrixPacking = fieldType.getLayoutQualifier().matrixPacking;
Jamie Madill529077d2013-06-20 11:55:54 -0400408 ASSERT(matrixPacking != EmpUnspecified);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000409
Jamie Madill98493dd2013-07-08 14:39:03 -0400410 if (fieldType.isMatrix())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000411 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400412 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill529077d2013-06-20 11:55:54 -0400413 const TString &matrixPackString = (matrixPacking == EmpRowMajor ? "column_major" : "row_major");
Jamie Madill98493dd2013-07-08 14:39:03 -0400414 return matrixPackString + " " + typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000415 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400416 else if (fieldType.getStruct())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000417 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400418 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill98493dd2013-07-08 14:39:03 -0400419 return structureTypeName(*fieldType.getStruct(), matrixPacking == EmpColumnMajor, blockStorage == EbsStd140);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000420 }
421 else
422 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400423 return typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000424 }
425}
426
Jamie Madill98493dd2013-07-08 14:39:03 -0400427TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, TLayoutBlockStorage blockStorage)
428{
429 TString hlsl;
430
Jamie Madill3891fd22014-06-13 10:04:30 -0400431 Std140PaddingHelper padHelper(mStd140StructElementIndexes);
Jamie Madill98493dd2013-07-08 14:39:03 -0400432
433 for (unsigned int typeIndex = 0; typeIndex < interfaceBlock.fields().size(); typeIndex++)
434 {
435 const TField &field = *interfaceBlock.fields()[typeIndex];
436 const TType &fieldType = *field.type();
437
438 if (blockStorage == EbsStd140)
439 {
440 // 2 and 3 component vector types in some cases need pre-padding
Jamie Madill3891fd22014-06-13 10:04:30 -0400441 hlsl += padHelper.prePaddingString(fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -0400442 }
443
444 hlsl += " " + interfaceBlockFieldTypeString(field, blockStorage) +
445 " " + decorate(field.name()) + arrayString(fieldType) + ";\n";
446
447 // must pad out after matrices and arrays, where HLSL usually allows itself room to pack stuff
448 if (blockStorage == EbsStd140)
449 {
450 const bool useHLSLRowMajorPacking = (fieldType.getLayoutQualifier().matrixPacking == EmpColumnMajor);
Jamie Madill3891fd22014-06-13 10:04:30 -0400451 hlsl += padHelper.postPaddingString(fieldType, useHLSLRowMajorPacking);
Jamie Madill98493dd2013-07-08 14:39:03 -0400452 }
453 }
454
455 return hlsl;
456}
457
458TString OutputHLSL::interfaceBlockStructString(const TInterfaceBlock &interfaceBlock)
459{
460 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
461
462 return "struct " + interfaceBlockStructNameString(interfaceBlock) + "\n"
463 "{\n" +
464 interfaceBlockFieldString(interfaceBlock, blockStorage) +
465 "};\n\n";
466}
467
468TString OutputHLSL::interfaceBlockString(const TInterfaceBlock &interfaceBlock, unsigned int registerIndex, unsigned int arrayIndex)
469{
470 const TString &arrayIndexString = (arrayIndex != GL_INVALID_INDEX ? decorate(str(arrayIndex)) : "");
471 const TString &blockName = interfaceBlock.name() + arrayIndexString;
472 TString hlsl;
473
474 hlsl += "cbuffer " + blockName + " : register(b" + str(registerIndex) + ")\n"
475 "{\n";
476
477 if (interfaceBlock.hasInstanceName())
478 {
479 hlsl += " " + interfaceBlockStructNameString(interfaceBlock) + " " + interfaceBlockInstanceString(interfaceBlock, arrayIndex) + ";\n";
480 }
481 else
482 {
483 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
484 hlsl += interfaceBlockFieldString(interfaceBlock, blockStorage);
485 }
486
487 hlsl += "};\n\n";
488
489 return hlsl;
490}
491
Jamie Madill440dc742013-06-20 11:55:55 -0400492// Use the same layout for packed and shared
Jamie Madill834e8b72014-04-11 13:33:58 -0400493void setBlockLayout(gl::InterfaceBlock *interfaceBlock, gl::BlockLayoutType newLayout)
Jamie Madill440dc742013-06-20 11:55:55 -0400494{
495 interfaceBlock->layout = newLayout;
496 interfaceBlock->blockInfo.clear();
497
498 switch (newLayout)
499 {
Jamie Madill834e8b72014-04-11 13:33:58 -0400500 case gl::BLOCKLAYOUT_SHARED:
501 case gl::BLOCKLAYOUT_PACKED:
Jamie Madill440dc742013-06-20 11:55:55 -0400502 {
Vladimir Vukicevic24d8d672014-05-27 12:07:51 -0400503 gl::HLSLBlockEncoder hlslEncoder(&interfaceBlock->blockInfo, gl::HLSLBlockEncoder::ENCODE_PACKED);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400504 hlslEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400505 interfaceBlock->dataSize = hlslEncoder.getBlockSize();
506 }
507 break;
508
Jamie Madill834e8b72014-04-11 13:33:58 -0400509 case gl::BLOCKLAYOUT_STANDARD:
Jamie Madill440dc742013-06-20 11:55:55 -0400510 {
Jamie Madill834e8b72014-04-11 13:33:58 -0400511 gl::Std140BlockEncoder stdEncoder(&interfaceBlock->blockInfo);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400512 stdEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400513 interfaceBlock->dataSize = stdEncoder.getBlockSize();
514 }
515 break;
516
517 default:
518 UNREACHABLE();
519 break;
520 }
521}
522
Jamie Madill834e8b72014-04-11 13:33:58 -0400523gl::BlockLayoutType convertBlockLayoutType(TLayoutBlockStorage blockStorage)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400524{
525 switch (blockStorage)
526 {
Jamie Madill834e8b72014-04-11 13:33:58 -0400527 case EbsPacked: return gl::BLOCKLAYOUT_PACKED;
528 case EbsShared: return gl::BLOCKLAYOUT_SHARED;
529 case EbsStd140: return gl::BLOCKLAYOUT_STANDARD;
530 default: UNREACHABLE(); return gl::BLOCKLAYOUT_SHARED;
Jamie Madill574d9dd2013-06-20 11:55:56 -0400531 }
532}
533
Jamie Madill98493dd2013-07-08 14:39:03 -0400534TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400535{
536 TString init;
537
538 TString preIndentString;
539 TString fullIndentString;
540
541 for (int spaces = 0; spaces < (indent * 4); spaces++)
542 {
543 preIndentString += ' ';
544 }
545
546 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
547 {
548 fullIndentString += ' ';
549 }
550
551 init += preIndentString + "{\n";
552
Jamie Madill98493dd2013-07-08 14:39:03 -0400553 const TFieldList &fields = structure.fields();
554 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400555 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400556 const TField &field = *fields[fieldIndex];
557 const TString &fieldName = rhsStructName + "." + decorate(field.name());
558 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400559
Jamie Madill98493dd2013-07-08 14:39:03 -0400560 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400561 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400562 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400563 }
564 else
565 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400566 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400567 }
568 }
569
570 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
571
572 return init;
573}
574
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000575void OutputHLSL::header()
576{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000577 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000578
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000579 TString uniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000580 TString interfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000581 TString varyings;
582 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400583 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000584
Jamie Madillc2141fb2013-08-30 13:21:08 -0400585 for (ReferencedSymbols::const_iterator uniformIt = mReferencedUniforms.begin(); uniformIt != mReferencedUniforms.end(); uniformIt++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000586 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400587 const TIntermSymbol &uniform = *uniformIt->second;
588 const TType &type = uniform.getType();
589 const TString &name = uniform.getSymbol();
590
591 int registerIndex = declareUniformAndAssignRegister(type, name);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000592
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000593 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture
594 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400595 uniforms += "uniform " + samplerString(type) + " sampler_" + decorateUniform(name, type) + arrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400596 " : register(s" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000597
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000598 uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400599 " : register(t" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000600 }
601 else
602 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400603 const TStructure *structure = type.getStruct();
604 const TString &typeName = (structure ? structureTypeName(*structure, false, false) : typeString(type));
605
606 const TString &registerString = TString("register(") + RegisterPrefix(type) + str(registerIndex) + ")";
607
608 uniforms += "uniform " + typeName + " " + decorateUniform(name, type) + arrayString(type) + " : " + registerString + ";\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000609 }
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000610 }
611
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000612 for (ReferencedSymbols::const_iterator interfaceBlockIt = mReferencedInterfaceBlocks.begin(); interfaceBlockIt != mReferencedInterfaceBlocks.end(); interfaceBlockIt++)
613 {
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000614 const TType &nodeType = interfaceBlockIt->second->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -0400615 const TInterfaceBlock &interfaceBlock = *nodeType.getInterfaceBlock();
616 const TFieldList &fieldList = interfaceBlock.fields();
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000617
Jamie Madill98493dd2013-07-08 14:39:03 -0400618 unsigned int arraySize = static_cast<unsigned int>(interfaceBlock.arraySize());
Jamie Madill834e8b72014-04-11 13:33:58 -0400619 gl::InterfaceBlock activeBlock(interfaceBlock.name().c_str(), arraySize, mInterfaceBlockRegister);
Jamie Madill98493dd2013-07-08 14:39:03 -0400620 for (unsigned int typeIndex = 0; typeIndex < fieldList.size(); typeIndex++)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000621 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400622 const TField &field = *fieldList[typeIndex];
623 const TString &fullUniformName = interfaceBlockFieldString(interfaceBlock, field);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400624 declareInterfaceBlockField(*field.type(), fullUniformName, activeBlock.fields);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000625 }
626
Jamie Madill98493dd2013-07-08 14:39:03 -0400627 mInterfaceBlockRegister += std::max(1u, arraySize);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000628
Jamie Madill834e8b72014-04-11 13:33:58 -0400629 gl::BlockLayoutType blockLayoutType = convertBlockLayoutType(interfaceBlock.blockStorage());
Jamie Madill98493dd2013-07-08 14:39:03 -0400630 setBlockLayout(&activeBlock, blockLayoutType);
Jamie Madill9060a4e2013-08-12 16:22:57 -0700631
632 if (interfaceBlock.matrixPacking() == EmpRowMajor)
633 {
634 activeBlock.isRowMajorLayout = true;
635 }
636
Jamie Madill98493dd2013-07-08 14:39:03 -0400637 mActiveInterfaceBlocks.push_back(activeBlock);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000638
Jamie Madill98493dd2013-07-08 14:39:03 -0400639 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000640 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400641 interfaceBlocks += interfaceBlockStructString(interfaceBlock);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000642 }
643
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000644 if (arraySize > 0)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000645 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000646 for (unsigned int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
647 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400648 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex + arrayIndex, arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000649 }
650 }
651 else
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000652 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400653 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000654 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000655 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000656
Jamie Madill829f59e2013-11-13 19:40:54 -0500657 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400658 {
659 TIntermTyped *structNode = flaggedStructIt->first;
660 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400661 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400662 const TString &originalName = mFlaggedStructOriginalNames[structNode];
663
Jamie Madill98493dd2013-07-08 14:39:03 -0400664 flaggedStructs += "static " + decorate(structure.name()) + " " + mappedName + " =\n";
665 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400666 flaggedStructs += "\n";
667 }
668
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000669 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
670 {
671 const TType &type = varying->second->getType();
672 const TString &name = varying->second->getSymbol();
673
674 // Program linking depends on this exact format
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +0000675 varyings += "static " + interpolationString(type.getQualifier()) + " " + typeString(type) + " " +
676 decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madill47fdd132013-08-30 13:21:04 -0400677
Jamie Madill94599662013-08-30 13:21:10 -0400678 declareVaryingToList(type, type.getQualifier(), name, mActiveVaryings);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000679 }
680
681 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
682 {
683 const TType &type = attribute->second->getType();
684 const TString &name = attribute->second->getSymbol();
685
686 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400687
Jamie Madill834e8b72014-04-11 13:33:58 -0400688 gl::Attribute attributeVar(glVariableType(type), glVariablePrecision(type), name.c_str(),
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400689 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
690 mActiveAttributes.push_back(attributeVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000691 }
692
Jamie Madill529077d2013-06-20 11:55:54 -0400693 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
694 {
695 out << *structDeclaration;
696 }
697
698 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
699 {
700 out << *constructor;
701 }
702
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500703 if (mUsesDiscardRewriting)
704 {
705 out << "#define ANGLE_USES_DISCARD_REWRITING" << "\n";
706 }
707
Nicolas Capens655fe362014-04-11 13:12:34 -0400708 if (mUsesNestedBreak)
709 {
710 out << "#define ANGLE_USES_NESTED_BREAK" << "\n";
711 }
712
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400713 if (mContext.shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000714 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000715 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000716 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000717
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000718 out << "// Varyings\n";
719 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400720 out << "\n";
721
722 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000723 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500724 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000725 {
Jamie Madill46131a32013-06-20 11:55:50 -0400726 const TString &variableName = outputVariableIt->first;
727 const TType &variableType = outputVariableIt->second->getType();
728 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
729
730 out << "static " + typeString(variableType) + " out_" + variableName + arrayString(variableType) +
731 " = " + initializer(variableType) + ";\n";
732
Jamie Madill834e8b72014-04-11 13:33:58 -0400733 gl::Attribute outputVar(glVariableType(variableType), glVariablePrecision(variableType), variableName.c_str(),
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400734 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
Jamie Madill46131a32013-06-20 11:55:50 -0400735 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000736 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000737 }
Jamie Madill46131a32013-06-20 11:55:50 -0400738 else
739 {
740 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
741
742 out << "static float4 gl_Color[" << numColorValues << "] =\n"
743 "{\n";
744 for (unsigned int i = 0; i < numColorValues; i++)
745 {
746 out << " float4(0, 0, 0, 0)";
747 if (i + 1 != numColorValues)
748 {
749 out << ",";
750 }
751 out << "\n";
752 }
753
754 out << "};\n";
755 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000756
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400757 if (mUsesFragDepth)
758 {
759 out << "static float gl_Depth = 0.0;\n";
760 }
761
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000762 if (mUsesFragCoord)
763 {
764 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
765 }
766
767 if (mUsesPointCoord)
768 {
769 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
770 }
771
772 if (mUsesFrontFacing)
773 {
774 out << "static bool gl_FrontFacing = false;\n";
775 }
776
777 out << "\n";
778
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000779 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000780 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000781 out << "struct gl_DepthRangeParameters\n"
782 "{\n"
783 " float near;\n"
784 " float far;\n"
785 " float diff;\n"
786 "};\n"
787 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000788 }
789
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000790 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000791 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000792 out << "cbuffer DriverConstants : register(b1)\n"
793 "{\n";
794
795 if (mUsesDepthRange)
796 {
797 out << " float3 dx_DepthRange : packoffset(c0);\n";
798 }
799
800 if (mUsesFragCoord)
801 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000802 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000803 }
804
805 if (mUsesFragCoord || mUsesFrontFacing)
806 {
807 out << " float3 dx_DepthFront : packoffset(c2);\n";
808 }
809
810 out << "};\n";
811 }
812 else
813 {
814 if (mUsesDepthRange)
815 {
816 out << "uniform float3 dx_DepthRange : register(c0);";
817 }
818
819 if (mUsesFragCoord)
820 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000821 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000822 }
823
824 if (mUsesFragCoord || mUsesFrontFacing)
825 {
826 out << "uniform float3 dx_DepthFront : register(c2);\n";
827 }
828 }
829
830 out << "\n";
831
832 if (mUsesDepthRange)
833 {
834 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
835 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000836 }
837
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000838 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000839 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000840
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000841 if (!interfaceBlocks.empty())
842 {
843 out << interfaceBlocks;
844 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400845
846 if (!flaggedStructs.empty())
847 {
848 out << "// Std140 Structures accessed by value\n";
849 out << "\n";
850 out << flaggedStructs;
851 out << "\n";
852 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000853 }
854
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000855 if (usingMRTExtension && mNumRenderTargets > 1)
856 {
857 out << "#define GL_USES_MRT\n";
858 }
859
860 if (mUsesFragColor)
861 {
862 out << "#define GL_USES_FRAG_COLOR\n";
863 }
864
865 if (mUsesFragData)
866 {
867 out << "#define GL_USES_FRAG_DATA\n";
868 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000869 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000870 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000871 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000872 out << "// Attributes\n";
873 out << attributes;
874 out << "\n"
875 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
876
877 if (mUsesPointSize)
878 {
879 out << "static float gl_PointSize = float(1);\n";
880 }
881
882 out << "\n"
883 "// Varyings\n";
884 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000885 out << "\n";
886
887 if (mUsesDepthRange)
888 {
889 out << "struct gl_DepthRangeParameters\n"
890 "{\n"
891 " float near;\n"
892 " float far;\n"
893 " float diff;\n"
894 "};\n"
895 "\n";
896 }
897
898 if (mOutputType == SH_HLSL11_OUTPUT)
899 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000900 if (mUsesDepthRange)
901 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000902 out << "cbuffer DriverConstants : register(b1)\n"
903 "{\n"
904 " float3 dx_DepthRange : packoffset(c0);\n"
905 "};\n"
906 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000907 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000908 }
909 else
910 {
911 if (mUsesDepthRange)
912 {
913 out << "uniform float3 dx_DepthRange : register(c0);\n";
914 }
915
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000916 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000917 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000918 }
919
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000920 if (mUsesDepthRange)
921 {
922 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
923 "\n";
924 }
925
926 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000927 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000928
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000929 if (!interfaceBlocks.empty())
930 {
931 out << interfaceBlocks;
932 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400933
934 if (!flaggedStructs.empty())
935 {
936 out << "// Std140 Structures accessed by value\n";
937 out << "\n";
938 out << flaggedStructs;
939 out << "\n";
940 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000941 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400942 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000943
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400944 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
945 {
946 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400947 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000948 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400949 switch(textureFunction->sampler)
950 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400951 case EbtSampler2D: out << "int2 "; break;
952 case EbtSampler3D: out << "int3 "; break;
953 case EbtSamplerCube: out << "int2 "; break;
954 case EbtSampler2DArray: out << "int3 "; break;
955 case EbtISampler2D: out << "int2 "; break;
956 case EbtISampler3D: out << "int3 "; break;
957 case EbtISamplerCube: out << "int2 "; break;
958 case EbtISampler2DArray: out << "int3 "; break;
959 case EbtUSampler2D: out << "int2 "; break;
960 case EbtUSampler3D: out << "int3 "; break;
961 case EbtUSamplerCube: out << "int2 "; break;
962 case EbtUSampler2DArray: out << "int3 "; break;
963 case EbtSampler2DShadow: out << "int2 "; break;
964 case EbtSamplerCubeShadow: out << "int2 "; break;
965 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400966 default: UNREACHABLE();
967 }
968 }
969 else // Sampling function
970 {
971 switch(textureFunction->sampler)
972 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400973 case EbtSampler2D: out << "float4 "; break;
974 case EbtSampler3D: out << "float4 "; break;
975 case EbtSamplerCube: out << "float4 "; break;
976 case EbtSampler2DArray: out << "float4 "; break;
977 case EbtISampler2D: out << "int4 "; break;
978 case EbtISampler3D: out << "int4 "; break;
979 case EbtISamplerCube: out << "int4 "; break;
980 case EbtISampler2DArray: out << "int4 "; break;
981 case EbtUSampler2D: out << "uint4 "; break;
982 case EbtUSampler3D: out << "uint4 "; break;
983 case EbtUSamplerCube: out << "uint4 "; break;
984 case EbtUSampler2DArray: out << "uint4 "; break;
985 case EbtSampler2DShadow: out << "float "; break;
986 case EbtSamplerCubeShadow: out << "float "; break;
987 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400988 default: UNREACHABLE();
989 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000990 }
991
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400992 // Function name
993 out << textureFunction->name();
994
995 // Argument list
996 int hlslCoords = 4;
997
998 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000999 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001000 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001001 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001002 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
1003 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
1004 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001005 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001006
Nicolas Capens75fb4752013-07-10 15:14:47 -04001007 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001008 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001009 case TextureFunction::IMPLICIT: break;
1010 case TextureFunction::BIAS: hlslCoords = 4; break;
1011 case TextureFunction::LOD: hlslCoords = 4; break;
1012 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001013 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001014 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00001015 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001016 }
1017 else if (mOutputType == SH_HLSL11_OUTPUT)
1018 {
1019 switch(textureFunction->sampler)
1020 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04001021 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
1022 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
1023 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
1024 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
1025 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
1026 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -05001027 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001028 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
1029 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
1030 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -05001031 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001032 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
1033 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
1034 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
1035 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001036 default: UNREACHABLE();
1037 }
1038 }
1039 else UNREACHABLE();
1040
Nicolas Capensfc014542014-02-18 14:47:13 -05001041 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001042 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001043 switch(textureFunction->coords)
1044 {
1045 case 2: out << ", int2 t"; break;
1046 case 3: out << ", int3 t"; break;
1047 default: UNREACHABLE();
1048 }
1049 }
1050 else // Floating-point coordinates (except textureSize)
1051 {
1052 switch(textureFunction->coords)
1053 {
1054 case 1: out << ", int lod"; break; // textureSize()
1055 case 2: out << ", float2 t"; break;
1056 case 3: out << ", float3 t"; break;
1057 case 4: out << ", float4 t"; break;
1058 default: UNREACHABLE();
1059 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001060 }
1061
Nicolas Capensd11d5492014-02-19 17:06:10 -05001062 if (textureFunction->method == TextureFunction::GRAD)
1063 {
1064 switch(textureFunction->sampler)
1065 {
1066 case EbtSampler2D:
1067 case EbtISampler2D:
1068 case EbtUSampler2D:
1069 case EbtSampler2DArray:
1070 case EbtISampler2DArray:
1071 case EbtUSampler2DArray:
1072 case EbtSampler2DShadow:
1073 case EbtSampler2DArrayShadow:
1074 out << ", float2 ddx, float2 ddy";
1075 break;
1076 case EbtSampler3D:
1077 case EbtISampler3D:
1078 case EbtUSampler3D:
1079 case EbtSamplerCube:
1080 case EbtISamplerCube:
1081 case EbtUSamplerCube:
1082 case EbtSamplerCubeShadow:
1083 out << ", float3 ddx, float3 ddy";
1084 break;
1085 default: UNREACHABLE();
1086 }
1087 }
1088
Nicolas Capens75fb4752013-07-10 15:14:47 -04001089 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +00001090 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001091 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001092 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001093 case TextureFunction::LOD: out << ", float lod"; break;
1094 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001095 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -04001096 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -05001097 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -05001098 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001099 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +00001100 }
1101
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001102 if (textureFunction->offset)
1103 {
1104 switch(textureFunction->sampler)
1105 {
1106 case EbtSampler2D: out << ", int2 offset"; break;
1107 case EbtSampler3D: out << ", int3 offset"; break;
1108 case EbtSampler2DArray: out << ", int2 offset"; break;
1109 case EbtISampler2D: out << ", int2 offset"; break;
1110 case EbtISampler3D: out << ", int3 offset"; break;
1111 case EbtISampler2DArray: out << ", int2 offset"; break;
1112 case EbtUSampler2D: out << ", int2 offset"; break;
1113 case EbtUSampler3D: out << ", int3 offset"; break;
1114 case EbtUSampler2DArray: out << ", int2 offset"; break;
1115 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -05001116 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001117 default: UNREACHABLE();
1118 }
1119 }
1120
Nicolas Capens84cfa122014-04-14 13:48:45 -04001121 if (textureFunction->method == TextureFunction::BIAS ||
1122 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001123 {
1124 out << ", float bias";
1125 }
1126
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001127 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001128 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001129
Nicolas Capens75fb4752013-07-10 15:14:47 -04001130 if (textureFunction->method == TextureFunction::SIZE)
1131 {
1132 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
1133 {
1134 if (IsSamplerArray(textureFunction->sampler))
1135 {
1136 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
1137 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
1138 }
1139 else
1140 {
1141 out << " uint width; uint height; uint numberOfLevels;\n"
1142 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
1143 }
1144 }
1145 else if (IsSampler3D(textureFunction->sampler))
1146 {
1147 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
1148 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
1149 }
1150 else UNREACHABLE();
1151
1152 switch(textureFunction->sampler)
1153 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04001154 case EbtSampler2D: out << " return int2(width, height);"; break;
1155 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
1156 case EbtSamplerCube: out << " return int2(width, height);"; break;
1157 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
1158 case EbtISampler2D: out << " return int2(width, height);"; break;
1159 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
1160 case EbtISamplerCube: out << " return int2(width, height);"; break;
1161 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
1162 case EbtUSampler2D: out << " return int2(width, height);"; break;
1163 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
1164 case EbtUSamplerCube: out << " return int2(width, height);"; break;
1165 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
1166 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
1167 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
1168 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001169 default: UNREACHABLE();
1170 }
1171 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001172 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001173 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001174 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1175 {
1176 out << " float width; float height; float layers; float levels;\n";
1177
1178 out << " uint mip = 0;\n";
1179
1180 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
1181
1182 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
1183 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
1184 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
1185 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
1186
1187 // FACE_POSITIVE_X = 000b
1188 // FACE_NEGATIVE_X = 001b
1189 // FACE_POSITIVE_Y = 010b
1190 // FACE_NEGATIVE_Y = 011b
1191 // FACE_POSITIVE_Z = 100b
1192 // FACE_NEGATIVE_Z = 101b
1193 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
1194
1195 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
1196 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
1197 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
1198
1199 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
1200 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
1201 }
1202 else if (IsIntegerSampler(textureFunction->sampler) &&
1203 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001204 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001205 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001206 {
Nicolas Capens93e50de2013-07-09 13:46:28 -04001207 if (IsSamplerArray(textureFunction->sampler))
1208 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001209 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001210
Nicolas Capens9edebd62013-08-06 10:59:10 -04001211 if (textureFunction->method == TextureFunction::LOD0)
1212 {
1213 out << " uint mip = 0;\n";
1214 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001215 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1216 {
1217 out << " uint mip = bias;\n";
1218 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001219 else
1220 {
1221 if (textureFunction->method == TextureFunction::IMPLICIT ||
1222 textureFunction->method == TextureFunction::BIAS)
1223 {
1224 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1225 " float2 tSized = float2(t.x * width, t.y * height);\n"
1226 " float dx = length(ddx(tSized));\n"
1227 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001228 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001229
1230 if (textureFunction->method == TextureFunction::BIAS)
1231 {
1232 out << " lod += bias;\n";
1233 }
1234 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001235 else if (textureFunction->method == TextureFunction::GRAD)
1236 {
1237 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1238 " float lod = log2(max(length(ddx), length(ddy)));\n";
1239 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001240
1241 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1242 }
1243
1244 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001245 }
1246 else
1247 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001248 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001249
Nicolas Capens9edebd62013-08-06 10:59:10 -04001250 if (textureFunction->method == TextureFunction::LOD0)
1251 {
1252 out << " uint mip = 0;\n";
1253 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001254 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1255 {
1256 out << " uint mip = bias;\n";
1257 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001258 else
1259 {
1260 if (textureFunction->method == TextureFunction::IMPLICIT ||
1261 textureFunction->method == TextureFunction::BIAS)
1262 {
1263 out << " x.GetDimensions(0, width, height, levels);\n"
1264 " float2 tSized = float2(t.x * width, t.y * height);\n"
1265 " float dx = length(ddx(tSized));\n"
1266 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001267 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001268
1269 if (textureFunction->method == TextureFunction::BIAS)
1270 {
1271 out << " lod += bias;\n";
1272 }
1273 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05001274 else if (textureFunction->method == TextureFunction::LOD)
1275 {
1276 out << " x.GetDimensions(0, width, height, levels);\n";
1277 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001278 else if (textureFunction->method == TextureFunction::GRAD)
1279 {
1280 out << " x.GetDimensions(0, width, height, levels);\n"
1281 " float lod = log2(max(length(ddx), length(ddy)));\n";
1282 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001283
1284 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1285 }
1286
1287 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001288 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001289 }
1290 else if (IsSampler3D(textureFunction->sampler))
1291 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001292 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001293
Nicolas Capens9edebd62013-08-06 10:59:10 -04001294 if (textureFunction->method == TextureFunction::LOD0)
1295 {
1296 out << " uint mip = 0;\n";
1297 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001298 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1299 {
1300 out << " uint mip = bias;\n";
1301 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001302 else
1303 {
1304 if (textureFunction->method == TextureFunction::IMPLICIT ||
1305 textureFunction->method == TextureFunction::BIAS)
1306 {
1307 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1308 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
1309 " float dx = length(ddx(tSized));\n"
1310 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001311 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001312
1313 if (textureFunction->method == TextureFunction::BIAS)
1314 {
1315 out << " lod += bias;\n";
1316 }
1317 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001318 else if (textureFunction->method == TextureFunction::GRAD)
1319 {
1320 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1321 " float lod = log2(max(length(ddx), length(ddy)));\n";
1322 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001323
1324 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1325 }
1326
1327 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001328 }
1329 else UNREACHABLE();
1330 }
1331
1332 out << " return ";
1333
1334 // HLSL intrinsic
1335 if (mOutputType == SH_HLSL9_OUTPUT)
1336 {
1337 switch(textureFunction->sampler)
1338 {
1339 case EbtSampler2D: out << "tex2D"; break;
1340 case EbtSamplerCube: out << "texCUBE"; break;
1341 default: UNREACHABLE();
1342 }
1343
Nicolas Capens75fb4752013-07-10 15:14:47 -04001344 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001345 {
1346 case TextureFunction::IMPLICIT: out << "(s, "; break;
1347 case TextureFunction::BIAS: out << "bias(s, "; break;
1348 case TextureFunction::LOD: out << "lod(s, "; break;
1349 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001350 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001351 default: UNREACHABLE();
1352 }
1353 }
1354 else if (mOutputType == SH_HLSL11_OUTPUT)
1355 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001356 if (textureFunction->method == TextureFunction::GRAD)
1357 {
1358 if (IsIntegerSampler(textureFunction->sampler))
1359 {
1360 out << "x.Load(";
1361 }
1362 else if (IsShadowSampler(textureFunction->sampler))
1363 {
1364 out << "x.SampleCmpLevelZero(s, ";
1365 }
1366 else
1367 {
1368 out << "x.SampleGrad(s, ";
1369 }
1370 }
1371 else if (IsIntegerSampler(textureFunction->sampler) ||
1372 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001373 {
1374 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001375 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001376 else if (IsShadowSampler(textureFunction->sampler))
1377 {
1378 out << "x.SampleCmp(s, ";
1379 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001380 else
1381 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001382 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001383 {
1384 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1385 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1386 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1387 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001388 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001389 default: UNREACHABLE();
1390 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001391 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001392 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001393 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001394
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001395 // Integer sampling requires integer addresses
1396 TString addressx = "";
1397 TString addressy = "";
1398 TString addressz = "";
1399 TString close = "";
1400
Nicolas Capensfc014542014-02-18 14:47:13 -05001401 if (IsIntegerSampler(textureFunction->sampler) ||
1402 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001403 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001404 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001405 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001406 case 2: out << "int3("; break;
1407 case 3: out << "int4("; break;
1408 default: UNREACHABLE();
1409 }
1410
Nicolas Capensfc014542014-02-18 14:47:13 -05001411 // Convert from normalized floating-point to integer
1412 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001413 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001414 addressx = "int(floor(width * frac((";
1415 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001416
Nicolas Capensfc014542014-02-18 14:47:13 -05001417 if (IsSamplerArray(textureFunction->sampler))
1418 {
1419 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1420 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001421 else if (IsSamplerCube(textureFunction->sampler))
1422 {
1423 addressz = "((((";
1424 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001425 else
1426 {
1427 addressz = "int(floor(depth * frac((";
1428 }
1429
1430 close = "))))";
1431 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001432 }
1433 else
1434 {
1435 switch(hlslCoords)
1436 {
1437 case 2: out << "float2("; break;
1438 case 3: out << "float3("; break;
1439 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001440 default: UNREACHABLE();
1441 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001442 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001443
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001444 TString proj = ""; // Only used for projected textures
1445
1446 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001447 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001448 switch(textureFunction->coords)
1449 {
1450 case 3: proj = " / t.z"; break;
1451 case 4: proj = " / t.w"; break;
1452 default: UNREACHABLE();
1453 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001454 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001455
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001456 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001457
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001458 if (mOutputType == SH_HLSL9_OUTPUT)
1459 {
1460 if (hlslCoords >= 3)
1461 {
1462 if (textureFunction->coords < 3)
1463 {
1464 out << ", 0";
1465 }
1466 else
1467 {
1468 out << ", t.z" + proj;
1469 }
1470 }
1471
1472 if (hlslCoords == 4)
1473 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001474 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001475 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001476 case TextureFunction::BIAS: out << ", bias"; break;
1477 case TextureFunction::LOD: out << ", lod"; break;
1478 case TextureFunction::LOD0: out << ", 0"; break;
1479 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001480 default: UNREACHABLE();
1481 }
1482 }
1483
1484 out << "));\n";
1485 }
1486 else if (mOutputType == SH_HLSL11_OUTPUT)
1487 {
1488 if (hlslCoords >= 3)
1489 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001490 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1491 {
1492 out << ", face";
1493 }
1494 else
1495 {
1496 out << ", " + addressz + ("t.z" + proj) + close;
1497 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001498 }
1499
Nicolas Capensd11d5492014-02-19 17:06:10 -05001500 if (textureFunction->method == TextureFunction::GRAD)
1501 {
1502 if (IsIntegerSampler(textureFunction->sampler))
1503 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001504 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001505 }
1506 else if (IsShadowSampler(textureFunction->sampler))
1507 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001508 // Compare value
Nicolas Capensd11d5492014-02-19 17:06:10 -05001509 switch(textureFunction->coords)
1510 {
1511 case 3: out << "), t.z"; break;
1512 case 4: out << "), t.w"; break;
1513 default: UNREACHABLE();
1514 }
1515 }
1516 else
1517 {
1518 out << "), ddx, ddy";
1519 }
1520 }
1521 else if (IsIntegerSampler(textureFunction->sampler) ||
1522 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001523 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001524 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001525 }
1526 else if (IsShadowSampler(textureFunction->sampler))
1527 {
1528 // Compare value
1529 switch(textureFunction->coords)
1530 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001531 case 3: out << "), t.z"; break;
1532 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001533 default: UNREACHABLE();
1534 }
1535 }
1536 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001537 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001538 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001539 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001540 case TextureFunction::IMPLICIT: out << ")"; break;
1541 case TextureFunction::BIAS: out << "), bias"; break;
1542 case TextureFunction::LOD: out << "), lod"; break;
1543 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001544 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001545 default: UNREACHABLE();
1546 }
1547 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001548
1549 if (textureFunction->offset)
1550 {
1551 out << ", offset";
1552 }
1553
1554 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001555 }
1556 else UNREACHABLE();
1557 }
1558
1559 out << "\n"
1560 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001561 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001562 }
1563
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001564 if (mUsesFragCoord)
1565 {
1566 out << "#define GL_USES_FRAG_COORD\n";
1567 }
1568
1569 if (mUsesPointCoord)
1570 {
1571 out << "#define GL_USES_POINT_COORD\n";
1572 }
1573
1574 if (mUsesFrontFacing)
1575 {
1576 out << "#define GL_USES_FRONT_FACING\n";
1577 }
1578
1579 if (mUsesPointSize)
1580 {
1581 out << "#define GL_USES_POINT_SIZE\n";
1582 }
1583
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001584 if (mUsesFragDepth)
1585 {
1586 out << "#define GL_USES_FRAG_DEPTH\n";
1587 }
1588
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001589 if (mUsesDepthRange)
1590 {
1591 out << "#define GL_USES_DEPTH_RANGE\n";
1592 }
1593
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001594 if (mUsesXor)
1595 {
1596 out << "bool xor(bool p, bool q)\n"
1597 "{\n"
1598 " return (p || q) && !(p && q);\n"
1599 "}\n"
1600 "\n";
1601 }
1602
1603 if (mUsesMod1)
1604 {
1605 out << "float mod(float x, float y)\n"
1606 "{\n"
1607 " return x - y * floor(x / y);\n"
1608 "}\n"
1609 "\n";
1610 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001611
1612 if (mUsesMod2v)
1613 {
1614 out << "float2 mod(float2 x, float2 y)\n"
1615 "{\n"
1616 " return x - y * floor(x / y);\n"
1617 "}\n"
1618 "\n";
1619 }
1620
1621 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001622 {
1623 out << "float2 mod(float2 x, float y)\n"
1624 "{\n"
1625 " return x - y * floor(x / y);\n"
1626 "}\n"
1627 "\n";
1628 }
1629
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001630 if (mUsesMod3v)
1631 {
1632 out << "float3 mod(float3 x, float3 y)\n"
1633 "{\n"
1634 " return x - y * floor(x / y);\n"
1635 "}\n"
1636 "\n";
1637 }
1638
1639 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001640 {
1641 out << "float3 mod(float3 x, float y)\n"
1642 "{\n"
1643 " return x - y * floor(x / y);\n"
1644 "}\n"
1645 "\n";
1646 }
1647
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001648 if (mUsesMod4v)
1649 {
1650 out << "float4 mod(float4 x, float4 y)\n"
1651 "{\n"
1652 " return x - y * floor(x / y);\n"
1653 "}\n"
1654 "\n";
1655 }
1656
1657 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001658 {
1659 out << "float4 mod(float4 x, float y)\n"
1660 "{\n"
1661 " return x - y * floor(x / y);\n"
1662 "}\n"
1663 "\n";
1664 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001665
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001666 if (mUsesFaceforward1)
1667 {
1668 out << "float faceforward(float N, float I, float Nref)\n"
1669 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001670 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001671 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001672 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001673 " }\n"
1674 " else\n"
1675 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001676 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001677 " }\n"
1678 "}\n"
1679 "\n";
1680 }
1681
1682 if (mUsesFaceforward2)
1683 {
1684 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1685 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001686 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001687 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001688 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001689 " }\n"
1690 " else\n"
1691 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001692 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001693 " }\n"
1694 "}\n"
1695 "\n";
1696 }
1697
1698 if (mUsesFaceforward3)
1699 {
1700 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1701 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001702 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001703 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001704 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001705 " }\n"
1706 " else\n"
1707 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001708 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001709 " }\n"
1710 "}\n"
1711 "\n";
1712 }
1713
1714 if (mUsesFaceforward4)
1715 {
1716 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1717 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001718 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001719 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001720 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001721 " }\n"
1722 " else\n"
1723 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001724 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001725 " }\n"
1726 "}\n"
1727 "\n";
1728 }
1729
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001730 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001731 {
1732 out << "float atanyx(float y, float x)\n"
1733 "{\n"
1734 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1735 " return atan2(y, x);\n"
1736 "}\n";
1737 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001738
1739 if (mUsesAtan2_2)
1740 {
1741 out << "float2 atanyx(float2 y, float2 x)\n"
1742 "{\n"
1743 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1744 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1745 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1746 "}\n";
1747 }
1748
1749 if (mUsesAtan2_3)
1750 {
1751 out << "float3 atanyx(float3 y, float3 x)\n"
1752 "{\n"
1753 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1754 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1755 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1756 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1757 "}\n";
1758 }
1759
1760 if (mUsesAtan2_4)
1761 {
1762 out << "float4 atanyx(float4 y, float4 x)\n"
1763 "{\n"
1764 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1765 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1766 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1767 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1768 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1769 "}\n";
1770 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001771}
1772
1773void OutputHLSL::visitSymbol(TIntermSymbol *node)
1774{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001775 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001776
Jamie Madill570e04d2013-06-21 09:15:33 -04001777 // Handle accessing std140 structs by value
1778 if (mFlaggedStructMappedNames.count(node) > 0)
1779 {
1780 out << mFlaggedStructMappedNames[node];
1781 return;
1782 }
1783
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001784 TString name = node->getSymbol();
1785
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001786 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001787 {
1788 mUsesDepthRange = true;
1789 out << name;
1790 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001791 else
1792 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001793 TQualifier qualifier = node->getQualifier();
1794
1795 if (qualifier == EvqUniform)
1796 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001797 const TType& nodeType = node->getType();
1798 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1799
1800 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001801 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001802 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001803 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001804 else
1805 {
1806 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001807 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001808
1809 out << decorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001810 }
Jamie Madill19571812013-08-12 15:26:34 -07001811 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001812 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001813 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001814 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001815 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001816 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001817 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001818 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001819 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001820 }
Jamie Madill19571812013-08-12 15:26:34 -07001821 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001822 {
1823 mReferencedOutputVariables[name] = node;
1824 out << "out_" << name;
1825 }
1826 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001827 {
1828 out << "gl_Color[0]";
1829 mUsesFragColor = true;
1830 }
1831 else if (qualifier == EvqFragData)
1832 {
1833 out << "gl_Color";
1834 mUsesFragData = true;
1835 }
1836 else if (qualifier == EvqFragCoord)
1837 {
1838 mUsesFragCoord = true;
1839 out << name;
1840 }
1841 else if (qualifier == EvqPointCoord)
1842 {
1843 mUsesPointCoord = true;
1844 out << name;
1845 }
1846 else if (qualifier == EvqFrontFacing)
1847 {
1848 mUsesFrontFacing = true;
1849 out << name;
1850 }
1851 else if (qualifier == EvqPointSize)
1852 {
1853 mUsesPointSize = true;
1854 out << name;
1855 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001856 else if (name == "gl_FragDepthEXT")
1857 {
1858 mUsesFragDepth = true;
1859 out << "gl_Depth";
1860 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001861 else if (qualifier == EvqInternal)
1862 {
1863 out << name;
1864 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001865 else
1866 {
1867 out << decorate(name);
1868 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001869 }
1870}
1871
1872bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1873{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001874 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001875
Jamie Madill570e04d2013-06-21 09:15:33 -04001876 // Handle accessing std140 structs by value
1877 if (mFlaggedStructMappedNames.count(node) > 0)
1878 {
1879 out << mFlaggedStructMappedNames[node];
1880 return false;
1881 }
1882
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001883 switch (node->getOp())
1884 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001885 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001886 case EOpInitialize:
1887 if (visit == PreVisit)
1888 {
1889 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1890 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1891 // new variable is created before the assignment is evaluated), so we need to convert
1892 // this to "float t = x, x = t;".
1893
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001894 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1895 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001896
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001897 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1898 expression->traverse(&searchSymbol);
1899 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001900
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001901 if (sameSymbol)
1902 {
1903 // Type already printed
1904 out << "t" + str(mUniqueIndex) + " = ";
1905 expression->traverse(this);
1906 out << ", ";
1907 symbolNode->traverse(this);
1908 out << " = t" + str(mUniqueIndex);
1909
1910 mUniqueIndex++;
1911 return false;
1912 }
1913 }
1914 else if (visit == InVisit)
1915 {
1916 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001917 }
1918 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001919 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1920 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1921 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1922 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1923 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1924 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001925 if (visit == PreVisit)
1926 {
1927 out << "(";
1928 }
1929 else if (visit == InVisit)
1930 {
1931 out << " = mul(";
1932 node->getLeft()->traverse(this);
1933 out << ", transpose(";
1934 }
1935 else
1936 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001937 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001938 }
1939 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001940 case EOpMatrixTimesMatrixAssign:
1941 if (visit == PreVisit)
1942 {
1943 out << "(";
1944 }
1945 else if (visit == InVisit)
1946 {
1947 out << " = mul(";
1948 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001949 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001950 }
1951 else
1952 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001953 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001954 }
1955 break;
1956 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001957 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001958 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001959 const TType& leftType = node->getLeft()->getType();
1960 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001961 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001962 if (visit == PreVisit)
1963 {
1964 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1965 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1966
1967 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1968 out << interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1969
1970 return false;
1971 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001972 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001973 else
1974 {
1975 outputTriplet(visit, "", "[", "]");
1976 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001977 }
1978 break;
1979 case EOpIndexIndirect:
1980 // We do not currently support indirect references to interface blocks
1981 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1982 outputTriplet(visit, "", "[", "]");
1983 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001984 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001985 if (visit == InVisit)
1986 {
1987 const TStructure* structure = node->getLeft()->getType().getStruct();
1988 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1989 const TField* field = structure->fields()[index->getIConst(0)];
1990 out << "." + decorateField(field->name(), *structure);
1991
1992 return false;
1993 }
1994 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001995 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001996 if (visit == InVisit)
1997 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001998 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1999 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
2000 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
2001 out << "." + decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002002
2003 return false;
2004 }
2005 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002006 case EOpVectorSwizzle:
2007 if (visit == InVisit)
2008 {
2009 out << ".";
2010
2011 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
2012
2013 if (swizzle)
2014 {
2015 TIntermSequence &sequence = swizzle->getSequence();
2016
2017 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2018 {
2019 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
2020
2021 if (element)
2022 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002023 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002024
2025 switch (i)
2026 {
2027 case 0: out << "x"; break;
2028 case 1: out << "y"; break;
2029 case 2: out << "z"; break;
2030 case 3: out << "w"; break;
2031 default: UNREACHABLE();
2032 }
2033 }
2034 else UNREACHABLE();
2035 }
2036 }
2037 else UNREACHABLE();
2038
2039 return false; // Fully processed
2040 }
2041 break;
2042 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
2043 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
2044 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
2045 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002046 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002047 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002048 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002049 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002050 if (node->getOp() == EOpEqual)
2051 {
2052 outputTriplet(visit, "(", " == ", ")");
2053 }
2054 else
2055 {
2056 outputTriplet(visit, "(", " != ", ")");
2057 }
2058 }
2059 else if (node->getLeft()->getBasicType() == EbtStruct)
2060 {
2061 if (node->getOp() == EOpEqual)
2062 {
2063 out << "(";
2064 }
2065 else
2066 {
2067 out << "!(";
2068 }
2069
Jamie Madill98493dd2013-07-08 14:39:03 -04002070 const TStructure &structure = *node->getLeft()->getType().getStruct();
2071 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002072
Jamie Madill98493dd2013-07-08 14:39:03 -04002073 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002074 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002075 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002076
2077 node->getLeft()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04002078 out << "." + decorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002079 node->getRight()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04002080 out << "." + decorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002081
Jamie Madill98493dd2013-07-08 14:39:03 -04002082 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002083 {
2084 out << " && ";
2085 }
2086 }
2087
2088 out << ")";
2089
2090 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002091 }
2092 else
2093 {
Jamie Madill0b20c942013-07-19 16:36:56 -04002094 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002095
2096 if (node->getOp() == EOpEqual)
2097 {
Jamie Madill0b20c942013-07-19 16:36:56 -04002098 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002099 }
2100 else
2101 {
Jamie Madill0b20c942013-07-19 16:36:56 -04002102 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002103 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002104 }
2105 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002106 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2107 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2108 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2109 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2110 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00002111 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00002112 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
2113 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00002114 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00002115 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002116 if (node->getRight()->hasSideEffects())
2117 {
2118 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
2119 return false;
2120 }
2121 else
2122 {
2123 outputTriplet(visit, "(", " || ", ")");
2124 return true;
2125 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002126 case EOpLogicalXor:
2127 mUsesXor = true;
2128 outputTriplet(visit, "xor(", ", ", ")");
2129 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00002130 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002131 if (node->getRight()->hasSideEffects())
2132 {
2133 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
2134 return false;
2135 }
2136 else
2137 {
2138 outputTriplet(visit, "(", " && ", ")");
2139 return true;
2140 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002141 default: UNREACHABLE();
2142 }
2143
2144 return true;
2145}
2146
2147bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
2148{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002149 switch (node->getOp())
2150 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002151 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
2152 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
2153 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
2154 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
2155 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
2156 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
2157 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002158 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04002159 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002160 case EOpConvFloatToBool:
2161 switch (node->getOperand()->getType().getNominalSize())
2162 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002163 case 1: outputTriplet(visit, "bool(", "", ")"); break;
2164 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
2165 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
2166 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002167 default: UNREACHABLE();
2168 }
2169 break;
2170 case EOpConvBoolToFloat:
2171 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04002172 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002173 switch (node->getOperand()->getType().getNominalSize())
2174 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002175 case 1: outputTriplet(visit, "float(", "", ")"); break;
2176 case 2: outputTriplet(visit, "float2(", "", ")"); break;
2177 case 3: outputTriplet(visit, "float3(", "", ")"); break;
2178 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002179 default: UNREACHABLE();
2180 }
2181 break;
2182 case EOpConvFloatToInt:
2183 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04002184 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002185 switch (node->getOperand()->getType().getNominalSize())
2186 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002187 case 1: outputTriplet(visit, "int(", "", ")"); break;
2188 case 2: outputTriplet(visit, "int2(", "", ")"); break;
2189 case 3: outputTriplet(visit, "int3(", "", ")"); break;
2190 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002191 default: UNREACHABLE();
2192 }
2193 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002194 case EOpConvFloatToUInt:
2195 case EOpConvBoolToUInt:
2196 case EOpConvIntToUInt:
Jamie Madilla16f1672013-07-03 15:15:17 -04002197 switch (node->getOperand()->getType().getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002198 {
2199 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002200 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
2201 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
2202 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002203 default: UNREACHABLE();
2204 }
2205 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002206 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
2207 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
2208 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
2209 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
2210 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
2211 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
2212 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
2213 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
2214 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
2215 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
2216 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
2217 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
2218 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
2219 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
2220 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
2221 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
2222 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
2223 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
2224 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
2225 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
2226 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002227 case EOpDFdx:
2228 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2229 {
2230 outputTriplet(visit, "(", "", ", 0.0)");
2231 }
2232 else
2233 {
2234 outputTriplet(visit, "ddx(", "", ")");
2235 }
2236 break;
2237 case EOpDFdy:
2238 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2239 {
2240 outputTriplet(visit, "(", "", ", 0.0)");
2241 }
2242 else
2243 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00002244 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002245 }
2246 break;
2247 case EOpFwidth:
2248 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2249 {
2250 outputTriplet(visit, "(", "", ", 0.0)");
2251 }
2252 else
2253 {
2254 outputTriplet(visit, "fwidth(", "", ")");
2255 }
2256 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002257 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
2258 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002259 default: UNREACHABLE();
2260 }
2261
2262 return true;
2263}
2264
2265bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2266{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002267 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002268
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002269 switch (node->getOp())
2270 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002271 case EOpSequence:
2272 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002273 if (mInsideFunction)
2274 {
Jamie Madill075edd82013-07-08 13:30:19 -04002275 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002276 out << "{\n";
2277 }
2278
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002279 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
2280 {
Jamie Madill075edd82013-07-08 13:30:19 -04002281 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002282
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002283 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002284
2285 out << ";\n";
2286 }
2287
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002288 if (mInsideFunction)
2289 {
Jamie Madill075edd82013-07-08 13:30:19 -04002290 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002291 out << "}\n";
2292 }
2293
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002294 return false;
2295 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002296 case EOpDeclaration:
2297 if (visit == PreVisit)
2298 {
2299 TIntermSequence &sequence = node->getSequence();
2300 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002301
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00002302 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002303 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002304 if (variable->getType().getStruct())
2305 {
Jamie Madillbfa91f42014-06-05 15:45:18 -04002306 addConstructor(variable->getType(), structNameString(*variable->getType().getStruct()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00002307 }
2308
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002309 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002310 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00002311 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002312 {
2313 out << "static ";
2314 }
2315
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002316 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002317
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002318 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002319 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002320 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002321
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002322 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002323 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002324 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002325 out << arrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05002326 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002327 }
2328 else
2329 {
2330 (*sit)->traverse(this);
2331 }
2332
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002333 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002334 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002335 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002336 }
2337 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002338 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002339 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2340 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002341 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002342 }
2343 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002344 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002345 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002346 {
2347 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2348 {
2349 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2350
2351 if (symbol)
2352 {
2353 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2354 mReferencedVaryings[symbol->getSymbol()] = symbol;
2355 }
2356 else
2357 {
2358 (*sit)->traverse(this);
2359 }
2360 }
2361 }
2362
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002363 return false;
2364 }
2365 else if (visit == InVisit)
2366 {
2367 out << ", ";
2368 }
2369 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002370 case EOpPrototype:
2371 if (visit == PreVisit)
2372 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002373 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002374
2375 TIntermSequence &arguments = node->getSequence();
2376
2377 for (unsigned int i = 0; i < arguments.size(); i++)
2378 {
2379 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2380
2381 if (symbol)
2382 {
2383 out << argumentString(symbol);
2384
2385 if (i < arguments.size() - 1)
2386 {
2387 out << ", ";
2388 }
2389 }
2390 else UNREACHABLE();
2391 }
2392
2393 out << ");\n";
2394
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002395 // Also prototype the Lod0 variant if needed
2396 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2397 {
2398 mOutputLod0Function = true;
2399 node->traverse(this);
2400 mOutputLod0Function = false;
2401 }
2402
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002403 return false;
2404 }
2405 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002406 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002407 case EOpFunction:
2408 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002409 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002410
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002411 out << typeString(node->getType()) << " ";
2412
2413 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002414 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002415 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002416 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002417 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002418 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002419 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002420 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002421
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002422 TIntermSequence &sequence = node->getSequence();
2423 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2424
2425 for (unsigned int i = 0; i < arguments.size(); i++)
2426 {
2427 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2428
2429 if (symbol)
2430 {
2431 if (symbol->getType().getStruct())
2432 {
Jamie Madillbfa91f42014-06-05 15:45:18 -04002433 addConstructor(symbol->getType(), structNameString(*symbol->getType().getStruct()), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002434 }
2435
2436 out << argumentString(symbol);
2437
2438 if (i < arguments.size() - 1)
2439 {
2440 out << ", ";
2441 }
2442 }
2443 else UNREACHABLE();
2444 }
2445
2446 out << ")\n"
2447 "{\n";
2448
2449 if (sequence.size() > 1)
2450 {
2451 mInsideFunction = true;
2452 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002453 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002454 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002455
2456 out << "}\n";
2457
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002458 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2459 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002460 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002461 {
2462 mOutputLod0Function = true;
2463 node->traverse(this);
2464 mOutputLod0Function = false;
2465 }
2466 }
2467
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002468 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002469 }
2470 break;
2471 case EOpFunctionCall:
2472 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002473 TString name = TFunction::unmangleName(node->getName());
2474 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002475 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002476
2477 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002478 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002479 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002480 }
2481 else
2482 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002483 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2484
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002485 TextureFunction textureFunction;
2486 textureFunction.sampler = samplerType;
2487 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002488 textureFunction.method = TextureFunction::IMPLICIT;
2489 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002490 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002491
2492 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002493 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002494 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002495 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002496 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002497 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002498 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002499 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002500 }
Nicolas Capens46485082014-04-15 13:12:50 -04002501 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2502 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002503 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002504 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002505 }
Nicolas Capens46485082014-04-15 13:12:50 -04002506 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002507 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002508 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002509 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002510 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002511 else if (name == "textureSize")
2512 {
2513 textureFunction.method = TextureFunction::SIZE;
2514 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002515 else if (name == "textureOffset")
2516 {
2517 textureFunction.method = TextureFunction::IMPLICIT;
2518 textureFunction.offset = true;
2519 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002520 else if (name == "textureProjOffset")
2521 {
2522 textureFunction.method = TextureFunction::IMPLICIT;
2523 textureFunction.offset = true;
2524 textureFunction.proj = true;
2525 }
2526 else if (name == "textureLodOffset")
2527 {
2528 textureFunction.method = TextureFunction::LOD;
2529 textureFunction.offset = true;
2530 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002531 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 Capens46485082014-04-15 13:12:50 -04002546 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002547 {
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 Capens46485082014-04-15 13:12:50 -04002555 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002556 {
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;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002623 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
2624 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", &node->getSequence()); break;
2625 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", &node->getSequence()); break;
2626 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", &node->getSequence()); break;
2627 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", &node->getSequence()); break;
2628 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", &node->getSequence()); break;
2629 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", &node->getSequence()); break;
2630 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", &node->getSequence()); break;
2631 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", &node->getSequence()); break;
2632 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", &node->getSequence()); break;
2633 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", &node->getSequence()); break;
2634 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", &node->getSequence()); break;
2635 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", &node->getSequence()); break;
2636 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", &node->getSequence()); break;
2637 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", &node->getSequence()); break;
2638 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", &node->getSequence()); break;
2639 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", &node->getSequence()); break;
2640 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", &node->getSequence()); break;
2641 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", &node->getSequence()); break;
2642 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", &node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002643 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002644 {
2645 const TString &structName = structNameString(*node->getType().getStruct());
2646 addConstructor(node->getType(), structName, &node->getSequence());
2647 outputTriplet(visit, structName + "_ctor(", ", ", ")");
2648 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002649 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002650 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2651 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2652 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2653 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2654 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2655 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002656 case EOpMod:
2657 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002658 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002659 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2660 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2661 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002662 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002663 case 11: mUsesMod1 = true; break;
2664 case 22: mUsesMod2v = true; break;
2665 case 21: mUsesMod2f = true; break;
2666 case 33: mUsesMod3v = true; break;
2667 case 31: mUsesMod3f = true; break;
2668 case 44: mUsesMod4v = true; break;
2669 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002670 default: UNREACHABLE();
2671 }
2672
2673 outputTriplet(visit, "mod(", ", ", ")");
2674 }
2675 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002676 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002677 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002678 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002679 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2680 {
2681 case 1: mUsesAtan2_1 = true; break;
2682 case 2: mUsesAtan2_2 = true; break;
2683 case 3: mUsesAtan2_3 = true; break;
2684 case 4: mUsesAtan2_4 = true; break;
2685 default: UNREACHABLE();
2686 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002687 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002688 break;
2689 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2690 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2691 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2692 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2693 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2694 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2695 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2696 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2697 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002698 case EOpFaceForward:
2699 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002700 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002701 {
2702 case 1: mUsesFaceforward1 = true; break;
2703 case 2: mUsesFaceforward2 = true; break;
2704 case 3: mUsesFaceforward3 = true; break;
2705 case 4: mUsesFaceforward4 = true; break;
2706 default: UNREACHABLE();
2707 }
2708
2709 outputTriplet(visit, "faceforward(", ", ", ")");
2710 }
2711 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002712 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2713 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2714 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002715 default: UNREACHABLE();
2716 }
2717
2718 return true;
2719}
2720
2721bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2722{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002723 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002724
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002725 if (node->usesTernaryOperator())
2726 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002727 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002728 }
2729 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002730 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002731 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002732
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002733 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002734
2735 node->getCondition()->traverse(this);
2736
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002737 out << ")\n";
2738
Jamie Madill075edd82013-07-08 13:30:19 -04002739 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002740 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002741
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002742 bool discard = false;
2743
daniel@transgaming.combb885322010-04-15 20:45:24 +00002744 if (node->getTrueBlock())
2745 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002746 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002747
2748 // Detect true discard
2749 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002750 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002751
Jamie Madill075edd82013-07-08 13:30:19 -04002752 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002753 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002754
2755 if (node->getFalseBlock())
2756 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002757 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002758
Jamie Madill075edd82013-07-08 13:30:19 -04002759 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002760 out << "{\n";
2761
Jamie Madill075edd82013-07-08 13:30:19 -04002762 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002763 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002764
Jamie Madill075edd82013-07-08 13:30:19 -04002765 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002766 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002767
2768 // Detect false discard
2769 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2770 }
2771
2772 // ANGLE issue 486: Detect problematic conditional discard
2773 if (discard && FindSideEffectRewriting::search(node))
2774 {
2775 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002776 }
2777 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002778
2779 return false;
2780}
2781
2782void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2783{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002784 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002785}
2786
2787bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2788{
Nicolas Capens655fe362014-04-11 13:12:34 -04002789 mNestedLoopDepth++;
2790
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002791 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2792
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002793 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002794 {
2795 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2796 }
2797
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002798 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002799 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002800 if (handleExcessiveLoop(node))
2801 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002802 mInsideDiscontinuousLoop = wasDiscontinuous;
2803 mNestedLoopDepth--;
2804
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002805 return false;
2806 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002807 }
2808
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002809 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002810
alokp@chromium.org52813552010-11-16 18:36:09 +00002811 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002812 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002813 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002814
Jamie Madill075edd82013-07-08 13:30:19 -04002815 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002816 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002817 }
2818 else
2819 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002820 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002821
2822 if (node->getInit())
2823 {
2824 node->getInit()->traverse(this);
2825 }
2826
2827 out << "; ";
2828
alokp@chromium.org52813552010-11-16 18:36:09 +00002829 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002830 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002831 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002832 }
2833
2834 out << "; ";
2835
alokp@chromium.org52813552010-11-16 18:36:09 +00002836 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002837 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002838 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002839 }
2840
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002841 out << ")\n";
2842
Jamie Madill075edd82013-07-08 13:30:19 -04002843 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002844 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002845 }
2846
2847 if (node->getBody())
2848 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002849 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002850 }
2851
Jamie Madill075edd82013-07-08 13:30:19 -04002852 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002853 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002854
alokp@chromium.org52813552010-11-16 18:36:09 +00002855 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002856 {
Jamie Madill075edd82013-07-08 13:30:19 -04002857 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002858 out << "while(\n";
2859
alokp@chromium.org52813552010-11-16 18:36:09 +00002860 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002861
daniel@transgaming.com73536982012-03-21 20:45:49 +00002862 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002863 }
2864
daniel@transgaming.com73536982012-03-21 20:45:49 +00002865 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002866
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002867 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002868 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002869
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002870 return false;
2871}
2872
2873bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2874{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002875 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002876
2877 switch (node->getFlowOp())
2878 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002879 case EOpKill:
2880 outputTriplet(visit, "discard;\n", "", "");
2881 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002882 case EOpBreak:
2883 if (visit == PreVisit)
2884 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002885 if (mNestedLoopDepth > 1)
2886 {
2887 mUsesNestedBreak = true;
2888 }
2889
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002890 if (mExcessiveLoopIndex)
2891 {
2892 out << "{Break";
2893 mExcessiveLoopIndex->traverse(this);
2894 out << " = true; break;}\n";
2895 }
2896 else
2897 {
2898 out << "break;\n";
2899 }
2900 }
2901 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002902 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002903 case EOpReturn:
2904 if (visit == PreVisit)
2905 {
2906 if (node->getExpression())
2907 {
2908 out << "return ";
2909 }
2910 else
2911 {
2912 out << "return;\n";
2913 }
2914 }
2915 else if (visit == PostVisit)
2916 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002917 if (node->getExpression())
2918 {
2919 out << ";\n";
2920 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002921 }
2922 break;
2923 default: UNREACHABLE();
2924 }
2925
2926 return true;
2927}
2928
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002929void OutputHLSL::traverseStatements(TIntermNode *node)
2930{
2931 if (isSingleStatement(node))
2932 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002933 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002934 }
2935
2936 node->traverse(this);
2937}
2938
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002939bool OutputHLSL::isSingleStatement(TIntermNode *node)
2940{
2941 TIntermAggregate *aggregate = node->getAsAggregate();
2942
2943 if (aggregate)
2944 {
2945 if (aggregate->getOp() == EOpSequence)
2946 {
2947 return false;
2948 }
2949 else
2950 {
2951 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2952 {
2953 if (!isSingleStatement(*sit))
2954 {
2955 return false;
2956 }
2957 }
2958
2959 return true;
2960 }
2961 }
2962
2963 return true;
2964}
2965
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002966// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2967// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002968bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2969{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002970 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002971 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002972
2973 // Parse loops of the form:
2974 // for(int index = initial; index [comparator] limit; index += increment)
2975 TIntermSymbol *index = NULL;
2976 TOperator comparator = EOpNull;
2977 int initial = 0;
2978 int limit = 0;
2979 int increment = 0;
2980
2981 // Parse index name and intial value
2982 if (node->getInit())
2983 {
2984 TIntermAggregate *init = node->getInit()->getAsAggregate();
2985
2986 if (init)
2987 {
2988 TIntermSequence &sequence = init->getSequence();
2989 TIntermTyped *variable = sequence[0]->getAsTyped();
2990
2991 if (variable && variable->getQualifier() == EvqTemporary)
2992 {
2993 TIntermBinary *assign = variable->getAsBinaryNode();
2994
2995 if (assign->getOp() == EOpInitialize)
2996 {
2997 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2998 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2999
3000 if (symbol && constant)
3001 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003002 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003003 {
3004 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003005 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003006 }
3007 }
3008 }
3009 }
3010 }
3011 }
3012
3013 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00003014 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003015 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003016 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003017
3018 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
3019 {
3020 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3021
3022 if (constant)
3023 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003024 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003025 {
3026 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003027 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003028 }
3029 }
3030 }
3031 }
3032
3033 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00003034 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003035 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003036 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3037 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003038
3039 if (binaryTerminal)
3040 {
3041 TOperator op = binaryTerminal->getOp();
3042 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3043
3044 if (constant)
3045 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003046 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003047 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003048 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003049
3050 switch (op)
3051 {
3052 case EOpAddAssign: increment = value; break;
3053 case EOpSubAssign: increment = -value; break;
3054 default: UNIMPLEMENTED();
3055 }
3056 }
3057 }
3058 }
3059 else if (unaryTerminal)
3060 {
3061 TOperator op = unaryTerminal->getOp();
3062
3063 switch (op)
3064 {
3065 case EOpPostIncrement: increment = 1; break;
3066 case EOpPostDecrement: increment = -1; break;
3067 case EOpPreIncrement: increment = 1; break;
3068 case EOpPreDecrement: increment = -1; break;
3069 default: UNIMPLEMENTED();
3070 }
3071 }
3072 }
3073
3074 if (index != NULL && comparator != EOpNull && increment != 0)
3075 {
3076 if (comparator == EOpLessThanEqual)
3077 {
3078 comparator = EOpLessThan;
3079 limit += 1;
3080 }
3081
3082 if (comparator == EOpLessThan)
3083 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00003084 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003085
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003086 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003087 {
3088 return false; // Not an excessive loop
3089 }
3090
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003091 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
3092 mExcessiveLoopIndex = index;
3093
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003094 out << "{int ";
3095 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003096 out << ";\n"
3097 "bool Break";
3098 index->traverse(this);
3099 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003100
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003101 bool firstLoopFragment = true;
3102
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003103 while (iterations > 0)
3104 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003105 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003106
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003107 if (!firstLoopFragment)
3108 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003109 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003110 index->traverse(this);
3111 out << ") {\n";
3112 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00003113
3114 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
3115 {
3116 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
3117 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003118
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003119 // for(int index = initial; index < clampedLimit; index += increment)
3120
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003121 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003122 index->traverse(this);
3123 out << " = ";
3124 out << initial;
3125
3126 out << "; ";
3127 index->traverse(this);
3128 out << " < ";
3129 out << clampedLimit;
3130
3131 out << "; ";
3132 index->traverse(this);
3133 out << " += ";
3134 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003135 out << ")\n";
3136
Jamie Madill075edd82013-07-08 13:30:19 -04003137 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003138 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003139
3140 if (node->getBody())
3141 {
3142 node->getBody()->traverse(this);
3143 }
3144
Jamie Madill075edd82013-07-08 13:30:19 -04003145 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003146 out << ";}\n";
3147
3148 if (!firstLoopFragment)
3149 {
3150 out << "}\n";
3151 }
3152
3153 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003154
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003155 initial += MAX_LOOP_ITERATIONS * increment;
3156 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003157 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003158
3159 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003160
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003161 mExcessiveLoopIndex = restoreIndex;
3162
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003163 return true;
3164 }
3165 else UNIMPLEMENTED();
3166 }
3167
3168 return false; // Not handled as an excessive loop
3169}
3170
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003171void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003172{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00003173 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003174
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003175 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003176 {
3177 out << preString;
3178 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003179 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003180 {
3181 out << inString;
3182 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003183 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003184 {
3185 out << postString;
3186 }
3187}
3188
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003189void OutputHLSL::outputLineDirective(int line)
3190{
3191 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
3192 {
baustin@google.com8ab69842011-06-02 21:53:45 +00003193 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003194 mBody << "#line " << line;
3195
3196 if (mContext.sourcePath)
3197 {
3198 mBody << " \"" << mContext.sourcePath << "\"";
3199 }
3200
3201 mBody << "\n";
3202 }
3203}
3204
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003205TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3206{
3207 TQualifier qualifier = symbol->getQualifier();
3208 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003209 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003210
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003211 if (name.empty()) // HLSL demands named arguments, also for prototypes
3212 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00003213 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003214 }
3215 else
3216 {
3217 name = decorate(name);
3218 }
3219
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003220 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
3221 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003222 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
3223 qualifierString(qualifier) + " " + samplerString(type) + " sampler_" + name + arrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003224 }
3225
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003226 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003227}
3228
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003229TString OutputHLSL::interpolationString(TQualifier qualifier)
3230{
3231 switch(qualifier)
3232 {
3233 case EvqVaryingIn: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003234 case EvqFragmentIn: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003235 case EvqInvariantVaryingIn: return "";
3236 case EvqSmoothIn: return "linear";
3237 case EvqFlatIn: return "nointerpolation";
3238 case EvqCentroidIn: return "centroid";
3239 case EvqVaryingOut: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003240 case EvqVertexOut: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003241 case EvqInvariantVaryingOut: return "";
3242 case EvqSmoothOut: return "linear";
3243 case EvqFlatOut: return "nointerpolation";
3244 case EvqCentroidOut: return "centroid";
3245 default: UNREACHABLE();
3246 }
3247
3248 return "";
3249}
3250
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003251TString OutputHLSL::qualifierString(TQualifier qualifier)
3252{
3253 switch(qualifier)
3254 {
3255 case EvqIn: return "in";
Geoff Lang5f5320a2014-05-26 13:56:11 -04003256 case EvqOut: return "inout"; // 'out' results in an HLSL error if not all fields are written, for GLSL it's undefined
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003257 case EvqInOut: return "inout";
3258 case EvqConstReadOnly: return "const";
3259 default: UNREACHABLE();
3260 }
3261
3262 return "";
3263}
3264
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003265TString OutputHLSL::typeString(const TType &type)
3266{
Jamie Madill98493dd2013-07-08 14:39:03 -04003267 const TStructure* structure = type.getStruct();
3268 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003269 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003270 const TString& typeName = structure->name();
3271 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003272 {
Jamie Madillbfa91f42014-06-05 15:45:18 -04003273 return structNameString(*type.getStruct());
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003274 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003275 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003276 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003277 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003278 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003279 }
3280 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003281 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003282 int cols = type.getCols();
3283 int rows = type.getRows();
3284 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003285 }
3286 else
3287 {
3288 switch (type.getBasicType())
3289 {
3290 case EbtFloat:
3291 switch (type.getNominalSize())
3292 {
3293 case 1: return "float";
3294 case 2: return "float2";
3295 case 3: return "float3";
3296 case 4: return "float4";
3297 }
3298 case EbtInt:
3299 switch (type.getNominalSize())
3300 {
3301 case 1: return "int";
3302 case 2: return "int2";
3303 case 3: return "int3";
3304 case 4: return "int4";
3305 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003306 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04003307 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003308 {
3309 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003310 case 2: return "uint2";
3311 case 3: return "uint3";
3312 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003313 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003314 case EbtBool:
3315 switch (type.getNominalSize())
3316 {
3317 case 1: return "bool";
3318 case 2: return "bool2";
3319 case 3: return "bool3";
3320 case 4: return "bool4";
3321 }
3322 case EbtVoid:
3323 return "void";
3324 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003325 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04003326 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003327 case EbtSampler2DArray:
3328 case EbtISampler2DArray:
3329 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003330 return "sampler2D";
3331 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003332 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04003333 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003334 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00003335 case EbtSamplerExternalOES:
3336 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00003337 default:
3338 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003339 }
3340 }
3341
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003342 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003343 return "<unknown type>";
3344}
3345
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003346TString OutputHLSL::textureString(const TType &type)
3347{
3348 switch (type.getBasicType())
3349 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003350 case EbtSampler2D: return "Texture2D";
3351 case EbtSamplerCube: return "TextureCube";
3352 case EbtSamplerExternalOES: return "Texture2D";
3353 case EbtSampler2DArray: return "Texture2DArray";
3354 case EbtSampler3D: return "Texture3D";
3355 case EbtISampler2D: return "Texture2D<int4>";
3356 case EbtISampler3D: return "Texture3D<int4>";
Nicolas Capens0027fa92014-02-20 14:26:42 -05003357 case EbtISamplerCube: return "Texture2DArray<int4>";
Nicolas Capenscb127d32013-07-15 17:26:18 -04003358 case EbtISampler2DArray: return "Texture2DArray<int4>";
3359 case EbtUSampler2D: return "Texture2D<uint4>";
3360 case EbtUSampler3D: return "Texture3D<uint4>";
Nicolas Capens0027fa92014-02-20 14:26:42 -05003361 case EbtUSamplerCube: return "Texture2DArray<uint4>";
Nicolas Capenscb127d32013-07-15 17:26:18 -04003362 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
3363 case EbtSampler2DShadow: return "Texture2D";
3364 case EbtSamplerCubeShadow: return "TextureCube";
3365 case EbtSampler2DArrayShadow: return "Texture2DArray";
3366 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003367 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04003368
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003369 return "<unknown texture type>";
3370}
3371
Nicolas Capenscb127d32013-07-15 17:26:18 -04003372TString OutputHLSL::samplerString(const TType &type)
3373{
3374 if (IsShadowSampler(type.getBasicType()))
3375 {
3376 return "SamplerComparisonState";
3377 }
3378 else
3379 {
3380 return "SamplerState";
3381 }
3382}
3383
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003384TString OutputHLSL::arrayString(const TType &type)
3385{
3386 if (!type.isArray())
3387 {
3388 return "";
3389 }
3390
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003391 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003392}
3393
3394TString OutputHLSL::initializer(const TType &type)
3395{
3396 TString string;
3397
Jamie Madill94bf7f22013-07-08 13:31:15 -04003398 size_t size = type.getObjectSize();
3399 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003400 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003401 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003402
Jamie Madill94bf7f22013-07-08 13:31:15 -04003403 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003404 {
3405 string += ", ";
3406 }
3407 }
3408
daniel@transgaming.comead23042010-04-29 03:35:36 +00003409 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003410}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003411
Jamie Madill98493dd2013-07-08 14:39:03 -04003412TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003413{
Jamie Madill98493dd2013-07-08 14:39:03 -04003414 const TFieldList &fields = structure.fields();
3415 const bool isNameless = (structure.name() == "");
3416 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003417 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3418
Jamie Madill98493dd2013-07-08 14:39:03 -04003419 TString string;
3420 string += declareString + "\n"
3421 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003422
Jamie Madill3891fd22014-06-13 10:04:30 -04003423 Std140PaddingHelper padHelper(mStd140StructElementIndexes);
Jamie Madillc835df62013-06-21 09:15:32 -04003424
Jamie Madill9cf6c072013-06-20 11:55:53 -04003425 for (unsigned int i = 0; i < fields.size(); i++)
3426 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003427 const TField &field = *fields[i];
3428 const TType &fieldType = *field.type();
3429 const TStructure *fieldStruct = fieldType.getStruct();
Jamie Madill3891fd22014-06-13 10:04:30 -04003430 const TString &fieldTypeString = fieldStruct ?
3431 structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) :
3432 typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003433
Jamie Madillc835df62013-06-21 09:15:32 -04003434 if (useStd140Packing)
3435 {
Jamie Madill3891fd22014-06-13 10:04:30 -04003436 string += padHelper.prePaddingString(fieldType);
Jamie Madillc835df62013-06-21 09:15:32 -04003437 }
3438
Jamie Madill98493dd2013-07-08 14:39:03 -04003439 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003440
3441 if (useStd140Packing)
3442 {
Jamie Madill3891fd22014-06-13 10:04:30 -04003443 string += padHelper.postPaddingString(fieldType, useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003444 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003445 }
3446
3447 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003448 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003449
Jamie Madill98493dd2013-07-08 14:39:03 -04003450 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003451}
3452
Jamie Madill98493dd2013-07-08 14:39:03 -04003453TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003454{
Jamie Madill98493dd2013-07-08 14:39:03 -04003455 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003456 {
3457 return "";
3458 }
3459
3460 TString prefix = "";
3461
3462 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3463 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003464
3465 if (useStd140Packing)
3466 {
3467 prefix += "std";
3468 }
3469
Jamie Madill9cf6c072013-06-20 11:55:53 -04003470 if (useHLSLRowMajorPacking)
3471 {
Jamie Madillc835df62013-06-21 09:15:32 -04003472 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003473 prefix += "rm";
3474 }
3475
Jamie Madillbfa91f42014-06-05 15:45:18 -04003476 return prefix + structNameString(structure);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003477}
3478
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003479void OutputHLSL::outputConstructor(Visit visit, const TType &type, const TString &name, const TIntermSequence *parameters)
3480{
3481 TInfoSinkBase &out = mBody;
3482
3483 if (visit == PreVisit)
3484 {
3485 addConstructor(type, name, parameters);
3486
3487 out << name + "(";
3488 }
3489 else if (visit == InVisit)
3490 {
3491 out << ", ";
3492 }
3493 else if (visit == PostVisit)
3494 {
3495 out << ")";
3496 }
3497}
3498
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003499void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003500{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003501 if (name == "")
3502 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003503 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003504 }
3505
Jamie Madill96509e42014-05-29 14:33:27 -04003506 if (type.getStruct() && mStructNames.find(name) != mStructNames.end())
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003507 {
3508 return; // Already added
3509 }
3510
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003511 TType ctorType = type;
3512 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003513 ctorType.setPrecision(EbpHigh);
3514 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003515
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003516 typedef std::vector<TType> ParameterArray;
3517 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003518
Jamie Madill98493dd2013-07-08 14:39:03 -04003519 const TStructure* structure = type.getStruct();
3520 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003521 {
Jamie Madill96509e42014-05-29 14:33:27 -04003522 mStructNames.insert(name);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003523
Jamie Madill80ebce52014-06-06 11:54:12 -04003524 // Add element index
3525 storeStd140ElementIndex(*structure, false);
3526 storeStd140ElementIndex(*structure, true);
3527
Jamie Madill98493dd2013-07-08 14:39:03 -04003528 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003529
Jamie Madill98493dd2013-07-08 14:39:03 -04003530 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003531 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003532 // Add row-major packed struct for interface blocks
3533 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003534 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003535 "#pragma pack_matrix(column_major)\n";
3536
Jamie Madill98493dd2013-07-08 14:39:03 -04003537 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003538 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003539 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003540 "#pragma pack_matrix(column_major)\n";
3541
Jamie Madill98493dd2013-07-08 14:39:03 -04003542 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003543 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003544 mStructDeclarations.push_back(std140String);
3545 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003546 }
3547
Jamie Madill98493dd2013-07-08 14:39:03 -04003548 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003549 for (unsigned int i = 0; i < fields.size(); i++)
3550 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003551 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003552 }
3553 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003554 else if (parameters)
3555 {
3556 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3557 {
3558 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3559 }
3560 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003561 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003562
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003563 TString constructor;
3564
3565 if (ctorType.getStruct())
3566 {
Jamie Madill96509e42014-05-29 14:33:27 -04003567 constructor += name + " " + name + "_ctor(";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003568 }
3569 else // Built-in type
3570 {
Jamie Madill96509e42014-05-29 14:33:27 -04003571 constructor += typeString(ctorType) + " " + name + "(";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003572 }
3573
3574 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3575 {
3576 const TType &type = ctorParameters[parameter];
3577
3578 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3579
3580 if (parameter < ctorParameters.size() - 1)
3581 {
3582 constructor += ", ";
3583 }
3584 }
3585
3586 constructor += ")\n"
3587 "{\n";
3588
3589 if (ctorType.getStruct())
3590 {
Jamie Madill96509e42014-05-29 14:33:27 -04003591 constructor += " " + name + " structure = {";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003592 }
3593 else
3594 {
3595 constructor += " return " + typeString(ctorType) + "(";
3596 }
3597
3598 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3599 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003600 int rows = ctorType.getRows();
3601 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003602 const TType &parameter = ctorParameters[0];
3603
3604 if (parameter.isScalar())
3605 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003606 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003607 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003608 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003609 {
3610 constructor += TString((row == col) ? "x0" : "0.0");
3611
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003612 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003613 {
3614 constructor += ", ";
3615 }
3616 }
3617 }
3618 }
3619 else if (parameter.isMatrix())
3620 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003621 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003622 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003623 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003624 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003625 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003626 {
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003627 constructor += TString("x0") + "[" + str(row) + "][" + str(col) + "]";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003628 }
3629 else
3630 {
3631 constructor += TString((row == col) ? "1.0" : "0.0");
3632 }
3633
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003634 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003635 {
3636 constructor += ", ";
3637 }
3638 }
3639 }
3640 }
3641 else UNREACHABLE();
3642 }
3643 else
3644 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003645 size_t remainingComponents = ctorType.getObjectSize();
3646 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003647
3648 while (remainingComponents > 0)
3649 {
3650 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003651 const size_t parameterSize = parameter.getObjectSize();
3652 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003653
3654 constructor += "x" + str(parameterIndex);
3655
Nicolas Capensf7f76162014-06-06 15:48:21 -04003656 if (ctorType.getStruct())
3657 {
3658 ASSERT(remainingComponents == parameterSize || moreParameters);
3659 ASSERT(parameterSize <= remainingComponents);
3660
3661 remainingComponents -= parameterSize;
3662 }
3663 else if (parameter.isScalar())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003664 {
3665 remainingComponents -= parameter.getObjectSize();
3666 }
3667 else if (parameter.isVector())
3668 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003669 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003670 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003671 ASSERT(parameterSize <= remainingComponents);
3672 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003673 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003674 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003675 {
3676 switch (remainingComponents)
3677 {
3678 case 1: constructor += ".x"; break;
3679 case 2: constructor += ".xy"; break;
3680 case 3: constructor += ".xyz"; break;
3681 case 4: constructor += ".xyzw"; break;
3682 default: UNREACHABLE();
3683 }
3684
3685 remainingComponents = 0;
3686 }
3687 else UNREACHABLE();
3688 }
Nicolas Capensf7f76162014-06-06 15:48:21 -04003689 else if (parameter.isMatrix())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003690 {
Nicolas Capensf7f76162014-06-06 15:48:21 -04003691 int column = 0;
3692 while (remainingComponents > 0 && column < parameter.getCols())
3693 {
3694 constructor += "[" + str(column) + "]";
3695
3696 if (remainingComponents < static_cast<size_t>(parameter.getRows()))
3697 {
3698 switch (remainingComponents)
3699 {
3700 case 1: constructor += ".x"; break;
3701 case 2: constructor += ".xy"; break;
3702 case 3: constructor += ".xyz"; break;
3703 default: UNREACHABLE();
3704 }
3705
3706 remainingComponents = 0;
3707 }
3708 else
3709 {
3710 remainingComponents -= parameter.getRows();
3711
3712 if (remainingComponents > 0)
3713 {
3714 constructor += ", x" + str(parameterIndex);
3715 }
3716 }
3717
3718 column++;
3719 }
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003720 }
3721 else UNREACHABLE();
3722
3723 if (moreParameters)
3724 {
3725 parameterIndex++;
3726 }
3727
3728 if (remainingComponents)
3729 {
3730 constructor += ", ";
3731 }
3732 }
3733 }
3734
3735 if (ctorType.getStruct())
3736 {
3737 constructor += "};\n"
3738 " return structure;\n"
3739 "}\n";
3740 }
3741 else
3742 {
3743 constructor += ");\n"
3744 "}\n";
3745 }
3746
daniel@transgaming.com63691862010-04-29 03:32:42 +00003747 mConstructors.insert(constructor);
3748}
3749
Jamie Madill80ebce52014-06-06 11:54:12 -04003750void OutputHLSL::storeStd140ElementIndex(const TStructure &structure, bool useHLSLRowMajorPacking)
3751{
Jamie Madill3891fd22014-06-13 10:04:30 -04003752 Std140PaddingHelper padHelper(mStd140StructElementIndexes);
Jamie Madill80ebce52014-06-06 11:54:12 -04003753 const TFieldList &fields = structure.fields();
3754
3755 for (unsigned int i = 0; i < fields.size(); i++)
3756 {
Jamie Madill3891fd22014-06-13 10:04:30 -04003757 padHelper.prePadding(*fields[i]->type());
Jamie Madill80ebce52014-06-06 11:54:12 -04003758 }
3759
3760 // Add remaining element index to the global map, for use with nested structs in standard layouts
3761 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, true);
Jamie Madill3891fd22014-06-13 10:04:30 -04003762 mStd140StructElementIndexes[structName] = padHelper.elementIndex();
Jamie Madill80ebce52014-06-06 11:54:12 -04003763}
3764
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003765const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3766{
3767 TInfoSinkBase &out = mBody;
3768
Jamie Madill98493dd2013-07-08 14:39:03 -04003769 const TStructure* structure = type.getStruct();
3770 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003771 {
Jamie Madillbfa91f42014-06-05 15:45:18 -04003772 out << structNameString(*structure) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003773
Jamie Madill98493dd2013-07-08 14:39:03 -04003774 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003775
Jamie Madill98493dd2013-07-08 14:39:03 -04003776 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003777 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003778 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003779
3780 constUnion = writeConstantUnion(*fieldType, constUnion);
3781
Jamie Madill98493dd2013-07-08 14:39:03 -04003782 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003783 {
3784 out << ", ";
3785 }
3786 }
3787
3788 out << ")";
3789 }
3790 else
3791 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003792 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003793 bool writeType = size > 1;
3794
3795 if (writeType)
3796 {
3797 out << typeString(type) << "(";
3798 }
3799
Jamie Madill94bf7f22013-07-08 13:31:15 -04003800 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003801 {
3802 switch (constUnion->getType())
3803 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003804 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003805 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003806 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003807 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003808 default: UNREACHABLE();
3809 }
3810
3811 if (i != size - 1)
3812 {
3813 out << ", ";
3814 }
3815 }
3816
3817 if (writeType)
3818 {
3819 out << ")";
3820 }
3821 }
3822
3823 return constUnion;
3824}
3825
Jamie Madillbfa91f42014-06-05 15:45:18 -04003826TString OutputHLSL::structNameString(const TStructure &structure)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003827{
Jamie Madillbfa91f42014-06-05 15:45:18 -04003828 if (structure.name().empty())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003829 {
Jamie Madillbfa91f42014-06-05 15:45:18 -04003830 return "";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003831 }
3832
Jamie Madillbfa91f42014-06-05 15:45:18 -04003833 return "ss_" + str(structure.uniqueId()) + structure.name();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003834}
3835
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003836TString OutputHLSL::decorate(const TString &string)
3837{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003838 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003839 {
3840 return "_" + string;
3841 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003842
3843 return string;
3844}
3845
apatrick@chromium.org65756022012-01-17 21:45:38 +00003846TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003847{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003848 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003849 {
3850 return "ex_" + string;
3851 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003852
3853 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003854}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003855
Jamie Madill98493dd2013-07-08 14:39:03 -04003856TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003857{
Jamie Madill98493dd2013-07-08 14:39:03 -04003858 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003859 {
3860 return decorate(string);
3861 }
3862
3863 return string;
3864}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003865
Jamie Madill834e8b72014-04-11 13:33:58 -04003866void OutputHLSL::declareInterfaceBlockField(const TType &type, const TString &name, std::vector<gl::InterfaceBlockField>& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003867{
Jamie Madill98493dd2013-07-08 14:39:03 -04003868 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003869
3870 if (!structure)
3871 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003872 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill834e8b72014-04-11 13:33:58 -04003873 gl::InterfaceBlockField field(glVariableType(type), glVariablePrecision(type), name.c_str(),
3874 (unsigned int)type.getArraySize(), isRowMajorMatrix);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003875 output.push_back(field);
3876 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003877 else
3878 {
Jamie Madill834e8b72014-04-11 13:33:58 -04003879 gl::InterfaceBlockField structField(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), false);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003880
3881 const TFieldList &fields = structure->fields();
3882
3883 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3884 {
3885 TField *field = fields[fieldIndex];
3886 TType *fieldType = field->type();
3887
3888 // make sure to copy matrix packing information
3889 fieldType->setLayoutQualifier(type.getLayoutQualifier());
3890
3891 declareInterfaceBlockField(*fieldType, field->name(), structField.fields);
3892 }
3893
3894 output.push_back(structField);
3895 }
3896}
3897
Jamie Madill834e8b72014-04-11 13:33:58 -04003898gl::Uniform OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, std::vector<gl::Uniform>& output)
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003899{
3900 const TStructure *structure = type.getStruct();
3901
3902 if (!structure)
3903 {
Jamie Madill834e8b72014-04-11 13:33:58 -04003904 gl::Uniform uniform(glVariableType(type), glVariablePrecision(type), name.c_str(),
3905 (unsigned int)type.getArraySize(), (unsigned int)registerIndex, 0);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003906 output.push_back(uniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003907
3908 return uniform;
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003909 }
3910 else
3911 {
Jamie Madill834e8b72014-04-11 13:33:58 -04003912 gl::Uniform structUniform(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(),
3913 (unsigned int)registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003914
Jamie Madill98493dd2013-07-08 14:39:03 -04003915 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003916
Jamie Madill98493dd2013-07-08 14:39:03 -04003917 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003918 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003919 TField *field = fields[fieldIndex];
3920 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003921
Jamie Madill56093782013-08-30 13:21:11 -04003922 declareUniformToList(*fieldType, field->name(), GL_INVALID_INDEX, structUniform.fields);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003923 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003924
Jamie Madill56093782013-08-30 13:21:11 -04003925 // assign register offset information -- this will override the information in any sub-structures.
Vladimir Vukicevic24d8d672014-05-27 12:07:51 -04003926 HLSLVariableGetRegisterInfo(registerIndex, &structUniform, mOutputType);
Jamie Madill56093782013-08-30 13:21:11 -04003927
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003928 output.push_back(structUniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003929
3930 return structUniform;
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003931 }
3932}
3933
Jamie Madill834e8b72014-04-11 13:33:58 -04003934gl::InterpolationType getInterpolationType(TQualifier qualifier)
Jamie Madill139b9092013-08-30 13:21:06 -04003935{
3936 switch (qualifier)
3937 {
3938 case EvqFlatIn:
3939 case EvqFlatOut:
Jamie Madill834e8b72014-04-11 13:33:58 -04003940 return gl::INTERPOLATION_FLAT;
Jamie Madill139b9092013-08-30 13:21:06 -04003941
3942 case EvqSmoothIn:
3943 case EvqSmoothOut:
3944 case EvqVertexOut:
3945 case EvqFragmentIn:
Jamie Madill384b6042013-09-13 10:06:24 -04003946 case EvqVaryingIn:
3947 case EvqVaryingOut:
Jamie Madill834e8b72014-04-11 13:33:58 -04003948 return gl::INTERPOLATION_SMOOTH;
Jamie Madill139b9092013-08-30 13:21:06 -04003949
3950 case EvqCentroidIn:
3951 case EvqCentroidOut:
Jamie Madill834e8b72014-04-11 13:33:58 -04003952 return gl::INTERPOLATION_CENTROID;
Jamie Madill139b9092013-08-30 13:21:06 -04003953
3954 default: UNREACHABLE();
Jamie Madill834e8b72014-04-11 13:33:58 -04003955 return gl::INTERPOLATION_SMOOTH;
Jamie Madill139b9092013-08-30 13:21:06 -04003956 }
3957}
3958
Jamie Madill834e8b72014-04-11 13:33:58 -04003959void OutputHLSL::declareVaryingToList(const TType &type, TQualifier baseTypeQualifier, const TString &name, std::vector<gl::Varying>& fieldsOut)
Jamie Madill47fdd132013-08-30 13:21:04 -04003960{
3961 const TStructure *structure = type.getStruct();
3962
Jamie Madill834e8b72014-04-11 13:33:58 -04003963 gl::InterpolationType interpolation = getInterpolationType(baseTypeQualifier);
Jamie Madill47fdd132013-08-30 13:21:04 -04003964 if (!structure)
3965 {
Jamie Madill834e8b72014-04-11 13:33:58 -04003966 gl::Varying varying(glVariableType(type), glVariablePrecision(type), name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003967 fieldsOut.push_back(varying);
3968 }
3969 else
3970 {
Jamie Madill834e8b72014-04-11 13:33:58 -04003971 gl::Varying structVarying(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003972 const TFieldList &fields = structure->fields();
3973
Jamie Madill28167c62013-08-30 13:21:10 -04003974 structVarying.structName = structure->name().c_str();
3975
Jamie Madill47fdd132013-08-30 13:21:04 -04003976 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3977 {
3978 const TField &field = *fields[fieldIndex];
Jamie Madill94599662013-08-30 13:21:10 -04003979 declareVaryingToList(*field.type(), baseTypeQualifier, field.name(), structVarying.fields);
Jamie Madill47fdd132013-08-30 13:21:04 -04003980 }
3981
3982 fieldsOut.push_back(structVarying);
3983 }
3984}
3985
Jamie Madillc2141fb2013-08-30 13:21:08 -04003986int OutputHLSL::declareUniformAndAssignRegister(const TType &type, const TString &name)
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003987{
Jamie Madillc2141fb2013-08-30 13:21:08 -04003988 int registerIndex = (IsSampler(type.getBasicType()) ? mSamplerRegister : mUniformRegister);
3989
Jamie Madill834e8b72014-04-11 13:33:58 -04003990 const gl::Uniform &uniform = declareUniformToList(type, name, registerIndex, mActiveUniforms);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003991
3992 if (IsSampler(type.getBasicType()))
3993 {
Vladimir Vukicevic24d8d672014-05-27 12:07:51 -04003994 mSamplerRegister += gl::HLSLVariableRegisterCount(uniform, mOutputType);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003995 }
3996 else
3997 {
Vladimir Vukicevic24d8d672014-05-27 12:07:51 -04003998 mUniformRegister += gl::HLSLVariableRegisterCount(uniform, mOutputType);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003999 }
4000
4001 return registerIndex;
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00004002}
4003
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004004GLenum OutputHLSL::glVariableType(const TType &type)
4005{
4006 if (type.getBasicType() == EbtFloat)
4007 {
4008 if (type.isScalar())
4009 {
4010 return GL_FLOAT;
4011 }
4012 else if (type.isVector())
4013 {
4014 switch(type.getNominalSize())
4015 {
4016 case 2: return GL_FLOAT_VEC2;
4017 case 3: return GL_FLOAT_VEC3;
4018 case 4: return GL_FLOAT_VEC4;
4019 default: UNREACHABLE();
4020 }
4021 }
4022 else if (type.isMatrix())
4023 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00004024 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004025 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00004026 case 2:
4027 switch(type.getRows())
4028 {
4029 case 2: return GL_FLOAT_MAT2;
4030 case 3: return GL_FLOAT_MAT2x3;
4031 case 4: return GL_FLOAT_MAT2x4;
4032 default: UNREACHABLE();
4033 }
4034
4035 case 3:
4036 switch(type.getRows())
4037 {
4038 case 2: return GL_FLOAT_MAT3x2;
4039 case 3: return GL_FLOAT_MAT3;
4040 case 4: return GL_FLOAT_MAT3x4;
4041 default: UNREACHABLE();
4042 }
4043
4044 case 4:
4045 switch(type.getRows())
4046 {
4047 case 2: return GL_FLOAT_MAT4x2;
4048 case 3: return GL_FLOAT_MAT4x3;
4049 case 4: return GL_FLOAT_MAT4;
4050 default: UNREACHABLE();
4051 }
4052
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004053 default: UNREACHABLE();
4054 }
4055 }
4056 else UNREACHABLE();
4057 }
4058 else if (type.getBasicType() == EbtInt)
4059 {
4060 if (type.isScalar())
4061 {
4062 return GL_INT;
4063 }
4064 else if (type.isVector())
4065 {
4066 switch(type.getNominalSize())
4067 {
4068 case 2: return GL_INT_VEC2;
4069 case 3: return GL_INT_VEC3;
4070 case 4: return GL_INT_VEC4;
4071 default: UNREACHABLE();
4072 }
4073 }
4074 else UNREACHABLE();
4075 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004076 else if (type.getBasicType() == EbtUInt)
4077 {
4078 if (type.isScalar())
4079 {
4080 return GL_UNSIGNED_INT;
4081 }
4082 else if (type.isVector())
4083 {
Jamie Madill22d63da2013-06-07 12:45:12 -04004084 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00004085 {
4086 case 2: return GL_UNSIGNED_INT_VEC2;
4087 case 3: return GL_UNSIGNED_INT_VEC3;
4088 case 4: return GL_UNSIGNED_INT_VEC4;
4089 default: UNREACHABLE();
4090 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004091 }
4092 else UNREACHABLE();
4093 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004094 else if (type.getBasicType() == EbtBool)
4095 {
4096 if (type.isScalar())
4097 {
4098 return GL_BOOL;
4099 }
4100 else if (type.isVector())
4101 {
4102 switch(type.getNominalSize())
4103 {
4104 case 2: return GL_BOOL_VEC2;
4105 case 3: return GL_BOOL_VEC3;
4106 case 4: return GL_BOOL_VEC4;
4107 default: UNREACHABLE();
4108 }
4109 }
4110 else UNREACHABLE();
4111 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004112
4113 switch(type.getBasicType())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004114 {
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004115 case EbtSampler2D: return GL_SAMPLER_2D;
4116 case EbtSampler3D: return GL_SAMPLER_3D;
4117 case EbtSamplerCube: return GL_SAMPLER_CUBE;
4118 case EbtSampler2DArray: return GL_SAMPLER_2D_ARRAY;
4119 case EbtISampler2D: return GL_INT_SAMPLER_2D;
4120 case EbtISampler3D: return GL_INT_SAMPLER_3D;
4121 case EbtISamplerCube: return GL_INT_SAMPLER_CUBE;
4122 case EbtISampler2DArray: return GL_INT_SAMPLER_2D_ARRAY;
4123 case EbtUSampler2D: return GL_UNSIGNED_INT_SAMPLER_2D;
4124 case EbtUSampler3D: return GL_UNSIGNED_INT_SAMPLER_3D;
4125 case EbtUSamplerCube: return GL_UNSIGNED_INT_SAMPLER_CUBE;
4126 case EbtUSampler2DArray: return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
4127 case EbtSampler2DShadow: return GL_SAMPLER_2D_SHADOW;
4128 case EbtSamplerCubeShadow: return GL_SAMPLER_CUBE_SHADOW;
4129 case EbtSampler2DArrayShadow: return GL_SAMPLER_2D_ARRAY_SHADOW;
4130 default: UNREACHABLE();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004131 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004132
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004133 return GL_NONE;
4134}
4135
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004136GLenum OutputHLSL::glVariablePrecision(const TType &type)
4137{
4138 if (type.getBasicType() == EbtFloat)
4139 {
4140 switch (type.getPrecision())
4141 {
4142 case EbpHigh: return GL_HIGH_FLOAT;
4143 case EbpMedium: return GL_MEDIUM_FLOAT;
4144 case EbpLow: return GL_LOW_FLOAT;
4145 case EbpUndefined:
4146 // Should be defined as the default precision by the parser
4147 default: UNREACHABLE();
4148 }
4149 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004150 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004151 {
4152 switch (type.getPrecision())
4153 {
4154 case EbpHigh: return GL_HIGH_INT;
4155 case EbpMedium: return GL_MEDIUM_INT;
4156 case EbpLow: return GL_LOW_INT;
4157 case EbpUndefined:
4158 // Should be defined as the default precision by the parser
4159 default: UNREACHABLE();
4160 }
4161 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004162
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00004163 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004164 return GL_NONE;
4165}
4166
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004167bool OutputHLSL::isVaryingOut(TQualifier qualifier)
4168{
4169 switch(qualifier)
4170 {
4171 case EvqVaryingOut:
4172 case EvqInvariantVaryingOut:
4173 case EvqSmoothOut:
4174 case EvqFlatOut:
4175 case EvqCentroidOut:
Jamie Madill19571812013-08-12 15:26:34 -07004176 case EvqVertexOut:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004177 return true;
Jamie Madill10567262014-04-17 16:40:00 -04004178
4179 default: break;
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004180 }
4181
4182 return false;
4183}
4184
4185bool OutputHLSL::isVaryingIn(TQualifier qualifier)
4186{
4187 switch(qualifier)
4188 {
4189 case EvqVaryingIn:
4190 case EvqInvariantVaryingIn:
4191 case EvqSmoothIn:
4192 case EvqFlatIn:
4193 case EvqCentroidIn:
Jamie Madill19571812013-08-12 15:26:34 -07004194 case EvqFragmentIn:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004195 return true;
Jamie Madill10567262014-04-17 16:40:00 -04004196
4197 default: break;
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004198 }
4199
4200 return false;
4201}
4202
4203bool OutputHLSL::isVarying(TQualifier qualifier)
4204{
4205 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
4206}
4207
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004208}