blob: 2d7818b64b4632ad0cdc8f736d8ca83e2fea2e8d [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/OutputHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
alokp@chromium.org79fb1012012-04-26 21:07:39 +00009#include "common/angleutils.h"
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +000010#include "common/utilities.h"
Jamie Madill834e8b72014-04-11 13:33:58 -040011#include "common/blocklayout.h"
Geoff Lang17732822013-08-29 13:46:49 -040012#include "compiler/translator/compilerdebug.h"
13#include "compiler/translator/InfoSink.h"
14#include "compiler/translator/DetectDiscontinuity.h"
15#include "compiler/translator/SearchSymbol.h"
16#include "compiler/translator/UnfoldShortCircuit.h"
Geoff Lang17732822013-08-29 13:46:49 -040017#include "compiler/translator/FlagStd140Structs.h"
Jamie Madill3c9eeb92013-11-04 11:09:26 -050018#include "compiler/translator/NodeSearch.h"
Jamie Madille53c98b2014-02-03 11:57:13 -050019#include "compiler/translator/RewriteElseBlocks.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000020
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000021#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000022#include <cfloat>
23#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000024
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000025namespace sh
26{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000027
Nicolas Capense0ba27a2013-06-24 16:10:52 -040028TString OutputHLSL::TextureFunction::name() const
29{
30 TString name = "gl_texture";
31
Nicolas Capens6d232bb2013-07-08 15:56:38 -040032 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040033 {
34 name += "2D";
35 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040036 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040037 {
38 name += "3D";
39 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040040 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040041 {
42 name += "Cube";
43 }
44 else UNREACHABLE();
45
46 if (proj)
47 {
48 name += "Proj";
49 }
50
Nicolas Capensb1f45b72013-12-19 17:37:19 -050051 if (offset)
52 {
53 name += "Offset";
54 }
55
Nicolas Capens75fb4752013-07-10 15:14:47 -040056 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040057 {
Nicolas Capensfc014542014-02-18 14:47:13 -050058 case IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040059 case BIAS: break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050060 case LOD: name += "Lod"; break;
61 case LOD0: name += "Lod0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040062 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050063 case SIZE: name += "Size"; break;
64 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -050065 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040066 default: UNREACHABLE();
67 }
68
69 return name + "(";
70}
71
Jamie Madillc2141fb2013-08-30 13:21:08 -040072const char *RegisterPrefix(const TType &type)
73{
74 if (IsSampler(type.getBasicType()))
75 {
76 return "s";
77 }
78 else
79 {
80 return "c";
81 }
82}
83
Nicolas Capense0ba27a2013-06-24 16:10:52 -040084bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
85{
86 if (sampler < rhs.sampler) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040087 if (sampler > rhs.sampler) return false;
88
Nicolas Capense0ba27a2013-06-24 16:10:52 -040089 if (coords < rhs.coords) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040090 if (coords > rhs.coords) return false;
91
Nicolas Capense0ba27a2013-06-24 16:10:52 -040092 if (!proj && rhs.proj) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040093 if (proj && !rhs.proj) return false;
94
95 if (!offset && rhs.offset) return true;
96 if (offset && !rhs.offset) return false;
97
Nicolas Capens75fb4752013-07-10 15:14:47 -040098 if (method < rhs.method) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040099 if (method > rhs.method) return false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400100
101 return false;
102}
103
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000104OutputHLSL::OutputHLSL(TParseContext &context, const ShBuiltInResources& resources, ShShaderOutput outputType)
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +0000105 : TIntermTraverser(true, true, true), mContext(context), mOutputType(outputType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000107 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000108 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000109
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000110 mUsesFragColor = false;
111 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000112 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000113 mUsesFragCoord = false;
114 mUsesPointCoord = false;
115 mUsesFrontFacing = false;
116 mUsesPointSize = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400117 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000118 mUsesXor = false;
119 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000120 mUsesMod2v = false;
121 mUsesMod2f = false;
122 mUsesMod3v = false;
123 mUsesMod3f = false;
124 mUsesMod4v = false;
125 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000126 mUsesFaceforward1 = false;
127 mUsesFaceforward2 = false;
128 mUsesFaceforward3 = false;
129 mUsesFaceforward4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000130 mUsesAtan2_1 = false;
131 mUsesAtan2_2 = false;
132 mUsesAtan2_3 = false;
133 mUsesAtan2_4 = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500134 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400135 mUsesNestedBreak = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000136
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000137 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
138
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000139 mScopeDepth = 0;
140
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000141 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000142
143 mContainsLoopDiscontinuity = false;
144 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000145 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400146 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000147
148 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000149
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000150 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000151 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000152 if (mContext.shaderType == SH_FRAGMENT_SHADER)
153 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000154 mUniformRegister = 3; // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000155 }
156 else
157 {
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000158 mUniformRegister = 2; // Reserve registers for dx_DepthRange and dx_ViewAdjust
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000159 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000160 }
161 else
162 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000163 mUniformRegister = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000164 }
165
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000166 mSamplerRegister = 0;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000167 mInterfaceBlockRegister = 2; // Reserve registers for the default uniform block and driver constants
Jamie Madill574d9dd2013-06-20 11:55:56 -0400168 mPaddingCounter = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000169}
170
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000171OutputHLSL::~OutputHLSL()
172{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000173 delete mUnfoldShortCircuit;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000174}
175
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000176void OutputHLSL::output()
177{
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +0000178 mContainsLoopDiscontinuity = mContext.shaderType == SH_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400179 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
180 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000181
Jamie Madille53c98b2014-02-03 11:57:13 -0500182 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
183 // use a vertex attribute as a condition, and some related computation in the else block.
184 if (mOutputType == SH_HLSL9_OUTPUT && mContext.shaderType == SH_VERTEX_SHADER)
185 {
186 RewriteElseBlocks(mContext.treeRoot);
187 }
188
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000189 mContext.treeRoot->traverse(this); // Output the body first to determine what has to go in the header
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000190 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000191
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000192 mContext.infoSink().obj << mHeader.c_str();
193 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000194}
195
Jamie Madill570e04d2013-06-21 09:15:33 -0400196void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
197{
198 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
199 {
200 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
201
202 // This will mark the necessary block elements as referenced
203 flaggedNode->traverse(this);
204 TString structName(mBody.c_str());
205 mBody.erase();
206
207 mFlaggedStructOriginalNames[flaggedNode] = structName;
208
209 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
210 {
211 structName.erase(pos, 1);
212 }
213
214 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
215 }
216}
217
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000218TInfoSinkBase &OutputHLSL::getBodyStream()
219{
220 return mBody;
221}
222
Jamie Madill834e8b72014-04-11 13:33:58 -0400223const std::vector<gl::Uniform> &OutputHLSL::getUniforms()
daniel@transgaming.com043da132012-12-20 21:12:22 +0000224{
225 return mActiveUniforms;
226}
227
Jamie Madill834e8b72014-04-11 13:33:58 -0400228const std::vector<gl::InterfaceBlock> &OutputHLSL::getInterfaceBlocks() const
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000229{
230 return mActiveInterfaceBlocks;
231}
232
Jamie Madill834e8b72014-04-11 13:33:58 -0400233const std::vector<gl::Attribute> &OutputHLSL::getOutputVariables() const
Jamie Madill46131a32013-06-20 11:55:50 -0400234{
235 return mActiveOutputVariables;
236}
237
Jamie Madill834e8b72014-04-11 13:33:58 -0400238const std::vector<gl::Attribute> &OutputHLSL::getAttributes() const
Jamie Madilldefb6742013-06-20 11:55:51 -0400239{
240 return mActiveAttributes;
241}
242
Jamie Madill834e8b72014-04-11 13:33:58 -0400243const std::vector<gl::Varying> &OutputHLSL::getVaryings() const
Jamie Madill47fdd132013-08-30 13:21:04 -0400244{
245 return mActiveVaryings;
246}
247
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000248int OutputHLSL::vectorSize(const TType &type) const
249{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000250 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000251 int arraySize = type.isArray() ? type.getArraySize() : 1;
252
253 return elementSize * arraySize;
254}
255
Jamie Madill98493dd2013-07-08 14:39:03 -0400256TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, const TField &field)
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000257{
Jamie Madill98493dd2013-07-08 14:39:03 -0400258 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000259 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400260 return interfaceBlock.name() + "." + field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000261 }
262 else
263 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400264 return field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000265 }
266}
267
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000268TString OutputHLSL::decoratePrivate(const TString &privateText)
269{
270 return "dx_" + privateText;
271}
272
Jamie Madill98493dd2013-07-08 14:39:03 -0400273TString OutputHLSL::interfaceBlockStructNameString(const TInterfaceBlock &interfaceBlock)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000274{
Jamie Madill98493dd2013-07-08 14:39:03 -0400275 return decoratePrivate(interfaceBlock.name()) + "_type";
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000276}
277
Jamie Madill98493dd2013-07-08 14:39:03 -0400278TString OutputHLSL::interfaceBlockInstanceString(const TInterfaceBlock& interfaceBlock, unsigned int arrayIndex)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000279{
Jamie Madill98493dd2013-07-08 14:39:03 -0400280 if (!interfaceBlock.hasInstanceName())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000281 {
282 return "";
283 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400284 else if (interfaceBlock.isArray())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000285 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400286 return decoratePrivate(interfaceBlock.instanceName()) + "_" + str(arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000287 }
288 else
289 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400290 return decorate(interfaceBlock.instanceName());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000291 }
292}
293
Jamie Madill98493dd2013-07-08 14:39:03 -0400294TString OutputHLSL::interfaceBlockFieldTypeString(const TField &field, TLayoutBlockStorage blockStorage)
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000295{
Jamie Madill98493dd2013-07-08 14:39:03 -0400296 const TType &fieldType = *field.type();
297 const TLayoutMatrixPacking matrixPacking = fieldType.getLayoutQualifier().matrixPacking;
Jamie Madill529077d2013-06-20 11:55:54 -0400298 ASSERT(matrixPacking != EmpUnspecified);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000299
Jamie Madill98493dd2013-07-08 14:39:03 -0400300 if (fieldType.isMatrix())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000301 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400302 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill529077d2013-06-20 11:55:54 -0400303 const TString &matrixPackString = (matrixPacking == EmpRowMajor ? "column_major" : "row_major");
Jamie Madill98493dd2013-07-08 14:39:03 -0400304 return matrixPackString + " " + typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000305 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400306 else if (fieldType.getStruct())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000307 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400308 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill98493dd2013-07-08 14:39:03 -0400309 return structureTypeName(*fieldType.getStruct(), matrixPacking == EmpColumnMajor, blockStorage == EbsStd140);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000310 }
311 else
312 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400313 return typeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000314 }
315}
316
Jamie Madill98493dd2013-07-08 14:39:03 -0400317TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, TLayoutBlockStorage blockStorage)
318{
319 TString hlsl;
320
321 int elementIndex = 0;
322
323 for (unsigned int typeIndex = 0; typeIndex < interfaceBlock.fields().size(); typeIndex++)
324 {
325 const TField &field = *interfaceBlock.fields()[typeIndex];
326 const TType &fieldType = *field.type();
327
328 if (blockStorage == EbsStd140)
329 {
330 // 2 and 3 component vector types in some cases need pre-padding
331 hlsl += std140PrePaddingString(fieldType, &elementIndex);
332 }
333
334 hlsl += " " + interfaceBlockFieldTypeString(field, blockStorage) +
335 " " + decorate(field.name()) + arrayString(fieldType) + ";\n";
336
337 // must pad out after matrices and arrays, where HLSL usually allows itself room to pack stuff
338 if (blockStorage == EbsStd140)
339 {
340 const bool useHLSLRowMajorPacking = (fieldType.getLayoutQualifier().matrixPacking == EmpColumnMajor);
341 hlsl += std140PostPaddingString(fieldType, useHLSLRowMajorPacking);
342 }
343 }
344
345 return hlsl;
346}
347
348TString OutputHLSL::interfaceBlockStructString(const TInterfaceBlock &interfaceBlock)
349{
350 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
351
352 return "struct " + interfaceBlockStructNameString(interfaceBlock) + "\n"
353 "{\n" +
354 interfaceBlockFieldString(interfaceBlock, blockStorage) +
355 "};\n\n";
356}
357
358TString OutputHLSL::interfaceBlockString(const TInterfaceBlock &interfaceBlock, unsigned int registerIndex, unsigned int arrayIndex)
359{
360 const TString &arrayIndexString = (arrayIndex != GL_INVALID_INDEX ? decorate(str(arrayIndex)) : "");
361 const TString &blockName = interfaceBlock.name() + arrayIndexString;
362 TString hlsl;
363
364 hlsl += "cbuffer " + blockName + " : register(b" + str(registerIndex) + ")\n"
365 "{\n";
366
367 if (interfaceBlock.hasInstanceName())
368 {
369 hlsl += " " + interfaceBlockStructNameString(interfaceBlock) + " " + interfaceBlockInstanceString(interfaceBlock, arrayIndex) + ";\n";
370 }
371 else
372 {
373 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
374 hlsl += interfaceBlockFieldString(interfaceBlock, blockStorage);
375 }
376
377 hlsl += "};\n\n";
378
379 return hlsl;
380}
381
Jamie Madill574d9dd2013-06-20 11:55:56 -0400382TString OutputHLSL::std140PrePaddingString(const TType &type, int *elementIndex)
383{
384 if (type.getBasicType() == EbtStruct || type.isMatrix() || type.isArray())
385 {
386 // no padding needed, HLSL will align the field to a new register
387 *elementIndex = 0;
388 return "";
389 }
390
391 const GLenum glType = glVariableType(type);
392 const int numComponents = gl::UniformComponentCount(glType);
393
394 if (numComponents >= 4)
395 {
396 // no padding needed, HLSL will align the field to a new register
397 *elementIndex = 0;
398 return "";
399 }
400
401 if (*elementIndex + numComponents > 4)
402 {
403 // no padding needed, HLSL will align the field to a new register
404 *elementIndex = numComponents;
405 return "";
406 }
407
408 TString padding;
409
410 const int alignment = numComponents == 3 ? 4 : numComponents;
411 const int paddingOffset = (*elementIndex % alignment);
412
413 if (paddingOffset != 0)
414 {
415 // padding is neccessary
416 for (int paddingIndex = paddingOffset; paddingIndex < alignment; paddingIndex++)
417 {
418 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
419 }
420
421 *elementIndex += (alignment - paddingOffset);
422 }
423
424 *elementIndex += numComponents;
425 *elementIndex %= 4;
426
427 return padding;
428}
429
Jamie Madille4075c92013-06-21 09:15:32 -0400430TString OutputHLSL::std140PostPaddingString(const TType &type, bool useHLSLRowMajorPacking)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400431{
Jamie Madillc835df62013-06-21 09:15:32 -0400432 if (!type.isMatrix() && !type.isArray() && type.getBasicType() != EbtStruct)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400433 {
434 return "";
435 }
436
Jamie Madill574d9dd2013-06-20 11:55:56 -0400437 int numComponents = 0;
438
439 if (type.isMatrix())
440 {
Jamie Madille4075c92013-06-21 09:15:32 -0400441 // This method can also be called from structureString, which does not use layout qualifiers.
442 // Thus, use the method parameter for determining the matrix packing.
443 //
444 // Note HLSL row major packing corresponds to GL API column-major, and vice-versa, since we
445 // wish to always transpose GL matrices to play well with HLSL's matrix array indexing.
446 //
447 const bool isRowMajorMatrix = !useHLSLRowMajorPacking;
Jamie Madillc835df62013-06-21 09:15:32 -0400448 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400449 numComponents = gl::MatrixComponentCount(glType, isRowMajorMatrix);
450 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400451 else if (type.getStruct())
Jamie Madillc835df62013-06-21 09:15:32 -0400452 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400453 const TString &structName = structureTypeName(*type.getStruct(), useHLSLRowMajorPacking, true);
Jamie Madille4075c92013-06-21 09:15:32 -0400454 numComponents = mStd140StructElementIndexes[structName];
455
456 if (numComponents == 0)
457 {
458 return "";
459 }
Jamie Madillc835df62013-06-21 09:15:32 -0400460 }
Jamie Madill574d9dd2013-06-20 11:55:56 -0400461 else
462 {
Jamie Madillc835df62013-06-21 09:15:32 -0400463 const GLenum glType = glVariableType(type);
Jamie Madill574d9dd2013-06-20 11:55:56 -0400464 numComponents = gl::UniformComponentCount(glType);
465 }
466
467 TString padding;
468 for (int paddingOffset = numComponents; paddingOffset < 4; paddingOffset++)
469 {
470 padding += " float pad_" + str(mPaddingCounter++) + ";\n";
471 }
472 return padding;
473}
474
Jamie Madill440dc742013-06-20 11:55:55 -0400475// Use the same layout for packed and shared
Jamie Madill834e8b72014-04-11 13:33:58 -0400476void setBlockLayout(gl::InterfaceBlock *interfaceBlock, gl::BlockLayoutType newLayout)
Jamie Madill440dc742013-06-20 11:55:55 -0400477{
478 interfaceBlock->layout = newLayout;
479 interfaceBlock->blockInfo.clear();
480
481 switch (newLayout)
482 {
Jamie Madill834e8b72014-04-11 13:33:58 -0400483 case gl::BLOCKLAYOUT_SHARED:
484 case gl::BLOCKLAYOUT_PACKED:
Jamie Madill440dc742013-06-20 11:55:55 -0400485 {
Vladimir Vukicevic24d8d672014-05-27 12:07:51 -0400486 gl::HLSLBlockEncoder hlslEncoder(&interfaceBlock->blockInfo, gl::HLSLBlockEncoder::ENCODE_PACKED);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400487 hlslEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400488 interfaceBlock->dataSize = hlslEncoder.getBlockSize();
489 }
490 break;
491
Jamie Madill834e8b72014-04-11 13:33:58 -0400492 case gl::BLOCKLAYOUT_STANDARD:
Jamie Madill440dc742013-06-20 11:55:55 -0400493 {
Jamie Madill834e8b72014-04-11 13:33:58 -0400494 gl::Std140BlockEncoder stdEncoder(&interfaceBlock->blockInfo);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400495 stdEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400496 interfaceBlock->dataSize = stdEncoder.getBlockSize();
497 }
498 break;
499
500 default:
501 UNREACHABLE();
502 break;
503 }
504}
505
Jamie Madill834e8b72014-04-11 13:33:58 -0400506gl::BlockLayoutType convertBlockLayoutType(TLayoutBlockStorage blockStorage)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400507{
508 switch (blockStorage)
509 {
Jamie Madill834e8b72014-04-11 13:33:58 -0400510 case EbsPacked: return gl::BLOCKLAYOUT_PACKED;
511 case EbsShared: return gl::BLOCKLAYOUT_SHARED;
512 case EbsStd140: return gl::BLOCKLAYOUT_STANDARD;
513 default: UNREACHABLE(); return gl::BLOCKLAYOUT_SHARED;
Jamie Madill574d9dd2013-06-20 11:55:56 -0400514 }
515}
516
Jamie Madill98493dd2013-07-08 14:39:03 -0400517TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400518{
519 TString init;
520
521 TString preIndentString;
522 TString fullIndentString;
523
524 for (int spaces = 0; spaces < (indent * 4); spaces++)
525 {
526 preIndentString += ' ';
527 }
528
529 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
530 {
531 fullIndentString += ' ';
532 }
533
534 init += preIndentString + "{\n";
535
Jamie Madill98493dd2013-07-08 14:39:03 -0400536 const TFieldList &fields = structure.fields();
537 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400538 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400539 const TField &field = *fields[fieldIndex];
540 const TString &fieldName = rhsStructName + "." + decorate(field.name());
541 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400542
Jamie Madill98493dd2013-07-08 14:39:03 -0400543 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400544 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400545 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400546 }
547 else
548 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400549 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400550 }
551 }
552
553 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
554
555 return init;
556}
557
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000558void OutputHLSL::header()
559{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000560 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000561
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000562 TString uniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000563 TString interfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000564 TString varyings;
565 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400566 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000567
Jamie Madillc2141fb2013-08-30 13:21:08 -0400568 for (ReferencedSymbols::const_iterator uniformIt = mReferencedUniforms.begin(); uniformIt != mReferencedUniforms.end(); uniformIt++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000569 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400570 const TIntermSymbol &uniform = *uniformIt->second;
571 const TType &type = uniform.getType();
572 const TString &name = uniform.getSymbol();
573
574 int registerIndex = declareUniformAndAssignRegister(type, name);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000575
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000576 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture
577 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400578 uniforms += "uniform " + samplerString(type) + " sampler_" + decorateUniform(name, type) + arrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400579 " : register(s" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000580
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000581 uniforms += "uniform " + textureString(type) + " texture_" + decorateUniform(name, type) + arrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400582 " : register(t" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000583 }
584 else
585 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400586 const TStructure *structure = type.getStruct();
587 const TString &typeName = (structure ? structureTypeName(*structure, false, false) : typeString(type));
588
589 const TString &registerString = TString("register(") + RegisterPrefix(type) + str(registerIndex) + ")";
590
591 uniforms += "uniform " + typeName + " " + decorateUniform(name, type) + arrayString(type) + " : " + registerString + ";\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000592 }
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000593 }
594
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000595 for (ReferencedSymbols::const_iterator interfaceBlockIt = mReferencedInterfaceBlocks.begin(); interfaceBlockIt != mReferencedInterfaceBlocks.end(); interfaceBlockIt++)
596 {
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000597 const TType &nodeType = interfaceBlockIt->second->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -0400598 const TInterfaceBlock &interfaceBlock = *nodeType.getInterfaceBlock();
599 const TFieldList &fieldList = interfaceBlock.fields();
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000600
Jamie Madill98493dd2013-07-08 14:39:03 -0400601 unsigned int arraySize = static_cast<unsigned int>(interfaceBlock.arraySize());
Jamie Madill834e8b72014-04-11 13:33:58 -0400602 gl::InterfaceBlock activeBlock(interfaceBlock.name().c_str(), arraySize, mInterfaceBlockRegister);
Jamie Madill98493dd2013-07-08 14:39:03 -0400603 for (unsigned int typeIndex = 0; typeIndex < fieldList.size(); typeIndex++)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000604 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400605 const TField &field = *fieldList[typeIndex];
606 const TString &fullUniformName = interfaceBlockFieldString(interfaceBlock, field);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400607 declareInterfaceBlockField(*field.type(), fullUniformName, activeBlock.fields);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000608 }
609
Jamie Madill98493dd2013-07-08 14:39:03 -0400610 mInterfaceBlockRegister += std::max(1u, arraySize);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000611
Jamie Madill834e8b72014-04-11 13:33:58 -0400612 gl::BlockLayoutType blockLayoutType = convertBlockLayoutType(interfaceBlock.blockStorage());
Jamie Madill98493dd2013-07-08 14:39:03 -0400613 setBlockLayout(&activeBlock, blockLayoutType);
Jamie Madill9060a4e2013-08-12 16:22:57 -0700614
615 if (interfaceBlock.matrixPacking() == EmpRowMajor)
616 {
617 activeBlock.isRowMajorLayout = true;
618 }
619
Jamie Madill98493dd2013-07-08 14:39:03 -0400620 mActiveInterfaceBlocks.push_back(activeBlock);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000621
Jamie Madill98493dd2013-07-08 14:39:03 -0400622 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000623 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400624 interfaceBlocks += interfaceBlockStructString(interfaceBlock);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000625 }
626
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000627 if (arraySize > 0)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000628 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000629 for (unsigned int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
630 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400631 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex + arrayIndex, arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000632 }
633 }
634 else
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000635 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400636 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000637 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000638 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000639
Jamie Madill829f59e2013-11-13 19:40:54 -0500640 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400641 {
642 TIntermTyped *structNode = flaggedStructIt->first;
643 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400644 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400645 const TString &originalName = mFlaggedStructOriginalNames[structNode];
646
Jamie Madill98493dd2013-07-08 14:39:03 -0400647 flaggedStructs += "static " + decorate(structure.name()) + " " + mappedName + " =\n";
648 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400649 flaggedStructs += "\n";
650 }
651
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000652 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
653 {
654 const TType &type = varying->second->getType();
655 const TString &name = varying->second->getSymbol();
656
657 // Program linking depends on this exact format
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +0000658 varyings += "static " + interpolationString(type.getQualifier()) + " " + typeString(type) + " " +
659 decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madill47fdd132013-08-30 13:21:04 -0400660
Jamie Madill94599662013-08-30 13:21:10 -0400661 declareVaryingToList(type, type.getQualifier(), name, mActiveVaryings);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000662 }
663
664 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
665 {
666 const TType &type = attribute->second->getType();
667 const TString &name = attribute->second->getSymbol();
668
669 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400670
Jamie Madill834e8b72014-04-11 13:33:58 -0400671 gl::Attribute attributeVar(glVariableType(type), glVariablePrecision(type), name.c_str(),
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400672 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
673 mActiveAttributes.push_back(attributeVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000674 }
675
Jamie Madill529077d2013-06-20 11:55:54 -0400676 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
677 {
678 out << *structDeclaration;
679 }
680
681 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
682 {
683 out << *constructor;
684 }
685
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500686 if (mUsesDiscardRewriting)
687 {
688 out << "#define ANGLE_USES_DISCARD_REWRITING" << "\n";
689 }
690
Nicolas Capens655fe362014-04-11 13:12:34 -0400691 if (mUsesNestedBreak)
692 {
693 out << "#define ANGLE_USES_NESTED_BREAK" << "\n";
694 }
695
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400696 if (mContext.shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000697 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000698 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000699 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000700
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000701 out << "// Varyings\n";
702 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400703 out << "\n";
704
705 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000706 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500707 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000708 {
Jamie Madill46131a32013-06-20 11:55:50 -0400709 const TString &variableName = outputVariableIt->first;
710 const TType &variableType = outputVariableIt->second->getType();
711 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
712
713 out << "static " + typeString(variableType) + " out_" + variableName + arrayString(variableType) +
714 " = " + initializer(variableType) + ";\n";
715
Jamie Madill834e8b72014-04-11 13:33:58 -0400716 gl::Attribute outputVar(glVariableType(variableType), glVariablePrecision(variableType), variableName.c_str(),
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400717 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
Jamie Madill46131a32013-06-20 11:55:50 -0400718 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000719 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000720 }
Jamie Madill46131a32013-06-20 11:55:50 -0400721 else
722 {
723 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
724
725 out << "static float4 gl_Color[" << numColorValues << "] =\n"
726 "{\n";
727 for (unsigned int i = 0; i < numColorValues; i++)
728 {
729 out << " float4(0, 0, 0, 0)";
730 if (i + 1 != numColorValues)
731 {
732 out << ",";
733 }
734 out << "\n";
735 }
736
737 out << "};\n";
738 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000739
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400740 if (mUsesFragDepth)
741 {
742 out << "static float gl_Depth = 0.0;\n";
743 }
744
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000745 if (mUsesFragCoord)
746 {
747 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
748 }
749
750 if (mUsesPointCoord)
751 {
752 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
753 }
754
755 if (mUsesFrontFacing)
756 {
757 out << "static bool gl_FrontFacing = false;\n";
758 }
759
760 out << "\n";
761
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000762 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000763 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000764 out << "struct gl_DepthRangeParameters\n"
765 "{\n"
766 " float near;\n"
767 " float far;\n"
768 " float diff;\n"
769 "};\n"
770 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000771 }
772
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000773 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000774 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000775 out << "cbuffer DriverConstants : register(b1)\n"
776 "{\n";
777
778 if (mUsesDepthRange)
779 {
780 out << " float3 dx_DepthRange : packoffset(c0);\n";
781 }
782
783 if (mUsesFragCoord)
784 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000785 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000786 }
787
788 if (mUsesFragCoord || mUsesFrontFacing)
789 {
790 out << " float3 dx_DepthFront : packoffset(c2);\n";
791 }
792
793 out << "};\n";
794 }
795 else
796 {
797 if (mUsesDepthRange)
798 {
799 out << "uniform float3 dx_DepthRange : register(c0);";
800 }
801
802 if (mUsesFragCoord)
803 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000804 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000805 }
806
807 if (mUsesFragCoord || mUsesFrontFacing)
808 {
809 out << "uniform float3 dx_DepthFront : register(c2);\n";
810 }
811 }
812
813 out << "\n";
814
815 if (mUsesDepthRange)
816 {
817 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
818 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000819 }
820
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000821 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000822 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000823
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000824 if (!interfaceBlocks.empty())
825 {
826 out << interfaceBlocks;
827 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400828
829 if (!flaggedStructs.empty())
830 {
831 out << "// Std140 Structures accessed by value\n";
832 out << "\n";
833 out << flaggedStructs;
834 out << "\n";
835 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000836 }
837
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000838 if (usingMRTExtension && mNumRenderTargets > 1)
839 {
840 out << "#define GL_USES_MRT\n";
841 }
842
843 if (mUsesFragColor)
844 {
845 out << "#define GL_USES_FRAG_COLOR\n";
846 }
847
848 if (mUsesFragData)
849 {
850 out << "#define GL_USES_FRAG_DATA\n";
851 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000853 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000855 out << "// Attributes\n";
856 out << attributes;
857 out << "\n"
858 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
859
860 if (mUsesPointSize)
861 {
862 out << "static float gl_PointSize = float(1);\n";
863 }
864
865 out << "\n"
866 "// Varyings\n";
867 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000868 out << "\n";
869
870 if (mUsesDepthRange)
871 {
872 out << "struct gl_DepthRangeParameters\n"
873 "{\n"
874 " float near;\n"
875 " float far;\n"
876 " float diff;\n"
877 "};\n"
878 "\n";
879 }
880
881 if (mOutputType == SH_HLSL11_OUTPUT)
882 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000883 if (mUsesDepthRange)
884 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000885 out << "cbuffer DriverConstants : register(b1)\n"
886 "{\n"
887 " float3 dx_DepthRange : packoffset(c0);\n"
888 "};\n"
889 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000890 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000891 }
892 else
893 {
894 if (mUsesDepthRange)
895 {
896 out << "uniform float3 dx_DepthRange : register(c0);\n";
897 }
898
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000899 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000900 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000901 }
902
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000903 if (mUsesDepthRange)
904 {
905 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
906 "\n";
907 }
908
909 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000911
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000912 if (!interfaceBlocks.empty())
913 {
914 out << interfaceBlocks;
915 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400916
917 if (!flaggedStructs.empty())
918 {
919 out << "// Std140 Structures accessed by value\n";
920 out << "\n";
921 out << flaggedStructs;
922 out << "\n";
923 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000924 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400925 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000926
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400927 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
928 {
929 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400930 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000931 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400932 switch(textureFunction->sampler)
933 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400934 case EbtSampler2D: out << "int2 "; break;
935 case EbtSampler3D: out << "int3 "; break;
936 case EbtSamplerCube: out << "int2 "; break;
937 case EbtSampler2DArray: out << "int3 "; break;
938 case EbtISampler2D: out << "int2 "; break;
939 case EbtISampler3D: out << "int3 "; break;
940 case EbtISamplerCube: out << "int2 "; break;
941 case EbtISampler2DArray: out << "int3 "; break;
942 case EbtUSampler2D: out << "int2 "; break;
943 case EbtUSampler3D: out << "int3 "; break;
944 case EbtUSamplerCube: out << "int2 "; break;
945 case EbtUSampler2DArray: out << "int3 "; break;
946 case EbtSampler2DShadow: out << "int2 "; break;
947 case EbtSamplerCubeShadow: out << "int2 "; break;
948 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400949 default: UNREACHABLE();
950 }
951 }
952 else // Sampling function
953 {
954 switch(textureFunction->sampler)
955 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400956 case EbtSampler2D: out << "float4 "; break;
957 case EbtSampler3D: out << "float4 "; break;
958 case EbtSamplerCube: out << "float4 "; break;
959 case EbtSampler2DArray: out << "float4 "; break;
960 case EbtISampler2D: out << "int4 "; break;
961 case EbtISampler3D: out << "int4 "; break;
962 case EbtISamplerCube: out << "int4 "; break;
963 case EbtISampler2DArray: out << "int4 "; break;
964 case EbtUSampler2D: out << "uint4 "; break;
965 case EbtUSampler3D: out << "uint4 "; break;
966 case EbtUSamplerCube: out << "uint4 "; break;
967 case EbtUSampler2DArray: out << "uint4 "; break;
968 case EbtSampler2DShadow: out << "float "; break;
969 case EbtSamplerCubeShadow: out << "float "; break;
970 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400971 default: UNREACHABLE();
972 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000973 }
974
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400975 // Function name
976 out << textureFunction->name();
977
978 // Argument list
979 int hlslCoords = 4;
980
981 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000982 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400983 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000984 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400985 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
986 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
987 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000988 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400989
Nicolas Capens75fb4752013-07-10 15:14:47 -0400990 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000991 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400992 case TextureFunction::IMPLICIT: break;
993 case TextureFunction::BIAS: hlslCoords = 4; break;
994 case TextureFunction::LOD: hlslCoords = 4; break;
995 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400996 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400997 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000998 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400999 }
1000 else if (mOutputType == SH_HLSL11_OUTPUT)
1001 {
1002 switch(textureFunction->sampler)
1003 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04001004 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
1005 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
1006 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
1007 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
1008 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
1009 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -05001010 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001011 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
1012 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
1013 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -05001014 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001015 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
1016 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
1017 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
1018 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001019 default: UNREACHABLE();
1020 }
1021 }
1022 else UNREACHABLE();
1023
Nicolas Capensfc014542014-02-18 14:47:13 -05001024 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001025 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001026 switch(textureFunction->coords)
1027 {
1028 case 2: out << ", int2 t"; break;
1029 case 3: out << ", int3 t"; break;
1030 default: UNREACHABLE();
1031 }
1032 }
1033 else // Floating-point coordinates (except textureSize)
1034 {
1035 switch(textureFunction->coords)
1036 {
1037 case 1: out << ", int lod"; break; // textureSize()
1038 case 2: out << ", float2 t"; break;
1039 case 3: out << ", float3 t"; break;
1040 case 4: out << ", float4 t"; break;
1041 default: UNREACHABLE();
1042 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001043 }
1044
Nicolas Capensd11d5492014-02-19 17:06:10 -05001045 if (textureFunction->method == TextureFunction::GRAD)
1046 {
1047 switch(textureFunction->sampler)
1048 {
1049 case EbtSampler2D:
1050 case EbtISampler2D:
1051 case EbtUSampler2D:
1052 case EbtSampler2DArray:
1053 case EbtISampler2DArray:
1054 case EbtUSampler2DArray:
1055 case EbtSampler2DShadow:
1056 case EbtSampler2DArrayShadow:
1057 out << ", float2 ddx, float2 ddy";
1058 break;
1059 case EbtSampler3D:
1060 case EbtISampler3D:
1061 case EbtUSampler3D:
1062 case EbtSamplerCube:
1063 case EbtISamplerCube:
1064 case EbtUSamplerCube:
1065 case EbtSamplerCubeShadow:
1066 out << ", float3 ddx, float3 ddy";
1067 break;
1068 default: UNREACHABLE();
1069 }
1070 }
1071
Nicolas Capens75fb4752013-07-10 15:14:47 -04001072 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +00001073 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001074 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001075 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001076 case TextureFunction::LOD: out << ", float lod"; break;
1077 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001078 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -04001079 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -05001080 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -05001081 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001082 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +00001083 }
1084
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001085 if (textureFunction->offset)
1086 {
1087 switch(textureFunction->sampler)
1088 {
1089 case EbtSampler2D: out << ", int2 offset"; break;
1090 case EbtSampler3D: out << ", int3 offset"; break;
1091 case EbtSampler2DArray: out << ", int2 offset"; break;
1092 case EbtISampler2D: out << ", int2 offset"; break;
1093 case EbtISampler3D: out << ", int3 offset"; break;
1094 case EbtISampler2DArray: out << ", int2 offset"; break;
1095 case EbtUSampler2D: out << ", int2 offset"; break;
1096 case EbtUSampler3D: out << ", int3 offset"; break;
1097 case EbtUSampler2DArray: out << ", int2 offset"; break;
1098 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -05001099 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001100 default: UNREACHABLE();
1101 }
1102 }
1103
Nicolas Capens84cfa122014-04-14 13:48:45 -04001104 if (textureFunction->method == TextureFunction::BIAS ||
1105 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001106 {
1107 out << ", float bias";
1108 }
1109
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001110 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001111 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001112
Nicolas Capens75fb4752013-07-10 15:14:47 -04001113 if (textureFunction->method == TextureFunction::SIZE)
1114 {
1115 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
1116 {
1117 if (IsSamplerArray(textureFunction->sampler))
1118 {
1119 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
1120 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
1121 }
1122 else
1123 {
1124 out << " uint width; uint height; uint numberOfLevels;\n"
1125 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
1126 }
1127 }
1128 else if (IsSampler3D(textureFunction->sampler))
1129 {
1130 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
1131 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
1132 }
1133 else UNREACHABLE();
1134
1135 switch(textureFunction->sampler)
1136 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04001137 case EbtSampler2D: out << " return int2(width, height);"; break;
1138 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
1139 case EbtSamplerCube: out << " return int2(width, height);"; break;
1140 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
1141 case EbtISampler2D: out << " return int2(width, height);"; break;
1142 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
1143 case EbtISamplerCube: out << " return int2(width, height);"; break;
1144 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
1145 case EbtUSampler2D: out << " return int2(width, height);"; break;
1146 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
1147 case EbtUSamplerCube: out << " return int2(width, height);"; break;
1148 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
1149 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
1150 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
1151 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001152 default: UNREACHABLE();
1153 }
1154 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001155 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001156 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001157 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1158 {
1159 out << " float width; float height; float layers; float levels;\n";
1160
1161 out << " uint mip = 0;\n";
1162
1163 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
1164
1165 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
1166 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
1167 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
1168 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
1169
1170 // FACE_POSITIVE_X = 000b
1171 // FACE_NEGATIVE_X = 001b
1172 // FACE_POSITIVE_Y = 010b
1173 // FACE_NEGATIVE_Y = 011b
1174 // FACE_POSITIVE_Z = 100b
1175 // FACE_NEGATIVE_Z = 101b
1176 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
1177
1178 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
1179 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
1180 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
1181
1182 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
1183 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
1184 }
1185 else if (IsIntegerSampler(textureFunction->sampler) &&
1186 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001187 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001188 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001189 {
Nicolas Capens93e50de2013-07-09 13:46:28 -04001190 if (IsSamplerArray(textureFunction->sampler))
1191 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001192 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001193
Nicolas Capens9edebd62013-08-06 10:59:10 -04001194 if (textureFunction->method == TextureFunction::LOD0)
1195 {
1196 out << " uint mip = 0;\n";
1197 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001198 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1199 {
1200 out << " uint mip = bias;\n";
1201 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001202 else
1203 {
1204 if (textureFunction->method == TextureFunction::IMPLICIT ||
1205 textureFunction->method == TextureFunction::BIAS)
1206 {
1207 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1208 " float2 tSized = float2(t.x * width, t.y * height);\n"
1209 " float dx = length(ddx(tSized));\n"
1210 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001211 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001212
1213 if (textureFunction->method == TextureFunction::BIAS)
1214 {
1215 out << " lod += bias;\n";
1216 }
1217 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001218 else if (textureFunction->method == TextureFunction::GRAD)
1219 {
1220 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1221 " float lod = log2(max(length(ddx), length(ddy)));\n";
1222 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001223
1224 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1225 }
1226
1227 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001228 }
1229 else
1230 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001231 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001232
Nicolas Capens9edebd62013-08-06 10:59:10 -04001233 if (textureFunction->method == TextureFunction::LOD0)
1234 {
1235 out << " uint mip = 0;\n";
1236 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001237 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1238 {
1239 out << " uint mip = bias;\n";
1240 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001241 else
1242 {
1243 if (textureFunction->method == TextureFunction::IMPLICIT ||
1244 textureFunction->method == TextureFunction::BIAS)
1245 {
1246 out << " x.GetDimensions(0, width, height, levels);\n"
1247 " float2 tSized = float2(t.x * width, t.y * height);\n"
1248 " float dx = length(ddx(tSized));\n"
1249 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001250 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001251
1252 if (textureFunction->method == TextureFunction::BIAS)
1253 {
1254 out << " lod += bias;\n";
1255 }
1256 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05001257 else if (textureFunction->method == TextureFunction::LOD)
1258 {
1259 out << " x.GetDimensions(0, width, height, levels);\n";
1260 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001261 else if (textureFunction->method == TextureFunction::GRAD)
1262 {
1263 out << " x.GetDimensions(0, width, height, levels);\n"
1264 " float lod = log2(max(length(ddx), length(ddy)));\n";
1265 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001266
1267 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1268 }
1269
1270 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001271 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001272 }
1273 else if (IsSampler3D(textureFunction->sampler))
1274 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001275 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001276
Nicolas Capens9edebd62013-08-06 10:59:10 -04001277 if (textureFunction->method == TextureFunction::LOD0)
1278 {
1279 out << " uint mip = 0;\n";
1280 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001281 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1282 {
1283 out << " uint mip = bias;\n";
1284 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001285 else
1286 {
1287 if (textureFunction->method == TextureFunction::IMPLICIT ||
1288 textureFunction->method == TextureFunction::BIAS)
1289 {
1290 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1291 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
1292 " float dx = length(ddx(tSized));\n"
1293 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001294 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001295
1296 if (textureFunction->method == TextureFunction::BIAS)
1297 {
1298 out << " lod += bias;\n";
1299 }
1300 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001301 else if (textureFunction->method == TextureFunction::GRAD)
1302 {
1303 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1304 " float lod = log2(max(length(ddx), length(ddy)));\n";
1305 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001306
1307 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1308 }
1309
1310 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001311 }
1312 else UNREACHABLE();
1313 }
1314
1315 out << " return ";
1316
1317 // HLSL intrinsic
1318 if (mOutputType == SH_HLSL9_OUTPUT)
1319 {
1320 switch(textureFunction->sampler)
1321 {
1322 case EbtSampler2D: out << "tex2D"; break;
1323 case EbtSamplerCube: out << "texCUBE"; break;
1324 default: UNREACHABLE();
1325 }
1326
Nicolas Capens75fb4752013-07-10 15:14:47 -04001327 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001328 {
1329 case TextureFunction::IMPLICIT: out << "(s, "; break;
1330 case TextureFunction::BIAS: out << "bias(s, "; break;
1331 case TextureFunction::LOD: out << "lod(s, "; break;
1332 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001333 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001334 default: UNREACHABLE();
1335 }
1336 }
1337 else if (mOutputType == SH_HLSL11_OUTPUT)
1338 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001339 if (textureFunction->method == TextureFunction::GRAD)
1340 {
1341 if (IsIntegerSampler(textureFunction->sampler))
1342 {
1343 out << "x.Load(";
1344 }
1345 else if (IsShadowSampler(textureFunction->sampler))
1346 {
1347 out << "x.SampleCmpLevelZero(s, ";
1348 }
1349 else
1350 {
1351 out << "x.SampleGrad(s, ";
1352 }
1353 }
1354 else if (IsIntegerSampler(textureFunction->sampler) ||
1355 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001356 {
1357 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001358 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001359 else if (IsShadowSampler(textureFunction->sampler))
1360 {
1361 out << "x.SampleCmp(s, ";
1362 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001363 else
1364 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001365 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001366 {
1367 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1368 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1369 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1370 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001371 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001372 default: UNREACHABLE();
1373 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001374 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001375 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001376 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001377
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001378 // Integer sampling requires integer addresses
1379 TString addressx = "";
1380 TString addressy = "";
1381 TString addressz = "";
1382 TString close = "";
1383
Nicolas Capensfc014542014-02-18 14:47:13 -05001384 if (IsIntegerSampler(textureFunction->sampler) ||
1385 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001386 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001387 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001388 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001389 case 2: out << "int3("; break;
1390 case 3: out << "int4("; break;
1391 default: UNREACHABLE();
1392 }
1393
Nicolas Capensfc014542014-02-18 14:47:13 -05001394 // Convert from normalized floating-point to integer
1395 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001396 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001397 addressx = "int(floor(width * frac((";
1398 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001399
Nicolas Capensfc014542014-02-18 14:47:13 -05001400 if (IsSamplerArray(textureFunction->sampler))
1401 {
1402 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1403 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001404 else if (IsSamplerCube(textureFunction->sampler))
1405 {
1406 addressz = "((((";
1407 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001408 else
1409 {
1410 addressz = "int(floor(depth * frac((";
1411 }
1412
1413 close = "))))";
1414 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001415 }
1416 else
1417 {
1418 switch(hlslCoords)
1419 {
1420 case 2: out << "float2("; break;
1421 case 3: out << "float3("; break;
1422 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001423 default: UNREACHABLE();
1424 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001425 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001426
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001427 TString proj = ""; // Only used for projected textures
1428
1429 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001430 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001431 switch(textureFunction->coords)
1432 {
1433 case 3: proj = " / t.z"; break;
1434 case 4: proj = " / t.w"; break;
1435 default: UNREACHABLE();
1436 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001437 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001438
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001439 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001440
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001441 if (mOutputType == SH_HLSL9_OUTPUT)
1442 {
1443 if (hlslCoords >= 3)
1444 {
1445 if (textureFunction->coords < 3)
1446 {
1447 out << ", 0";
1448 }
1449 else
1450 {
1451 out << ", t.z" + proj;
1452 }
1453 }
1454
1455 if (hlslCoords == 4)
1456 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001457 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001458 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001459 case TextureFunction::BIAS: out << ", bias"; break;
1460 case TextureFunction::LOD: out << ", lod"; break;
1461 case TextureFunction::LOD0: out << ", 0"; break;
1462 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001463 default: UNREACHABLE();
1464 }
1465 }
1466
1467 out << "));\n";
1468 }
1469 else if (mOutputType == SH_HLSL11_OUTPUT)
1470 {
1471 if (hlslCoords >= 3)
1472 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001473 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1474 {
1475 out << ", face";
1476 }
1477 else
1478 {
1479 out << ", " + addressz + ("t.z" + proj) + close;
1480 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001481 }
1482
Nicolas Capensd11d5492014-02-19 17:06:10 -05001483 if (textureFunction->method == TextureFunction::GRAD)
1484 {
1485 if (IsIntegerSampler(textureFunction->sampler))
1486 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001487 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001488 }
1489 else if (IsShadowSampler(textureFunction->sampler))
1490 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001491 // Compare value
Nicolas Capensd11d5492014-02-19 17:06:10 -05001492 switch(textureFunction->coords)
1493 {
1494 case 3: out << "), t.z"; break;
1495 case 4: out << "), t.w"; break;
1496 default: UNREACHABLE();
1497 }
1498 }
1499 else
1500 {
1501 out << "), ddx, ddy";
1502 }
1503 }
1504 else if (IsIntegerSampler(textureFunction->sampler) ||
1505 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001506 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001507 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001508 }
1509 else if (IsShadowSampler(textureFunction->sampler))
1510 {
1511 // Compare value
1512 switch(textureFunction->coords)
1513 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001514 case 3: out << "), t.z"; break;
1515 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001516 default: UNREACHABLE();
1517 }
1518 }
1519 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001520 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001521 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001522 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001523 case TextureFunction::IMPLICIT: out << ")"; break;
1524 case TextureFunction::BIAS: out << "), bias"; break;
1525 case TextureFunction::LOD: out << "), lod"; break;
1526 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001527 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001528 default: UNREACHABLE();
1529 }
1530 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001531
1532 if (textureFunction->offset)
1533 {
1534 out << ", offset";
1535 }
1536
1537 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001538 }
1539 else UNREACHABLE();
1540 }
1541
1542 out << "\n"
1543 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001544 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001545 }
1546
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001547 if (mUsesFragCoord)
1548 {
1549 out << "#define GL_USES_FRAG_COORD\n";
1550 }
1551
1552 if (mUsesPointCoord)
1553 {
1554 out << "#define GL_USES_POINT_COORD\n";
1555 }
1556
1557 if (mUsesFrontFacing)
1558 {
1559 out << "#define GL_USES_FRONT_FACING\n";
1560 }
1561
1562 if (mUsesPointSize)
1563 {
1564 out << "#define GL_USES_POINT_SIZE\n";
1565 }
1566
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001567 if (mUsesFragDepth)
1568 {
1569 out << "#define GL_USES_FRAG_DEPTH\n";
1570 }
1571
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001572 if (mUsesDepthRange)
1573 {
1574 out << "#define GL_USES_DEPTH_RANGE\n";
1575 }
1576
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001577 if (mUsesXor)
1578 {
1579 out << "bool xor(bool p, bool q)\n"
1580 "{\n"
1581 " return (p || q) && !(p && q);\n"
1582 "}\n"
1583 "\n";
1584 }
1585
1586 if (mUsesMod1)
1587 {
1588 out << "float mod(float x, float y)\n"
1589 "{\n"
1590 " return x - y * floor(x / y);\n"
1591 "}\n"
1592 "\n";
1593 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001594
1595 if (mUsesMod2v)
1596 {
1597 out << "float2 mod(float2 x, float2 y)\n"
1598 "{\n"
1599 " return x - y * floor(x / y);\n"
1600 "}\n"
1601 "\n";
1602 }
1603
1604 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001605 {
1606 out << "float2 mod(float2 x, float y)\n"
1607 "{\n"
1608 " return x - y * floor(x / y);\n"
1609 "}\n"
1610 "\n";
1611 }
1612
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001613 if (mUsesMod3v)
1614 {
1615 out << "float3 mod(float3 x, float3 y)\n"
1616 "{\n"
1617 " return x - y * floor(x / y);\n"
1618 "}\n"
1619 "\n";
1620 }
1621
1622 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001623 {
1624 out << "float3 mod(float3 x, float y)\n"
1625 "{\n"
1626 " return x - y * floor(x / y);\n"
1627 "}\n"
1628 "\n";
1629 }
1630
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001631 if (mUsesMod4v)
1632 {
1633 out << "float4 mod(float4 x, float4 y)\n"
1634 "{\n"
1635 " return x - y * floor(x / y);\n"
1636 "}\n"
1637 "\n";
1638 }
1639
1640 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001641 {
1642 out << "float4 mod(float4 x, float y)\n"
1643 "{\n"
1644 " return x - y * floor(x / y);\n"
1645 "}\n"
1646 "\n";
1647 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001648
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001649 if (mUsesFaceforward1)
1650 {
1651 out << "float faceforward(float N, float I, float Nref)\n"
1652 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001653 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001654 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001655 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001656 " }\n"
1657 " else\n"
1658 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001659 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001660 " }\n"
1661 "}\n"
1662 "\n";
1663 }
1664
1665 if (mUsesFaceforward2)
1666 {
1667 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1668 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001669 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001670 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001671 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001672 " }\n"
1673 " else\n"
1674 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001675 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001676 " }\n"
1677 "}\n"
1678 "\n";
1679 }
1680
1681 if (mUsesFaceforward3)
1682 {
1683 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1684 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001685 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001686 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001687 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001688 " }\n"
1689 " else\n"
1690 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001691 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001692 " }\n"
1693 "}\n"
1694 "\n";
1695 }
1696
1697 if (mUsesFaceforward4)
1698 {
1699 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1700 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001701 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001702 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001703 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001704 " }\n"
1705 " else\n"
1706 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001707 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001708 " }\n"
1709 "}\n"
1710 "\n";
1711 }
1712
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001713 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001714 {
1715 out << "float atanyx(float y, float x)\n"
1716 "{\n"
1717 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1718 " return atan2(y, x);\n"
1719 "}\n";
1720 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001721
1722 if (mUsesAtan2_2)
1723 {
1724 out << "float2 atanyx(float2 y, float2 x)\n"
1725 "{\n"
1726 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1727 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1728 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1729 "}\n";
1730 }
1731
1732 if (mUsesAtan2_3)
1733 {
1734 out << "float3 atanyx(float3 y, float3 x)\n"
1735 "{\n"
1736 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1737 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1738 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1739 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1740 "}\n";
1741 }
1742
1743 if (mUsesAtan2_4)
1744 {
1745 out << "float4 atanyx(float4 y, float4 x)\n"
1746 "{\n"
1747 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1748 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1749 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1750 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1751 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1752 "}\n";
1753 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001754}
1755
1756void OutputHLSL::visitSymbol(TIntermSymbol *node)
1757{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001758 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001759
Jamie Madill570e04d2013-06-21 09:15:33 -04001760 // Handle accessing std140 structs by value
1761 if (mFlaggedStructMappedNames.count(node) > 0)
1762 {
1763 out << mFlaggedStructMappedNames[node];
1764 return;
1765 }
1766
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001767 TString name = node->getSymbol();
1768
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001769 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001770 {
1771 mUsesDepthRange = true;
1772 out << name;
1773 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001774 else
1775 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001776 TQualifier qualifier = node->getQualifier();
1777
1778 if (qualifier == EvqUniform)
1779 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001780 const TType& nodeType = node->getType();
1781 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1782
1783 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001784 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001785 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001786 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001787 else
1788 {
1789 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001790 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001791
1792 out << decorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001793 }
Jamie Madill19571812013-08-12 15:26:34 -07001794 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001795 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001796 mReferencedAttributes[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001797 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001798 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00001799 else if (isVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001800 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001801 mReferencedVaryings[name] = node;
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001802 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001803 }
Jamie Madill19571812013-08-12 15:26:34 -07001804 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001805 {
1806 mReferencedOutputVariables[name] = node;
1807 out << "out_" << name;
1808 }
1809 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001810 {
1811 out << "gl_Color[0]";
1812 mUsesFragColor = true;
1813 }
1814 else if (qualifier == EvqFragData)
1815 {
1816 out << "gl_Color";
1817 mUsesFragData = true;
1818 }
1819 else if (qualifier == EvqFragCoord)
1820 {
1821 mUsesFragCoord = true;
1822 out << name;
1823 }
1824 else if (qualifier == EvqPointCoord)
1825 {
1826 mUsesPointCoord = true;
1827 out << name;
1828 }
1829 else if (qualifier == EvqFrontFacing)
1830 {
1831 mUsesFrontFacing = true;
1832 out << name;
1833 }
1834 else if (qualifier == EvqPointSize)
1835 {
1836 mUsesPointSize = true;
1837 out << name;
1838 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001839 else if (name == "gl_FragDepthEXT")
1840 {
1841 mUsesFragDepth = true;
1842 out << "gl_Depth";
1843 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001844 else if (qualifier == EvqInternal)
1845 {
1846 out << name;
1847 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001848 else
1849 {
1850 out << decorate(name);
1851 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001852 }
1853}
1854
1855bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1856{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001857 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001858
Jamie Madill570e04d2013-06-21 09:15:33 -04001859 // Handle accessing std140 structs by value
1860 if (mFlaggedStructMappedNames.count(node) > 0)
1861 {
1862 out << mFlaggedStructMappedNames[node];
1863 return false;
1864 }
1865
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001866 switch (node->getOp())
1867 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001868 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001869 case EOpInitialize:
1870 if (visit == PreVisit)
1871 {
1872 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1873 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1874 // new variable is created before the assignment is evaluated), so we need to convert
1875 // this to "float t = x, x = t;".
1876
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001877 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1878 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001879
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001880 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1881 expression->traverse(&searchSymbol);
1882 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001883
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001884 if (sameSymbol)
1885 {
1886 // Type already printed
1887 out << "t" + str(mUniqueIndex) + " = ";
1888 expression->traverse(this);
1889 out << ", ";
1890 symbolNode->traverse(this);
1891 out << " = t" + str(mUniqueIndex);
1892
1893 mUniqueIndex++;
1894 return false;
1895 }
1896 }
1897 else if (visit == InVisit)
1898 {
1899 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001900 }
1901 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001902 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1903 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1904 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1905 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1906 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1907 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001908 if (visit == PreVisit)
1909 {
1910 out << "(";
1911 }
1912 else if (visit == InVisit)
1913 {
1914 out << " = mul(";
1915 node->getLeft()->traverse(this);
1916 out << ", transpose(";
1917 }
1918 else
1919 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001920 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001921 }
1922 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001923 case EOpMatrixTimesMatrixAssign:
1924 if (visit == PreVisit)
1925 {
1926 out << "(";
1927 }
1928 else if (visit == InVisit)
1929 {
1930 out << " = mul(";
1931 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001932 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001933 }
1934 else
1935 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001936 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001937 }
1938 break;
1939 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001940 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001941 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001942 const TType& leftType = node->getLeft()->getType();
1943 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001944 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001945 if (visit == PreVisit)
1946 {
1947 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1948 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1949
1950 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1951 out << interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1952
1953 return false;
1954 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001955 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001956 else
1957 {
1958 outputTriplet(visit, "", "[", "]");
1959 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001960 }
1961 break;
1962 case EOpIndexIndirect:
1963 // We do not currently support indirect references to interface blocks
1964 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1965 outputTriplet(visit, "", "[", "]");
1966 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001967 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001968 if (visit == InVisit)
1969 {
1970 const TStructure* structure = node->getLeft()->getType().getStruct();
1971 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1972 const TField* field = structure->fields()[index->getIConst(0)];
1973 out << "." + decorateField(field->name(), *structure);
1974
1975 return false;
1976 }
1977 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001978 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001979 if (visit == InVisit)
1980 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001981 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1982 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1983 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
1984 out << "." + decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001985
1986 return false;
1987 }
1988 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001989 case EOpVectorSwizzle:
1990 if (visit == InVisit)
1991 {
1992 out << ".";
1993
1994 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1995
1996 if (swizzle)
1997 {
1998 TIntermSequence &sequence = swizzle->getSequence();
1999
2000 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2001 {
2002 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
2003
2004 if (element)
2005 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002006 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002007
2008 switch (i)
2009 {
2010 case 0: out << "x"; break;
2011 case 1: out << "y"; break;
2012 case 2: out << "z"; break;
2013 case 3: out << "w"; break;
2014 default: UNREACHABLE();
2015 }
2016 }
2017 else UNREACHABLE();
2018 }
2019 }
2020 else UNREACHABLE();
2021
2022 return false; // Fully processed
2023 }
2024 break;
2025 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
2026 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
2027 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
2028 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002029 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002030 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002031 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002032 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002033 if (node->getOp() == EOpEqual)
2034 {
2035 outputTriplet(visit, "(", " == ", ")");
2036 }
2037 else
2038 {
2039 outputTriplet(visit, "(", " != ", ")");
2040 }
2041 }
2042 else if (node->getLeft()->getBasicType() == EbtStruct)
2043 {
2044 if (node->getOp() == EOpEqual)
2045 {
2046 out << "(";
2047 }
2048 else
2049 {
2050 out << "!(";
2051 }
2052
Jamie Madill98493dd2013-07-08 14:39:03 -04002053 const TStructure &structure = *node->getLeft()->getType().getStruct();
2054 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002055
Jamie Madill98493dd2013-07-08 14:39:03 -04002056 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002057 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002058 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002059
2060 node->getLeft()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04002061 out << "." + decorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002062 node->getRight()->traverse(this);
Jamie Madill98493dd2013-07-08 14:39:03 -04002063 out << "." + decorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002064
Jamie Madill98493dd2013-07-08 14:39:03 -04002065 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002066 {
2067 out << " && ";
2068 }
2069 }
2070
2071 out << ")";
2072
2073 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002074 }
2075 else
2076 {
Jamie Madill0b20c942013-07-19 16:36:56 -04002077 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002078
2079 if (node->getOp() == EOpEqual)
2080 {
Jamie Madill0b20c942013-07-19 16:36:56 -04002081 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002082 }
2083 else
2084 {
Jamie Madill0b20c942013-07-19 16:36:56 -04002085 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002086 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00002087 }
2088 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002089 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2090 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2091 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2092 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2093 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00002094 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00002095 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
2096 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00002097 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00002098 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002099 if (node->getRight()->hasSideEffects())
2100 {
2101 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
2102 return false;
2103 }
2104 else
2105 {
2106 outputTriplet(visit, "(", " || ", ")");
2107 return true;
2108 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002109 case EOpLogicalXor:
2110 mUsesXor = true;
2111 outputTriplet(visit, "xor(", ", ", ")");
2112 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00002113 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002114 if (node->getRight()->hasSideEffects())
2115 {
2116 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
2117 return false;
2118 }
2119 else
2120 {
2121 outputTriplet(visit, "(", " && ", ")");
2122 return true;
2123 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002124 default: UNREACHABLE();
2125 }
2126
2127 return true;
2128}
2129
2130bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
2131{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002132 switch (node->getOp())
2133 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002134 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
2135 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
2136 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
2137 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
2138 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
2139 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
2140 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002141 case EOpConvIntToBool:
Nicolas Capensab60b932013-06-05 10:31:21 -04002142 case EOpConvUIntToBool:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002143 case EOpConvFloatToBool:
2144 switch (node->getOperand()->getType().getNominalSize())
2145 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002146 case 1: outputTriplet(visit, "bool(", "", ")"); break;
2147 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
2148 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
2149 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002150 default: UNREACHABLE();
2151 }
2152 break;
2153 case EOpConvBoolToFloat:
2154 case EOpConvIntToFloat:
Nicolas Capensab60b932013-06-05 10:31:21 -04002155 case EOpConvUIntToFloat:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002156 switch (node->getOperand()->getType().getNominalSize())
2157 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002158 case 1: outputTriplet(visit, "float(", "", ")"); break;
2159 case 2: outputTriplet(visit, "float2(", "", ")"); break;
2160 case 3: outputTriplet(visit, "float3(", "", ")"); break;
2161 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002162 default: UNREACHABLE();
2163 }
2164 break;
2165 case EOpConvFloatToInt:
2166 case EOpConvBoolToInt:
Nicolas Capensab60b932013-06-05 10:31:21 -04002167 case EOpConvUIntToInt:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002168 switch (node->getOperand()->getType().getNominalSize())
2169 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002170 case 1: outputTriplet(visit, "int(", "", ")"); break;
2171 case 2: outputTriplet(visit, "int2(", "", ")"); break;
2172 case 3: outputTriplet(visit, "int3(", "", ")"); break;
2173 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002174 default: UNREACHABLE();
2175 }
2176 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002177 case EOpConvFloatToUInt:
2178 case EOpConvBoolToUInt:
2179 case EOpConvIntToUInt:
Jamie Madilla16f1672013-07-03 15:15:17 -04002180 switch (node->getOperand()->getType().getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002181 {
2182 case 1: outputTriplet(visit, "uint(", "", ")"); break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002183 case 2: outputTriplet(visit, "uint2(", "", ")"); break;
2184 case 3: outputTriplet(visit, "uint3(", "", ")"); break;
2185 case 4: outputTriplet(visit, "uint4(", "", ")"); break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002186 default: UNREACHABLE();
2187 }
2188 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002189 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
2190 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
2191 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
2192 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
2193 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
2194 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
2195 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
2196 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
2197 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
2198 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
2199 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
2200 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
2201 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
2202 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
2203 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
2204 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
2205 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
2206 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
2207 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
2208 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
2209 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002210 case EOpDFdx:
2211 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2212 {
2213 outputTriplet(visit, "(", "", ", 0.0)");
2214 }
2215 else
2216 {
2217 outputTriplet(visit, "ddx(", "", ")");
2218 }
2219 break;
2220 case EOpDFdy:
2221 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2222 {
2223 outputTriplet(visit, "(", "", ", 0.0)");
2224 }
2225 else
2226 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00002227 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002228 }
2229 break;
2230 case EOpFwidth:
2231 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2232 {
2233 outputTriplet(visit, "(", "", ", 0.0)");
2234 }
2235 else
2236 {
2237 outputTriplet(visit, "fwidth(", "", ")");
2238 }
2239 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002240 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
2241 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002242 default: UNREACHABLE();
2243 }
2244
2245 return true;
2246}
2247
2248bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2249{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002250 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002251
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002252 switch (node->getOp())
2253 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002254 case EOpSequence:
2255 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002256 if (mInsideFunction)
2257 {
Jamie Madill075edd82013-07-08 13:30:19 -04002258 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002259 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002260
2261 mScopeDepth++;
2262
2263 if (mScopeBracket.size() < mScopeDepth)
2264 {
2265 mScopeBracket.push_back(0); // New scope level
2266 }
2267 else
2268 {
2269 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
2270 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002271 }
2272
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002273 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
2274 {
Jamie Madill075edd82013-07-08 13:30:19 -04002275 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002276
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002277 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002278
2279 out << ";\n";
2280 }
2281
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002282 if (mInsideFunction)
2283 {
Jamie Madill075edd82013-07-08 13:30:19 -04002284 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002285 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002286
2287 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002288 }
2289
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002290 return false;
2291 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002292 case EOpDeclaration:
2293 if (visit == PreVisit)
2294 {
2295 TIntermSequence &sequence = node->getSequence();
2296 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002297
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00002298 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002299 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002300 if (variable->getType().getStruct())
2301 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002302 addConstructor(variable->getType(), scopedStruct(variable->getType().getStruct()->name()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00002303 }
2304
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002305 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002306 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00002307 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002308 {
2309 out << "static ";
2310 }
2311
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002312 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002313
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002314 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002315 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002316 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002317
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002318 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002319 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002320 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002321 out << arrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05002322 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002323 }
2324 else
2325 {
2326 (*sit)->traverse(this);
2327 }
2328
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002329 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002330 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002331 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002332 }
2333 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002334 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002335 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2336 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002337 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002338 }
2339 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002340 }
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00002341 else if (variable && isVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002342 {
2343 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2344 {
2345 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2346
2347 if (symbol)
2348 {
2349 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2350 mReferencedVaryings[symbol->getSymbol()] = symbol;
2351 }
2352 else
2353 {
2354 (*sit)->traverse(this);
2355 }
2356 }
2357 }
2358
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002359 return false;
2360 }
2361 else if (visit == InVisit)
2362 {
2363 out << ", ";
2364 }
2365 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002366 case EOpPrototype:
2367 if (visit == PreVisit)
2368 {
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002369 out << typeString(node->getType()) << " " << decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002370
2371 TIntermSequence &arguments = node->getSequence();
2372
2373 for (unsigned int i = 0; i < arguments.size(); i++)
2374 {
2375 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2376
2377 if (symbol)
2378 {
2379 out << argumentString(symbol);
2380
2381 if (i < arguments.size() - 1)
2382 {
2383 out << ", ";
2384 }
2385 }
2386 else UNREACHABLE();
2387 }
2388
2389 out << ");\n";
2390
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002391 // Also prototype the Lod0 variant if needed
2392 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2393 {
2394 mOutputLod0Function = true;
2395 node->traverse(this);
2396 mOutputLod0Function = false;
2397 }
2398
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002399 return false;
2400 }
2401 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002402 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002403 case EOpFunction:
2404 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002405 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002406
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002407 out << typeString(node->getType()) << " ";
2408
2409 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002410 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002411 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002412 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002413 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002414 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002415 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002416 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002417
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002418 TIntermSequence &sequence = node->getSequence();
2419 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2420
2421 for (unsigned int i = 0; i < arguments.size(); i++)
2422 {
2423 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2424
2425 if (symbol)
2426 {
2427 if (symbol->getType().getStruct())
2428 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002429 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getStruct()->name()), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002430 }
2431
2432 out << argumentString(symbol);
2433
2434 if (i < arguments.size() - 1)
2435 {
2436 out << ", ";
2437 }
2438 }
2439 else UNREACHABLE();
2440 }
2441
2442 out << ")\n"
2443 "{\n";
2444
2445 if (sequence.size() > 1)
2446 {
2447 mInsideFunction = true;
2448 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002449 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002450 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002451
2452 out << "}\n";
2453
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002454 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2455 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002456 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002457 {
2458 mOutputLod0Function = true;
2459 node->traverse(this);
2460 mOutputLod0Function = false;
2461 }
2462 }
2463
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002464 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002465 }
2466 break;
2467 case EOpFunctionCall:
2468 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002469 TString name = TFunction::unmangleName(node->getName());
2470 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002471 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002472
2473 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002474 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002475 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002476 }
2477 else
2478 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002479 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2480
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002481 TextureFunction textureFunction;
2482 textureFunction.sampler = samplerType;
2483 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002484 textureFunction.method = TextureFunction::IMPLICIT;
2485 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002486 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002487
2488 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002489 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002490 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002491 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002492 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002493 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002494 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002495 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002496 }
Nicolas Capens46485082014-04-15 13:12:50 -04002497 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2498 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002499 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002500 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002501 }
Nicolas Capens46485082014-04-15 13:12:50 -04002502 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002503 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002504 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002505 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002506 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002507 else if (name == "textureSize")
2508 {
2509 textureFunction.method = TextureFunction::SIZE;
2510 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002511 else if (name == "textureOffset")
2512 {
2513 textureFunction.method = TextureFunction::IMPLICIT;
2514 textureFunction.offset = true;
2515 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002516 else if (name == "textureProjOffset")
2517 {
2518 textureFunction.method = TextureFunction::IMPLICIT;
2519 textureFunction.offset = true;
2520 textureFunction.proj = true;
2521 }
2522 else if (name == "textureLodOffset")
2523 {
2524 textureFunction.method = TextureFunction::LOD;
2525 textureFunction.offset = true;
2526 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002527 else if (name == "textureProjLodOffset")
2528 {
2529 textureFunction.method = TextureFunction::LOD;
2530 textureFunction.proj = true;
2531 textureFunction.offset = true;
2532 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002533 else if (name == "texelFetch")
2534 {
2535 textureFunction.method = TextureFunction::FETCH;
2536 }
2537 else if (name == "texelFetchOffset")
2538 {
2539 textureFunction.method = TextureFunction::FETCH;
2540 textureFunction.offset = true;
2541 }
Nicolas Capens46485082014-04-15 13:12:50 -04002542 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002543 {
2544 textureFunction.method = TextureFunction::GRAD;
2545 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002546 else if (name == "textureGradOffset")
2547 {
2548 textureFunction.method = TextureFunction::GRAD;
2549 textureFunction.offset = true;
2550 }
Nicolas Capens46485082014-04-15 13:12:50 -04002551 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002552 {
2553 textureFunction.method = TextureFunction::GRAD;
2554 textureFunction.proj = true;
2555 }
2556 else if (name == "textureProjGradOffset")
2557 {
2558 textureFunction.method = TextureFunction::GRAD;
2559 textureFunction.proj = true;
2560 textureFunction.offset = true;
2561 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002562 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002563
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002564 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002565 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002566 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2567
2568 if (textureFunction.offset)
2569 {
2570 mandatoryArgumentCount++;
2571 }
2572
2573 bool bias = (arguments.size() > mandatoryArgumentCount); // Bias argument is optional
2574
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002575 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2576 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002577 if (bias)
2578 {
2579 textureFunction.method = TextureFunction::LOD0BIAS;
2580 }
2581 else
2582 {
2583 textureFunction.method = TextureFunction::LOD0;
2584 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002585 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002586 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002587 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002588 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002589 }
2590 }
2591
2592 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002593
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002594 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002595 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002596
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002597 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2598 {
2599 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2600 {
2601 out << "texture_";
2602 (*arg)->traverse(this);
2603 out << ", sampler_";
2604 }
2605
2606 (*arg)->traverse(this);
2607
2608 if (arg < arguments.end() - 1)
2609 {
2610 out << ", ";
2611 }
2612 }
2613
2614 out << ")";
2615
2616 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002617 }
2618 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002619 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002620 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002621 addConstructor(node->getType(), "vec1", &node->getSequence());
2622 outputTriplet(visit, "vec1(", "", ")");
2623 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002624 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002625 addConstructor(node->getType(), "vec2", &node->getSequence());
2626 outputTriplet(visit, "vec2(", ", ", ")");
2627 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002628 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002629 addConstructor(node->getType(), "vec3", &node->getSequence());
2630 outputTriplet(visit, "vec3(", ", ", ")");
2631 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002632 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002633 addConstructor(node->getType(), "vec4", &node->getSequence());
2634 outputTriplet(visit, "vec4(", ", ", ")");
2635 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002636 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002637 addConstructor(node->getType(), "bvec1", &node->getSequence());
2638 outputTriplet(visit, "bvec1(", "", ")");
2639 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002640 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002641 addConstructor(node->getType(), "bvec2", &node->getSequence());
2642 outputTriplet(visit, "bvec2(", ", ", ")");
2643 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002644 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002645 addConstructor(node->getType(), "bvec3", &node->getSequence());
2646 outputTriplet(visit, "bvec3(", ", ", ")");
2647 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002648 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002649 addConstructor(node->getType(), "bvec4", &node->getSequence());
2650 outputTriplet(visit, "bvec4(", ", ", ")");
2651 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002652 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002653 addConstructor(node->getType(), "ivec1", &node->getSequence());
2654 outputTriplet(visit, "ivec1(", "", ")");
2655 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002656 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002657 addConstructor(node->getType(), "ivec2", &node->getSequence());
2658 outputTriplet(visit, "ivec2(", ", ", ")");
2659 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002660 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002661 addConstructor(node->getType(), "ivec3", &node->getSequence());
2662 outputTriplet(visit, "ivec3(", ", ", ")");
2663 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002664 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002665 addConstructor(node->getType(), "ivec4", &node->getSequence());
2666 outputTriplet(visit, "ivec4(", ", ", ")");
2667 break;
Nicolas Capensab60b932013-06-05 10:31:21 -04002668 case EOpConstructUInt:
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002669 addConstructor(node->getType(), "uvec1", &node->getSequence());
2670 outputTriplet(visit, "uvec1(", "", ")");
2671 break;
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00002672 case EOpConstructUVec2:
2673 addConstructor(node->getType(), "uvec2", &node->getSequence());
2674 outputTriplet(visit, "uvec2(", ", ", ")");
2675 break;
2676 case EOpConstructUVec3:
2677 addConstructor(node->getType(), "uvec3", &node->getSequence());
2678 outputTriplet(visit, "uvec3(", ", ", ")");
2679 break;
2680 case EOpConstructUVec4:
2681 addConstructor(node->getType(), "uvec4", &node->getSequence());
2682 outputTriplet(visit, "uvec4(", ", ", ")");
2683 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002684 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002685 addConstructor(node->getType(), "mat2", &node->getSequence());
2686 outputTriplet(visit, "mat2(", ", ", ")");
2687 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002688 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002689 addConstructor(node->getType(), "mat3", &node->getSequence());
2690 outputTriplet(visit, "mat3(", ", ", ")");
2691 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002692 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002693 addConstructor(node->getType(), "mat4", &node->getSequence());
2694 outputTriplet(visit, "mat4(", ", ", ")");
2695 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002696 case EOpConstructStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04002697 addConstructor(node->getType(), scopedStruct(node->getType().getStruct()->name()), &node->getSequence());
2698 outputTriplet(visit, structLookup(node->getType().getStruct()->name()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002699 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002700 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2701 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2702 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2703 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2704 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2705 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002706 case EOpMod:
2707 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002708 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002709 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2710 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2711 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002712 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002713 case 11: mUsesMod1 = true; break;
2714 case 22: mUsesMod2v = true; break;
2715 case 21: mUsesMod2f = true; break;
2716 case 33: mUsesMod3v = true; break;
2717 case 31: mUsesMod3f = true; break;
2718 case 44: mUsesMod4v = true; break;
2719 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002720 default: UNREACHABLE();
2721 }
2722
2723 outputTriplet(visit, "mod(", ", ", ")");
2724 }
2725 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002726 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002727 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002728 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002729 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2730 {
2731 case 1: mUsesAtan2_1 = true; break;
2732 case 2: mUsesAtan2_2 = true; break;
2733 case 3: mUsesAtan2_3 = true; break;
2734 case 4: mUsesAtan2_4 = true; break;
2735 default: UNREACHABLE();
2736 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002737 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002738 break;
2739 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2740 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2741 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2742 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2743 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2744 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2745 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2746 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2747 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002748 case EOpFaceForward:
2749 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002750 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002751 {
2752 case 1: mUsesFaceforward1 = true; break;
2753 case 2: mUsesFaceforward2 = true; break;
2754 case 3: mUsesFaceforward3 = true; break;
2755 case 4: mUsesFaceforward4 = true; break;
2756 default: UNREACHABLE();
2757 }
2758
2759 outputTriplet(visit, "faceforward(", ", ", ")");
2760 }
2761 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002762 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2763 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2764 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002765 default: UNREACHABLE();
2766 }
2767
2768 return true;
2769}
2770
2771bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2772{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002773 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002774
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002775 if (node->usesTernaryOperator())
2776 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002777 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002778 }
2779 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002780 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002781 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002782
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002783 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002784
2785 node->getCondition()->traverse(this);
2786
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002787 out << ")\n";
2788
Jamie Madill075edd82013-07-08 13:30:19 -04002789 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002790 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002791
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002792 bool discard = false;
2793
daniel@transgaming.combb885322010-04-15 20:45:24 +00002794 if (node->getTrueBlock())
2795 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002796 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002797
2798 // Detect true discard
2799 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002800 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002801
Jamie Madill075edd82013-07-08 13:30:19 -04002802 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002803 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002804
2805 if (node->getFalseBlock())
2806 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002807 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002808
Jamie Madill075edd82013-07-08 13:30:19 -04002809 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002810 out << "{\n";
2811
Jamie Madill075edd82013-07-08 13:30:19 -04002812 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002813 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002814
Jamie Madill075edd82013-07-08 13:30:19 -04002815 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002816 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002817
2818 // Detect false discard
2819 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2820 }
2821
2822 // ANGLE issue 486: Detect problematic conditional discard
2823 if (discard && FindSideEffectRewriting::search(node))
2824 {
2825 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002826 }
2827 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002828
2829 return false;
2830}
2831
2832void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2833{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002834 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002835}
2836
2837bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2838{
Nicolas Capens655fe362014-04-11 13:12:34 -04002839 mNestedLoopDepth++;
2840
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002841 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2842
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002843 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002844 {
2845 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2846 }
2847
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002848 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002849 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002850 if (handleExcessiveLoop(node))
2851 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002852 mInsideDiscontinuousLoop = wasDiscontinuous;
2853 mNestedLoopDepth--;
2854
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002855 return false;
2856 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002857 }
2858
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002859 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002860
alokp@chromium.org52813552010-11-16 18:36:09 +00002861 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002862 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002863 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002864
Jamie Madill075edd82013-07-08 13:30:19 -04002865 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002866 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002867 }
2868 else
2869 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002870 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002871
2872 if (node->getInit())
2873 {
2874 node->getInit()->traverse(this);
2875 }
2876
2877 out << "; ";
2878
alokp@chromium.org52813552010-11-16 18:36:09 +00002879 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002880 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002881 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002882 }
2883
2884 out << "; ";
2885
alokp@chromium.org52813552010-11-16 18:36:09 +00002886 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002887 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002888 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002889 }
2890
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002891 out << ")\n";
2892
Jamie Madill075edd82013-07-08 13:30:19 -04002893 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002894 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002895 }
2896
2897 if (node->getBody())
2898 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002899 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002900 }
2901
Jamie Madill075edd82013-07-08 13:30:19 -04002902 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002903 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002904
alokp@chromium.org52813552010-11-16 18:36:09 +00002905 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002906 {
Jamie Madill075edd82013-07-08 13:30:19 -04002907 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002908 out << "while(\n";
2909
alokp@chromium.org52813552010-11-16 18:36:09 +00002910 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002911
daniel@transgaming.com73536982012-03-21 20:45:49 +00002912 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002913 }
2914
daniel@transgaming.com73536982012-03-21 20:45:49 +00002915 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002916
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002917 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002918 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002919
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002920 return false;
2921}
2922
2923bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2924{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002925 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002926
2927 switch (node->getFlowOp())
2928 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002929 case EOpKill:
2930 outputTriplet(visit, "discard;\n", "", "");
2931 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002932 case EOpBreak:
2933 if (visit == PreVisit)
2934 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002935 if (mNestedLoopDepth > 1)
2936 {
2937 mUsesNestedBreak = true;
2938 }
2939
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002940 if (mExcessiveLoopIndex)
2941 {
2942 out << "{Break";
2943 mExcessiveLoopIndex->traverse(this);
2944 out << " = true; break;}\n";
2945 }
2946 else
2947 {
2948 out << "break;\n";
2949 }
2950 }
2951 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002952 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002953 case EOpReturn:
2954 if (visit == PreVisit)
2955 {
2956 if (node->getExpression())
2957 {
2958 out << "return ";
2959 }
2960 else
2961 {
2962 out << "return;\n";
2963 }
2964 }
2965 else if (visit == PostVisit)
2966 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002967 if (node->getExpression())
2968 {
2969 out << ";\n";
2970 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002971 }
2972 break;
2973 default: UNREACHABLE();
2974 }
2975
2976 return true;
2977}
2978
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002979void OutputHLSL::traverseStatements(TIntermNode *node)
2980{
2981 if (isSingleStatement(node))
2982 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002983 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002984 }
2985
2986 node->traverse(this);
2987}
2988
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002989bool OutputHLSL::isSingleStatement(TIntermNode *node)
2990{
2991 TIntermAggregate *aggregate = node->getAsAggregate();
2992
2993 if (aggregate)
2994 {
2995 if (aggregate->getOp() == EOpSequence)
2996 {
2997 return false;
2998 }
2999 else
3000 {
3001 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
3002 {
3003 if (!isSingleStatement(*sit))
3004 {
3005 return false;
3006 }
3007 }
3008
3009 return true;
3010 }
3011 }
3012
3013 return true;
3014}
3015
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003016// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
3017// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003018bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
3019{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003020 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00003021 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003022
3023 // Parse loops of the form:
3024 // for(int index = initial; index [comparator] limit; index += increment)
3025 TIntermSymbol *index = NULL;
3026 TOperator comparator = EOpNull;
3027 int initial = 0;
3028 int limit = 0;
3029 int increment = 0;
3030
3031 // Parse index name and intial value
3032 if (node->getInit())
3033 {
3034 TIntermAggregate *init = node->getInit()->getAsAggregate();
3035
3036 if (init)
3037 {
3038 TIntermSequence &sequence = init->getSequence();
3039 TIntermTyped *variable = sequence[0]->getAsTyped();
3040
3041 if (variable && variable->getQualifier() == EvqTemporary)
3042 {
3043 TIntermBinary *assign = variable->getAsBinaryNode();
3044
3045 if (assign->getOp() == EOpInitialize)
3046 {
3047 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3048 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3049
3050 if (symbol && constant)
3051 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003052 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003053 {
3054 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003055 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003056 }
3057 }
3058 }
3059 }
3060 }
3061 }
3062
3063 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00003064 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003065 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003066 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003067
3068 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
3069 {
3070 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3071
3072 if (constant)
3073 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003074 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003075 {
3076 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003077 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003078 }
3079 }
3080 }
3081 }
3082
3083 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00003084 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003085 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003086 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3087 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003088
3089 if (binaryTerminal)
3090 {
3091 TOperator op = binaryTerminal->getOp();
3092 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3093
3094 if (constant)
3095 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003096 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003097 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003098 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003099
3100 switch (op)
3101 {
3102 case EOpAddAssign: increment = value; break;
3103 case EOpSubAssign: increment = -value; break;
3104 default: UNIMPLEMENTED();
3105 }
3106 }
3107 }
3108 }
3109 else if (unaryTerminal)
3110 {
3111 TOperator op = unaryTerminal->getOp();
3112
3113 switch (op)
3114 {
3115 case EOpPostIncrement: increment = 1; break;
3116 case EOpPostDecrement: increment = -1; break;
3117 case EOpPreIncrement: increment = 1; break;
3118 case EOpPreDecrement: increment = -1; break;
3119 default: UNIMPLEMENTED();
3120 }
3121 }
3122 }
3123
3124 if (index != NULL && comparator != EOpNull && increment != 0)
3125 {
3126 if (comparator == EOpLessThanEqual)
3127 {
3128 comparator = EOpLessThan;
3129 limit += 1;
3130 }
3131
3132 if (comparator == EOpLessThan)
3133 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00003134 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003135
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003136 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003137 {
3138 return false; // Not an excessive loop
3139 }
3140
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003141 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
3142 mExcessiveLoopIndex = index;
3143
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003144 out << "{int ";
3145 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003146 out << ";\n"
3147 "bool Break";
3148 index->traverse(this);
3149 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003150
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003151 bool firstLoopFragment = true;
3152
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003153 while (iterations > 0)
3154 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003155 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003156
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003157 if (!firstLoopFragment)
3158 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003159 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003160 index->traverse(this);
3161 out << ") {\n";
3162 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00003163
3164 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
3165 {
3166 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
3167 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003168
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003169 // for(int index = initial; index < clampedLimit; index += increment)
3170
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003171 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003172 index->traverse(this);
3173 out << " = ";
3174 out << initial;
3175
3176 out << "; ";
3177 index->traverse(this);
3178 out << " < ";
3179 out << clampedLimit;
3180
3181 out << "; ";
3182 index->traverse(this);
3183 out << " += ";
3184 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003185 out << ")\n";
3186
Jamie Madill075edd82013-07-08 13:30:19 -04003187 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003188 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003189
3190 if (node->getBody())
3191 {
3192 node->getBody()->traverse(this);
3193 }
3194
Jamie Madill075edd82013-07-08 13:30:19 -04003195 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003196 out << ";}\n";
3197
3198 if (!firstLoopFragment)
3199 {
3200 out << "}\n";
3201 }
3202
3203 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003204
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003205 initial += MAX_LOOP_ITERATIONS * increment;
3206 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003207 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003208
3209 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003210
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003211 mExcessiveLoopIndex = restoreIndex;
3212
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003213 return true;
3214 }
3215 else UNIMPLEMENTED();
3216 }
3217
3218 return false; // Not handled as an excessive loop
3219}
3220
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003221void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003222{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00003223 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003224
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003225 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003226 {
3227 out << preString;
3228 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003229 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003230 {
3231 out << inString;
3232 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003233 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003234 {
3235 out << postString;
3236 }
3237}
3238
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003239void OutputHLSL::outputLineDirective(int line)
3240{
3241 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
3242 {
baustin@google.com8ab69842011-06-02 21:53:45 +00003243 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003244 mBody << "#line " << line;
3245
3246 if (mContext.sourcePath)
3247 {
3248 mBody << " \"" << mContext.sourcePath << "\"";
3249 }
3250
3251 mBody << "\n";
3252 }
3253}
3254
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003255TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3256{
3257 TQualifier qualifier = symbol->getQualifier();
3258 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003259 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003260
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003261 if (name.empty()) // HLSL demands named arguments, also for prototypes
3262 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00003263 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003264 }
3265 else
3266 {
3267 name = decorate(name);
3268 }
3269
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003270 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
3271 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003272 return qualifierString(qualifier) + " " + textureString(type) + " texture_" + name + arrayString(type) + ", " +
3273 qualifierString(qualifier) + " " + samplerString(type) + " sampler_" + name + arrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003274 }
3275
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003276 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003277}
3278
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003279TString OutputHLSL::interpolationString(TQualifier qualifier)
3280{
3281 switch(qualifier)
3282 {
3283 case EvqVaryingIn: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003284 case EvqFragmentIn: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003285 case EvqInvariantVaryingIn: return "";
3286 case EvqSmoothIn: return "linear";
3287 case EvqFlatIn: return "nointerpolation";
3288 case EvqCentroidIn: return "centroid";
3289 case EvqVaryingOut: return "";
Jamie Madill19571812013-08-12 15:26:34 -07003290 case EvqVertexOut: return "";
shannon.woods%transgaming.com@gtempaccount.com1886fd42013-04-13 03:41:45 +00003291 case EvqInvariantVaryingOut: return "";
3292 case EvqSmoothOut: return "linear";
3293 case EvqFlatOut: return "nointerpolation";
3294 case EvqCentroidOut: return "centroid";
3295 default: UNREACHABLE();
3296 }
3297
3298 return "";
3299}
3300
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003301TString OutputHLSL::qualifierString(TQualifier qualifier)
3302{
3303 switch(qualifier)
3304 {
3305 case EvqIn: return "in";
Geoff Lang5f5320a2014-05-26 13:56:11 -04003306 case EvqOut: return "inout"; // 'out' results in an HLSL error if not all fields are written, for GLSL it's undefined
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003307 case EvqInOut: return "inout";
3308 case EvqConstReadOnly: return "const";
3309 default: UNREACHABLE();
3310 }
3311
3312 return "";
3313}
3314
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003315TString OutputHLSL::typeString(const TType &type)
3316{
Jamie Madill98493dd2013-07-08 14:39:03 -04003317 const TStructure* structure = type.getStruct();
3318 if (structure)
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003319 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003320 const TString& typeName = structure->name();
3321 if (typeName != "")
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003322 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003323 return structLookup(typeName);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003324 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003325 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003326 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003327 return structureString(*structure, false, false);
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003328 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00003329 }
3330 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003331 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00003332 int cols = type.getCols();
3333 int rows = type.getRows();
3334 return "float" + str(cols) + "x" + str(rows);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003335 }
3336 else
3337 {
3338 switch (type.getBasicType())
3339 {
3340 case EbtFloat:
3341 switch (type.getNominalSize())
3342 {
3343 case 1: return "float";
3344 case 2: return "float2";
3345 case 3: return "float3";
3346 case 4: return "float4";
3347 }
3348 case EbtInt:
3349 switch (type.getNominalSize())
3350 {
3351 case 1: return "int";
3352 case 2: return "int2";
3353 case 3: return "int3";
3354 case 4: return "int4";
3355 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003356 case EbtUInt:
Jamie Madill22d63da2013-06-07 12:45:12 -04003357 switch (type.getNominalSize())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003358 {
3359 case 1: return "uint";
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00003360 case 2: return "uint2";
3361 case 3: return "uint3";
3362 case 4: return "uint4";
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00003363 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003364 case EbtBool:
3365 switch (type.getNominalSize())
3366 {
3367 case 1: return "bool";
3368 case 2: return "bool2";
3369 case 3: return "bool3";
3370 case 4: return "bool4";
3371 }
3372 case EbtVoid:
3373 return "void";
3374 case EbtSampler2D:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003375 case EbtISampler2D:
Nicolas Capens075368e2013-06-24 15:58:30 -04003376 case EbtUSampler2D:
Nicolas Capensfb50dff2013-06-24 16:16:23 -04003377 case EbtSampler2DArray:
3378 case EbtISampler2DArray:
3379 case EbtUSampler2DArray:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003380 return "sampler2D";
3381 case EbtSamplerCube:
Nicolas Capens1f1a8332013-06-24 15:42:27 -04003382 case EbtISamplerCube:
Nicolas Capens075368e2013-06-24 15:58:30 -04003383 case EbtUSamplerCube:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003384 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00003385 case EbtSamplerExternalOES:
3386 return "sampler2D";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00003387 default:
3388 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003389 }
3390 }
3391
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003392 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003393 return "<unknown type>";
3394}
3395
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003396TString OutputHLSL::textureString(const TType &type)
3397{
3398 switch (type.getBasicType())
3399 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04003400 case EbtSampler2D: return "Texture2D";
3401 case EbtSamplerCube: return "TextureCube";
3402 case EbtSamplerExternalOES: return "Texture2D";
3403 case EbtSampler2DArray: return "Texture2DArray";
3404 case EbtSampler3D: return "Texture3D";
3405 case EbtISampler2D: return "Texture2D<int4>";
3406 case EbtISampler3D: return "Texture3D<int4>";
Nicolas Capens0027fa92014-02-20 14:26:42 -05003407 case EbtISamplerCube: return "Texture2DArray<int4>";
Nicolas Capenscb127d32013-07-15 17:26:18 -04003408 case EbtISampler2DArray: return "Texture2DArray<int4>";
3409 case EbtUSampler2D: return "Texture2D<uint4>";
3410 case EbtUSampler3D: return "Texture3D<uint4>";
Nicolas Capens0027fa92014-02-20 14:26:42 -05003411 case EbtUSamplerCube: return "Texture2DArray<uint4>";
Nicolas Capenscb127d32013-07-15 17:26:18 -04003412 case EbtUSampler2DArray: return "Texture2DArray<uint4>";
3413 case EbtSampler2DShadow: return "Texture2D";
3414 case EbtSamplerCubeShadow: return "TextureCube";
3415 case EbtSampler2DArrayShadow: return "Texture2DArray";
3416 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003417 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04003418
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +00003419 return "<unknown texture type>";
3420}
3421
Nicolas Capenscb127d32013-07-15 17:26:18 -04003422TString OutputHLSL::samplerString(const TType &type)
3423{
3424 if (IsShadowSampler(type.getBasicType()))
3425 {
3426 return "SamplerComparisonState";
3427 }
3428 else
3429 {
3430 return "SamplerState";
3431 }
3432}
3433
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003434TString OutputHLSL::arrayString(const TType &type)
3435{
3436 if (!type.isArray())
3437 {
3438 return "";
3439 }
3440
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003441 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003442}
3443
3444TString OutputHLSL::initializer(const TType &type)
3445{
3446 TString string;
3447
Jamie Madill94bf7f22013-07-08 13:31:15 -04003448 size_t size = type.getObjectSize();
3449 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003450 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003451 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003452
Jamie Madill94bf7f22013-07-08 13:31:15 -04003453 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003454 {
3455 string += ", ";
3456 }
3457 }
3458
daniel@transgaming.comead23042010-04-29 03:35:36 +00003459 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003460}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003461
Jamie Madill98493dd2013-07-08 14:39:03 -04003462TString OutputHLSL::structureString(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003463{
Jamie Madill98493dd2013-07-08 14:39:03 -04003464 const TFieldList &fields = structure.fields();
3465 const bool isNameless = (structure.name() == "");
3466 const TString &structName = structureTypeName(structure, useHLSLRowMajorPacking, useStd140Packing);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003467 const TString declareString = (isNameless ? "struct" : "struct " + structName);
3468
Jamie Madill98493dd2013-07-08 14:39:03 -04003469 TString string;
3470 string += declareString + "\n"
3471 "{\n";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003472
Jamie Madillc835df62013-06-21 09:15:32 -04003473 int elementIndex = 0;
3474
Jamie Madill9cf6c072013-06-20 11:55:53 -04003475 for (unsigned int i = 0; i < fields.size(); i++)
3476 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003477 const TField &field = *fields[i];
3478 const TType &fieldType = *field.type();
3479 const TStructure *fieldStruct = fieldType.getStruct();
3480 const TString &fieldTypeString = fieldStruct ? structureTypeName(*fieldStruct, useHLSLRowMajorPacking, useStd140Packing) : typeString(fieldType);
Jamie Madill9cf6c072013-06-20 11:55:53 -04003481
Jamie Madillc835df62013-06-21 09:15:32 -04003482 if (useStd140Packing)
3483 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003484 string += std140PrePaddingString(*field.type(), &elementIndex);
Jamie Madillc835df62013-06-21 09:15:32 -04003485 }
3486
Jamie Madill98493dd2013-07-08 14:39:03 -04003487 string += " " + fieldTypeString + " " + decorateField(field.name(), structure) + arrayString(fieldType) + ";\n";
Jamie Madillc835df62013-06-21 09:15:32 -04003488
3489 if (useStd140Packing)
3490 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003491 string += std140PostPaddingString(*field.type(), useHLSLRowMajorPacking);
Jamie Madillc835df62013-06-21 09:15:32 -04003492 }
Jamie Madill9cf6c072013-06-20 11:55:53 -04003493 }
3494
3495 // Nameless structs do not finish with a semicolon and newline, to leave room for an instance variable
Jamie Madill98493dd2013-07-08 14:39:03 -04003496 string += (isNameless ? "} " : "};\n");
Jamie Madill9cf6c072013-06-20 11:55:53 -04003497
Jamie Madille4075c92013-06-21 09:15:32 -04003498 // Add remaining element index to the global map, for use with nested structs in standard layouts
3499 if (useStd140Packing)
3500 {
3501 mStd140StructElementIndexes[structName] = elementIndex;
3502 }
3503
Jamie Madill98493dd2013-07-08 14:39:03 -04003504 return string;
Jamie Madill9cf6c072013-06-20 11:55:53 -04003505}
3506
Jamie Madill98493dd2013-07-08 14:39:03 -04003507TString OutputHLSL::structureTypeName(const TStructure &structure, bool useHLSLRowMajorPacking, bool useStd140Packing)
Jamie Madill9cf6c072013-06-20 11:55:53 -04003508{
Jamie Madill98493dd2013-07-08 14:39:03 -04003509 if (structure.name() == "")
Jamie Madill9cf6c072013-06-20 11:55:53 -04003510 {
3511 return "";
3512 }
3513
3514 TString prefix = "";
3515
3516 // Structs packed with row-major matrices in HLSL are prefixed with "rm"
3517 // GLSL column-major maps to HLSL row-major, and the converse is true
Jamie Madillc835df62013-06-21 09:15:32 -04003518
3519 if (useStd140Packing)
3520 {
3521 prefix += "std";
3522 }
3523
Jamie Madill9cf6c072013-06-20 11:55:53 -04003524 if (useHLSLRowMajorPacking)
3525 {
Jamie Madillc835df62013-06-21 09:15:32 -04003526 if (prefix != "") prefix += "_";
Jamie Madill9cf6c072013-06-20 11:55:53 -04003527 prefix += "rm";
3528 }
3529
Jamie Madill98493dd2013-07-08 14:39:03 -04003530 return prefix + structLookup(structure.name());
Jamie Madill9cf6c072013-06-20 11:55:53 -04003531}
3532
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003533void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00003534{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003535 if (name == "")
3536 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00003537 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00003538 }
3539
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00003540 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
3541 {
3542 return; // Already added
3543 }
3544
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003545 TType ctorType = type;
3546 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00003547 ctorType.setPrecision(EbpHigh);
3548 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00003549
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003550 TString ctorName = type.getStruct() ? decorate(name) : name;
3551
3552 typedef std::vector<TType> ParameterArray;
3553 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00003554
Jamie Madill98493dd2013-07-08 14:39:03 -04003555 const TStructure* structure = type.getStruct();
3556 if (structure)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003557 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003558 mStructNames.insert(decorate(name));
3559
Jamie Madill98493dd2013-07-08 14:39:03 -04003560 const TString &structString = structureString(*structure, false, false);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003561
Jamie Madill98493dd2013-07-08 14:39:03 -04003562 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structString) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003563 {
Jamie Madill9cf6c072013-06-20 11:55:53 -04003564 // Add row-major packed struct for interface blocks
3565 TString rowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003566 structureString(*structure, true, false) +
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003567 "#pragma pack_matrix(column_major)\n";
3568
Jamie Madill98493dd2013-07-08 14:39:03 -04003569 TString std140String = structureString(*structure, false, true);
Jamie Madillc835df62013-06-21 09:15:32 -04003570 TString std140RowMajorString = "#pragma pack_matrix(row_major)\n" +
Jamie Madill98493dd2013-07-08 14:39:03 -04003571 structureString(*structure, true, true) +
Jamie Madillc835df62013-06-21 09:15:32 -04003572 "#pragma pack_matrix(column_major)\n";
3573
Jamie Madill98493dd2013-07-08 14:39:03 -04003574 mStructDeclarations.push_back(structString);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +00003575 mStructDeclarations.push_back(rowMajorString);
Jamie Madillc835df62013-06-21 09:15:32 -04003576 mStructDeclarations.push_back(std140String);
3577 mStructDeclarations.push_back(std140RowMajorString);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003578 }
3579
Jamie Madill98493dd2013-07-08 14:39:03 -04003580 const TFieldList &fields = structure->fields();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003581 for (unsigned int i = 0; i < fields.size(); i++)
3582 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003583 ctorParameters.push_back(*fields[i]->type());
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003584 }
3585 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00003586 else if (parameters)
3587 {
3588 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
3589 {
3590 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
3591 }
3592 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00003593 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00003594
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003595 TString constructor;
3596
3597 if (ctorType.getStruct())
3598 {
3599 constructor += ctorName + " " + ctorName + "_ctor(";
3600 }
3601 else // Built-in type
3602 {
3603 constructor += typeString(ctorType) + " " + ctorName + "(";
3604 }
3605
3606 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
3607 {
3608 const TType &type = ctorParameters[parameter];
3609
3610 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
3611
3612 if (parameter < ctorParameters.size() - 1)
3613 {
3614 constructor += ", ";
3615 }
3616 }
3617
3618 constructor += ")\n"
3619 "{\n";
3620
3621 if (ctorType.getStruct())
3622 {
3623 constructor += " " + ctorName + " structure = {";
3624 }
3625 else
3626 {
3627 constructor += " return " + typeString(ctorType) + "(";
3628 }
3629
3630 if (ctorType.isMatrix() && ctorParameters.size() == 1)
3631 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003632 int rows = ctorType.getRows();
3633 int cols = ctorType.getCols();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003634 const TType &parameter = ctorParameters[0];
3635
3636 if (parameter.isScalar())
3637 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003638 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003639 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003640 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003641 {
3642 constructor += TString((row == col) ? "x0" : "0.0");
3643
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003644 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003645 {
3646 constructor += ", ";
3647 }
3648 }
3649 }
3650 }
3651 else if (parameter.isMatrix())
3652 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003653 for (int row = 0; row < rows; row++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003654 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003655 for (int col = 0; col < cols; col++)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003656 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003657 if (row < parameter.getRows() && col < parameter.getCols())
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003658 {
3659 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
3660 }
3661 else
3662 {
3663 constructor += TString((row == col) ? "1.0" : "0.0");
3664 }
3665
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003666 if (row < rows - 1 || col < cols - 1)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003667 {
3668 constructor += ", ";
3669 }
3670 }
3671 }
3672 }
3673 else UNREACHABLE();
3674 }
3675 else
3676 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003677 size_t remainingComponents = ctorType.getObjectSize();
3678 size_t parameterIndex = 0;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003679
3680 while (remainingComponents > 0)
3681 {
3682 const TType &parameter = ctorParameters[parameterIndex];
Jamie Madill94bf7f22013-07-08 13:31:15 -04003683 const size_t parameterSize = parameter.getObjectSize();
3684 bool moreParameters = parameterIndex + 1 < ctorParameters.size();
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003685
3686 constructor += "x" + str(parameterIndex);
3687
3688 if (parameter.isScalar())
3689 {
3690 remainingComponents -= parameter.getObjectSize();
3691 }
3692 else if (parameter.isVector())
3693 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003694 if (remainingComponents == parameterSize || moreParameters)
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003695 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003696 ASSERT(parameterSize <= remainingComponents);
3697 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003698 }
Jamie Madill94bf7f22013-07-08 13:31:15 -04003699 else if (remainingComponents < static_cast<size_t>(parameter.getNominalSize()))
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003700 {
3701 switch (remainingComponents)
3702 {
3703 case 1: constructor += ".x"; break;
3704 case 2: constructor += ".xy"; break;
3705 case 3: constructor += ".xyz"; break;
3706 case 4: constructor += ".xyzw"; break;
3707 default: UNREACHABLE();
3708 }
3709
3710 remainingComponents = 0;
3711 }
3712 else UNREACHABLE();
3713 }
3714 else if (parameter.isMatrix() || parameter.getStruct())
3715 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003716 ASSERT(remainingComponents == parameterSize || moreParameters);
3717 ASSERT(parameterSize <= remainingComponents);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003718
Jamie Madill94bf7f22013-07-08 13:31:15 -04003719 remainingComponents -= parameterSize;
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003720 }
3721 else UNREACHABLE();
3722
3723 if (moreParameters)
3724 {
3725 parameterIndex++;
3726 }
3727
3728 if (remainingComponents)
3729 {
3730 constructor += ", ";
3731 }
3732 }
3733 }
3734
3735 if (ctorType.getStruct())
3736 {
3737 constructor += "};\n"
3738 " return structure;\n"
3739 "}\n";
3740 }
3741 else
3742 {
3743 constructor += ");\n"
3744 "}\n";
3745 }
3746
daniel@transgaming.com63691862010-04-29 03:32:42 +00003747 mConstructors.insert(constructor);
3748}
3749
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003750const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3751{
3752 TInfoSinkBase &out = mBody;
3753
Jamie Madill98493dd2013-07-08 14:39:03 -04003754 const TStructure* structure = type.getStruct();
3755 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003756 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003757 out << structLookup(structure->name()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003758
Jamie Madill98493dd2013-07-08 14:39:03 -04003759 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003760
Jamie Madill98493dd2013-07-08 14:39:03 -04003761 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003762 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003763 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003764
3765 constUnion = writeConstantUnion(*fieldType, constUnion);
3766
Jamie Madill98493dd2013-07-08 14:39:03 -04003767 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003768 {
3769 out << ", ";
3770 }
3771 }
3772
3773 out << ")";
3774 }
3775 else
3776 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003777 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003778 bool writeType = size > 1;
3779
3780 if (writeType)
3781 {
3782 out << typeString(type) << "(";
3783 }
3784
Jamie Madill94bf7f22013-07-08 13:31:15 -04003785 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003786 {
3787 switch (constUnion->getType())
3788 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003789 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003790 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003791 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003792 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003793 default: UNREACHABLE();
3794 }
3795
3796 if (i != size - 1)
3797 {
3798 out << ", ";
3799 }
3800 }
3801
3802 if (writeType)
3803 {
3804 out << ")";
3805 }
3806 }
3807
3808 return constUnion;
3809}
3810
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003811TString OutputHLSL::scopeString(unsigned int depthLimit)
3812{
3813 TString string;
3814
3815 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
3816 {
Jamie Madill17a0e632014-04-25 11:29:54 -04003817 string += "_" + str(mScopeBracket[i]);
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00003818 }
3819
3820 return string;
3821}
3822
3823TString OutputHLSL::scopedStruct(const TString &typeName)
3824{
3825 if (typeName == "")
3826 {
3827 return typeName;
3828 }
3829
3830 return typeName + scopeString(mScopeDepth);
3831}
3832
3833TString OutputHLSL::structLookup(const TString &typeName)
3834{
3835 for (int depth = mScopeDepth; depth >= 0; depth--)
3836 {
3837 TString scopedName = decorate(typeName + scopeString(depth));
3838
3839 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
3840 {
3841 if (*structName == scopedName)
3842 {
3843 return scopedName;
3844 }
3845 }
3846 }
3847
3848 UNREACHABLE(); // Should have found a matching constructor
3849
3850 return typeName;
3851}
3852
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003853TString OutputHLSL::decorate(const TString &string)
3854{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00003855 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003856 {
3857 return "_" + string;
3858 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003859
3860 return string;
3861}
3862
apatrick@chromium.org65756022012-01-17 21:45:38 +00003863TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003864{
daniel@transgaming.comdb019952012-12-20 21:13:32 +00003865 if (type.getBasicType() == EbtSamplerExternalOES)
apatrick@chromium.org65756022012-01-17 21:45:38 +00003866 {
3867 return "ex_" + string;
3868 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00003869
3870 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003871}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003872
Jamie Madill98493dd2013-07-08 14:39:03 -04003873TString OutputHLSL::decorateField(const TString &string, const TStructure &structure)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003874{
Jamie Madill98493dd2013-07-08 14:39:03 -04003875 if (structure.name().compare(0, 3, "gl_") != 0)
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00003876 {
3877 return decorate(string);
3878 }
3879
3880 return string;
3881}
daniel@transgaming.com652468c2012-12-20 21:11:57 +00003882
Jamie Madill834e8b72014-04-11 13:33:58 -04003883void OutputHLSL::declareInterfaceBlockField(const TType &type, const TString &name, std::vector<gl::InterfaceBlockField>& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003884{
Jamie Madill98493dd2013-07-08 14:39:03 -04003885 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003886
3887 if (!structure)
3888 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003889 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill834e8b72014-04-11 13:33:58 -04003890 gl::InterfaceBlockField field(glVariableType(type), glVariablePrecision(type), name.c_str(),
3891 (unsigned int)type.getArraySize(), isRowMajorMatrix);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003892 output.push_back(field);
3893 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003894 else
3895 {
Jamie Madill834e8b72014-04-11 13:33:58 -04003896 gl::InterfaceBlockField structField(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), false);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003897
3898 const TFieldList &fields = structure->fields();
3899
3900 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3901 {
3902 TField *field = fields[fieldIndex];
3903 TType *fieldType = field->type();
3904
3905 // make sure to copy matrix packing information
3906 fieldType->setLayoutQualifier(type.getLayoutQualifier());
3907
3908 declareInterfaceBlockField(*fieldType, field->name(), structField.fields);
3909 }
3910
3911 output.push_back(structField);
3912 }
3913}
3914
Jamie Madill834e8b72014-04-11 13:33:58 -04003915gl::Uniform OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, std::vector<gl::Uniform>& output)
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003916{
3917 const TStructure *structure = type.getStruct();
3918
3919 if (!structure)
3920 {
Jamie Madill834e8b72014-04-11 13:33:58 -04003921 gl::Uniform uniform(glVariableType(type), glVariablePrecision(type), name.c_str(),
3922 (unsigned int)type.getArraySize(), (unsigned int)registerIndex, 0);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003923 output.push_back(uniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003924
3925 return uniform;
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003926 }
3927 else
3928 {
Jamie Madill834e8b72014-04-11 13:33:58 -04003929 gl::Uniform structUniform(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(),
3930 (unsigned int)registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003931
Jamie Madill98493dd2013-07-08 14:39:03 -04003932 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003933
Jamie Madill98493dd2013-07-08 14:39:03 -04003934 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003935 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003936 TField *field = fields[fieldIndex];
3937 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003938
Jamie Madill56093782013-08-30 13:21:11 -04003939 declareUniformToList(*fieldType, field->name(), GL_INVALID_INDEX, structUniform.fields);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003940 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003941
Jamie Madill56093782013-08-30 13:21:11 -04003942 // assign register offset information -- this will override the information in any sub-structures.
Vladimir Vukicevic24d8d672014-05-27 12:07:51 -04003943 HLSLVariableGetRegisterInfo(registerIndex, &structUniform, mOutputType);
Jamie Madill56093782013-08-30 13:21:11 -04003944
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003945 output.push_back(structUniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003946
3947 return structUniform;
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003948 }
3949}
3950
Jamie Madill834e8b72014-04-11 13:33:58 -04003951gl::InterpolationType getInterpolationType(TQualifier qualifier)
Jamie Madill139b9092013-08-30 13:21:06 -04003952{
3953 switch (qualifier)
3954 {
3955 case EvqFlatIn:
3956 case EvqFlatOut:
Jamie Madill834e8b72014-04-11 13:33:58 -04003957 return gl::INTERPOLATION_FLAT;
Jamie Madill139b9092013-08-30 13:21:06 -04003958
3959 case EvqSmoothIn:
3960 case EvqSmoothOut:
3961 case EvqVertexOut:
3962 case EvqFragmentIn:
Jamie Madill384b6042013-09-13 10:06:24 -04003963 case EvqVaryingIn:
3964 case EvqVaryingOut:
Jamie Madill834e8b72014-04-11 13:33:58 -04003965 return gl::INTERPOLATION_SMOOTH;
Jamie Madill139b9092013-08-30 13:21:06 -04003966
3967 case EvqCentroidIn:
3968 case EvqCentroidOut:
Jamie Madill834e8b72014-04-11 13:33:58 -04003969 return gl::INTERPOLATION_CENTROID;
Jamie Madill139b9092013-08-30 13:21:06 -04003970
3971 default: UNREACHABLE();
Jamie Madill834e8b72014-04-11 13:33:58 -04003972 return gl::INTERPOLATION_SMOOTH;
Jamie Madill139b9092013-08-30 13:21:06 -04003973 }
3974}
3975
Jamie Madill834e8b72014-04-11 13:33:58 -04003976void OutputHLSL::declareVaryingToList(const TType &type, TQualifier baseTypeQualifier, const TString &name, std::vector<gl::Varying>& fieldsOut)
Jamie Madill47fdd132013-08-30 13:21:04 -04003977{
3978 const TStructure *structure = type.getStruct();
3979
Jamie Madill834e8b72014-04-11 13:33:58 -04003980 gl::InterpolationType interpolation = getInterpolationType(baseTypeQualifier);
Jamie Madill47fdd132013-08-30 13:21:04 -04003981 if (!structure)
3982 {
Jamie Madill834e8b72014-04-11 13:33:58 -04003983 gl::Varying varying(glVariableType(type), glVariablePrecision(type), name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003984 fieldsOut.push_back(varying);
3985 }
3986 else
3987 {
Jamie Madill834e8b72014-04-11 13:33:58 -04003988 gl::Varying structVarying(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003989 const TFieldList &fields = structure->fields();
3990
Jamie Madill28167c62013-08-30 13:21:10 -04003991 structVarying.structName = structure->name().c_str();
3992
Jamie Madill47fdd132013-08-30 13:21:04 -04003993 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3994 {
3995 const TField &field = *fields[fieldIndex];
Jamie Madill94599662013-08-30 13:21:10 -04003996 declareVaryingToList(*field.type(), baseTypeQualifier, field.name(), structVarying.fields);
Jamie Madill47fdd132013-08-30 13:21:04 -04003997 }
3998
3999 fieldsOut.push_back(structVarying);
4000 }
4001}
4002
Jamie Madillc2141fb2013-08-30 13:21:08 -04004003int OutputHLSL::declareUniformAndAssignRegister(const TType &type, const TString &name)
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00004004{
Jamie Madillc2141fb2013-08-30 13:21:08 -04004005 int registerIndex = (IsSampler(type.getBasicType()) ? mSamplerRegister : mUniformRegister);
4006
Jamie Madill834e8b72014-04-11 13:33:58 -04004007 const gl::Uniform &uniform = declareUniformToList(type, name, registerIndex, mActiveUniforms);
Jamie Madillc2141fb2013-08-30 13:21:08 -04004008
4009 if (IsSampler(type.getBasicType()))
4010 {
Vladimir Vukicevic24d8d672014-05-27 12:07:51 -04004011 mSamplerRegister += gl::HLSLVariableRegisterCount(uniform, mOutputType);
Jamie Madillc2141fb2013-08-30 13:21:08 -04004012 }
4013 else
4014 {
Vladimir Vukicevic24d8d672014-05-27 12:07:51 -04004015 mUniformRegister += gl::HLSLVariableRegisterCount(uniform, mOutputType);
Jamie Madillc2141fb2013-08-30 13:21:08 -04004016 }
4017
4018 return registerIndex;
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00004019}
4020
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004021GLenum OutputHLSL::glVariableType(const TType &type)
4022{
4023 if (type.getBasicType() == EbtFloat)
4024 {
4025 if (type.isScalar())
4026 {
4027 return GL_FLOAT;
4028 }
4029 else if (type.isVector())
4030 {
4031 switch(type.getNominalSize())
4032 {
4033 case 2: return GL_FLOAT_VEC2;
4034 case 3: return GL_FLOAT_VEC3;
4035 case 4: return GL_FLOAT_VEC4;
4036 default: UNREACHABLE();
4037 }
4038 }
4039 else if (type.isMatrix())
4040 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00004041 switch (type.getCols())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004042 {
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +00004043 case 2:
4044 switch(type.getRows())
4045 {
4046 case 2: return GL_FLOAT_MAT2;
4047 case 3: return GL_FLOAT_MAT2x3;
4048 case 4: return GL_FLOAT_MAT2x4;
4049 default: UNREACHABLE();
4050 }
4051
4052 case 3:
4053 switch(type.getRows())
4054 {
4055 case 2: return GL_FLOAT_MAT3x2;
4056 case 3: return GL_FLOAT_MAT3;
4057 case 4: return GL_FLOAT_MAT3x4;
4058 default: UNREACHABLE();
4059 }
4060
4061 case 4:
4062 switch(type.getRows())
4063 {
4064 case 2: return GL_FLOAT_MAT4x2;
4065 case 3: return GL_FLOAT_MAT4x3;
4066 case 4: return GL_FLOAT_MAT4;
4067 default: UNREACHABLE();
4068 }
4069
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004070 default: UNREACHABLE();
4071 }
4072 }
4073 else UNREACHABLE();
4074 }
4075 else if (type.getBasicType() == EbtInt)
4076 {
4077 if (type.isScalar())
4078 {
4079 return GL_INT;
4080 }
4081 else if (type.isVector())
4082 {
4083 switch(type.getNominalSize())
4084 {
4085 case 2: return GL_INT_VEC2;
4086 case 3: return GL_INT_VEC3;
4087 case 4: return GL_INT_VEC4;
4088 default: UNREACHABLE();
4089 }
4090 }
4091 else UNREACHABLE();
4092 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004093 else if (type.getBasicType() == EbtUInt)
4094 {
4095 if (type.isScalar())
4096 {
4097 return GL_UNSIGNED_INT;
4098 }
4099 else if (type.isVector())
4100 {
Jamie Madill22d63da2013-06-07 12:45:12 -04004101 switch(type.getNominalSize())
shannonwoods@chromium.org8c788e82013-05-30 00:20:21 +00004102 {
4103 case 2: return GL_UNSIGNED_INT_VEC2;
4104 case 3: return GL_UNSIGNED_INT_VEC3;
4105 case 4: return GL_UNSIGNED_INT_VEC4;
4106 default: UNREACHABLE();
4107 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004108 }
4109 else UNREACHABLE();
4110 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004111 else if (type.getBasicType() == EbtBool)
4112 {
4113 if (type.isScalar())
4114 {
4115 return GL_BOOL;
4116 }
4117 else if (type.isVector())
4118 {
4119 switch(type.getNominalSize())
4120 {
4121 case 2: return GL_BOOL_VEC2;
4122 case 3: return GL_BOOL_VEC3;
4123 case 4: return GL_BOOL_VEC4;
4124 default: UNREACHABLE();
4125 }
4126 }
4127 else UNREACHABLE();
4128 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004129
4130 switch(type.getBasicType())
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004131 {
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004132 case EbtSampler2D: return GL_SAMPLER_2D;
4133 case EbtSampler3D: return GL_SAMPLER_3D;
4134 case EbtSamplerCube: return GL_SAMPLER_CUBE;
4135 case EbtSampler2DArray: return GL_SAMPLER_2D_ARRAY;
4136 case EbtISampler2D: return GL_INT_SAMPLER_2D;
4137 case EbtISampler3D: return GL_INT_SAMPLER_3D;
4138 case EbtISamplerCube: return GL_INT_SAMPLER_CUBE;
4139 case EbtISampler2DArray: return GL_INT_SAMPLER_2D_ARRAY;
4140 case EbtUSampler2D: return GL_UNSIGNED_INT_SAMPLER_2D;
4141 case EbtUSampler3D: return GL_UNSIGNED_INT_SAMPLER_3D;
4142 case EbtUSamplerCube: return GL_UNSIGNED_INT_SAMPLER_CUBE;
4143 case EbtUSampler2DArray: return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
4144 case EbtSampler2DShadow: return GL_SAMPLER_2D_SHADOW;
4145 case EbtSamplerCubeShadow: return GL_SAMPLER_CUBE_SHADOW;
4146 case EbtSampler2DArrayShadow: return GL_SAMPLER_2D_ARRAY_SHADOW;
4147 default: UNREACHABLE();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004148 }
Nicolas Capens354ed2d2013-07-11 11:26:26 -04004149
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00004150 return GL_NONE;
4151}
4152
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004153GLenum OutputHLSL::glVariablePrecision(const TType &type)
4154{
4155 if (type.getBasicType() == EbtFloat)
4156 {
4157 switch (type.getPrecision())
4158 {
4159 case EbpHigh: return GL_HIGH_FLOAT;
4160 case EbpMedium: return GL_MEDIUM_FLOAT;
4161 case EbpLow: return GL_LOW_FLOAT;
4162 case EbpUndefined:
4163 // Should be defined as the default precision by the parser
4164 default: UNREACHABLE();
4165 }
4166 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00004167 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004168 {
4169 switch (type.getPrecision())
4170 {
4171 case EbpHigh: return GL_HIGH_INT;
4172 case EbpMedium: return GL_MEDIUM_INT;
4173 case EbpLow: return GL_LOW_INT;
4174 case EbpUndefined:
4175 // Should be defined as the default precision by the parser
4176 default: UNREACHABLE();
4177 }
4178 }
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004179
shannon.woods@transgaming.com10aadb22013-02-28 23:17:52 +00004180 // Other types (boolean, sampler) don't have a precision
shannon.woods@transgaming.comfe3c0ef2013-02-28 23:17:22 +00004181 return GL_NONE;
4182}
4183
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004184bool OutputHLSL::isVaryingOut(TQualifier qualifier)
4185{
4186 switch(qualifier)
4187 {
4188 case EvqVaryingOut:
4189 case EvqInvariantVaryingOut:
4190 case EvqSmoothOut:
4191 case EvqFlatOut:
4192 case EvqCentroidOut:
Jamie Madill19571812013-08-12 15:26:34 -07004193 case EvqVertexOut:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004194 return true;
Jamie Madill10567262014-04-17 16:40:00 -04004195
4196 default: break;
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004197 }
4198
4199 return false;
4200}
4201
4202bool OutputHLSL::isVaryingIn(TQualifier qualifier)
4203{
4204 switch(qualifier)
4205 {
4206 case EvqVaryingIn:
4207 case EvqInvariantVaryingIn:
4208 case EvqSmoothIn:
4209 case EvqFlatIn:
4210 case EvqCentroidIn:
Jamie Madill19571812013-08-12 15:26:34 -07004211 case EvqFragmentIn:
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004212 return true;
Jamie Madill10567262014-04-17 16:40:00 -04004213
4214 default: break;
shannon.woods%transgaming.com@gtempaccount.com6f273e32013-04-13 03:41:15 +00004215 }
4216
4217 return false;
4218}
4219
4220bool OutputHLSL::isVarying(TQualifier qualifier)
4221{
4222 return isVaryingIn(qualifier) || isVaryingOut(qualifier);
4223}
4224
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004225}