blob: 5520367609c03a2825c188a7b928b3f801baec39 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/OutputHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
alokp@chromium.org79fb1012012-04-26 21:07:39 +00009#include "common/angleutils.h"
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +000010#include "common/utilities.h"
Geoff Lang17732822013-08-29 13:46:49 -040011#include "compiler/translator/compilerdebug.h"
12#include "compiler/translator/InfoSink.h"
13#include "compiler/translator/DetectDiscontinuity.h"
14#include "compiler/translator/SearchSymbol.h"
15#include "compiler/translator/UnfoldShortCircuit.h"
16#include "compiler/translator/HLSLLayoutEncoder.h"
17#include "compiler/translator/FlagStd140Structs.h"
Jamie Madill3c9eeb92013-11-04 11:09:26 -050018#include "compiler/translator/NodeSearch.h"
Jamie Madille53c98b2014-02-03 11:57:13 -050019#include "compiler/translator/RewriteElseBlocks.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000020
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000021#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000022#include <cfloat>
23#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000024
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000025namespace sh
26{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000027
Nicolas Capense0ba27a2013-06-24 16:10:52 -040028TString OutputHLSL::TextureFunction::name() const
29{
30 TString name = "gl_texture";
31
Nicolas Capens6d232bb2013-07-08 15:56:38 -040032 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040033 {
34 name += "2D";
35 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040036 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040037 {
38 name += "3D";
39 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040040 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040041 {
42 name += "Cube";
43 }
44 else UNREACHABLE();
45
46 if (proj)
47 {
48 name += "Proj";
49 }
50
Nicolas Capensb1f45b72013-12-19 17:37:19 -050051 if (offset)
52 {
53 name += "Offset";
54 }
55
Nicolas Capens75fb4752013-07-10 15:14:47 -040056 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040057 {
Nicolas Capensfc014542014-02-18 14:47:13 -050058 case IMPLICIT: break;
59 case BIAS: break;
60 case LOD: name += "Lod"; break;
61 case LOD0: name += "Lod0"; break;
62 case SIZE: name += "Size"; break;
63 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -050064 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040065 default: UNREACHABLE();
66 }
67
68 return name + "(";
69}
70
Jamie Madillc2141fb2013-08-30 13:21:08 -040071const char *RegisterPrefix(const TType &type)
72{
73 if (IsSampler(type.getBasicType()))
74 {
75 return "s";
76 }
77 else
78 {
79 return "c";
80 }
81}
82
Nicolas Capense0ba27a2013-06-24 16:10:52 -040083bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
84{
85 if (sampler < rhs.sampler) return true;
86 if (coords < rhs.coords) return true;
87 if (!proj && rhs.proj) return true;
Nicolas Capens75fb4752013-07-10 15:14:47 -040088 if (method < rhs.method) return true;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040089
90 return false;
91}
92
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +000093OutputHLSL::OutputHLSL(TParseContext &context, const ShBuiltInResources& resources, ShShaderOutput outputType)
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +000094 : TIntermTraverser(true, true, true), mContext(context), mOutputType(outputType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000096 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +000097 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000098
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +000099 mUsesFragColor = false;
100 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000101 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000102 mUsesFragCoord = false;
103 mUsesPointCoord = false;
104 mUsesFrontFacing = false;
105 mUsesPointSize = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400106 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000107 mUsesXor = false;
108 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000109 mUsesMod2v = false;
110 mUsesMod2f = false;
111 mUsesMod3v = false;
112 mUsesMod3f = false;
113 mUsesMod4v = false;
114 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000115 mUsesFaceforward1 = false;
116 mUsesFaceforward2 = false;
117 mUsesFaceforward3 = false;
118 mUsesFaceforward4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000119 mUsesAtan2_1 = false;
120 mUsesAtan2_2 = false;
121 mUsesAtan2_3 = false;
122 mUsesAtan2_4 = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500123 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400124 mUsesNestedBreak = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000125
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000126 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
127
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000128 mScopeDepth = 0;
129
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000130 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000131
132 mContainsLoopDiscontinuity = false;
133 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000134 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400135 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000136
137 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000138
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000139 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000140 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000141 if (mContext.shaderType == SH_FRAGMENT_SHADER)
142 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000143 mUniformRegister = 3; // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000144 }
145 else
146 {
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000147 mUniformRegister = 2; // Reserve registers for dx_DepthRange and dx_ViewAdjust
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000148 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000149 }
150 else
151 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000152 mUniformRegister = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000153 }
154
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000155 mSamplerRegister = 0;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000156 mInterfaceBlockRegister = 2; // Reserve registers for the default uniform block and driver constants
Jamie Madill574d9dd2013-06-20 11:55:56 -0400157 mPaddingCounter = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000158}
159
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000160OutputHLSL::~OutputHLSL()
161{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000162 delete mUnfoldShortCircuit;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000163}
164
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000165void OutputHLSL::output()
166{
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +0000167 mContainsLoopDiscontinuity = mContext.shaderType == SH_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400168 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
169 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000170
Jamie Madille53c98b2014-02-03 11:57:13 -0500171 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
172 // use a vertex attribute as a condition, and some related computation in the else block.
173 if (mOutputType == SH_HLSL9_OUTPUT && mContext.shaderType == SH_VERTEX_SHADER)
174 {
175 RewriteElseBlocks(mContext.treeRoot);
176 }
177
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000178 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 +0000179 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000180
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000181 mContext.infoSink().obj << mHeader.c_str();
182 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000183}
184
Jamie Madill570e04d2013-06-21 09:15:33 -0400185void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
186{
187 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
188 {
189 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
190
191 // This will mark the necessary block elements as referenced
192 flaggedNode->traverse(this);
193 TString structName(mBody.c_str());
194 mBody.erase();
195
196 mFlaggedStructOriginalNames[flaggedNode] = structName;
197
198 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
199 {
200 structName.erase(pos, 1);
201 }
202
203 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
204 }
205}
206
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000207TInfoSinkBase &OutputHLSL::getBodyStream()
208{
209 return mBody;
210}
211
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400212const std::vector<Uniform> &OutputHLSL::getUniforms()
daniel@transgaming.com043da132012-12-20 21:12:22 +0000213{
214 return mActiveUniforms;
215}
216
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000217const ActiveInterfaceBlocks &OutputHLSL::getInterfaceBlocks() const
218{
219 return mActiveInterfaceBlocks;
220}
221
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400222const std::vector<Attribute> &OutputHLSL::getOutputVariables() const
Jamie Madill46131a32013-06-20 11:55:50 -0400223{
224 return mActiveOutputVariables;
225}
226
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400227const std::vector<Attribute> &OutputHLSL::getAttributes() const
Jamie Madilldefb6742013-06-20 11:55:51 -0400228{
229 return mActiveAttributes;
230}
231
Jamie Madill47fdd132013-08-30 13:21:04 -0400232const std::vector<Varying> &OutputHLSL::getVaryings() const
233{
234 return mActiveVaryings;
235}
236
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000237int OutputHLSL::vectorSize(const TType &type) const
238{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000239 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000240 int arraySize = type.isArray() ? type.getArraySize() : 1;
241
242 return elementSize * arraySize;
243}
244
Jamie Madill98493dd2013-07-08 14:39:03 -0400245TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, const TField &field)
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000246{
Jamie Madill98493dd2013-07-08 14:39:03 -0400247 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000248 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400249 return interfaceBlock.name() + "." + field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000250 }
251 else
252 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400253 return field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000254 }
255}
256
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000257TString OutputHLSL::decoratePrivate(const TString &privateText)
258{
259 return "dx_" + privateText;
260}
261
Jamie Madill98493dd2013-07-08 14:39:03 -0400262TString OutputHLSL::interfaceBlockStructNameString(const TInterfaceBlock &interfaceBlock)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000263{
Jamie Madill98493dd2013-07-08 14:39:03 -0400264 return decoratePrivate(interfaceBlock.name()) + "_type";
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000265}
266
Jamie Madill98493dd2013-07-08 14:39:03 -0400267TString OutputHLSL::interfaceBlockInstanceString(const TInterfaceBlock& interfaceBlock, unsigned int arrayIndex)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000268{
Jamie Madill98493dd2013-07-08 14:39:03 -0400269 if (!interfaceBlock.hasInstanceName())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000270 {
271 return "";
272 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400273 else if (interfaceBlock.isArray())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000274 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400275 return decoratePrivate(interfaceBlock.instanceName()) + "_" + str(arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000276 }
277 else
278 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400279 return decorate(interfaceBlock.instanceName());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000280 }
281}
282
Jamie Madill98493dd2013-07-08 14:39:03 -0400283TString OutputHLSL::interfaceBlockFieldTypeString(const TField &field, TLayoutBlockStorage blockStorage)
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000284{
Jamie Madill98493dd2013-07-08 14:39:03 -0400285 const TType &fieldType = *field.type();
286 const TLayoutMatrixPacking matrixPacking = fieldType.getLayoutQualifier().matrixPacking;
Jamie Madill529077d2013-06-20 11:55:54 -0400287 ASSERT(matrixPacking != EmpUnspecified);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000288
Jamie Madill98493dd2013-07-08 14:39:03 -0400289 if (fieldType.isMatrix())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000290 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400291 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill529077d2013-06-20 11:55:54 -0400292 const TString &matrixPackString = (matrixPacking == EmpRowMajor ? "column_major" : "row_major");
Jamie Madill98493dd2013-07-08 14:39:03 -0400293 return matrixPackString + " " + typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000294 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400295 else if (fieldType.getStruct())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000296 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400297 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill98493dd2013-07-08 14:39:03 -0400298 return structureTypeName(*fieldType.getStruct(), matrixPacking == EmpColumnMajor, blockStorage == EbsStd140);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000299 }
300 else
301 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400302 return typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000303 }
304}
305
Jamie Madill98493dd2013-07-08 14:39:03 -0400306TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, TLayoutBlockStorage blockStorage)
307{
308 TString hlsl;
309
310 int elementIndex = 0;
311
312 for (unsigned int typeIndex = 0; typeIndex < interfaceBlock.fields().size(); typeIndex++)
313 {
314 const TField &field = *interfaceBlock.fields()[typeIndex];
315 const TType &fieldType = *field.type();
316
317 if (blockStorage == EbsStd140)
318 {
319 // 2 and 3 component vector types in some cases need pre-padding
320 hlsl += std140PrePaddingString(fieldType, &elementIndex);
321 }
322
323 hlsl += " " + interfaceBlockFieldTypeString(field, blockStorage) +
324 " " + decorate(field.name()) + arrayString(fieldType) + ";\n";
325
326 // must pad out after matrices and arrays, where HLSL usually allows itself room to pack stuff
327 if (blockStorage == EbsStd140)
328 {
329 const bool useHLSLRowMajorPacking = (fieldType.getLayoutQualifier().matrixPacking == EmpColumnMajor);
330 hlsl += std140PostPaddingString(fieldType, useHLSLRowMajorPacking);
331 }
332 }
333
334 return hlsl;
335}
336
337TString OutputHLSL::interfaceBlockStructString(const TInterfaceBlock &interfaceBlock)
338{
339 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
340
341 return "struct " + interfaceBlockStructNameString(interfaceBlock) + "\n"
342 "{\n" +
343 interfaceBlockFieldString(interfaceBlock, blockStorage) +
344 "};\n\n";
345}
346
347TString OutputHLSL::interfaceBlockString(const TInterfaceBlock &interfaceBlock, unsigned int registerIndex, unsigned int arrayIndex)
348{
349 const TString &arrayIndexString = (arrayIndex != GL_INVALID_INDEX ? decorate(str(arrayIndex)) : "");
350 const TString &blockName = interfaceBlock.name() + arrayIndexString;
351 TString hlsl;
352
353 hlsl += "cbuffer " + blockName + " : register(b" + str(registerIndex) + ")\n"
354 "{\n";
355
356 if (interfaceBlock.hasInstanceName())
357 {
358 hlsl += " " + interfaceBlockStructNameString(interfaceBlock) + " " + interfaceBlockInstanceString(interfaceBlock, arrayIndex) + ";\n";
359 }
360 else
361 {
362 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
363 hlsl += interfaceBlockFieldString(interfaceBlock, blockStorage);
364 }
365
366 hlsl += "};\n\n";
367
368 return hlsl;
369}
370
Jamie Madill574d9dd2013-06-20 11:55:56 -0400371TString OutputHLSL::std140PrePaddingString(const TType &type, int *elementIndex)
372{
373 if (type.getBasicType() == EbtStruct || type.isMatrix() || type.isArray())
374 {
375 // no padding needed, HLSL will align the field to a new register
376 *elementIndex = 0;
377 return "";
378 }
379
380 const GLenum glType = glVariableType(type);
381 const int numComponents = gl::UniformComponentCount(glType);
382
383 if (numComponents >= 4)
384 {
385 // no padding needed, HLSL will align the field to a new register
386 *elementIndex = 0;
387 return "";
388 }
389
390 if (*elementIndex + numComponents > 4)
391 {
392 // no padding needed, HLSL will align the field to a new register
393 *elementIndex = numComponents;
394 return "";
395 }
396
397 TString padding;
398
399 const int alignment = numComponents == 3 ? 4 : numComponents;
400 const int paddingOffset = (*elementIndex % alignment);
401
402 if (paddingOffset != 0)
403 {
404 // padding is neccessary
405 for (int paddingIndex = paddingOffset; paddingIndex < alignment; paddingIndex++)
406 {
407 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
408 }
409
410 *elementIndex += (alignment - paddingOffset);
411 }
412
413 *elementIndex += numComponents;
414 *elementIndex %= 4;
415
416 return padding;
417}
418
Jamie Madille4075c92013-06-21 09:15:32 -0400419TString OutputHLSL::std140PostPaddingString(const TType &type, bool useHLSLRowMajorPacking)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400420{
Jamie Madillc835df62013-06-21 09:15:32 -0400421 if (!type.isMatrix() && !type.isArray() && type.getBasicType() != EbtStruct)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400422 {
423 return "";
424 }
425
Jamie Madill574d9dd2013-06-20 11:55:56 -0400426 int numComponents = 0;
427
428 if (type.isMatrix())
429 {
Jamie Madille4075c92013-06-21 09:15:32 -0400430 // This method can also be called from structureString, which does not use layout qualifiers.
431 // Thus, use the method parameter for determining the matrix packing.
432 //
433 // Note HLSL row major packing corresponds to GL API column-major, and vice-versa, since we
434 // wish to always transpose GL matrices to play well with HLSL's matrix array indexing.
435 //
436 const bool isRowMajorMatrix = !useHLSLRowMajorPacking;
Jamie Madillc835df62013-06-21 09:15:32 -0400437 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400438 numComponents = gl::MatrixComponentCount(glType, isRowMajorMatrix);
439 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400440 else if (type.getStruct())
Jamie Madillc835df62013-06-21 09:15:32 -0400441 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400442 const TString &structName = structureTypeName(*type.getStruct(), useHLSLRowMajorPacking, true);
Jamie Madille4075c92013-06-21 09:15:32 -0400443 numComponents = mStd140StructElementIndexes[structName];
444
445 if (numComponents == 0)
446 {
447 return "";
448 }
Jamie Madillc835df62013-06-21 09:15:32 -0400449 }
Jamie Madill574d9dd2013-06-20 11:55:56 -0400450 else
451 {
Jamie Madillc835df62013-06-21 09:15:32 -0400452 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400453 numComponents = gl::UniformComponentCount(glType);
454 }
455
456 TString padding;
457 for (int paddingOffset = numComponents; paddingOffset < 4; paddingOffset++)
458 {
459 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
460 }
461 return padding;
462}
463
Jamie Madill440dc742013-06-20 11:55:55 -0400464// Use the same layout for packed and shared
465void setBlockLayout(InterfaceBlock *interfaceBlock, BlockLayoutType newLayout)
466{
467 interfaceBlock->layout = newLayout;
468 interfaceBlock->blockInfo.clear();
469
470 switch (newLayout)
471 {
472 case BLOCKLAYOUT_SHARED:
473 case BLOCKLAYOUT_PACKED:
474 {
475 HLSLBlockEncoder hlslEncoder(&interfaceBlock->blockInfo);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400476 hlslEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400477 interfaceBlock->dataSize = hlslEncoder.getBlockSize();
478 }
479 break;
480
481 case BLOCKLAYOUT_STANDARD:
482 {
483 Std140BlockEncoder stdEncoder(&interfaceBlock->blockInfo);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400484 stdEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400485 interfaceBlock->dataSize = stdEncoder.getBlockSize();
486 }
487 break;
488
489 default:
490 UNREACHABLE();
491 break;
492 }
493}
494
Jamie Madill574d9dd2013-06-20 11:55:56 -0400495BlockLayoutType convertBlockLayoutType(TLayoutBlockStorage blockStorage)
496{
497 switch (blockStorage)
498 {
499 case EbsPacked: return BLOCKLAYOUT_PACKED;
500 case EbsShared: return BLOCKLAYOUT_SHARED;
501 case EbsStd140: return BLOCKLAYOUT_STANDARD;
502 default: UNREACHABLE(); return BLOCKLAYOUT_SHARED;
503 }
504}
505
Jamie Madill98493dd2013-07-08 14:39:03 -0400506TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400507{
508 TString init;
509
510 TString preIndentString;
511 TString fullIndentString;
512
513 for (int spaces = 0; spaces < (indent * 4); spaces++)
514 {
515 preIndentString += ' ';
516 }
517
518 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
519 {
520 fullIndentString += ' ';
521 }
522
523 init += preIndentString + "{\n";
524
Jamie Madill98493dd2013-07-08 14:39:03 -0400525 const TFieldList &fields = structure.fields();
526 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400527 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400528 const TField &field = *fields[fieldIndex];
529 const TString &fieldName = rhsStructName + "." + decorate(field.name());
530 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400531
Jamie Madill98493dd2013-07-08 14:39:03 -0400532 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400533 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400534 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400535 }
536 else
537 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400538 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400539 }
540 }
541
542 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
543
544 return init;
545}
546
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000547void OutputHLSL::header()
548{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000549 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000550
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000551 TString uniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000552 TString interfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000553 TString varyings;
554 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400555 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000556
Jamie Madillc2141fb2013-08-30 13:21:08 -0400557 for (ReferencedSymbols::const_iterator uniformIt = mReferencedUniforms.begin(); uniformIt != mReferencedUniforms.end(); uniformIt++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000558 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400559 const TIntermSymbol &uniform = *uniformIt->second;
560 const TType &type = uniform.getType();
561 const TString &name = uniform.getSymbol();
562
563 int registerIndex = declareUniformAndAssignRegister(type, name);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000564
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000565 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture
566 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400567 uniforms += "uniform " + samplerString(type) + " sampler_" + decorateUniform(name, type) + arrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400568 " : register(s" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000569
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000570 uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400571 " : register(t" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000572 }
573 else
574 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400575 const TStructure *structure = type.getStruct();
576 const TString &typeName = (structure ? structureTypeName(*structure, false, false) : typeString(type));
577
578 const TString &registerString = TString("register(") + RegisterPrefix(type) + str(registerIndex) + ")";
579
580 uniforms += "uniform " + typeName + " " + decorateUniform(name, type) + arrayString(type) + " : " + registerString + ";\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000581 }
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000582 }
583
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000584 for (ReferencedSymbols::const_iterator interfaceBlockIt = mReferencedInterfaceBlocks.begin(); interfaceBlockIt != mReferencedInterfaceBlocks.end(); interfaceBlockIt++)
585 {
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000586 const TType &nodeType = interfaceBlockIt->second->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -0400587 const TInterfaceBlock &interfaceBlock = *nodeType.getInterfaceBlock();
588 const TFieldList &fieldList = interfaceBlock.fields();
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000589
Jamie Madill98493dd2013-07-08 14:39:03 -0400590 unsigned int arraySize = static_cast<unsigned int>(interfaceBlock.arraySize());
591 sh::InterfaceBlock activeBlock(interfaceBlock.name().c_str(), arraySize, mInterfaceBlockRegister);
592 for (unsigned int typeIndex = 0; typeIndex < fieldList.size(); typeIndex++)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000593 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400594 const TField &field = *fieldList[typeIndex];
595 const TString &fullUniformName = interfaceBlockFieldString(interfaceBlock, field);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400596 declareInterfaceBlockField(*field.type(), fullUniformName, activeBlock.fields);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000597 }
598
Jamie Madill98493dd2013-07-08 14:39:03 -0400599 mInterfaceBlockRegister += std::max(1u, arraySize);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000600
Jamie Madill98493dd2013-07-08 14:39:03 -0400601 BlockLayoutType blockLayoutType = convertBlockLayoutType(interfaceBlock.blockStorage());
602 setBlockLayout(&activeBlock, blockLayoutType);
Jamie Madill9060a4e2013-08-12 16:22:57 -0700603
604 if (interfaceBlock.matrixPacking() == EmpRowMajor)
605 {
606 activeBlock.isRowMajorLayout = true;
607 }
608
Jamie Madill98493dd2013-07-08 14:39:03 -0400609 mActiveInterfaceBlocks.push_back(activeBlock);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000610
Jamie Madill98493dd2013-07-08 14:39:03 -0400611 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000612 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400613 interfaceBlocks += interfaceBlockStructString(interfaceBlock);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000614 }
615
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000616 if (arraySize > 0)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000617 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000618 for (unsigned int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
619 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400620 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex + arrayIndex, arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000621 }
622 }
623 else
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000624 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400625 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000626 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000627 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000628
Jamie Madill829f59e2013-11-13 19:40:54 -0500629 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400630 {
631 TIntermTyped *structNode = flaggedStructIt->first;
632 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400633 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400634 const TString &originalName = mFlaggedStructOriginalNames[structNode];
635
Jamie Madill98493dd2013-07-08 14:39:03 -0400636 flaggedStructs += "static " + decorate(structure.name()) + " " + mappedName + " =\n";
637 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400638 flaggedStructs += "\n";
639 }
640
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000641 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
642 {
643 const TType &type = varying->second->getType();
644 const TString &name = varying->second->getSymbol();
645
646 // Program linking depends on this exact format
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +0000647 varyings += "static " + interpolationString(type.getQualifier()) + " " + typeString(type) + " " +
648 decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madill47fdd132013-08-30 13:21:04 -0400649
Jamie Madill94599662013-08-30 13:21:10 -0400650 declareVaryingToList(type, type.getQualifier(), name, mActiveVaryings);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000651 }
652
653 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
654 {
655 const TType &type = attribute->second->getType();
656 const TString &name = attribute->second->getSymbol();
657
658 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400659
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400660 Attribute attributeVar(glVariableType(type), glVariablePrecision(type), name.c_str(),
661 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
662 mActiveAttributes.push_back(attributeVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000663 }
664
Jamie Madill529077d2013-06-20 11:55:54 -0400665 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
666 {
667 out << *structDeclaration;
668 }
669
670 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
671 {
672 out << *constructor;
673 }
674
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500675 if (mUsesDiscardRewriting)
676 {
677 out << "#define ANGLE_USES_DISCARD_REWRITING" << "\n";
678 }
679
Nicolas Capens655fe362014-04-11 13:12:34 -0400680 if (mUsesNestedBreak)
681 {
682 out << "#define ANGLE_USES_NESTED_BREAK" << "\n";
683 }
684
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400685 if (mContext.shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000686 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000687 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000688 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000689
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000690 out << "// Varyings\n";
691 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400692 out << "\n";
693
694 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000695 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500696 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000697 {
Jamie Madill46131a32013-06-20 11:55:50 -0400698 const TString &variableName = outputVariableIt->first;
699 const TType &variableType = outputVariableIt->second->getType();
700 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
701
702 out << "static " + typeString(variableType) + " out_" + variableName + arrayString(variableType) +
703 " = " + initializer(variableType) + ";\n";
704
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400705 Attribute outputVar(glVariableType(variableType), glVariablePrecision(variableType), variableName.c_str(),
706 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
Jamie Madill46131a32013-06-20 11:55:50 -0400707 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000708 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000709 }
Jamie Madill46131a32013-06-20 11:55:50 -0400710 else
711 {
712 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
713
714 out << "static float4 gl_Color[" << numColorValues << "] =\n"
715 "{\n";
716 for (unsigned int i = 0; i < numColorValues; i++)
717 {
718 out << " float4(0, 0, 0, 0)";
719 if (i + 1 != numColorValues)
720 {
721 out << ",";
722 }
723 out << "\n";
724 }
725
726 out << "};\n";
727 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000728
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400729 if (mUsesFragDepth)
730 {
731 out << "static float gl_Depth = 0.0;\n";
732 }
733
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000734 if (mUsesFragCoord)
735 {
736 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
737 }
738
739 if (mUsesPointCoord)
740 {
741 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
742 }
743
744 if (mUsesFrontFacing)
745 {
746 out << "static bool gl_FrontFacing = false;\n";
747 }
748
749 out << "\n";
750
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000751 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000752 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000753 out << "struct gl_DepthRangeParameters\n"
754 "{\n"
755 " float near;\n"
756 " float far;\n"
757 " float diff;\n"
758 "};\n"
759 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000760 }
761
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000762 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000763 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000764 out << "cbuffer DriverConstants : register(b1)\n"
765 "{\n";
766
767 if (mUsesDepthRange)
768 {
769 out << " float3 dx_DepthRange : packoffset(c0);\n";
770 }
771
772 if (mUsesFragCoord)
773 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000774 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000775 }
776
777 if (mUsesFragCoord || mUsesFrontFacing)
778 {
779 out << " float3 dx_DepthFront : packoffset(c2);\n";
780 }
781
782 out << "};\n";
783 }
784 else
785 {
786 if (mUsesDepthRange)
787 {
788 out << "uniform float3 dx_DepthRange : register(c0);";
789 }
790
791 if (mUsesFragCoord)
792 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000793 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000794 }
795
796 if (mUsesFragCoord || mUsesFrontFacing)
797 {
798 out << "uniform float3 dx_DepthFront : register(c2);\n";
799 }
800 }
801
802 out << "\n";
803
804 if (mUsesDepthRange)
805 {
806 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
807 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000808 }
809
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000811 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000812
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000813 if (!interfaceBlocks.empty())
814 {
815 out << interfaceBlocks;
816 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400817
818 if (!flaggedStructs.empty())
819 {
820 out << "// Std140 Structures accessed by value\n";
821 out << "\n";
822 out << flaggedStructs;
823 out << "\n";
824 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000825 }
826
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000827 if (usingMRTExtension && mNumRenderTargets > 1)
828 {
829 out << "#define GL_USES_MRT\n";
830 }
831
832 if (mUsesFragColor)
833 {
834 out << "#define GL_USES_FRAG_COLOR\n";
835 }
836
837 if (mUsesFragData)
838 {
839 out << "#define GL_USES_FRAG_DATA\n";
840 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000841 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000842 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000843 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000844 out << "// Attributes\n";
845 out << attributes;
846 out << "\n"
847 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
848
849 if (mUsesPointSize)
850 {
851 out << "static float gl_PointSize = float(1);\n";
852 }
853
854 out << "\n"
855 "// Varyings\n";
856 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000857 out << "\n";
858
859 if (mUsesDepthRange)
860 {
861 out << "struct gl_DepthRangeParameters\n"
862 "{\n"
863 " float near;\n"
864 " float far;\n"
865 " float diff;\n"
866 "};\n"
867 "\n";
868 }
869
870 if (mOutputType == SH_HLSL11_OUTPUT)
871 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000872 if (mUsesDepthRange)
873 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000874 out << "cbuffer DriverConstants : register(b1)\n"
875 "{\n"
876 " float3 dx_DepthRange : packoffset(c0);\n"
877 "};\n"
878 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000879 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000880 }
881 else
882 {
883 if (mUsesDepthRange)
884 {
885 out << "uniform float3 dx_DepthRange : register(c0);\n";
886 }
887
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000888 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000889 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000890 }
891
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000892 if (mUsesDepthRange)
893 {
894 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
895 "\n";
896 }
897
898 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000899 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000900
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000901 if (!interfaceBlocks.empty())
902 {
903 out << interfaceBlocks;
904 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400905
906 if (!flaggedStructs.empty())
907 {
908 out << "// Std140 Structures accessed by value\n";
909 out << "\n";
910 out << flaggedStructs;
911 out << "\n";
912 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000913 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400914 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000915
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400916 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
917 {
918 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400919 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000920 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400921 switch(textureFunction->sampler)
922 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400923 case EbtSampler2D: out << "int2 "; break;
924 case EbtSampler3D: out << "int3 "; break;
925 case EbtSamplerCube: out << "int2 "; break;
926 case EbtSampler2DArray: out << "int3 "; break;
927 case EbtISampler2D: out << "int2 "; break;
928 case EbtISampler3D: out << "int3 "; break;
929 case EbtISamplerCube: out << "int2 "; break;
930 case EbtISampler2DArray: out << "int3 "; break;
931 case EbtUSampler2D: out << "int2 "; break;
932 case EbtUSampler3D: out << "int3 "; break;
933 case EbtUSamplerCube: out << "int2 "; break;
934 case EbtUSampler2DArray: out << "int3 "; break;
935 case EbtSampler2DShadow: out << "int2 "; break;
936 case EbtSamplerCubeShadow: out << "int2 "; break;
937 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400938 default: UNREACHABLE();
939 }
940 }
941 else // Sampling function
942 {
943 switch(textureFunction->sampler)
944 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400945 case EbtSampler2D: out << "float4 "; break;
946 case EbtSampler3D: out << "float4 "; break;
947 case EbtSamplerCube: out << "float4 "; break;
948 case EbtSampler2DArray: out << "float4 "; break;
949 case EbtISampler2D: out << "int4 "; break;
950 case EbtISampler3D: out << "int4 "; break;
951 case EbtISamplerCube: out << "int4 "; break;
952 case EbtISampler2DArray: out << "int4 "; break;
953 case EbtUSampler2D: out << "uint4 "; break;
954 case EbtUSampler3D: out << "uint4 "; break;
955 case EbtUSamplerCube: out << "uint4 "; break;
956 case EbtUSampler2DArray: out << "uint4 "; break;
957 case EbtSampler2DShadow: out << "float "; break;
958 case EbtSamplerCubeShadow: out << "float "; break;
959 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400960 default: UNREACHABLE();
961 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000962 }
963
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400964 // Function name
965 out << textureFunction->name();
966
967 // Argument list
968 int hlslCoords = 4;
969
970 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000971 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400972 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000973 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400974 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
975 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
976 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000977 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400978
Nicolas Capens75fb4752013-07-10 15:14:47 -0400979 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000980 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400981 case TextureFunction::IMPLICIT: break;
982 case TextureFunction::BIAS: hlslCoords = 4; break;
983 case TextureFunction::LOD: hlslCoords = 4; break;
984 case TextureFunction::LOD0: hlslCoords = 4; break;
985 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000986 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400987 }
988 else if (mOutputType == SH_HLSL11_OUTPUT)
989 {
990 switch(textureFunction->sampler)
991 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400992 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
993 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
994 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
995 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
996 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
997 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500998 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400999 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
1000 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
1001 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -05001002 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001003 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
1004 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
1005 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
1006 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001007 default: UNREACHABLE();
1008 }
1009 }
1010 else UNREACHABLE();
1011
Nicolas Capensfc014542014-02-18 14:47:13 -05001012 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001013 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001014 switch(textureFunction->coords)
1015 {
1016 case 2: out << ", int2 t"; break;
1017 case 3: out << ", int3 t"; break;
1018 default: UNREACHABLE();
1019 }
1020 }
1021 else // Floating-point coordinates (except textureSize)
1022 {
1023 switch(textureFunction->coords)
1024 {
1025 case 1: out << ", int lod"; break; // textureSize()
1026 case 2: out << ", float2 t"; break;
1027 case 3: out << ", float3 t"; break;
1028 case 4: out << ", float4 t"; break;
1029 default: UNREACHABLE();
1030 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001031 }
1032
Nicolas Capensd11d5492014-02-19 17:06:10 -05001033 if (textureFunction->method == TextureFunction::GRAD)
1034 {
1035 switch(textureFunction->sampler)
1036 {
1037 case EbtSampler2D:
1038 case EbtISampler2D:
1039 case EbtUSampler2D:
1040 case EbtSampler2DArray:
1041 case EbtISampler2DArray:
1042 case EbtUSampler2DArray:
1043 case EbtSampler2DShadow:
1044 case EbtSampler2DArrayShadow:
1045 out << ", float2 ddx, float2 ddy";
1046 break;
1047 case EbtSampler3D:
1048 case EbtISampler3D:
1049 case EbtUSampler3D:
1050 case EbtSamplerCube:
1051 case EbtISamplerCube:
1052 case EbtUSamplerCube:
1053 case EbtSamplerCubeShadow:
1054 out << ", float3 ddx, float3 ddy";
1055 break;
1056 default: UNREACHABLE();
1057 }
1058 }
1059
Nicolas Capens75fb4752013-07-10 15:14:47 -04001060 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +00001061 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001062 case TextureFunction::IMPLICIT: break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001063 case TextureFunction::BIAS: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001064 case TextureFunction::LOD: out << ", float lod"; break;
1065 case TextureFunction::LOD0: break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001066 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -05001067 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -05001068 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001069 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +00001070 }
1071
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001072 if (textureFunction->offset)
1073 {
1074 switch(textureFunction->sampler)
1075 {
1076 case EbtSampler2D: out << ", int2 offset"; break;
1077 case EbtSampler3D: out << ", int3 offset"; break;
1078 case EbtSampler2DArray: out << ", int2 offset"; break;
1079 case EbtISampler2D: out << ", int2 offset"; break;
1080 case EbtISampler3D: out << ", int3 offset"; break;
1081 case EbtISampler2DArray: out << ", int2 offset"; break;
1082 case EbtUSampler2D: out << ", int2 offset"; break;
1083 case EbtUSampler3D: out << ", int3 offset"; break;
1084 case EbtUSampler2DArray: out << ", int2 offset"; break;
1085 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -05001086 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001087 default: UNREACHABLE();
1088 }
1089 }
1090
1091 if (textureFunction->method == TextureFunction::BIAS)
1092 {
1093 out << ", float bias";
1094 }
1095
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001096 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001097 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001098
Nicolas Capens75fb4752013-07-10 15:14:47 -04001099 if (textureFunction->method == TextureFunction::SIZE)
1100 {
1101 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
1102 {
1103 if (IsSamplerArray(textureFunction->sampler))
1104 {
1105 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
1106 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
1107 }
1108 else
1109 {
1110 out << " uint width; uint height; uint numberOfLevels;\n"
1111 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
1112 }
1113 }
1114 else if (IsSampler3D(textureFunction->sampler))
1115 {
1116 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
1117 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
1118 }
1119 else UNREACHABLE();
1120
1121 switch(textureFunction->sampler)
1122 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04001123 case EbtSampler2D: out << " return int2(width, height);"; break;
1124 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
1125 case EbtSamplerCube: out << " return int2(width, height);"; break;
1126 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
1127 case EbtISampler2D: out << " return int2(width, height);"; break;
1128 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
1129 case EbtISamplerCube: out << " return int2(width, height);"; break;
1130 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
1131 case EbtUSampler2D: out << " return int2(width, height);"; break;
1132 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
1133 case EbtUSamplerCube: out << " return int2(width, height);"; break;
1134 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
1135 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
1136 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
1137 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001138 default: UNREACHABLE();
1139 }
1140 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001141 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001142 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001143 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1144 {
1145 out << " float width; float height; float layers; float levels;\n";
1146
1147 out << " uint mip = 0;\n";
1148
1149 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
1150
1151 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
1152 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
1153 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
1154 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
1155
1156 // FACE_POSITIVE_X = 000b
1157 // FACE_NEGATIVE_X = 001b
1158 // FACE_POSITIVE_Y = 010b
1159 // FACE_NEGATIVE_Y = 011b
1160 // FACE_POSITIVE_Z = 100b
1161 // FACE_NEGATIVE_Z = 101b
1162 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
1163
1164 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
1165 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
1166 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
1167
1168 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
1169 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
1170 }
1171 else if (IsIntegerSampler(textureFunction->sampler) &&
1172 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001173 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001174 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001175 {
Nicolas Capens93e50de2013-07-09 13:46:28 -04001176 if (IsSamplerArray(textureFunction->sampler))
1177 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001178 out << " float width; float height; float layers; float levels;\n";
1179
1180 if (textureFunction->method == TextureFunction::LOD0)
1181 {
1182 out << " uint mip = 0;\n";
1183 }
1184 else
1185 {
1186 if (textureFunction->method == TextureFunction::IMPLICIT ||
1187 textureFunction->method == TextureFunction::BIAS)
1188 {
1189 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1190 " float2 tSized = float2(t.x * width, t.y * height);\n"
1191 " float dx = length(ddx(tSized));\n"
1192 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001193 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001194
1195 if (textureFunction->method == TextureFunction::BIAS)
1196 {
1197 out << " lod += bias;\n";
1198 }
1199 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001200 else if (textureFunction->method == TextureFunction::GRAD)
1201 {
1202 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1203 " float lod = log2(max(length(ddx), length(ddy)));\n";
1204 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001205
1206 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1207 }
1208
1209 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001210 }
1211 else
1212 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001213 out << " float width; float height; float levels;\n";
1214
1215 if (textureFunction->method == TextureFunction::LOD0)
1216 {
1217 out << " uint mip = 0;\n";
1218 }
1219 else
1220 {
1221 if (textureFunction->method == TextureFunction::IMPLICIT ||
1222 textureFunction->method == TextureFunction::BIAS)
1223 {
1224 out << " x.GetDimensions(0, width, height, 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 Capens2adc2562014-02-14 23:50:59 -05001235 else if (textureFunction->method == TextureFunction::LOD)
1236 {
1237 out << " x.GetDimensions(0, width, height, levels);\n";
1238 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001239 else if (textureFunction->method == TextureFunction::GRAD)
1240 {
1241 out << " x.GetDimensions(0, width, height, levels);\n"
1242 " float lod = log2(max(length(ddx), length(ddy)));\n";
1243 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001244
1245 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1246 }
1247
1248 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001249 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001250 }
1251 else if (IsSampler3D(textureFunction->sampler))
1252 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001253 out << " float width; float height; float depth; float levels;\n";
1254
1255 if (textureFunction->method == TextureFunction::LOD0)
1256 {
1257 out << " uint mip = 0;\n";
1258 }
1259 else
1260 {
1261 if (textureFunction->method == TextureFunction::IMPLICIT ||
1262 textureFunction->method == TextureFunction::BIAS)
1263 {
1264 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1265 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
1266 " float dx = length(ddx(tSized));\n"
1267 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001268 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001269
1270 if (textureFunction->method == TextureFunction::BIAS)
1271 {
1272 out << " lod += bias;\n";
1273 }
1274 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001275 else if (textureFunction->method == TextureFunction::GRAD)
1276 {
1277 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1278 " float lod = log2(max(length(ddx), length(ddy)));\n";
1279 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001280
1281 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1282 }
1283
1284 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001285 }
1286 else UNREACHABLE();
1287 }
1288
1289 out << " return ";
1290
1291 // HLSL intrinsic
1292 if (mOutputType == SH_HLSL9_OUTPUT)
1293 {
1294 switch(textureFunction->sampler)
1295 {
1296 case EbtSampler2D: out << "tex2D"; break;
1297 case EbtSamplerCube: out << "texCUBE"; break;
1298 default: UNREACHABLE();
1299 }
1300
Nicolas Capens75fb4752013-07-10 15:14:47 -04001301 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001302 {
1303 case TextureFunction::IMPLICIT: out << "(s, "; break;
1304 case TextureFunction::BIAS: out << "bias(s, "; break;
1305 case TextureFunction::LOD: out << "lod(s, "; break;
1306 case TextureFunction::LOD0: out << "lod(s, "; break;
1307 default: UNREACHABLE();
1308 }
1309 }
1310 else if (mOutputType == SH_HLSL11_OUTPUT)
1311 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001312 if (textureFunction->method == TextureFunction::GRAD)
1313 {
1314 if (IsIntegerSampler(textureFunction->sampler))
1315 {
1316 out << "x.Load(";
1317 }
1318 else if (IsShadowSampler(textureFunction->sampler))
1319 {
1320 out << "x.SampleCmpLevelZero(s, ";
1321 }
1322 else
1323 {
1324 out << "x.SampleGrad(s, ";
1325 }
1326 }
1327 else if (IsIntegerSampler(textureFunction->sampler) ||
1328 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001329 {
1330 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001331 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001332 else if (IsShadowSampler(textureFunction->sampler))
1333 {
1334 out << "x.SampleCmp(s, ";
1335 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001336 else
1337 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001338 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001339 {
1340 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1341 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1342 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1343 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
1344 default: UNREACHABLE();
1345 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001346 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001347 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001348 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001349
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001350 // Integer sampling requires integer addresses
1351 TString addressx = "";
1352 TString addressy = "";
1353 TString addressz = "";
1354 TString close = "";
1355
Nicolas Capensfc014542014-02-18 14:47:13 -05001356 if (IsIntegerSampler(textureFunction->sampler) ||
1357 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001358 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001359 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001360 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001361 case 2: out << "int3("; break;
1362 case 3: out << "int4("; break;
1363 default: UNREACHABLE();
1364 }
1365
Nicolas Capensfc014542014-02-18 14:47:13 -05001366 // Convert from normalized floating-point to integer
1367 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001368 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001369 addressx = "int(floor(width * frac((";
1370 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001371
Nicolas Capensfc014542014-02-18 14:47:13 -05001372 if (IsSamplerArray(textureFunction->sampler))
1373 {
1374 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1375 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001376 else if (IsSamplerCube(textureFunction->sampler))
1377 {
1378 addressz = "((((";
1379 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001380 else
1381 {
1382 addressz = "int(floor(depth * frac((";
1383 }
1384
1385 close = "))))";
1386 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001387 }
1388 else
1389 {
1390 switch(hlslCoords)
1391 {
1392 case 2: out << "float2("; break;
1393 case 3: out << "float3("; break;
1394 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001395 default: UNREACHABLE();
1396 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001397 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001398
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001399 TString proj = ""; // Only used for projected textures
1400
1401 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001402 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001403 switch(textureFunction->coords)
1404 {
1405 case 3: proj = " / t.z"; break;
1406 case 4: proj = " / t.w"; break;
1407 default: UNREACHABLE();
1408 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001409 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001410
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001411 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001412
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001413 if (mOutputType == SH_HLSL9_OUTPUT)
1414 {
1415 if (hlslCoords >= 3)
1416 {
1417 if (textureFunction->coords < 3)
1418 {
1419 out << ", 0";
1420 }
1421 else
1422 {
1423 out << ", t.z" + proj;
1424 }
1425 }
1426
1427 if (hlslCoords == 4)
1428 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001429 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001430 {
1431 case TextureFunction::BIAS: out << ", bias"; break;
1432 case TextureFunction::LOD: out << ", lod"; break;
1433 case TextureFunction::LOD0: out << ", 0"; break;
1434 default: UNREACHABLE();
1435 }
1436 }
1437
1438 out << "));\n";
1439 }
1440 else if (mOutputType == SH_HLSL11_OUTPUT)
1441 {
1442 if (hlslCoords >= 3)
1443 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001444 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1445 {
1446 out << ", face";
1447 }
1448 else
1449 {
1450 out << ", " + addressz + ("t.z" + proj) + close;
1451 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001452 }
1453
Nicolas Capensd11d5492014-02-19 17:06:10 -05001454 if (textureFunction->method == TextureFunction::GRAD)
1455 {
1456 if (IsIntegerSampler(textureFunction->sampler))
1457 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001458 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001459 }
1460 else if (IsShadowSampler(textureFunction->sampler))
1461 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001462 // Compare value
Nicolas Capensd11d5492014-02-19 17:06:10 -05001463 switch(textureFunction->coords)
1464 {
1465 case 3: out << "), t.z"; break;
1466 case 4: out << "), t.w"; break;
1467 default: UNREACHABLE();
1468 }
1469 }
1470 else
1471 {
1472 out << "), ddx, ddy";
1473 }
1474 }
1475 else if (IsIntegerSampler(textureFunction->sampler) ||
1476 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001477 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001478 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001479 }
1480 else if (IsShadowSampler(textureFunction->sampler))
1481 {
1482 // Compare value
1483 switch(textureFunction->coords)
1484 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001485 case 3: out << "), t.z"; break;
1486 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001487 default: UNREACHABLE();
1488 }
1489 }
1490 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001491 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001492 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001493 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001494 case TextureFunction::IMPLICIT: out << ")"; break;
1495 case TextureFunction::BIAS: out << "), bias"; break;
1496 case TextureFunction::LOD: out << "), lod"; break;
1497 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001498 default: UNREACHABLE();
1499 }
1500 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001501
1502 if (textureFunction->offset)
1503 {
1504 out << ", offset";
1505 }
1506
1507 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001508 }
1509 else UNREACHABLE();
1510 }
1511
1512 out << "\n"
1513 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001514 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001515 }
1516
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001517 if (mUsesFragCoord)
1518 {
1519 out << "#define GL_USES_FRAG_COORD\n";
1520 }
1521
1522 if (mUsesPointCoord)
1523 {
1524 out << "#define GL_USES_POINT_COORD\n";
1525 }
1526
1527 if (mUsesFrontFacing)
1528 {
1529 out << "#define GL_USES_FRONT_FACING\n";
1530 }
1531
1532 if (mUsesPointSize)
1533 {
1534 out << "#define GL_USES_POINT_SIZE\n";
1535 }
1536
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001537 if (mUsesFragDepth)
1538 {
1539 out << "#define GL_USES_FRAG_DEPTH\n";
1540 }
1541
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001542 if (mUsesDepthRange)
1543 {
1544 out << "#define GL_USES_DEPTH_RANGE\n";
1545 }
1546
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001547 if (mUsesXor)
1548 {
1549 out << "bool xor(bool p, bool q)\n"
1550 "{\n"
1551 " return (p || q) && !(p && q);\n"
1552 "}\n"
1553 "\n";
1554 }
1555
1556 if (mUsesMod1)
1557 {
1558 out << "float mod(float x, float y)\n"
1559 "{\n"
1560 " return x - y * floor(x / y);\n"
1561 "}\n"
1562 "\n";
1563 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001564
1565 if (mUsesMod2v)
1566 {
1567 out << "float2 mod(float2 x, float2 y)\n"
1568 "{\n"
1569 " return x - y * floor(x / y);\n"
1570 "}\n"
1571 "\n";
1572 }
1573
1574 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001575 {
1576 out << "float2 mod(float2 x, float y)\n"
1577 "{\n"
1578 " return x - y * floor(x / y);\n"
1579 "}\n"
1580 "\n";
1581 }
1582
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001583 if (mUsesMod3v)
1584 {
1585 out << "float3 mod(float3 x, float3 y)\n"
1586 "{\n"
1587 " return x - y * floor(x / y);\n"
1588 "}\n"
1589 "\n";
1590 }
1591
1592 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001593 {
1594 out << "float3 mod(float3 x, float y)\n"
1595 "{\n"
1596 " return x - y * floor(x / y);\n"
1597 "}\n"
1598 "\n";
1599 }
1600
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001601 if (mUsesMod4v)
1602 {
1603 out << "float4 mod(float4 x, float4 y)\n"
1604 "{\n"
1605 " return x - y * floor(x / y);\n"
1606 "}\n"
1607 "\n";
1608 }
1609
1610 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001611 {
1612 out << "float4 mod(float4 x, float y)\n"
1613 "{\n"
1614 " return x - y * floor(x / y);\n"
1615 "}\n"
1616 "\n";
1617 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001618
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001619 if (mUsesFaceforward1)
1620 {
1621 out << "float faceforward(float N, float I, float Nref)\n"
1622 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001623 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001624 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001625 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001626 " }\n"
1627 " else\n"
1628 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001629 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001630 " }\n"
1631 "}\n"
1632 "\n";
1633 }
1634
1635 if (mUsesFaceforward2)
1636 {
1637 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1638 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001639 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001640 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001641 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001642 " }\n"
1643 " else\n"
1644 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001645 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001646 " }\n"
1647 "}\n"
1648 "\n";
1649 }
1650
1651 if (mUsesFaceforward3)
1652 {
1653 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1654 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001655 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001656 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001657 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001658 " }\n"
1659 " else\n"
1660 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001661 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001662 " }\n"
1663 "}\n"
1664 "\n";
1665 }
1666
1667 if (mUsesFaceforward4)
1668 {
1669 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1670 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001671 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001672 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001673 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001674 " }\n"
1675 " else\n"
1676 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001677 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001678 " }\n"
1679 "}\n"
1680 "\n";
1681 }
1682
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001683 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001684 {
1685 out << "float atanyx(float y, float x)\n"
1686 "{\n"
1687 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1688 " return atan2(y, x);\n"
1689 "}\n";
1690 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001691
1692 if (mUsesAtan2_2)
1693 {
1694 out << "float2 atanyx(float2 y, float2 x)\n"
1695 "{\n"
1696 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1697 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1698 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1699 "}\n";
1700 }
1701
1702 if (mUsesAtan2_3)
1703 {
1704 out << "float3 atanyx(float3 y, float3 x)\n"
1705 "{\n"
1706 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1707 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1708 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1709 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1710 "}\n";
1711 }
1712
1713 if (mUsesAtan2_4)
1714 {
1715 out << "float4 atanyx(float4 y, float4 x)\n"
1716 "{\n"
1717 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1718 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1719 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1720 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1721 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1722 "}\n";
1723 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001724}
1725
1726void OutputHLSL::visitSymbol(TIntermSymbol *node)
1727{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001728 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001729
Jamie Madill570e04d2013-06-21 09:15:33 -04001730 // Handle accessing std140 structs by value
1731 if (mFlaggedStructMappedNames.count(node) > 0)
1732 {
1733 out << mFlaggedStructMappedNames[node];
1734 return;
1735 }
1736
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001737 TString name = node->getSymbol();
1738
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001739 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001740 {
1741 mUsesDepthRange = true;
1742 out << name;
1743 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001744 else
1745 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001746 TQualifier qualifier = node->getQualifier();
1747
1748 if (qualifier == EvqUniform)
1749 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001750 const TType& nodeType = node->getType();
1751 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1752
1753 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001754 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001755 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001756 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001757 else
1758 {
1759 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001760 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001761
1762 out << decorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001763 }
Jamie Madill19571812013-08-12 15:26:34 -07001764 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001765 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001766 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001767 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001768 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001769 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001770 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001771 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001772 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001773 }
Jamie Madill19571812013-08-12 15:26:34 -07001774 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001775 {
1776 mReferencedOutputVariables[name] = node;
1777 out << "out_" << name;
1778 }
1779 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001780 {
1781 out << "gl_Color[0]";
1782 mUsesFragColor = true;
1783 }
1784 else if (qualifier == EvqFragData)
1785 {
1786 out << "gl_Color";
1787 mUsesFragData = true;
1788 }
1789 else if (qualifier == EvqFragCoord)
1790 {
1791 mUsesFragCoord = true;
1792 out << name;
1793 }
1794 else if (qualifier == EvqPointCoord)
1795 {
1796 mUsesPointCoord = true;
1797 out << name;
1798 }
1799 else if (qualifier == EvqFrontFacing)
1800 {
1801 mUsesFrontFacing = true;
1802 out << name;
1803 }
1804 else if (qualifier == EvqPointSize)
1805 {
1806 mUsesPointSize = true;
1807 out << name;
1808 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001809 else if (name == "gl_FragDepthEXT")
1810 {
1811 mUsesFragDepth = true;
1812 out << "gl_Depth";
1813 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001814 else if (qualifier == EvqInternal)
1815 {
1816 out << name;
1817 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001818 else
1819 {
1820 out << decorate(name);
1821 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001822 }
1823}
1824
1825bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1826{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001827 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001828
Jamie Madill570e04d2013-06-21 09:15:33 -04001829 // Handle accessing std140 structs by value
1830 if (mFlaggedStructMappedNames.count(node) > 0)
1831 {
1832 out << mFlaggedStructMappedNames[node];
1833 return false;
1834 }
1835
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001836 switch (node->getOp())
1837 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001838 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001839 case EOpInitialize:
1840 if (visit == PreVisit)
1841 {
1842 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1843 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1844 // new variable is created before the assignment is evaluated), so we need to convert
1845 // this to "float t = x, x = t;".
1846
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001847 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1848 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001849
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001850 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1851 expression->traverse(&searchSymbol);
1852 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001853
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001854 if (sameSymbol)
1855 {
1856 // Type already printed
1857 out << "t" + str(mUniqueIndex) + " = ";
1858 expression->traverse(this);
1859 out << ", ";
1860 symbolNode->traverse(this);
1861 out << " = t" + str(mUniqueIndex);
1862
1863 mUniqueIndex++;
1864 return false;
1865 }
1866 }
1867 else if (visit == InVisit)
1868 {
1869 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001870 }
1871 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001872 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1873 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1874 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1875 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1876 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1877 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001878 if (visit == PreVisit)
1879 {
1880 out << "(";
1881 }
1882 else if (visit == InVisit)
1883 {
1884 out << " = mul(";
1885 node->getLeft()->traverse(this);
1886 out << ", transpose(";
1887 }
1888 else
1889 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001890 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001891 }
1892 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001893 case EOpMatrixTimesMatrixAssign:
1894 if (visit == PreVisit)
1895 {
1896 out << "(";
1897 }
1898 else if (visit == InVisit)
1899 {
1900 out << " = mul(";
1901 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001902 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001903 }
1904 else
1905 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001906 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001907 }
1908 break;
1909 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001910 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001911 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001912 const TType& leftType = node->getLeft()->getType();
1913 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001914 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001915 if (visit == PreVisit)
1916 {
1917 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1918 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1919
1920 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1921 out << interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1922
1923 return false;
1924 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001925 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001926 else
1927 {
1928 outputTriplet(visit, "", "[", "]");
1929 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001930 }
1931 break;
1932 case EOpIndexIndirect:
1933 // We do not currently support indirect references to interface blocks
1934 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1935 outputTriplet(visit, "", "[", "]");
1936 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001937 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001938 if (visit == InVisit)
1939 {
1940 const TStructure* structure = node->getLeft()->getType().getStruct();
1941 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1942 const TField* field = structure->fields()[index->getIConst(0)];
1943 out << "." + decorateField(field->name(), *structure);
1944
1945 return false;
1946 }
1947 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001948 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001949 if (visit == InVisit)
1950 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001951 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1952 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1953 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
1954 out << "." + decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001955
1956 return false;
1957 }
1958 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001959 case EOpVectorSwizzle:
1960 if (visit == InVisit)
1961 {
1962 out << ".";
1963
1964 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1965
1966 if (swizzle)
1967 {
1968 TIntermSequence &sequence = swizzle->getSequence();
1969
1970 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1971 {
1972 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1973
1974 if (element)
1975 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001976 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001977
1978 switch (i)
1979 {
1980 case 0: out << "x"; break;
1981 case 1: out << "y"; break;
1982 case 2: out << "z"; break;
1983 case 3: out << "w"; break;
1984 default: UNREACHABLE();
1985 }
1986 }
1987 else UNREACHABLE();
1988 }
1989 }
1990 else UNREACHABLE();
1991
1992 return false; // Fully processed
1993 }
1994 break;
1995 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1996 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1997 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1998 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001999 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002000 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002001 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002002 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002003 if (node->getOp() == EOpEqual)
2004 {
2005 outputTriplet(visit, "(", " == ", ")");
2006 }
2007 else
2008 {
2009 outputTriplet(visit, "(", " != ", ")");
2010 }
2011 }
2012 else if (node->getLeft()->getBasicType() == EbtStruct)
2013 {
2014 if (node->getOp() == EOpEqual)
2015 {
2016 out << "(";
2017 }
2018 else
2019 {
2020 out << "!(";
2021 }
2022
Jamie Madill98493dd2013-07-08 14:39:03 -04002023 const TStructure &structure = *node->getLeft()->getType().getStruct();
2024 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002025
Jamie Madill98493dd2013-07-08 14:39:03 -04002026 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002027 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002028 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002029
2030 node->getLeft()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04002031 out << "." + decorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002032 node->getRight()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04002033 out << "." + decorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002034
Jamie Madill98493dd2013-07-08 14:39:03 -04002035 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002036 {
2037 out << " && ";
2038 }
2039 }
2040
2041 out << ")";
2042
2043 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002044 }
2045 else
2046 {
Jamie Madill0b20c942013-07-19 16:36:56 -04002047 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002048
2049 if (node->getOp() == EOpEqual)
2050 {
Jamie Madill0b20c942013-07-19 16:36:56 -04002051 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002052 }
2053 else
2054 {
Jamie Madill0b20c942013-07-19 16:36:56 -04002055 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002056 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002057 }
2058 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002059 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2060 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2061 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2062 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2063 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00002064 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00002065 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
2066 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00002067 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00002068 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002069 if (node->getRight()->hasSideEffects())
2070 {
2071 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
2072 return false;
2073 }
2074 else
2075 {
2076 outputTriplet(visit, "(", " || ", ")");
2077 return true;
2078 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002079 case EOpLogicalXor:
2080 mUsesXor = true;
2081 outputTriplet(visit, "xor(", ", ", ")");
2082 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00002083 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002084 if (node->getRight()->hasSideEffects())
2085 {
2086 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
2087 return false;
2088 }
2089 else
2090 {
2091 outputTriplet(visit, "(", " && ", ")");
2092 return true;
2093 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002094 default: UNREACHABLE();
2095 }
2096
2097 return true;
2098}
2099
2100bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
2101{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002102 switch (node->getOp())
2103 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002104 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
2105 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
2106 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
2107 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
2108 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
2109 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
2110 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002111 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04002112 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002113 case EOpConvFloatToBool:
2114 switch (node->getOperand()->getType().getNominalSize())
2115 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002116 case 1: outputTriplet(visit, "bool(", "", ")"); break;
2117 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
2118 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
2119 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002120 default: UNREACHABLE();
2121 }
2122 break;
2123 case EOpConvBoolToFloat:
2124 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04002125 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002126 switch (node->getOperand()->getType().getNominalSize())
2127 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002128 case 1: outputTriplet(visit, "float(", "", ")"); break;
2129 case 2: outputTriplet(visit, "float2(", "", ")"); break;
2130 case 3: outputTriplet(visit, "float3(", "", ")"); break;
2131 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002132 default: UNREACHABLE();
2133 }
2134 break;
2135 case EOpConvFloatToInt:
2136 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04002137 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002138 switch (node->getOperand()->getType().getNominalSize())
2139 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002140 case 1: outputTriplet(visit, "int(", "", ")"); break;
2141 case 2: outputTriplet(visit, "int2(", "", ")"); break;
2142 case 3: outputTriplet(visit, "int3(", "", ")"); break;
2143 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002144 default: UNREACHABLE();
2145 }
2146 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002147 case EOpConvFloatToUInt:
2148 case EOpConvBoolToUInt:
2149 case EOpConvIntToUInt:
Jamie Madilla16f1672013-07-03 15:15:17 -04002150 switch (node->getOperand()->getType().getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002151 {
2152 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002153 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
2154 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
2155 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002156 default: UNREACHABLE();
2157 }
2158 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002159 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
2160 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
2161 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
2162 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
2163 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
2164 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
2165 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
2166 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
2167 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
2168 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
2169 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
2170 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
2171 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
2172 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
2173 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
2174 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
2175 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
2176 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
2177 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
2178 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
2179 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002180 case EOpDFdx:
2181 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2182 {
2183 outputTriplet(visit, "(", "", ", 0.0)");
2184 }
2185 else
2186 {
2187 outputTriplet(visit, "ddx(", "", ")");
2188 }
2189 break;
2190 case EOpDFdy:
2191 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2192 {
2193 outputTriplet(visit, "(", "", ", 0.0)");
2194 }
2195 else
2196 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00002197 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002198 }
2199 break;
2200 case EOpFwidth:
2201 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2202 {
2203 outputTriplet(visit, "(", "", ", 0.0)");
2204 }
2205 else
2206 {
2207 outputTriplet(visit, "fwidth(", "", ")");
2208 }
2209 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002210 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
2211 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002212 default: UNREACHABLE();
2213 }
2214
2215 return true;
2216}
2217
2218bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2219{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002220 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002221
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002222 switch (node->getOp())
2223 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002224 case EOpSequence:
2225 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002226 if (mInsideFunction)
2227 {
Jamie Madill075edd82013-07-08 13:30:19 -04002228 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002229 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002230
2231 mScopeDepth++;
2232
2233 if (mScopeBracket.size() < mScopeDepth)
2234 {
2235 mScopeBracket.push_back(0); // New scope level
2236 }
2237 else
2238 {
2239 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
2240 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002241 }
2242
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002243 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
2244 {
Jamie Madill075edd82013-07-08 13:30:19 -04002245 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002246
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002247 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002248
2249 out << ";\n";
2250 }
2251
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002252 if (mInsideFunction)
2253 {
Jamie Madill075edd82013-07-08 13:30:19 -04002254 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002255 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002256
2257 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002258 }
2259
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002260 return false;
2261 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002262 case EOpDeclaration:
2263 if (visit == PreVisit)
2264 {
2265 TIntermSequence &sequence = node->getSequence();
2266 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002267
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00002268 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002269 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002270 if (variable->getType().getStruct())
2271 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002272 addConstructor(variable->getType(), scopedStruct(variable->getType().getStruct()->name()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00002273 }
2274
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002275 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002276 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00002277 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002278 {
2279 out << "static ";
2280 }
2281
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002282 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002283
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002284 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002285 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002286 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002287
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002288 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002289 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002290 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002291 out << arrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05002292 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002293 }
2294 else
2295 {
2296 (*sit)->traverse(this);
2297 }
2298
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002299 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002300 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002301 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002302 }
2303 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002304 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002305 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2306 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002307 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002308 }
2309 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002310 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002311 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002312 {
2313 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2314 {
2315 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2316
2317 if (symbol)
2318 {
2319 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2320 mReferencedVaryings[symbol->getSymbol()] = symbol;
2321 }
2322 else
2323 {
2324 (*sit)->traverse(this);
2325 }
2326 }
2327 }
2328
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002329 return false;
2330 }
2331 else if (visit == InVisit)
2332 {
2333 out << ", ";
2334 }
2335 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002336 case EOpPrototype:
2337 if (visit == PreVisit)
2338 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002339 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002340
2341 TIntermSequence &arguments = node->getSequence();
2342
2343 for (unsigned int i = 0; i < arguments.size(); i++)
2344 {
2345 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2346
2347 if (symbol)
2348 {
2349 out << argumentString(symbol);
2350
2351 if (i < arguments.size() - 1)
2352 {
2353 out << ", ";
2354 }
2355 }
2356 else UNREACHABLE();
2357 }
2358
2359 out << ");\n";
2360
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002361 // Also prototype the Lod0 variant if needed
2362 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2363 {
2364 mOutputLod0Function = true;
2365 node->traverse(this);
2366 mOutputLod0Function = false;
2367 }
2368
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002369 return false;
2370 }
2371 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002372 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002373 case EOpFunction:
2374 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002375 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002376
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002377 out << typeString(node->getType()) << " ";
2378
2379 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002380 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002381 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002382 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002383 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002384 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002385 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002386 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002387
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002388 TIntermSequence &sequence = node->getSequence();
2389 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2390
2391 for (unsigned int i = 0; i < arguments.size(); i++)
2392 {
2393 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2394
2395 if (symbol)
2396 {
2397 if (symbol->getType().getStruct())
2398 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002399 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getStruct()->name()), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002400 }
2401
2402 out << argumentString(symbol);
2403
2404 if (i < arguments.size() - 1)
2405 {
2406 out << ", ";
2407 }
2408 }
2409 else UNREACHABLE();
2410 }
2411
2412 out << ")\n"
2413 "{\n";
2414
2415 if (sequence.size() > 1)
2416 {
2417 mInsideFunction = true;
2418 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002419 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002420 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002421
2422 out << "}\n";
2423
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002424 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2425 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002426 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002427 {
2428 mOutputLod0Function = true;
2429 node->traverse(this);
2430 mOutputLod0Function = false;
2431 }
2432 }
2433
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002434 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002435 }
2436 break;
2437 case EOpFunctionCall:
2438 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002439 TString name = TFunction::unmangleName(node->getName());
2440 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002441 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002442
2443 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002444 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002445 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002446 }
2447 else
2448 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002449 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2450
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002451 TextureFunction textureFunction;
2452 textureFunction.sampler = samplerType;
2453 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002454 textureFunction.method = TextureFunction::IMPLICIT;
2455 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002456 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002457
2458 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002459 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002460 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002461 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002462 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002463 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002464 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002465 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002466 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002467 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002468 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002469 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002470 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002471 else if (name == "texture2DProjLod" || name == "textureProjLod")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002472 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002473 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002474 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002475 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002476 else if (name == "textureSize")
2477 {
2478 textureFunction.method = TextureFunction::SIZE;
2479 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002480 else if (name == "textureOffset")
2481 {
2482 textureFunction.method = TextureFunction::IMPLICIT;
2483 textureFunction.offset = true;
2484 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002485 else if (name == "textureProjOffset")
2486 {
2487 textureFunction.method = TextureFunction::IMPLICIT;
2488 textureFunction.offset = true;
2489 textureFunction.proj = true;
2490 }
2491 else if (name == "textureLodOffset")
2492 {
2493 textureFunction.method = TextureFunction::LOD;
2494 textureFunction.offset = true;
2495 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002496 else if (name == "textureProjLod")
2497 {
2498 textureFunction.method = TextureFunction::LOD;
2499 textureFunction.proj = true;
2500 }
2501 else if (name == "textureProjLodOffset")
2502 {
2503 textureFunction.method = TextureFunction::LOD;
2504 textureFunction.proj = true;
2505 textureFunction.offset = true;
2506 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002507 else if (name == "texelFetch")
2508 {
2509 textureFunction.method = TextureFunction::FETCH;
2510 }
2511 else if (name == "texelFetchOffset")
2512 {
2513 textureFunction.method = TextureFunction::FETCH;
2514 textureFunction.offset = true;
2515 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05002516 else if (name == "textureGrad")
2517 {
2518 textureFunction.method = TextureFunction::GRAD;
2519 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002520 else if (name == "textureGradOffset")
2521 {
2522 textureFunction.method = TextureFunction::GRAD;
2523 textureFunction.offset = true;
2524 }
Nicolas Capensf7378e32014-02-19 17:29:32 -05002525 else if (name == "textureProjGrad")
2526 {
2527 textureFunction.method = TextureFunction::GRAD;
2528 textureFunction.proj = true;
2529 }
2530 else if (name == "textureProjGradOffset")
2531 {
2532 textureFunction.method = TextureFunction::GRAD;
2533 textureFunction.proj = true;
2534 textureFunction.offset = true;
2535 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002536 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002537
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002538 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002539 {
2540 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2541 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002542 textureFunction.method = TextureFunction::LOD0;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002543 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002544 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002545 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002546 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2547
2548 if (textureFunction.offset)
2549 {
2550 mandatoryArgumentCount++;
2551 }
2552
2553 if (arguments.size() > mandatoryArgumentCount) // Bias argument is optional
2554 {
2555 textureFunction.method = TextureFunction::BIAS;
2556 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002557 }
2558 }
2559
2560 mUsesTexture.insert(textureFunction);
2561
2562 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002563 }
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002564
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002565 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2566 {
2567 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2568 {
2569 out << "texture_";
2570 (*arg)->traverse(this);
2571 out << ", sampler_";
2572 }
2573
2574 (*arg)->traverse(this);
2575
2576 if (arg < arguments.end() - 1)
2577 {
2578 out << ", ";
2579 }
2580 }
2581
2582 out << ")";
2583
2584 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002585 }
2586 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002587 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002588 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002589 addConstructor(node->getType(), "vec1", &node->getSequence());
2590 outputTriplet(visit, "vec1(", "", ")");
2591 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002592 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002593 addConstructor(node->getType(), "vec2", &node->getSequence());
2594 outputTriplet(visit, "vec2(", ", ", ")");
2595 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002596 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002597 addConstructor(node->getType(), "vec3", &node->getSequence());
2598 outputTriplet(visit, "vec3(", ", ", ")");
2599 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002600 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002601 addConstructor(node->getType(), "vec4", &node->getSequence());
2602 outputTriplet(visit, "vec4(", ", ", ")");
2603 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002604 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002605 addConstructor(node->getType(), "bvec1", &node->getSequence());
2606 outputTriplet(visit, "bvec1(", "", ")");
2607 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002608 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002609 addConstructor(node->getType(), "bvec2", &node->getSequence());
2610 outputTriplet(visit, "bvec2(", ", ", ")");
2611 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002612 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002613 addConstructor(node->getType(), "bvec3", &node->getSequence());
2614 outputTriplet(visit, "bvec3(", ", ", ")");
2615 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002616 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002617 addConstructor(node->getType(), "bvec4", &node->getSequence());
2618 outputTriplet(visit, "bvec4(", ", ", ")");
2619 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002620 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002621 addConstructor(node->getType(), "ivec1", &node->getSequence());
2622 outputTriplet(visit, "ivec1(", "", ")");
2623 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002624 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002625 addConstructor(node->getType(), "ivec2", &node->getSequence());
2626 outputTriplet(visit, "ivec2(", ", ", ")");
2627 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002628 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002629 addConstructor(node->getType(), "ivec3", &node->getSequence());
2630 outputTriplet(visit, "ivec3(", ", ", ")");
2631 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002632 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002633 addConstructor(node->getType(), "ivec4", &node->getSequence());
2634 outputTriplet(visit, "ivec4(", ", ", ")");
2635 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002636 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002637 addConstructor(node->getType(), "uvec1", &node->getSequence());
2638 outputTriplet(visit, "uvec1(", "", ")");
2639 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002640 case EOpConstructUVec2:
2641 addConstructor(node->getType(), "uvec2", &node->getSequence());
2642 outputTriplet(visit, "uvec2(", ", ", ")");
2643 break;
2644 case EOpConstructUVec3:
2645 addConstructor(node->getType(), "uvec3", &node->getSequence());
2646 outputTriplet(visit, "uvec3(", ", ", ")");
2647 break;
2648 case EOpConstructUVec4:
2649 addConstructor(node->getType(), "uvec4", &node->getSequence());
2650 outputTriplet(visit, "uvec4(", ", ", ")");
2651 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002652 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002653 addConstructor(node->getType(), "mat2", &node->getSequence());
2654 outputTriplet(visit, "mat2(", ", ", ")");
2655 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002656 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002657 addConstructor(node->getType(), "mat3", &node->getSequence());
2658 outputTriplet(visit, "mat3(", ", ", ")");
2659 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002660 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002661 addConstructor(node->getType(), "mat4", &node->getSequence());
2662 outputTriplet(visit, "mat4(", ", ", ")");
2663 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002664 case EOpConstructStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04002665 addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence());
2666 outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002667 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002668 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2669 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2670 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2671 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2672 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2673 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002674 case EOpMod:
2675 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002676 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002677 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2678 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2679 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002680 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002681 case 11: mUsesMod1 = true; break;
2682 case 22: mUsesMod2v = true; break;
2683 case 21: mUsesMod2f = true; break;
2684 case 33: mUsesMod3v = true; break;
2685 case 31: mUsesMod3f = true; break;
2686 case 44: mUsesMod4v = true; break;
2687 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002688 default: UNREACHABLE();
2689 }
2690
2691 outputTriplet(visit, "mod(", ", ", ")");
2692 }
2693 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002694 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002695 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002696 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002697 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2698 {
2699 case 1: mUsesAtan2_1 = true; break;
2700 case 2: mUsesAtan2_2 = true; break;
2701 case 3: mUsesAtan2_3 = true; break;
2702 case 4: mUsesAtan2_4 = true; break;
2703 default: UNREACHABLE();
2704 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002705 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002706 break;
2707 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2708 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2709 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2710 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2711 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2712 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2713 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2714 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2715 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002716 case EOpFaceForward:
2717 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002718 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002719 {
2720 case 1: mUsesFaceforward1 = true; break;
2721 case 2: mUsesFaceforward2 = true; break;
2722 case 3: mUsesFaceforward3 = true; break;
2723 case 4: mUsesFaceforward4 = true; break;
2724 default: UNREACHABLE();
2725 }
2726
2727 outputTriplet(visit, "faceforward(", ", ", ")");
2728 }
2729 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002730 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2731 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2732 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002733 default: UNREACHABLE();
2734 }
2735
2736 return true;
2737}
2738
2739bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2740{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002741 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002742
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002743 if (node->usesTernaryOperator())
2744 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002745 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002746 }
2747 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002748 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002749 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002750
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002751 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002752
2753 node->getCondition()->traverse(this);
2754
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002755 out << ")\n";
2756
Jamie Madill075edd82013-07-08 13:30:19 -04002757 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002758 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002759
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002760 bool discard = false;
2761
daniel@transgaming.combb885322010-04-15 20:45:24 +00002762 if (node->getTrueBlock())
2763 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002764 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002765
2766 // Detect true discard
2767 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002768 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002769
Jamie Madill075edd82013-07-08 13:30:19 -04002770 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002771 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002772
2773 if (node->getFalseBlock())
2774 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002775 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002776
Jamie Madill075edd82013-07-08 13:30:19 -04002777 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002778 out << "{\n";
2779
Jamie Madill075edd82013-07-08 13:30:19 -04002780 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002781 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002782
Jamie Madill075edd82013-07-08 13:30:19 -04002783 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002784 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002785
2786 // Detect false discard
2787 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2788 }
2789
2790 // ANGLE issue 486: Detect problematic conditional discard
2791 if (discard && FindSideEffectRewriting::search(node))
2792 {
2793 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002794 }
2795 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002796
2797 return false;
2798}
2799
2800void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2801{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002802 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002803}
2804
2805bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2806{
Nicolas Capens655fe362014-04-11 13:12:34 -04002807 mNestedLoopDepth++;
2808
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002809 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2810
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002811 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002812 {
2813 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2814 }
2815
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002816 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002817 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002818 if (handleExcessiveLoop(node))
2819 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002820 mInsideDiscontinuousLoop = wasDiscontinuous;
2821 mNestedLoopDepth--;
2822
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002823 return false;
2824 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002825 }
2826
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002827 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002828
alokp@chromium.org52813552010-11-16 18:36:09 +00002829 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002830 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002831 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002832
Jamie Madill075edd82013-07-08 13:30:19 -04002833 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002834 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002835 }
2836 else
2837 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002838 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002839
2840 if (node->getInit())
2841 {
2842 node->getInit()->traverse(this);
2843 }
2844
2845 out << "; ";
2846
alokp@chromium.org52813552010-11-16 18:36:09 +00002847 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002848 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002849 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002850 }
2851
2852 out << "; ";
2853
alokp@chromium.org52813552010-11-16 18:36:09 +00002854 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002855 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002856 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002857 }
2858
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002859 out << ")\n";
2860
Jamie Madill075edd82013-07-08 13:30:19 -04002861 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002862 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002863 }
2864
2865 if (node->getBody())
2866 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002867 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002868 }
2869
Jamie Madill075edd82013-07-08 13:30:19 -04002870 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002871 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002872
alokp@chromium.org52813552010-11-16 18:36:09 +00002873 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002874 {
Jamie Madill075edd82013-07-08 13:30:19 -04002875 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002876 out << "while(\n";
2877
alokp@chromium.org52813552010-11-16 18:36:09 +00002878 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002879
daniel@transgaming.com73536982012-03-21 20:45:49 +00002880 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002881 }
2882
daniel@transgaming.com73536982012-03-21 20:45:49 +00002883 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002884
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002885 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002886 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002887
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002888 return false;
2889}
2890
2891bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2892{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002893 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002894
2895 switch (node->getFlowOp())
2896 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002897 case EOpKill:
2898 outputTriplet(visit, "discard;\n", "", "");
2899 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002900 case EOpBreak:
2901 if (visit == PreVisit)
2902 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002903 if (mNestedLoopDepth > 1)
2904 {
2905 mUsesNestedBreak = true;
2906 }
2907
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002908 if (mExcessiveLoopIndex)
2909 {
2910 out << "{Break";
2911 mExcessiveLoopIndex->traverse(this);
2912 out << " = true; break;}\n";
2913 }
2914 else
2915 {
2916 out << "break;\n";
2917 }
2918 }
2919 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002920 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002921 case EOpReturn:
2922 if (visit == PreVisit)
2923 {
2924 if (node->getExpression())
2925 {
2926 out << "return ";
2927 }
2928 else
2929 {
2930 out << "return;\n";
2931 }
2932 }
2933 else if (visit == PostVisit)
2934 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002935 if (node->getExpression())
2936 {
2937 out << ";\n";
2938 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002939 }
2940 break;
2941 default: UNREACHABLE();
2942 }
2943
2944 return true;
2945}
2946
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002947void OutputHLSL::traverseStatements(TIntermNode *node)
2948{
2949 if (isSingleStatement(node))
2950 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002951 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002952 }
2953
2954 node->traverse(this);
2955}
2956
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002957bool OutputHLSL::isSingleStatement(TIntermNode *node)
2958{
2959 TIntermAggregate *aggregate = node->getAsAggregate();
2960
2961 if (aggregate)
2962 {
2963 if (aggregate->getOp() == EOpSequence)
2964 {
2965 return false;
2966 }
2967 else
2968 {
2969 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2970 {
2971 if (!isSingleStatement(*sit))
2972 {
2973 return false;
2974 }
2975 }
2976
2977 return true;
2978 }
2979 }
2980
2981 return true;
2982}
2983
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002984// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2985// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002986bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2987{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002988 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002989 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002990
2991 // Parse loops of the form:
2992 // for(int index = initial; index [comparator] limit; index += increment)
2993 TIntermSymbol *index = NULL;
2994 TOperator comparator = EOpNull;
2995 int initial = 0;
2996 int limit = 0;
2997 int increment = 0;
2998
2999 // Parse index name and intial value
3000 if (node->getInit())
3001 {
3002 TIntermAggregate *init = node->getInit()->getAsAggregate();
3003
3004 if (init)
3005 {
3006 TIntermSequence &sequence = init->getSequence();
3007 TIntermTyped *variable = sequence[0]->getAsTyped();
3008
3009 if (variable && variable->getQualifier() == EvqTemporary)
3010 {
3011 TIntermBinary *assign = variable->getAsBinaryNode();
3012
3013 if (assign->getOp() == EOpInitialize)
3014 {
3015 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3016 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3017
3018 if (symbol && constant)
3019 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003020 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003021 {
3022 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003023 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003024 }
3025 }
3026 }
3027 }
3028 }
3029 }
3030
3031 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00003032 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003033 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003034 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003035
3036 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
3037 {
3038 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3039
3040 if (constant)
3041 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003042 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003043 {
3044 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003045 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003046 }
3047 }
3048 }
3049 }
3050
3051 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00003052 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003053 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003054 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3055 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003056
3057 if (binaryTerminal)
3058 {
3059 TOperator op = binaryTerminal->getOp();
3060 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3061
3062 if (constant)
3063 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003064 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003065 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003066 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003067
3068 switch (op)
3069 {
3070 case EOpAddAssign: increment = value; break;
3071 case EOpSubAssign: increment = -value; break;
3072 default: UNIMPLEMENTED();
3073 }
3074 }
3075 }
3076 }
3077 else if (unaryTerminal)
3078 {
3079 TOperator op = unaryTerminal->getOp();
3080
3081 switch (op)
3082 {
3083 case EOpPostIncrement: increment = 1; break;
3084 case EOpPostDecrement: increment = -1; break;
3085 case EOpPreIncrement: increment = 1; break;
3086 case EOpPreDecrement: increment = -1; break;
3087 default: UNIMPLEMENTED();
3088 }
3089 }
3090 }
3091
3092 if (index != NULL && comparator != EOpNull && increment != 0)
3093 {
3094 if (comparator == EOpLessThanEqual)
3095 {
3096 comparator = EOpLessThan;
3097 limit += 1;
3098 }
3099
3100 if (comparator == EOpLessThan)
3101 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00003102 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003103
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003104 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003105 {
3106 return false; // Not an excessive loop
3107 }
3108
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003109 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
3110 mExcessiveLoopIndex = index;
3111
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003112 out << "{int ";
3113 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003114 out << ";\n"
3115 "bool Break";
3116 index->traverse(this);
3117 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003118
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003119 bool firstLoopFragment = true;
3120
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003121 while (iterations > 0)
3122 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003123 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003124
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003125 if (!firstLoopFragment)
3126 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003127 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003128 index->traverse(this);
3129 out << ") {\n";
3130 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00003131
3132 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
3133 {
3134 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
3135 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003136
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003137 // for(int index = initial; index < clampedLimit; index += increment)
3138
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003139 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003140 index->traverse(this);
3141 out << " = ";
3142 out << initial;
3143
3144 out << "; ";
3145 index->traverse(this);
3146 out << " < ";
3147 out << clampedLimit;
3148
3149 out << "; ";
3150 index->traverse(this);
3151 out << " += ";
3152 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003153 out << ")\n";
3154
Jamie Madill075edd82013-07-08 13:30:19 -04003155 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003156 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003157
3158 if (node->getBody())
3159 {
3160 node->getBody()->traverse(this);
3161 }
3162
Jamie Madill075edd82013-07-08 13:30:19 -04003163 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003164 out << ";}\n";
3165
3166 if (!firstLoopFragment)
3167 {
3168 out << "}\n";
3169 }
3170
3171 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003172
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003173 initial += MAX_LOOP_ITERATIONS * increment;
3174 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003175 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003176
3177 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003178
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003179 mExcessiveLoopIndex = restoreIndex;
3180
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003181 return true;
3182 }
3183 else UNIMPLEMENTED();
3184 }
3185
3186 return false; // Not handled as an excessive loop
3187}
3188
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003189void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003190{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00003191 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003192
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003193 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003194 {
3195 out << preString;
3196 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003197 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003198 {
3199 out << inString;
3200 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003201 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003202 {
3203 out << postString;
3204 }
3205}
3206
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003207void OutputHLSL::outputLineDirective(int line)
3208{
3209 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
3210 {
baustin@google.com8ab69842011-06-02 21:53:45 +00003211 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003212 mBody << "#line " << line;
3213
3214 if (mContext.sourcePath)
3215 {
3216 mBody << " \"" << mContext.sourcePath << "\"";
3217 }
3218
3219 mBody << "\n";
3220 }
3221}
3222
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003223TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3224{
3225 TQualifier qualifier = symbol->getQualifier();
3226 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003227 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003228
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003229 if (name.empty()) // HLSL demands named arguments, also for prototypes
3230 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00003231 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003232 }
3233 else
3234 {
3235 name = decorate(name);
3236 }
3237
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003238 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
3239 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003240 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
3241 qualifierString(qualifier) + " " + samplerString(type) + " sampler_" + name + arrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003242 }
3243
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003244 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003245}
3246
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003247TString OutputHLSL::interpolationString(TQualifier qualifier)
3248{
3249 switch(qualifier)
3250 {
3251 case EvqVaryingIn: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003252 case EvqFragmentIn: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003253 case EvqInvariantVaryingIn: return "";
3254 case EvqSmoothIn: return "linear";
3255 case EvqFlatIn: return "nointerpolation";
3256 case EvqCentroidIn: return "centroid";
3257 case EvqVaryingOut: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003258 case EvqVertexOut: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003259 case EvqInvariantVaryingOut: return "";
3260 case EvqSmoothOut: return "linear";
3261 case EvqFlatOut: return "nointerpolation";
3262 case EvqCentroidOut: return "centroid";
3263 default: UNREACHABLE();
3264 }
3265
3266 return "";
3267}
3268
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003269TString OutputHLSL::qualifierString(TQualifier qualifier)
3270{
3271 switch(qualifier)
3272 {
3273 case EvqIn: return "in";
3274 case EvqOut: return "out";
3275 case EvqInOut: return "inout";
3276 case EvqConstReadOnly: return "const";
3277 default: UNREACHABLE();
3278 }
3279
3280 return "";
3281}
3282
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003283TString OutputHLSL::typeString(const TType &type)
3284{
Jamie Madill98493dd2013-07-08 14:39:03 -04003285 const TStructure* structure = type.getStruct();
3286 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003287 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003288 const TString& typeName = structure->name();
3289 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003290 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003291 return structLookup(typeName);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003292 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003293 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003294 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003295 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003296 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003297 }
3298 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003299 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003300 int cols = type.getCols();
3301 int rows = type.getRows();
3302 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003303 }
3304 else
3305 {
3306 switch (type.getBasicType())
3307 {
3308 case EbtFloat:
3309 switch (type.getNominalSize())
3310 {
3311 case 1: return "float";
3312 case 2: return "float2";
3313 case 3: return "float3";
3314 case 4: return "float4";
3315 }
3316 case EbtInt:
3317 switch (type.getNominalSize())
3318 {
3319 case 1: return "int";
3320 case 2: return "int2";
3321 case 3: return "int3";
3322 case 4: return "int4";
3323 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003324 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04003325 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003326 {
3327 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003328 case 2: return "uint2";
3329 case 3: return "uint3";
3330 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003331 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003332 case EbtBool:
3333 switch (type.getNominalSize())
3334 {
3335 case 1: return "bool";
3336 case 2: return "bool2";
3337 case 3: return "bool3";
3338 case 4: return "bool4";
3339 }
3340 case EbtVoid:
3341 return "void";
3342 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003343 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04003344 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003345 case EbtSampler2DArray:
3346 case EbtISampler2DArray:
3347 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003348 return "sampler2D";
3349 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003350 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04003351 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003352 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00003353 case EbtSamplerExternalOES:
3354 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00003355 default:
3356 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003357 }
3358 }
3359
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003360 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003361 return "<unknown type>";
3362}
3363
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003364TString OutputHLSL::textureString(const TType &type)
3365{
3366 switch (type.getBasicType())
3367 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003368 case EbtSampler2D: return "Texture2D";
3369 case EbtSamplerCube: return "TextureCube";
3370 case EbtSamplerExternalOES: return "Texture2D";
3371 case EbtSampler2DArray: return "Texture2DArray";
3372 case EbtSampler3D: return "Texture3D";
3373 case EbtISampler2D: return "Texture2D<int4>";
3374 case EbtISampler3D: return "Texture3D<int4>";
Nicolas Capens0027fa92014-02-20 14:26:42 -05003375 case EbtISamplerCube: return "Texture2DArray<int4>";
Nicolas Capenscb127d32013-07-15 17:26:18 -04003376 case EbtISampler2DArray: return "Texture2DArray<int4>";
3377 case EbtUSampler2D: return "Texture2D<uint4>";
3378 case EbtUSampler3D: return "Texture3D<uint4>";
Nicolas Capens0027fa92014-02-20 14:26:42 -05003379 case EbtUSamplerCube: return "Texture2DArray<uint4>";
Nicolas Capenscb127d32013-07-15 17:26:18 -04003380 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
3381 case EbtSampler2DShadow: return "Texture2D";
3382 case EbtSamplerCubeShadow: return "TextureCube";
3383 case EbtSampler2DArrayShadow: return "Texture2DArray";
3384 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003385 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04003386
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003387 return "<unknown texture type>";
3388}
3389
Nicolas Capenscb127d32013-07-15 17:26:18 -04003390TString OutputHLSL::samplerString(const TType &type)
3391{
3392 if (IsShadowSampler(type.getBasicType()))
3393 {
3394 return "SamplerComparisonState";
3395 }
3396 else
3397 {
3398 return "SamplerState";
3399 }
3400}
3401
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003402TString OutputHLSL::arrayString(const TType &type)
3403{
3404 if (!type.isArray())
3405 {
3406 return "";
3407 }
3408
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003409 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003410}
3411
3412TString OutputHLSL::initializer(const TType &type)
3413{
3414 TString string;
3415
Jamie Madill94bf7f22013-07-08 13:31:15 -04003416 size_t size = type.getObjectSize();
3417 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003418 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003419 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003420
Jamie Madill94bf7f22013-07-08 13:31:15 -04003421 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003422 {
3423 string += ", ";
3424 }
3425 }
3426
daniel@transgaming.comead23042010-04-29 03:35:36 +00003427 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003428}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003429
Jamie Madill98493dd2013-07-08 14:39:03 -04003430TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003431{
Jamie Madill98493dd2013-07-08 14:39:03 -04003432 const TFieldList &fields = structure.fields();
3433 const bool isNameless = (structure.name() == "");
3434 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003435 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3436
Jamie Madill98493dd2013-07-08 14:39:03 -04003437 TString string;
3438 string += declareString + "\n"
3439 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003440
Jamie Madillc835df62013-06-21 09:15:32 -04003441 int elementIndex = 0;
3442
Jamie Madill9cf6c072013-06-20 11:55:53 -04003443 for (unsigned int i = 0; i < fields.size(); i++)
3444 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003445 const TField &field = *fields[i];
3446 const TType &fieldType = *field.type();
3447 const TStructure *fieldStruct = fieldType.getStruct();
3448 const TString &fieldTypeString = fieldStruct ? structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) : typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003449
Jamie Madillc835df62013-06-21 09:15:32 -04003450 if (useStd140Packing)
3451 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003452 string += std140PrePaddingString(*field.type(), &elementIndex);
Jamie Madillc835df62013-06-21 09:15:32 -04003453 }
3454
Jamie Madill98493dd2013-07-08 14:39:03 -04003455 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003456
3457 if (useStd140Packing)
3458 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003459 string += std140PostPaddingString(*field.type(), useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003460 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003461 }
3462
3463 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003464 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003465
Jamie Madille4075c92013-06-21 09:15:32 -04003466 // Add remaining element index to the global map, for use with nested structs in standard layouts
3467 if (useStd140Packing)
3468 {
3469 mStd140StructElementIndexes[structName] = elementIndex;
3470 }
3471
Jamie Madill98493dd2013-07-08 14:39:03 -04003472 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003473}
3474
Jamie Madill98493dd2013-07-08 14:39:03 -04003475TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003476{
Jamie Madill98493dd2013-07-08 14:39:03 -04003477 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003478 {
3479 return "";
3480 }
3481
3482 TString prefix = "";
3483
3484 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3485 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003486
3487 if (useStd140Packing)
3488 {
3489 prefix += "std";
3490 }
3491
Jamie Madill9cf6c072013-06-20 11:55:53 -04003492 if (useHLSLRowMajorPacking)
3493 {
Jamie Madillc835df62013-06-21 09:15:32 -04003494 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003495 prefix += "rm";
3496 }
3497
Jamie Madill98493dd2013-07-08 14:39:03 -04003498 return prefix + structLookup(structure.name());
Jamie Madill9cf6c072013-06-20 11:55:53 -04003499}
3500
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003501void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003502{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003503 if (name == "")
3504 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003505 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003506 }
3507
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003508 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
3509 {
3510 return; // Already added
3511 }
3512
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003513 TType ctorType = type;
3514 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003515 ctorType.setPrecision(EbpHigh);
3516 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003517
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003518 TString ctorName = type.getStruct() ? decorate(name) : name;
3519
3520 typedef std::vector<TType> ParameterArray;
3521 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003522
Jamie Madill98493dd2013-07-08 14:39:03 -04003523 const TStructure* structure = type.getStruct();
3524 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003525 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003526 mStructNames.insert(decorate(name));
3527
Jamie Madill98493dd2013-07-08 14:39:03 -04003528 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003529
Jamie Madill98493dd2013-07-08 14:39:03 -04003530 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003531 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003532 // Add row-major packed struct for interface blocks
3533 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003534 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003535 "#pragma pack_matrix(column_major)\n";
3536
Jamie Madill98493dd2013-07-08 14:39:03 -04003537 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003538 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003539 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003540 "#pragma pack_matrix(column_major)\n";
3541
Jamie Madill98493dd2013-07-08 14:39:03 -04003542 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003543 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003544 mStructDeclarations.push_back(std140String);
3545 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003546 }
3547
Jamie Madill98493dd2013-07-08 14:39:03 -04003548 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003549 for (unsigned int i = 0; i < fields.size(); i++)
3550 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003551 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003552 }
3553 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003554 else if (parameters)
3555 {
3556 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3557 {
3558 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3559 }
3560 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003561 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003562
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003563 TString constructor;
3564
3565 if (ctorType.getStruct())
3566 {
3567 constructor += ctorName + " " + ctorName + "_ctor(";
3568 }
3569 else // Built-in type
3570 {
3571 constructor += typeString(ctorType) + " " + ctorName + "(";
3572 }
3573
3574 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3575 {
3576 const TType &type = ctorParameters[parameter];
3577
3578 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3579
3580 if (parameter < ctorParameters.size() - 1)
3581 {
3582 constructor += ", ";
3583 }
3584 }
3585
3586 constructor += ")\n"
3587 "{\n";
3588
3589 if (ctorType.getStruct())
3590 {
3591 constructor += " " + ctorName + " structure = {";
3592 }
3593 else
3594 {
3595 constructor += " return " + typeString(ctorType) + "(";
3596 }
3597
3598 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3599 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003600 int rows = ctorType.getRows();
3601 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003602 const TType &parameter = ctorParameters[0];
3603
3604 if (parameter.isScalar())
3605 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003606 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003607 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003608 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003609 {
3610 constructor += TString((row == col) ? "x0" : "0.0");
3611
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003612 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003613 {
3614 constructor += ", ";
3615 }
3616 }
3617 }
3618 }
3619 else if (parameter.isMatrix())
3620 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003621 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003622 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003623 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003624 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003625 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003626 {
3627 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3628 }
3629 else
3630 {
3631 constructor += TString((row == col) ? "1.0" : "0.0");
3632 }
3633
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003634 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003635 {
3636 constructor += ", ";
3637 }
3638 }
3639 }
3640 }
3641 else UNREACHABLE();
3642 }
3643 else
3644 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003645 size_t remainingComponents = ctorType.getObjectSize();
3646 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003647
3648 while (remainingComponents > 0)
3649 {
3650 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003651 const size_t parameterSize = parameter.getObjectSize();
3652 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003653
3654 constructor += "x" + str(parameterIndex);
3655
3656 if (parameter.isScalar())
3657 {
3658 remainingComponents -= parameter.getObjectSize();
3659 }
3660 else if (parameter.isVector())
3661 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003662 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003663 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003664 ASSERT(parameterSize <= remainingComponents);
3665 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003666 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003667 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003668 {
3669 switch (remainingComponents)
3670 {
3671 case 1: constructor += ".x"; break;
3672 case 2: constructor += ".xy"; break;
3673 case 3: constructor += ".xyz"; break;
3674 case 4: constructor += ".xyzw"; break;
3675 default: UNREACHABLE();
3676 }
3677
3678 remainingComponents = 0;
3679 }
3680 else UNREACHABLE();
3681 }
3682 else if (parameter.isMatrix() || parameter.getStruct())
3683 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003684 ASSERT(remainingComponents == parameterSize || moreParameters);
3685 ASSERT(parameterSize <= remainingComponents);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003686
Jamie Madill94bf7f22013-07-08 13:31:15 -04003687 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003688 }
3689 else UNREACHABLE();
3690
3691 if (moreParameters)
3692 {
3693 parameterIndex++;
3694 }
3695
3696 if (remainingComponents)
3697 {
3698 constructor += ", ";
3699 }
3700 }
3701 }
3702
3703 if (ctorType.getStruct())
3704 {
3705 constructor += "};\n"
3706 " return structure;\n"
3707 "}\n";
3708 }
3709 else
3710 {
3711 constructor += ");\n"
3712 "}\n";
3713 }
3714
daniel@transgaming.com63691862010-04-29 03:32:42 +00003715 mConstructors.insert(constructor);
3716}
3717
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003718const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3719{
3720 TInfoSinkBase &out = mBody;
3721
Jamie Madill98493dd2013-07-08 14:39:03 -04003722 const TStructure* structure = type.getStruct();
3723 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003724 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003725 out << structLookup(structure->name()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003726
Jamie Madill98493dd2013-07-08 14:39:03 -04003727 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003728
Jamie Madill98493dd2013-07-08 14:39:03 -04003729 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003730 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003731 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003732
3733 constUnion = writeConstantUnion(*fieldType, constUnion);
3734
Jamie Madill98493dd2013-07-08 14:39:03 -04003735 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003736 {
3737 out << ", ";
3738 }
3739 }
3740
3741 out << ")";
3742 }
3743 else
3744 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003745 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003746 bool writeType = size > 1;
3747
3748 if (writeType)
3749 {
3750 out << typeString(type) << "(";
3751 }
3752
Jamie Madill94bf7f22013-07-08 13:31:15 -04003753 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003754 {
3755 switch (constUnion->getType())
3756 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003757 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003758 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003759 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003760 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003761 default: UNREACHABLE();
3762 }
3763
3764 if (i != size - 1)
3765 {
3766 out << ", ";
3767 }
3768 }
3769
3770 if (writeType)
3771 {
3772 out << ")";
3773 }
3774 }
3775
3776 return constUnion;
3777}
3778
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003779TString OutputHLSL::scopeString(unsigned int depthLimit)
3780{
3781 TString string;
3782
3783 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3784 {
3785 string += "_" + str(i);
3786 }
3787
3788 return string;
3789}
3790
3791TString OutputHLSL::scopedStruct(const TString &typeName)
3792{
3793 if (typeName == "")
3794 {
3795 return typeName;
3796 }
3797
3798 return typeName + scopeString(mScopeDepth);
3799}
3800
3801TString OutputHLSL::structLookup(const TString &typeName)
3802{
3803 for (int depth = mScopeDepth; depth >= 0; depth--)
3804 {
3805 TString scopedName = decorate(typeName + scopeString(depth));
3806
3807 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3808 {
3809 if (*structName == scopedName)
3810 {
3811 return scopedName;
3812 }
3813 }
3814 }
3815
3816 UNREACHABLE(); // Should have found a matching constructor
3817
3818 return typeName;
3819}
3820
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003821TString OutputHLSL::decorate(const TString &string)
3822{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003823 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003824 {
3825 return "_" + string;
3826 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003827
3828 return string;
3829}
3830
apatrick@chromium.org65756022012-01-17 21:45:38 +00003831TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003832{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003833 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003834 {
3835 return "ex_" + string;
3836 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003837
3838 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003839}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003840
Jamie Madill98493dd2013-07-08 14:39:03 -04003841TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003842{
Jamie Madill98493dd2013-07-08 14:39:03 -04003843 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003844 {
3845 return decorate(string);
3846 }
3847
3848 return string;
3849}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003850
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003851void OutputHLSL::declareInterfaceBlockField(const TType &type, const TString &name, std::vector<InterfaceBlockField>& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003852{
Jamie Madill98493dd2013-07-08 14:39:03 -04003853 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003854
3855 if (!structure)
3856 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003857 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003858 InterfaceBlockField field(glVariableType(type), glVariablePrecision(type), name.c_str(),
3859 (unsigned int)type.getArraySize(), isRowMajorMatrix);
3860 output.push_back(field);
3861 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003862 else
3863 {
Jamie Madill28167c62013-08-30 13:21:10 -04003864 InterfaceBlockField structField(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), false);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003865
3866 const TFieldList &fields = structure->fields();
3867
3868 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3869 {
3870 TField *field = fields[fieldIndex];
3871 TType *fieldType = field->type();
3872
3873 // make sure to copy matrix packing information
3874 fieldType->setLayoutQualifier(type.getLayoutQualifier());
3875
3876 declareInterfaceBlockField(*fieldType, field->name(), structField.fields);
3877 }
3878
3879 output.push_back(structField);
3880 }
3881}
3882
Jamie Madillc2141fb2013-08-30 13:21:08 -04003883Uniform OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, std::vector<Uniform>& output)
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003884{
3885 const TStructure *structure = type.getStruct();
3886
3887 if (!structure)
3888 {
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003889 Uniform uniform(glVariableType(type), glVariablePrecision(type), name.c_str(),
Jamie Madill56093782013-08-30 13:21:11 -04003890 (unsigned int)type.getArraySize(), (unsigned int)registerIndex, 0);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003891 output.push_back(uniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003892
3893 return uniform;
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003894 }
3895 else
3896 {
Jamie Madill56093782013-08-30 13:21:11 -04003897 Uniform structUniform(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(),
3898 (unsigned int)registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003899
Jamie Madill98493dd2013-07-08 14:39:03 -04003900 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003901
Jamie Madill98493dd2013-07-08 14:39:03 -04003902 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003903 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003904 TField *field = fields[fieldIndex];
3905 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003906
Jamie Madill56093782013-08-30 13:21:11 -04003907 declareUniformToList(*fieldType, field->name(), GL_INVALID_INDEX, structUniform.fields);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003908 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003909
Jamie Madill56093782013-08-30 13:21:11 -04003910 // assign register offset information -- this will override the information in any sub-structures.
3911 HLSLVariableGetRegisterInfo(registerIndex, &structUniform);
3912
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003913 output.push_back(structUniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003914
3915 return structUniform;
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003916 }
3917}
3918
Jamie Madill139b9092013-08-30 13:21:06 -04003919InterpolationType getInterpolationType(TQualifier qualifier)
3920{
3921 switch (qualifier)
3922 {
3923 case EvqFlatIn:
3924 case EvqFlatOut:
3925 return INTERPOLATION_FLAT;
3926
3927 case EvqSmoothIn:
3928 case EvqSmoothOut:
3929 case EvqVertexOut:
3930 case EvqFragmentIn:
Jamie Madill384b6042013-09-13 10:06:24 -04003931 case EvqVaryingIn:
3932 case EvqVaryingOut:
Jamie Madill139b9092013-08-30 13:21:06 -04003933 return INTERPOLATION_SMOOTH;
3934
3935 case EvqCentroidIn:
3936 case EvqCentroidOut:
3937 return INTERPOLATION_CENTROID;
3938
3939 default: UNREACHABLE();
3940 return INTERPOLATION_SMOOTH;
3941 }
3942}
3943
Jamie Madill94599662013-08-30 13:21:10 -04003944void OutputHLSL::declareVaryingToList(const TType &type, TQualifier baseTypeQualifier, const TString &name, std::vector<Varying>& fieldsOut)
Jamie Madill47fdd132013-08-30 13:21:04 -04003945{
3946 const TStructure *structure = type.getStruct();
3947
Jamie Madill94599662013-08-30 13:21:10 -04003948 InterpolationType interpolation = getInterpolationType(baseTypeQualifier);
Jamie Madill47fdd132013-08-30 13:21:04 -04003949 if (!structure)
3950 {
Jamie Madill139b9092013-08-30 13:21:06 -04003951 Varying varying(glVariableType(type), glVariablePrecision(type), name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003952 fieldsOut.push_back(varying);
3953 }
3954 else
3955 {
Jamie Madill28167c62013-08-30 13:21:10 -04003956 Varying structVarying(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003957 const TFieldList &fields = structure->fields();
3958
Jamie Madill28167c62013-08-30 13:21:10 -04003959 structVarying.structName = structure->name().c_str();
3960
Jamie Madill47fdd132013-08-30 13:21:04 -04003961 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3962 {
3963 const TField &field = *fields[fieldIndex];
Jamie Madill94599662013-08-30 13:21:10 -04003964 declareVaryingToList(*field.type(), baseTypeQualifier, field.name(), structVarying.fields);
Jamie Madill47fdd132013-08-30 13:21:04 -04003965 }
3966
3967 fieldsOut.push_back(structVarying);
3968 }
3969}
3970
Jamie Madillc2141fb2013-08-30 13:21:08 -04003971int OutputHLSL::declareUniformAndAssignRegister(const TType &type, const TString &name)
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003972{
Jamie Madillc2141fb2013-08-30 13:21:08 -04003973 int registerIndex = (IsSampler(type.getBasicType()) ? mSamplerRegister : mUniformRegister);
3974
3975 const Uniform &uniform = declareUniformToList(type, name, registerIndex, mActiveUniforms);
3976
3977 if (IsSampler(type.getBasicType()))
3978 {
3979 mSamplerRegister += HLSLVariableRegisterCount(uniform);
3980 }
3981 else
3982 {
3983 mUniformRegister += HLSLVariableRegisterCount(uniform);
3984 }
3985
3986 return registerIndex;
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003987}
3988
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003989GLenum OutputHLSL::glVariableType(const TType &type)
3990{
3991 if (type.getBasicType() == EbtFloat)
3992 {
3993 if (type.isScalar())
3994 {
3995 return GL_FLOAT;
3996 }
3997 else if (type.isVector())
3998 {
3999 switch(type.getNominalSize())
4000 {
4001 case 2: return GL_FLOAT_VEC2;
4002 case 3: return GL_FLOAT_VEC3;
4003 case 4: return GL_FLOAT_VEC4;
4004 default: UNREACHABLE();
4005 }
4006 }
4007 else if (type.isMatrix())
4008 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00004009 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004010 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00004011 case 2:
4012 switch(type.getRows())
4013 {
4014 case 2: return GL_FLOAT_MAT2;
4015 case 3: return GL_FLOAT_MAT2x3;
4016 case 4: return GL_FLOAT_MAT2x4;
4017 default: UNREACHABLE();
4018 }
4019
4020 case 3:
4021 switch(type.getRows())
4022 {
4023 case 2: return GL_FLOAT_MAT3x2;
4024 case 3: return GL_FLOAT_MAT3;
4025 case 4: return GL_FLOAT_MAT3x4;
4026 default: UNREACHABLE();
4027 }
4028
4029 case 4:
4030 switch(type.getRows())
4031 {
4032 case 2: return GL_FLOAT_MAT4x2;
4033 case 3: return GL_FLOAT_MAT4x3;
4034 case 4: return GL_FLOAT_MAT4;
4035 default: UNREACHABLE();
4036 }
4037
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004038 default: UNREACHABLE();
4039 }
4040 }
4041 else UNREACHABLE();
4042 }
4043 else if (type.getBasicType() == EbtInt)
4044 {
4045 if (type.isScalar())
4046 {
4047 return GL_INT;
4048 }
4049 else if (type.isVector())
4050 {
4051 switch(type.getNominalSize())
4052 {
4053 case 2: return GL_INT_VEC2;
4054 case 3: return GL_INT_VEC3;
4055 case 4: return GL_INT_VEC4;
4056 default: UNREACHABLE();
4057 }
4058 }
4059 else UNREACHABLE();
4060 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004061 else if (type.getBasicType() == EbtUInt)
4062 {
4063 if (type.isScalar())
4064 {
4065 return GL_UNSIGNED_INT;
4066 }
4067 else if (type.isVector())
4068 {
Jamie Madill22d63da2013-06-07 12:45:12 -04004069 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00004070 {
4071 case 2: return GL_UNSIGNED_INT_VEC2;
4072 case 3: return GL_UNSIGNED_INT_VEC3;
4073 case 4: return GL_UNSIGNED_INT_VEC4;
4074 default: UNREACHABLE();
4075 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004076 }
4077 else UNREACHABLE();
4078 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004079 else if (type.getBasicType() == EbtBool)
4080 {
4081 if (type.isScalar())
4082 {
4083 return GL_BOOL;
4084 }
4085 else if (type.isVector())
4086 {
4087 switch(type.getNominalSize())
4088 {
4089 case 2: return GL_BOOL_VEC2;
4090 case 3: return GL_BOOL_VEC3;
4091 case 4: return GL_BOOL_VEC4;
4092 default: UNREACHABLE();
4093 }
4094 }
4095 else UNREACHABLE();
4096 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004097
4098 switch(type.getBasicType())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004099 {
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004100 case EbtSampler2D: return GL_SAMPLER_2D;
4101 case EbtSampler3D: return GL_SAMPLER_3D;
4102 case EbtSamplerCube: return GL_SAMPLER_CUBE;
4103 case EbtSampler2DArray: return GL_SAMPLER_2D_ARRAY;
4104 case EbtISampler2D: return GL_INT_SAMPLER_2D;
4105 case EbtISampler3D: return GL_INT_SAMPLER_3D;
4106 case EbtISamplerCube: return GL_INT_SAMPLER_CUBE;
4107 case EbtISampler2DArray: return GL_INT_SAMPLER_2D_ARRAY;
4108 case EbtUSampler2D: return GL_UNSIGNED_INT_SAMPLER_2D;
4109 case EbtUSampler3D: return GL_UNSIGNED_INT_SAMPLER_3D;
4110 case EbtUSamplerCube: return GL_UNSIGNED_INT_SAMPLER_CUBE;
4111 case EbtUSampler2DArray: return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
4112 case EbtSampler2DShadow: return GL_SAMPLER_2D_SHADOW;
4113 case EbtSamplerCubeShadow: return GL_SAMPLER_CUBE_SHADOW;
4114 case EbtSampler2DArrayShadow: return GL_SAMPLER_2D_ARRAY_SHADOW;
4115 default: UNREACHABLE();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004116 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004117
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004118 return GL_NONE;
4119}
4120
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004121GLenum OutputHLSL::glVariablePrecision(const TType &type)
4122{
4123 if (type.getBasicType() == EbtFloat)
4124 {
4125 switch (type.getPrecision())
4126 {
4127 case EbpHigh: return GL_HIGH_FLOAT;
4128 case EbpMedium: return GL_MEDIUM_FLOAT;
4129 case EbpLow: return GL_LOW_FLOAT;
4130 case EbpUndefined:
4131 // Should be defined as the default precision by the parser
4132 default: UNREACHABLE();
4133 }
4134 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004135 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004136 {
4137 switch (type.getPrecision())
4138 {
4139 case EbpHigh: return GL_HIGH_INT;
4140 case EbpMedium: return GL_MEDIUM_INT;
4141 case EbpLow: return GL_LOW_INT;
4142 case EbpUndefined:
4143 // Should be defined as the default precision by the parser
4144 default: UNREACHABLE();
4145 }
4146 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004147
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00004148 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004149 return GL_NONE;
4150}
4151
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004152bool OutputHLSL::isVaryingOut(TQualifier qualifier)
4153{
4154 switch(qualifier)
4155 {
4156 case EvqVaryingOut:
4157 case EvqInvariantVaryingOut:
4158 case EvqSmoothOut:
4159 case EvqFlatOut:
4160 case EvqCentroidOut:
Jamie Madill19571812013-08-12 15:26:34 -07004161 case EvqVertexOut:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004162 return true;
4163 }
4164
4165 return false;
4166}
4167
4168bool OutputHLSL::isVaryingIn(TQualifier qualifier)
4169{
4170 switch(qualifier)
4171 {
4172 case EvqVaryingIn:
4173 case EvqInvariantVaryingIn:
4174 case EvqSmoothIn:
4175 case EvqFlatIn:
4176 case EvqCentroidIn:
Jamie Madill19571812013-08-12 15:26:34 -07004177 case EvqFragmentIn:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004178 return true;
4179 }
4180
4181 return false;
4182}
4183
4184bool OutputHLSL::isVarying(TQualifier qualifier)
4185{
4186 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
4187}
4188
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004189}