blob: dc79c517ed2e667a4c06b955bafcd4430010ab59 [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 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04002151 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.com67de6d62010-04-29 03:35:30 +00002158 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
2159 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
2160 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
2161 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
2162 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
2163 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
2164 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
2165 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
2166 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
2167 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
2168 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
2169 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
2170 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
2171 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
2172 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
2173 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
2174 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
2175 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
2176 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
2177 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
2178 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002179 case EOpDFdx:
2180 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2181 {
2182 outputTriplet(visit, "(", "", ", 0.0)");
2183 }
2184 else
2185 {
2186 outputTriplet(visit, "ddx(", "", ")");
2187 }
2188 break;
2189 case EOpDFdy:
2190 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2191 {
2192 outputTriplet(visit, "(", "", ", 0.0)");
2193 }
2194 else
2195 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00002196 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002197 }
2198 break;
2199 case EOpFwidth:
2200 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2201 {
2202 outputTriplet(visit, "(", "", ", 0.0)");
2203 }
2204 else
2205 {
2206 outputTriplet(visit, "fwidth(", "", ")");
2207 }
2208 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002209 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
2210 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002211 default: UNREACHABLE();
2212 }
2213
2214 return true;
2215}
2216
2217bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2218{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002219 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002220
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002221 switch (node->getOp())
2222 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002223 case EOpSequence:
2224 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002225 if (mInsideFunction)
2226 {
Jamie Madill075edd82013-07-08 13:30:19 -04002227 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002228 out << "{\n";
2229 }
2230
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002231 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
2232 {
Jamie Madill075edd82013-07-08 13:30:19 -04002233 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002234
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002235 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002236
2237 out << ";\n";
2238 }
2239
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002240 if (mInsideFunction)
2241 {
Jamie Madill075edd82013-07-08 13:30:19 -04002242 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002243 out << "}\n";
2244 }
2245
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002246 return false;
2247 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002248 case EOpDeclaration:
2249 if (visit == PreVisit)
2250 {
2251 TIntermSequence &sequence = node->getSequence();
2252 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002253
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00002254 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002255 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002256 if (variable->getType().getStruct())
2257 {
Jamie Madillbfa91f42014-06-05 15:45:18 -04002258 addConstructor(variable->getType(), structNameString(*variable->getType().getStruct()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00002259 }
2260
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002261 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002262 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00002263 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002264 {
2265 out << "static ";
2266 }
2267
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002268 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002269
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002270 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002271 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002272 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002273
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002274 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002275 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002276 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002277 out << arrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05002278 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002279 }
2280 else
2281 {
2282 (*sit)->traverse(this);
2283 }
2284
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002285 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002286 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002287 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002288 }
2289 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002290 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002291 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2292 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002293 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002294 }
2295 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002296 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002297 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002298 {
2299 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2300 {
2301 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2302
2303 if (symbol)
2304 {
2305 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2306 mReferencedVaryings[symbol->getSymbol()] = symbol;
2307 }
2308 else
2309 {
2310 (*sit)->traverse(this);
2311 }
2312 }
2313 }
2314
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002315 return false;
2316 }
2317 else if (visit == InVisit)
2318 {
2319 out << ", ";
2320 }
2321 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002322 case EOpPrototype:
2323 if (visit == PreVisit)
2324 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002325 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002326
2327 TIntermSequence &arguments = node->getSequence();
2328
2329 for (unsigned int i = 0; i < arguments.size(); i++)
2330 {
2331 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2332
2333 if (symbol)
2334 {
2335 out << argumentString(symbol);
2336
2337 if (i < arguments.size() - 1)
2338 {
2339 out << ", ";
2340 }
2341 }
2342 else UNREACHABLE();
2343 }
2344
2345 out << ");\n";
2346
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002347 // Also prototype the Lod0 variant if needed
2348 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2349 {
2350 mOutputLod0Function = true;
2351 node->traverse(this);
2352 mOutputLod0Function = false;
2353 }
2354
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002355 return false;
2356 }
2357 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002358 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002359 case EOpFunction:
2360 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002361 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002362
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002363 out << typeString(node->getType()) << " ";
2364
2365 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002366 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002367 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002368 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002369 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002370 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002371 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002372 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002373
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002374 TIntermSequence &sequence = node->getSequence();
2375 TIntermSequence &arguments = sequence[0]->getAsAggregate()->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 if (symbol->getType().getStruct())
2384 {
Jamie Madillbfa91f42014-06-05 15:45:18 -04002385 addConstructor(symbol->getType(), structNameString(*symbol->getType().getStruct()), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002386 }
2387
2388 out << argumentString(symbol);
2389
2390 if (i < arguments.size() - 1)
2391 {
2392 out << ", ";
2393 }
2394 }
2395 else UNREACHABLE();
2396 }
2397
2398 out << ")\n"
2399 "{\n";
2400
2401 if (sequence.size() > 1)
2402 {
2403 mInsideFunction = true;
2404 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002405 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002406 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002407
2408 out << "}\n";
2409
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002410 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2411 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002412 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002413 {
2414 mOutputLod0Function = true;
2415 node->traverse(this);
2416 mOutputLod0Function = false;
2417 }
2418 }
2419
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002420 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002421 }
2422 break;
2423 case EOpFunctionCall:
2424 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002425 TString name = TFunction::unmangleName(node->getName());
2426 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002427 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002428
2429 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002430 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002431 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002432 }
2433 else
2434 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002435 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2436
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002437 TextureFunction textureFunction;
2438 textureFunction.sampler = samplerType;
2439 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002440 textureFunction.method = TextureFunction::IMPLICIT;
2441 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002442 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002443
2444 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002445 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002446 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002447 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002448 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002449 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002450 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002451 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002452 }
Nicolas Capens46485082014-04-15 13:12:50 -04002453 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2454 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002455 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002456 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002457 }
Nicolas Capens46485082014-04-15 13:12:50 -04002458 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002459 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002460 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002461 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002462 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002463 else if (name == "textureSize")
2464 {
2465 textureFunction.method = TextureFunction::SIZE;
2466 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002467 else if (name == "textureOffset")
2468 {
2469 textureFunction.method = TextureFunction::IMPLICIT;
2470 textureFunction.offset = true;
2471 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002472 else if (name == "textureProjOffset")
2473 {
2474 textureFunction.method = TextureFunction::IMPLICIT;
2475 textureFunction.offset = true;
2476 textureFunction.proj = true;
2477 }
2478 else if (name == "textureLodOffset")
2479 {
2480 textureFunction.method = TextureFunction::LOD;
2481 textureFunction.offset = true;
2482 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002483 else if (name == "textureProjLodOffset")
2484 {
2485 textureFunction.method = TextureFunction::LOD;
2486 textureFunction.proj = true;
2487 textureFunction.offset = true;
2488 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002489 else if (name == "texelFetch")
2490 {
2491 textureFunction.method = TextureFunction::FETCH;
2492 }
2493 else if (name == "texelFetchOffset")
2494 {
2495 textureFunction.method = TextureFunction::FETCH;
2496 textureFunction.offset = true;
2497 }
Nicolas Capens46485082014-04-15 13:12:50 -04002498 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002499 {
2500 textureFunction.method = TextureFunction::GRAD;
2501 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002502 else if (name == "textureGradOffset")
2503 {
2504 textureFunction.method = TextureFunction::GRAD;
2505 textureFunction.offset = true;
2506 }
Nicolas Capens46485082014-04-15 13:12:50 -04002507 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002508 {
2509 textureFunction.method = TextureFunction::GRAD;
2510 textureFunction.proj = true;
2511 }
2512 else if (name == "textureProjGradOffset")
2513 {
2514 textureFunction.method = TextureFunction::GRAD;
2515 textureFunction.proj = true;
2516 textureFunction.offset = true;
2517 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002518 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002519
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002520 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002521 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002522 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2523
2524 if (textureFunction.offset)
2525 {
2526 mandatoryArgumentCount++;
2527 }
2528
2529 bool bias = (arguments.size() > mandatoryArgumentCount); // Bias argument is optional
2530
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002531 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2532 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002533 if (bias)
2534 {
2535 textureFunction.method = TextureFunction::LOD0BIAS;
2536 }
2537 else
2538 {
2539 textureFunction.method = TextureFunction::LOD0;
2540 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002541 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002542 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002543 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002544 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002545 }
2546 }
2547
2548 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002549
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002550 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002551 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002552
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002553 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2554 {
2555 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2556 {
2557 out << "texture_";
2558 (*arg)->traverse(this);
2559 out << ", sampler_";
2560 }
2561
2562 (*arg)->traverse(this);
2563
2564 if (arg < arguments.end() - 1)
2565 {
2566 out << ", ";
2567 }
2568 }
2569
2570 out << ")";
2571
2572 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002573 }
2574 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002575 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
2576 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", &node->getSequence()); break;
2577 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", &node->getSequence()); break;
2578 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", &node->getSequence()); break;
2579 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", &node->getSequence()); break;
2580 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", &node->getSequence()); break;
2581 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", &node->getSequence()); break;
2582 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", &node->getSequence()); break;
2583 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", &node->getSequence()); break;
2584 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", &node->getSequence()); break;
2585 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", &node->getSequence()); break;
2586 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", &node->getSequence()); break;
2587 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", &node->getSequence()); break;
2588 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", &node->getSequence()); break;
2589 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", &node->getSequence()); break;
2590 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", &node->getSequence()); break;
2591 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", &node->getSequence()); break;
2592 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", &node->getSequence()); break;
2593 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", &node->getSequence()); break;
2594 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", &node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002595 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002596 {
2597 const TString &structName = structNameString(*node->getType().getStruct());
2598 addConstructor(node->getType(), structName, &node->getSequence());
2599 outputTriplet(visit, structName + "_ctor(", ", ", ")");
2600 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002601 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002602 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2603 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2604 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2605 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2606 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2607 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002608 case EOpMod:
2609 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002610 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002611 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2612 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2613 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002614 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002615 case 11: mUsesMod1 = true; break;
2616 case 22: mUsesMod2v = true; break;
2617 case 21: mUsesMod2f = true; break;
2618 case 33: mUsesMod3v = true; break;
2619 case 31: mUsesMod3f = true; break;
2620 case 44: mUsesMod4v = true; break;
2621 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002622 default: UNREACHABLE();
2623 }
2624
2625 outputTriplet(visit, "mod(", ", ", ")");
2626 }
2627 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002628 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002629 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002630 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002631 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2632 {
2633 case 1: mUsesAtan2_1 = true; break;
2634 case 2: mUsesAtan2_2 = true; break;
2635 case 3: mUsesAtan2_3 = true; break;
2636 case 4: mUsesAtan2_4 = true; break;
2637 default: UNREACHABLE();
2638 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002639 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002640 break;
2641 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2642 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2643 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2644 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2645 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2646 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2647 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2648 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2649 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002650 case EOpFaceForward:
2651 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002652 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002653 {
2654 case 1: mUsesFaceforward1 = true; break;
2655 case 2: mUsesFaceforward2 = true; break;
2656 case 3: mUsesFaceforward3 = true; break;
2657 case 4: mUsesFaceforward4 = true; break;
2658 default: UNREACHABLE();
2659 }
2660
2661 outputTriplet(visit, "faceforward(", ", ", ")");
2662 }
2663 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002664 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2665 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2666 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002667 default: UNREACHABLE();
2668 }
2669
2670 return true;
2671}
2672
2673bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2674{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002675 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002676
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002677 if (node->usesTernaryOperator())
2678 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002679 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002680 }
2681 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002682 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002683 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002684
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002685 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002686
2687 node->getCondition()->traverse(this);
2688
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002689 out << ")\n";
2690
Jamie Madill075edd82013-07-08 13:30:19 -04002691 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002692 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002693
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002694 bool discard = false;
2695
daniel@transgaming.combb885322010-04-15 20:45:24 +00002696 if (node->getTrueBlock())
2697 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002698 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002699
2700 // Detect true discard
2701 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002702 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002703
Jamie Madill075edd82013-07-08 13:30:19 -04002704 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002705 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002706
2707 if (node->getFalseBlock())
2708 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002709 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002710
Jamie Madill075edd82013-07-08 13:30:19 -04002711 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002712 out << "{\n";
2713
Jamie Madill075edd82013-07-08 13:30:19 -04002714 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002715 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002716
Jamie Madill075edd82013-07-08 13:30:19 -04002717 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002718 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002719
2720 // Detect false discard
2721 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2722 }
2723
2724 // ANGLE issue 486: Detect problematic conditional discard
2725 if (discard && FindSideEffectRewriting::search(node))
2726 {
2727 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002728 }
2729 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002730
2731 return false;
2732}
2733
2734void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2735{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002736 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002737}
2738
2739bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2740{
Nicolas Capens655fe362014-04-11 13:12:34 -04002741 mNestedLoopDepth++;
2742
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002743 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2744
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002745 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002746 {
2747 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2748 }
2749
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002750 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002751 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002752 if (handleExcessiveLoop(node))
2753 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002754 mInsideDiscontinuousLoop = wasDiscontinuous;
2755 mNestedLoopDepth--;
2756
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002757 return false;
2758 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002759 }
2760
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002761 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002762
alokp@chromium.org52813552010-11-16 18:36:09 +00002763 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002764 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002765 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002766
Jamie Madill075edd82013-07-08 13:30:19 -04002767 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002768 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002769 }
2770 else
2771 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002772 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002773
2774 if (node->getInit())
2775 {
2776 node->getInit()->traverse(this);
2777 }
2778
2779 out << "; ";
2780
alokp@chromium.org52813552010-11-16 18:36:09 +00002781 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002782 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002783 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002784 }
2785
2786 out << "; ";
2787
alokp@chromium.org52813552010-11-16 18:36:09 +00002788 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002789 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002790 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002791 }
2792
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002793 out << ")\n";
2794
Jamie Madill075edd82013-07-08 13:30:19 -04002795 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002796 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002797 }
2798
2799 if (node->getBody())
2800 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002801 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002802 }
2803
Jamie Madill075edd82013-07-08 13:30:19 -04002804 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002805 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002806
alokp@chromium.org52813552010-11-16 18:36:09 +00002807 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002808 {
Jamie Madill075edd82013-07-08 13:30:19 -04002809 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002810 out << "while(\n";
2811
alokp@chromium.org52813552010-11-16 18:36:09 +00002812 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002813
daniel@transgaming.com73536982012-03-21 20:45:49 +00002814 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002815 }
2816
daniel@transgaming.com73536982012-03-21 20:45:49 +00002817 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002818
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002819 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002820 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002821
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002822 return false;
2823}
2824
2825bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2826{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002827 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002828
2829 switch (node->getFlowOp())
2830 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002831 case EOpKill:
2832 outputTriplet(visit, "discard;\n", "", "");
2833 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002834 case EOpBreak:
2835 if (visit == PreVisit)
2836 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002837 if (mNestedLoopDepth > 1)
2838 {
2839 mUsesNestedBreak = true;
2840 }
2841
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002842 if (mExcessiveLoopIndex)
2843 {
2844 out << "{Break";
2845 mExcessiveLoopIndex->traverse(this);
2846 out << " = true; break;}\n";
2847 }
2848 else
2849 {
2850 out << "break;\n";
2851 }
2852 }
2853 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002854 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002855 case EOpReturn:
2856 if (visit == PreVisit)
2857 {
2858 if (node->getExpression())
2859 {
2860 out << "return ";
2861 }
2862 else
2863 {
2864 out << "return;\n";
2865 }
2866 }
2867 else if (visit == PostVisit)
2868 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002869 if (node->getExpression())
2870 {
2871 out << ";\n";
2872 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002873 }
2874 break;
2875 default: UNREACHABLE();
2876 }
2877
2878 return true;
2879}
2880
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002881void OutputHLSL::traverseStatements(TIntermNode *node)
2882{
2883 if (isSingleStatement(node))
2884 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002885 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002886 }
2887
2888 node->traverse(this);
2889}
2890
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002891bool OutputHLSL::isSingleStatement(TIntermNode *node)
2892{
2893 TIntermAggregate *aggregate = node->getAsAggregate();
2894
2895 if (aggregate)
2896 {
2897 if (aggregate->getOp() == EOpSequence)
2898 {
2899 return false;
2900 }
2901 else
2902 {
2903 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2904 {
2905 if (!isSingleStatement(*sit))
2906 {
2907 return false;
2908 }
2909 }
2910
2911 return true;
2912 }
2913 }
2914
2915 return true;
2916}
2917
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002918// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2919// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002920bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2921{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002922 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002923 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002924
2925 // Parse loops of the form:
2926 // for(int index = initial; index [comparator] limit; index += increment)
2927 TIntermSymbol *index = NULL;
2928 TOperator comparator = EOpNull;
2929 int initial = 0;
2930 int limit = 0;
2931 int increment = 0;
2932
2933 // Parse index name and intial value
2934 if (node->getInit())
2935 {
2936 TIntermAggregate *init = node->getInit()->getAsAggregate();
2937
2938 if (init)
2939 {
2940 TIntermSequence &sequence = init->getSequence();
2941 TIntermTyped *variable = sequence[0]->getAsTyped();
2942
2943 if (variable && variable->getQualifier() == EvqTemporary)
2944 {
2945 TIntermBinary *assign = variable->getAsBinaryNode();
2946
2947 if (assign->getOp() == EOpInitialize)
2948 {
2949 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2950 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2951
2952 if (symbol && constant)
2953 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002954 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002955 {
2956 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002957 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002958 }
2959 }
2960 }
2961 }
2962 }
2963 }
2964
2965 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002966 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002967 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002968 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002969
2970 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2971 {
2972 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2973
2974 if (constant)
2975 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002976 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002977 {
2978 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002979 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002980 }
2981 }
2982 }
2983 }
2984
2985 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002986 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002987 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002988 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2989 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002990
2991 if (binaryTerminal)
2992 {
2993 TOperator op = binaryTerminal->getOp();
2994 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2995
2996 if (constant)
2997 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002998 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002999 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003000 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003001
3002 switch (op)
3003 {
3004 case EOpAddAssign: increment = value; break;
3005 case EOpSubAssign: increment = -value; break;
3006 default: UNIMPLEMENTED();
3007 }
3008 }
3009 }
3010 }
3011 else if (unaryTerminal)
3012 {
3013 TOperator op = unaryTerminal->getOp();
3014
3015 switch (op)
3016 {
3017 case EOpPostIncrement: increment = 1; break;
3018 case EOpPostDecrement: increment = -1; break;
3019 case EOpPreIncrement: increment = 1; break;
3020 case EOpPreDecrement: increment = -1; break;
3021 default: UNIMPLEMENTED();
3022 }
3023 }
3024 }
3025
3026 if (index != NULL && comparator != EOpNull && increment != 0)
3027 {
3028 if (comparator == EOpLessThanEqual)
3029 {
3030 comparator = EOpLessThan;
3031 limit += 1;
3032 }
3033
3034 if (comparator == EOpLessThan)
3035 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00003036 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003037
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003038 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003039 {
3040 return false; // Not an excessive loop
3041 }
3042
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003043 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
3044 mExcessiveLoopIndex = index;
3045
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003046 out << "{int ";
3047 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003048 out << ";\n"
3049 "bool Break";
3050 index->traverse(this);
3051 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003052
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003053 bool firstLoopFragment = true;
3054
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003055 while (iterations > 0)
3056 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003057 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003058
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003059 if (!firstLoopFragment)
3060 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003061 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003062 index->traverse(this);
3063 out << ") {\n";
3064 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00003065
3066 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
3067 {
3068 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
3069 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003070
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003071 // for(int index = initial; index < clampedLimit; index += increment)
3072
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003073 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003074 index->traverse(this);
3075 out << " = ";
3076 out << initial;
3077
3078 out << "; ";
3079 index->traverse(this);
3080 out << " < ";
3081 out << clampedLimit;
3082
3083 out << "; ";
3084 index->traverse(this);
3085 out << " += ";
3086 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003087 out << ")\n";
3088
Jamie Madill075edd82013-07-08 13:30:19 -04003089 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003090 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003091
3092 if (node->getBody())
3093 {
3094 node->getBody()->traverse(this);
3095 }
3096
Jamie Madill075edd82013-07-08 13:30:19 -04003097 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003098 out << ";}\n";
3099
3100 if (!firstLoopFragment)
3101 {
3102 out << "}\n";
3103 }
3104
3105 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003106
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003107 initial += MAX_LOOP_ITERATIONS * increment;
3108 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003109 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003110
3111 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003112
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003113 mExcessiveLoopIndex = restoreIndex;
3114
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003115 return true;
3116 }
3117 else UNIMPLEMENTED();
3118 }
3119
3120 return false; // Not handled as an excessive loop
3121}
3122
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003123void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003124{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00003125 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003126
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003127 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003128 {
3129 out << preString;
3130 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003131 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003132 {
3133 out << inString;
3134 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003135 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003136 {
3137 out << postString;
3138 }
3139}
3140
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003141void OutputHLSL::outputLineDirective(int line)
3142{
3143 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
3144 {
baustin@google.com8ab69842011-06-02 21:53:45 +00003145 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003146 mBody << "#line " << line;
3147
3148 if (mContext.sourcePath)
3149 {
3150 mBody << " \"" << mContext.sourcePath << "\"";
3151 }
3152
3153 mBody << "\n";
3154 }
3155}
3156
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003157TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3158{
3159 TQualifier qualifier = symbol->getQualifier();
3160 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003161 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003162
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003163 if (name.empty()) // HLSL demands named arguments, also for prototypes
3164 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00003165 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003166 }
3167 else
3168 {
3169 name = decorate(name);
3170 }
3171
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003172 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
3173 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003174 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
3175 qualifierString(qualifier) + " " + samplerString(type) + " sampler_" + name + arrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003176 }
3177
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003178 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003179}
3180
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003181TString OutputHLSL::interpolationString(TQualifier qualifier)
3182{
3183 switch(qualifier)
3184 {
3185 case EvqVaryingIn: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003186 case EvqFragmentIn: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003187 case EvqInvariantVaryingIn: return "";
3188 case EvqSmoothIn: return "linear";
3189 case EvqFlatIn: return "nointerpolation";
3190 case EvqCentroidIn: return "centroid";
3191 case EvqVaryingOut: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003192 case EvqVertexOut: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003193 case EvqInvariantVaryingOut: return "";
3194 case EvqSmoothOut: return "linear";
3195 case EvqFlatOut: return "nointerpolation";
3196 case EvqCentroidOut: return "centroid";
3197 default: UNREACHABLE();
3198 }
3199
3200 return "";
3201}
3202
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003203TString OutputHLSL::qualifierString(TQualifier qualifier)
3204{
3205 switch(qualifier)
3206 {
3207 case EvqIn: return "in";
Geoff Lang5f5320a2014-05-26 13:56:11 -04003208 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 +00003209 case EvqInOut: return "inout";
3210 case EvqConstReadOnly: return "const";
3211 default: UNREACHABLE();
3212 }
3213
3214 return "";
3215}
3216
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003217TString OutputHLSL::typeString(const TType &type)
3218{
Jamie Madill98493dd2013-07-08 14:39:03 -04003219 const TStructure* structure = type.getStruct();
3220 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003221 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003222 const TString& typeName = structure->name();
3223 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003224 {
Jamie Madillbfa91f42014-06-05 15:45:18 -04003225 return structNameString(*type.getStruct());
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003226 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003227 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003228 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003229 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003230 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003231 }
3232 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003233 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003234 int cols = type.getCols();
3235 int rows = type.getRows();
3236 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003237 }
3238 else
3239 {
3240 switch (type.getBasicType())
3241 {
3242 case EbtFloat:
3243 switch (type.getNominalSize())
3244 {
3245 case 1: return "float";
3246 case 2: return "float2";
3247 case 3: return "float3";
3248 case 4: return "float4";
3249 }
3250 case EbtInt:
3251 switch (type.getNominalSize())
3252 {
3253 case 1: return "int";
3254 case 2: return "int2";
3255 case 3: return "int3";
3256 case 4: return "int4";
3257 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003258 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04003259 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003260 {
3261 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003262 case 2: return "uint2";
3263 case 3: return "uint3";
3264 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003265 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003266 case EbtBool:
3267 switch (type.getNominalSize())
3268 {
3269 case 1: return "bool";
3270 case 2: return "bool2";
3271 case 3: return "bool3";
3272 case 4: return "bool4";
3273 }
3274 case EbtVoid:
3275 return "void";
3276 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003277 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04003278 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003279 case EbtSampler2DArray:
3280 case EbtISampler2DArray:
3281 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003282 return "sampler2D";
3283 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003284 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04003285 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003286 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00003287 case EbtSamplerExternalOES:
3288 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00003289 default:
3290 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003291 }
3292 }
3293
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003294 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003295 return "<unknown type>";
3296}
3297
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003298TString OutputHLSL::textureString(const TType &type)
3299{
3300 switch (type.getBasicType())
3301 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003302 case EbtSampler2D: return "Texture2D";
3303 case EbtSamplerCube: return "TextureCube";
3304 case EbtSamplerExternalOES: return "Texture2D";
3305 case EbtSampler2DArray: return "Texture2DArray";
3306 case EbtSampler3D: return "Texture3D";
3307 case EbtISampler2D: return "Texture2D<int4>";
3308 case EbtISampler3D: return "Texture3D<int4>";
Nicolas Capens0027fa92014-02-20 14:26:42 -05003309 case EbtISamplerCube: return "Texture2DArray<int4>";
Nicolas Capenscb127d32013-07-15 17:26:18 -04003310 case EbtISampler2DArray: return "Texture2DArray<int4>";
3311 case EbtUSampler2D: return "Texture2D<uint4>";
3312 case EbtUSampler3D: return "Texture3D<uint4>";
Nicolas Capens0027fa92014-02-20 14:26:42 -05003313 case EbtUSamplerCube: return "Texture2DArray<uint4>";
Nicolas Capenscb127d32013-07-15 17:26:18 -04003314 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
3315 case EbtSampler2DShadow: return "Texture2D";
3316 case EbtSamplerCubeShadow: return "TextureCube";
3317 case EbtSampler2DArrayShadow: return "Texture2DArray";
3318 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003319 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04003320
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003321 return "<unknown texture type>";
3322}
3323
Nicolas Capenscb127d32013-07-15 17:26:18 -04003324TString OutputHLSL::samplerString(const TType &type)
3325{
3326 if (IsShadowSampler(type.getBasicType()))
3327 {
3328 return "SamplerComparisonState";
3329 }
3330 else
3331 {
3332 return "SamplerState";
3333 }
3334}
3335
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003336TString OutputHLSL::arrayString(const TType &type)
3337{
3338 if (!type.isArray())
3339 {
3340 return "";
3341 }
3342
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003343 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003344}
3345
3346TString OutputHLSL::initializer(const TType &type)
3347{
3348 TString string;
3349
Jamie Madill94bf7f22013-07-08 13:31:15 -04003350 size_t size = type.getObjectSize();
3351 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003352 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003353 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003354
Jamie Madill94bf7f22013-07-08 13:31:15 -04003355 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003356 {
3357 string += ", ";
3358 }
3359 }
3360
daniel@transgaming.comead23042010-04-29 03:35:36 +00003361 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003362}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003363
Jamie Madill98493dd2013-07-08 14:39:03 -04003364TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003365{
Jamie Madill98493dd2013-07-08 14:39:03 -04003366 const TFieldList &fields = structure.fields();
3367 const bool isNameless = (structure.name() == "");
3368 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003369 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3370
Jamie Madill98493dd2013-07-08 14:39:03 -04003371 TString string;
3372 string += declareString + "\n"
3373 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003374
Jamie Madill3891fd22014-06-13 10:04:30 -04003375 Std140PaddingHelper padHelper(mStd140StructElementIndexes);
Jamie Madillc835df62013-06-21 09:15:32 -04003376
Jamie Madill9cf6c072013-06-20 11:55:53 -04003377 for (unsigned int i = 0; i < fields.size(); i++)
3378 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003379 const TField &field = *fields[i];
3380 const TType &fieldType = *field.type();
3381 const TStructure *fieldStruct = fieldType.getStruct();
Jamie Madill3891fd22014-06-13 10:04:30 -04003382 const TString &fieldTypeString = fieldStruct ?
3383 structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) :
3384 typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003385
Jamie Madillc835df62013-06-21 09:15:32 -04003386 if (useStd140Packing)
3387 {
Jamie Madill3891fd22014-06-13 10:04:30 -04003388 string += padHelper.prePaddingString(fieldType);
Jamie Madillc835df62013-06-21 09:15:32 -04003389 }
3390
Jamie Madill98493dd2013-07-08 14:39:03 -04003391 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003392
3393 if (useStd140Packing)
3394 {
Jamie Madill3891fd22014-06-13 10:04:30 -04003395 string += padHelper.postPaddingString(fieldType, useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003396 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003397 }
3398
3399 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003400 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003401
Jamie Madill98493dd2013-07-08 14:39:03 -04003402 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003403}
3404
Jamie Madill98493dd2013-07-08 14:39:03 -04003405TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003406{
Jamie Madill98493dd2013-07-08 14:39:03 -04003407 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003408 {
3409 return "";
3410 }
3411
3412 TString prefix = "";
3413
3414 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3415 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003416
3417 if (useStd140Packing)
3418 {
3419 prefix += "std";
3420 }
3421
Jamie Madill9cf6c072013-06-20 11:55:53 -04003422 if (useHLSLRowMajorPacking)
3423 {
Jamie Madillc835df62013-06-21 09:15:32 -04003424 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003425 prefix += "rm";
3426 }
3427
Jamie Madillbfa91f42014-06-05 15:45:18 -04003428 return prefix + structNameString(structure);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003429}
3430
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003431void OutputHLSL::outputConstructor(Visit visit, const TType &type, const TString &name, const TIntermSequence *parameters)
3432{
3433 TInfoSinkBase &out = mBody;
3434
3435 if (visit == PreVisit)
3436 {
3437 addConstructor(type, name, parameters);
3438
3439 out << name + "(";
3440 }
3441 else if (visit == InVisit)
3442 {
3443 out << ", ";
3444 }
3445 else if (visit == PostVisit)
3446 {
3447 out << ")";
3448 }
3449}
3450
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003451void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003452{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003453 if (name == "")
3454 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003455 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003456 }
3457
Jamie Madill96509e42014-05-29 14:33:27 -04003458 if (type.getStruct() && mStructNames.find(name) != mStructNames.end())
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003459 {
3460 return; // Already added
3461 }
3462
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003463 TType ctorType = type;
3464 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003465 ctorType.setPrecision(EbpHigh);
3466 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003467
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003468 typedef std::vector<TType> ParameterArray;
3469 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003470
Jamie Madill98493dd2013-07-08 14:39:03 -04003471 const TStructure* structure = type.getStruct();
3472 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003473 {
Jamie Madill96509e42014-05-29 14:33:27 -04003474 mStructNames.insert(name);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003475
Jamie Madill80ebce52014-06-06 11:54:12 -04003476 // Add element index
3477 storeStd140ElementIndex(*structure, false);
3478 storeStd140ElementIndex(*structure, true);
3479
Jamie Madill98493dd2013-07-08 14:39:03 -04003480 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003481
Jamie Madill98493dd2013-07-08 14:39:03 -04003482 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003483 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003484 // Add row-major packed struct for interface blocks
3485 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003486 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003487 "#pragma pack_matrix(column_major)\n";
3488
Jamie Madill98493dd2013-07-08 14:39:03 -04003489 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003490 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003491 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003492 "#pragma pack_matrix(column_major)\n";
3493
Jamie Madill98493dd2013-07-08 14:39:03 -04003494 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003495 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003496 mStructDeclarations.push_back(std140String);
3497 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003498 }
3499
Jamie Madill98493dd2013-07-08 14:39:03 -04003500 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003501 for (unsigned int i = 0; i < fields.size(); i++)
3502 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003503 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003504 }
3505 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003506 else if (parameters)
3507 {
3508 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3509 {
3510 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3511 }
3512 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003513 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003514
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003515 TString constructor;
3516
3517 if (ctorType.getStruct())
3518 {
Jamie Madill96509e42014-05-29 14:33:27 -04003519 constructor += name + " " + name + "_ctor(";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003520 }
3521 else // Built-in type
3522 {
Jamie Madill96509e42014-05-29 14:33:27 -04003523 constructor += typeString(ctorType) + " " + name + "(";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003524 }
3525
3526 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3527 {
3528 const TType &type = ctorParameters[parameter];
3529
3530 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3531
3532 if (parameter < ctorParameters.size() - 1)
3533 {
3534 constructor += ", ";
3535 }
3536 }
3537
3538 constructor += ")\n"
3539 "{\n";
3540
3541 if (ctorType.getStruct())
3542 {
Jamie Madill96509e42014-05-29 14:33:27 -04003543 constructor += " " + name + " structure = {";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003544 }
3545 else
3546 {
3547 constructor += " return " + typeString(ctorType) + "(";
3548 }
3549
3550 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3551 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003552 int rows = ctorType.getRows();
3553 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003554 const TType &parameter = ctorParameters[0];
3555
3556 if (parameter.isScalar())
3557 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003558 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003559 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003560 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003561 {
3562 constructor += TString((row == col) ? "x0" : "0.0");
3563
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003564 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003565 {
3566 constructor += ", ";
3567 }
3568 }
3569 }
3570 }
3571 else if (parameter.isMatrix())
3572 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003573 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003574 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003575 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003576 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003577 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003578 {
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003579 constructor += TString("x0") + "[" + str(row) + "][" + str(col) + "]";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003580 }
3581 else
3582 {
3583 constructor += TString((row == col) ? "1.0" : "0.0");
3584 }
3585
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003586 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003587 {
3588 constructor += ", ";
3589 }
3590 }
3591 }
3592 }
Nicolas Capens2a592922014-06-11 16:22:57 -04003593 else
3594 {
3595 ASSERT(rows == 2 && cols == 2 && parameter.isVector() && parameter.getNominalSize() == 4);
3596
3597 constructor += "x0";
3598 }
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003599 }
3600 else
3601 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003602 size_t remainingComponents = ctorType.getObjectSize();
3603 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003604
3605 while (remainingComponents > 0)
3606 {
3607 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003608 const size_t parameterSize = parameter.getObjectSize();
3609 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003610
3611 constructor += "x" + str(parameterIndex);
3612
Nicolas Capensf7f76162014-06-06 15:48:21 -04003613 if (ctorType.getStruct())
3614 {
3615 ASSERT(remainingComponents == parameterSize || moreParameters);
3616 ASSERT(parameterSize <= remainingComponents);
3617
3618 remainingComponents -= parameterSize;
3619 }
3620 else if (parameter.isScalar())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003621 {
3622 remainingComponents -= parameter.getObjectSize();
3623 }
3624 else if (parameter.isVector())
3625 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003626 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003627 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003628 ASSERT(parameterSize <= remainingComponents);
3629 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003630 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003631 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003632 {
3633 switch (remainingComponents)
3634 {
3635 case 1: constructor += ".x"; break;
3636 case 2: constructor += ".xy"; break;
3637 case 3: constructor += ".xyz"; break;
3638 case 4: constructor += ".xyzw"; break;
3639 default: UNREACHABLE();
3640 }
3641
3642 remainingComponents = 0;
3643 }
3644 else UNREACHABLE();
3645 }
Nicolas Capensf7f76162014-06-06 15:48:21 -04003646 else if (parameter.isMatrix())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003647 {
Nicolas Capensf7f76162014-06-06 15:48:21 -04003648 int column = 0;
3649 while (remainingComponents > 0 && column < parameter.getCols())
3650 {
3651 constructor += "[" + str(column) + "]";
3652
3653 if (remainingComponents < static_cast<size_t>(parameter.getRows()))
3654 {
3655 switch (remainingComponents)
3656 {
3657 case 1: constructor += ".x"; break;
3658 case 2: constructor += ".xy"; break;
3659 case 3: constructor += ".xyz"; break;
3660 default: UNREACHABLE();
3661 }
3662
3663 remainingComponents = 0;
3664 }
3665 else
3666 {
3667 remainingComponents -= parameter.getRows();
3668
3669 if (remainingComponents > 0)
3670 {
3671 constructor += ", x" + str(parameterIndex);
3672 }
3673 }
3674
3675 column++;
3676 }
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003677 }
3678 else UNREACHABLE();
3679
3680 if (moreParameters)
3681 {
3682 parameterIndex++;
3683 }
3684
3685 if (remainingComponents)
3686 {
3687 constructor += ", ";
3688 }
3689 }
3690 }
3691
3692 if (ctorType.getStruct())
3693 {
3694 constructor += "};\n"
3695 " return structure;\n"
3696 "}\n";
3697 }
3698 else
3699 {
3700 constructor += ");\n"
3701 "}\n";
3702 }
3703
daniel@transgaming.com63691862010-04-29 03:32:42 +00003704 mConstructors.insert(constructor);
3705}
3706
Jamie Madill80ebce52014-06-06 11:54:12 -04003707void OutputHLSL::storeStd140ElementIndex(const TStructure &structure, bool useHLSLRowMajorPacking)
3708{
Jamie Madill3891fd22014-06-13 10:04:30 -04003709 Std140PaddingHelper padHelper(mStd140StructElementIndexes);
Jamie Madill80ebce52014-06-06 11:54:12 -04003710 const TFieldList &fields = structure.fields();
3711
3712 for (unsigned int i = 0; i < fields.size(); i++)
3713 {
Jamie Madill3891fd22014-06-13 10:04:30 -04003714 padHelper.prePadding(*fields[i]->type());
Jamie Madill80ebce52014-06-06 11:54:12 -04003715 }
3716
3717 // Add remaining element index to the global map, for use with nested structs in standard layouts
3718 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, true);
Jamie Madill3891fd22014-06-13 10:04:30 -04003719 mStd140StructElementIndexes[structName] = padHelper.elementIndex();
Jamie Madill80ebce52014-06-06 11:54:12 -04003720}
3721
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003722const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3723{
3724 TInfoSinkBase &out = mBody;
3725
Jamie Madill98493dd2013-07-08 14:39:03 -04003726 const TStructure* structure = type.getStruct();
3727 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003728 {
Jamie Madillbfa91f42014-06-05 15:45:18 -04003729 out << structNameString(*structure) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003730
Jamie Madill98493dd2013-07-08 14:39:03 -04003731 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003732
Jamie Madill98493dd2013-07-08 14:39:03 -04003733 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003734 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003735 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003736
3737 constUnion = writeConstantUnion(*fieldType, constUnion);
3738
Jamie Madill98493dd2013-07-08 14:39:03 -04003739 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003740 {
3741 out << ", ";
3742 }
3743 }
3744
3745 out << ")";
3746 }
3747 else
3748 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003749 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003750 bool writeType = size > 1;
3751
3752 if (writeType)
3753 {
3754 out << typeString(type) << "(";
3755 }
3756
Jamie Madill94bf7f22013-07-08 13:31:15 -04003757 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003758 {
3759 switch (constUnion->getType())
3760 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003761 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003762 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003763 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003764 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003765 default: UNREACHABLE();
3766 }
3767
3768 if (i != size - 1)
3769 {
3770 out << ", ";
3771 }
3772 }
3773
3774 if (writeType)
3775 {
3776 out << ")";
3777 }
3778 }
3779
3780 return constUnion;
3781}
3782
Jamie Madillbfa91f42014-06-05 15:45:18 -04003783TString OutputHLSL::structNameString(const TStructure &structure)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003784{
Jamie Madillbfa91f42014-06-05 15:45:18 -04003785 if (structure.name().empty())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003786 {
Jamie Madillbfa91f42014-06-05 15:45:18 -04003787 return "";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003788 }
3789
Jamie Madillbfa91f42014-06-05 15:45:18 -04003790 return "ss_" + str(structure.uniqueId()) + structure.name();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003791}
3792
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003793TString OutputHLSL::decorate(const TString &string)
3794{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003795 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003796 {
3797 return "_" + string;
3798 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003799
3800 return string;
3801}
3802
apatrick@chromium.org65756022012-01-17 21:45:38 +00003803TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003804{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003805 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003806 {
3807 return "ex_" + string;
3808 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003809
3810 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003811}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003812
Jamie Madill98493dd2013-07-08 14:39:03 -04003813TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003814{
Jamie Madill98493dd2013-07-08 14:39:03 -04003815 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003816 {
3817 return decorate(string);
3818 }
3819
3820 return string;
3821}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003822
Jamie Madill834e8b72014-04-11 13:33:58 -04003823void OutputHLSL::declareInterfaceBlockField(const TType &type, const TString &name, std::vector<gl::InterfaceBlockField>& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003824{
Jamie Madill98493dd2013-07-08 14:39:03 -04003825 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003826
3827 if (!structure)
3828 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003829 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill834e8b72014-04-11 13:33:58 -04003830 gl::InterfaceBlockField field(glVariableType(type), glVariablePrecision(type), name.c_str(),
3831 (unsigned int)type.getArraySize(), isRowMajorMatrix);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003832 output.push_back(field);
3833 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003834 else
3835 {
Jamie Madill834e8b72014-04-11 13:33:58 -04003836 gl::InterfaceBlockField structField(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), false);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003837
3838 const TFieldList &fields = structure->fields();
3839
3840 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3841 {
3842 TField *field = fields[fieldIndex];
3843 TType *fieldType = field->type();
3844
3845 // make sure to copy matrix packing information
3846 fieldType->setLayoutQualifier(type.getLayoutQualifier());
3847
3848 declareInterfaceBlockField(*fieldType, field->name(), structField.fields);
3849 }
3850
3851 output.push_back(structField);
3852 }
3853}
3854
Jamie Madill834e8b72014-04-11 13:33:58 -04003855gl::Uniform OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, std::vector<gl::Uniform>& output)
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003856{
3857 const TStructure *structure = type.getStruct();
3858
3859 if (!structure)
3860 {
Jamie Madill834e8b72014-04-11 13:33:58 -04003861 gl::Uniform uniform(glVariableType(type), glVariablePrecision(type), name.c_str(),
3862 (unsigned int)type.getArraySize(), (unsigned int)registerIndex, 0);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003863 output.push_back(uniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003864
3865 return uniform;
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003866 }
3867 else
3868 {
Jamie Madill834e8b72014-04-11 13:33:58 -04003869 gl::Uniform structUniform(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(),
3870 (unsigned int)registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003871
Jamie Madill98493dd2013-07-08 14:39:03 -04003872 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003873
Jamie Madill98493dd2013-07-08 14:39:03 -04003874 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003875 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003876 TField *field = fields[fieldIndex];
3877 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003878
Jamie Madill56093782013-08-30 13:21:11 -04003879 declareUniformToList(*fieldType, field->name(), GL_INVALID_INDEX, structUniform.fields);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003880 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003881
Jamie Madill56093782013-08-30 13:21:11 -04003882 // assign register offset information -- this will override the information in any sub-structures.
Vladimir Vukicevic24d8d672014-05-27 12:07:51 -04003883 HLSLVariableGetRegisterInfo(registerIndex, &structUniform, mOutputType);
Jamie Madill56093782013-08-30 13:21:11 -04003884
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003885 output.push_back(structUniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003886
3887 return structUniform;
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003888 }
3889}
3890
Jamie Madill834e8b72014-04-11 13:33:58 -04003891gl::InterpolationType getInterpolationType(TQualifier qualifier)
Jamie Madill139b9092013-08-30 13:21:06 -04003892{
3893 switch (qualifier)
3894 {
3895 case EvqFlatIn:
3896 case EvqFlatOut:
Jamie Madill834e8b72014-04-11 13:33:58 -04003897 return gl::INTERPOLATION_FLAT;
Jamie Madill139b9092013-08-30 13:21:06 -04003898
3899 case EvqSmoothIn:
3900 case EvqSmoothOut:
3901 case EvqVertexOut:
3902 case EvqFragmentIn:
Jamie Madill384b6042013-09-13 10:06:24 -04003903 case EvqVaryingIn:
3904 case EvqVaryingOut:
Jamie Madill834e8b72014-04-11 13:33:58 -04003905 return gl::INTERPOLATION_SMOOTH;
Jamie Madill139b9092013-08-30 13:21:06 -04003906
3907 case EvqCentroidIn:
3908 case EvqCentroidOut:
Jamie Madill834e8b72014-04-11 13:33:58 -04003909 return gl::INTERPOLATION_CENTROID;
Jamie Madill139b9092013-08-30 13:21:06 -04003910
3911 default: UNREACHABLE();
Jamie Madill834e8b72014-04-11 13:33:58 -04003912 return gl::INTERPOLATION_SMOOTH;
Jamie Madill139b9092013-08-30 13:21:06 -04003913 }
3914}
3915
Jamie Madill834e8b72014-04-11 13:33:58 -04003916void OutputHLSL::declareVaryingToList(const TType &type, TQualifier baseTypeQualifier, const TString &name, std::vector<gl::Varying>& fieldsOut)
Jamie Madill47fdd132013-08-30 13:21:04 -04003917{
3918 const TStructure *structure = type.getStruct();
3919
Jamie Madill834e8b72014-04-11 13:33:58 -04003920 gl::InterpolationType interpolation = getInterpolationType(baseTypeQualifier);
Jamie Madill47fdd132013-08-30 13:21:04 -04003921 if (!structure)
3922 {
Jamie Madill834e8b72014-04-11 13:33:58 -04003923 gl::Varying varying(glVariableType(type), glVariablePrecision(type), name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003924 fieldsOut.push_back(varying);
3925 }
3926 else
3927 {
Jamie Madill834e8b72014-04-11 13:33:58 -04003928 gl::Varying structVarying(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003929 const TFieldList &fields = structure->fields();
3930
Jamie Madill28167c62013-08-30 13:21:10 -04003931 structVarying.structName = structure->name().c_str();
3932
Jamie Madill47fdd132013-08-30 13:21:04 -04003933 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3934 {
3935 const TField &field = *fields[fieldIndex];
Jamie Madill94599662013-08-30 13:21:10 -04003936 declareVaryingToList(*field.type(), baseTypeQualifier, field.name(), structVarying.fields);
Jamie Madill47fdd132013-08-30 13:21:04 -04003937 }
3938
3939 fieldsOut.push_back(structVarying);
3940 }
3941}
3942
Jamie Madillc2141fb2013-08-30 13:21:08 -04003943int OutputHLSL::declareUniformAndAssignRegister(const TType &type, const TString &name)
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003944{
Jamie Madillc2141fb2013-08-30 13:21:08 -04003945 int registerIndex = (IsSampler(type.getBasicType()) ? mSamplerRegister : mUniformRegister);
3946
Jamie Madill834e8b72014-04-11 13:33:58 -04003947 const gl::Uniform &uniform = declareUniformToList(type, name, registerIndex, mActiveUniforms);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003948
3949 if (IsSampler(type.getBasicType()))
3950 {
Vladimir Vukicevic24d8d672014-05-27 12:07:51 -04003951 mSamplerRegister += gl::HLSLVariableRegisterCount(uniform, mOutputType);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003952 }
3953 else
3954 {
Vladimir Vukicevic24d8d672014-05-27 12:07:51 -04003955 mUniformRegister += gl::HLSLVariableRegisterCount(uniform, mOutputType);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003956 }
3957
3958 return registerIndex;
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003959}
3960
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003961GLenum OutputHLSL::glVariableType(const TType &type)
3962{
3963 if (type.getBasicType() == EbtFloat)
3964 {
3965 if (type.isScalar())
3966 {
3967 return GL_FLOAT;
3968 }
3969 else if (type.isVector())
3970 {
3971 switch(type.getNominalSize())
3972 {
3973 case 2: return GL_FLOAT_VEC2;
3974 case 3: return GL_FLOAT_VEC3;
3975 case 4: return GL_FLOAT_VEC4;
3976 default: UNREACHABLE();
3977 }
3978 }
3979 else if (type.isMatrix())
3980 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003981 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003982 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003983 case 2:
3984 switch(type.getRows())
3985 {
3986 case 2: return GL_FLOAT_MAT2;
3987 case 3: return GL_FLOAT_MAT2x3;
3988 case 4: return GL_FLOAT_MAT2x4;
3989 default: UNREACHABLE();
3990 }
3991
3992 case 3:
3993 switch(type.getRows())
3994 {
3995 case 2: return GL_FLOAT_MAT3x2;
3996 case 3: return GL_FLOAT_MAT3;
3997 case 4: return GL_FLOAT_MAT3x4;
3998 default: UNREACHABLE();
3999 }
4000
4001 case 4:
4002 switch(type.getRows())
4003 {
4004 case 2: return GL_FLOAT_MAT4x2;
4005 case 3: return GL_FLOAT_MAT4x3;
4006 case 4: return GL_FLOAT_MAT4;
4007 default: UNREACHABLE();
4008 }
4009
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004010 default: UNREACHABLE();
4011 }
4012 }
4013 else UNREACHABLE();
4014 }
4015 else if (type.getBasicType() == EbtInt)
4016 {
4017 if (type.isScalar())
4018 {
4019 return GL_INT;
4020 }
4021 else if (type.isVector())
4022 {
4023 switch(type.getNominalSize())
4024 {
4025 case 2: return GL_INT_VEC2;
4026 case 3: return GL_INT_VEC3;
4027 case 4: return GL_INT_VEC4;
4028 default: UNREACHABLE();
4029 }
4030 }
4031 else UNREACHABLE();
4032 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004033 else if (type.getBasicType() == EbtUInt)
4034 {
4035 if (type.isScalar())
4036 {
4037 return GL_UNSIGNED_INT;
4038 }
4039 else if (type.isVector())
4040 {
Jamie Madill22d63da2013-06-07 12:45:12 -04004041 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00004042 {
4043 case 2: return GL_UNSIGNED_INT_VEC2;
4044 case 3: return GL_UNSIGNED_INT_VEC3;
4045 case 4: return GL_UNSIGNED_INT_VEC4;
4046 default: UNREACHABLE();
4047 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004048 }
4049 else UNREACHABLE();
4050 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004051 else if (type.getBasicType() == EbtBool)
4052 {
4053 if (type.isScalar())
4054 {
4055 return GL_BOOL;
4056 }
4057 else if (type.isVector())
4058 {
4059 switch(type.getNominalSize())
4060 {
4061 case 2: return GL_BOOL_VEC2;
4062 case 3: return GL_BOOL_VEC3;
4063 case 4: return GL_BOOL_VEC4;
4064 default: UNREACHABLE();
4065 }
4066 }
4067 else UNREACHABLE();
4068 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004069
4070 switch(type.getBasicType())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004071 {
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004072 case EbtSampler2D: return GL_SAMPLER_2D;
4073 case EbtSampler3D: return GL_SAMPLER_3D;
4074 case EbtSamplerCube: return GL_SAMPLER_CUBE;
4075 case EbtSampler2DArray: return GL_SAMPLER_2D_ARRAY;
4076 case EbtISampler2D: return GL_INT_SAMPLER_2D;
4077 case EbtISampler3D: return GL_INT_SAMPLER_3D;
4078 case EbtISamplerCube: return GL_INT_SAMPLER_CUBE;
4079 case EbtISampler2DArray: return GL_INT_SAMPLER_2D_ARRAY;
4080 case EbtUSampler2D: return GL_UNSIGNED_INT_SAMPLER_2D;
4081 case EbtUSampler3D: return GL_UNSIGNED_INT_SAMPLER_3D;
4082 case EbtUSamplerCube: return GL_UNSIGNED_INT_SAMPLER_CUBE;
4083 case EbtUSampler2DArray: return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
4084 case EbtSampler2DShadow: return GL_SAMPLER_2D_SHADOW;
4085 case EbtSamplerCubeShadow: return GL_SAMPLER_CUBE_SHADOW;
4086 case EbtSampler2DArrayShadow: return GL_SAMPLER_2D_ARRAY_SHADOW;
4087 default: UNREACHABLE();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004088 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004089
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004090 return GL_NONE;
4091}
4092
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004093GLenum OutputHLSL::glVariablePrecision(const TType &type)
4094{
4095 if (type.getBasicType() == EbtFloat)
4096 {
4097 switch (type.getPrecision())
4098 {
4099 case EbpHigh: return GL_HIGH_FLOAT;
4100 case EbpMedium: return GL_MEDIUM_FLOAT;
4101 case EbpLow: return GL_LOW_FLOAT;
4102 case EbpUndefined:
4103 // Should be defined as the default precision by the parser
4104 default: UNREACHABLE();
4105 }
4106 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004107 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004108 {
4109 switch (type.getPrecision())
4110 {
4111 case EbpHigh: return GL_HIGH_INT;
4112 case EbpMedium: return GL_MEDIUM_INT;
4113 case EbpLow: return GL_LOW_INT;
4114 case EbpUndefined:
4115 // Should be defined as the default precision by the parser
4116 default: UNREACHABLE();
4117 }
4118 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004119
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00004120 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004121 return GL_NONE;
4122}
4123
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004124bool OutputHLSL::isVaryingOut(TQualifier qualifier)
4125{
4126 switch(qualifier)
4127 {
4128 case EvqVaryingOut:
4129 case EvqInvariantVaryingOut:
4130 case EvqSmoothOut:
4131 case EvqFlatOut:
4132 case EvqCentroidOut:
Jamie Madill19571812013-08-12 15:26:34 -07004133 case EvqVertexOut:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004134 return true;
Jamie Madill10567262014-04-17 16:40:00 -04004135
4136 default: break;
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004137 }
4138
4139 return false;
4140}
4141
4142bool OutputHLSL::isVaryingIn(TQualifier qualifier)
4143{
4144 switch(qualifier)
4145 {
4146 case EvqVaryingIn:
4147 case EvqInvariantVaryingIn:
4148 case EvqSmoothIn:
4149 case EvqFlatIn:
4150 case EvqCentroidIn:
Jamie Madill19571812013-08-12 15:26:34 -07004151 case EvqFragmentIn:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004152 return true;
Jamie Madill10567262014-04-17 16:40:00 -04004153
4154 default: break;
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004155 }
4156
4157 return false;
4158}
4159
4160bool OutputHLSL::isVarying(TQualifier qualifier)
4161{
4162 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
4163}
4164
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004165}