blob: 220898ef191ee9d0efa7772cbb6a044ff356dc5f [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"
Jamie Madill033dae62014-06-18 12:56:28 -040020#include "compiler/translator/UtilsHLSL.h"
21#include "compiler/translator/util.h"
Jamie Madill8daaba12014-06-13 10:04:33 -040022#include "compiler/translator/StructureHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000023
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000024#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000025#include <cfloat>
26#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000027
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000028namespace sh
29{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000030
Nicolas Capense0ba27a2013-06-24 16:10:52 -040031TString OutputHLSL::TextureFunction::name() const
32{
33 TString name = "gl_texture";
34
Nicolas Capens6d232bb2013-07-08 15:56:38 -040035 if (IsSampler2D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040036 {
37 name += "2D";
38 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040039 else if (IsSampler3D(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040040 {
41 name += "3D";
42 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -040043 else if (IsSamplerCube(sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -040044 {
45 name += "Cube";
46 }
47 else UNREACHABLE();
48
49 if (proj)
50 {
51 name += "Proj";
52 }
53
Nicolas Capensb1f45b72013-12-19 17:37:19 -050054 if (offset)
55 {
56 name += "Offset";
57 }
58
Nicolas Capens75fb4752013-07-10 15:14:47 -040059 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -040060 {
Nicolas Capensfc014542014-02-18 14:47:13 -050061 case IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040062 case BIAS: break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050063 case LOD: name += "Lod"; break;
64 case LOD0: name += "Lod0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -040065 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -050066 case SIZE: name += "Size"; break;
67 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -050068 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -040069 default: UNREACHABLE();
70 }
71
72 return name + "(";
73}
74
Jamie Madillc2141fb2013-08-30 13:21:08 -040075const char *RegisterPrefix(const TType &type)
76{
77 if (IsSampler(type.getBasicType()))
78 {
79 return "s";
80 }
81 else
82 {
83 return "c";
84 }
85}
86
Nicolas Capense0ba27a2013-06-24 16:10:52 -040087bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
88{
89 if (sampler < rhs.sampler) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040090 if (sampler > rhs.sampler) return false;
91
Nicolas Capense0ba27a2013-06-24 16:10:52 -040092 if (coords < rhs.coords) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040093 if (coords > rhs.coords) return false;
94
Nicolas Capense0ba27a2013-06-24 16:10:52 -040095 if (!proj && rhs.proj) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -040096 if (proj && !rhs.proj) return false;
97
98 if (!offset && rhs.offset) return true;
99 if (offset && !rhs.offset) return false;
100
Nicolas Capens75fb4752013-07-10 15:14:47 -0400101 if (method < rhs.method) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400102 if (method > rhs.method) return false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400103
104 return false;
105}
106
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000107OutputHLSL::OutputHLSL(TParseContext &context, const ShBuiltInResources& resources, ShShaderOutput outputType)
shannon.woods@transgaming.comb73964e2013-01-25 21:49:14 +0000108 : TIntermTraverser(true, true, true), mContext(context), mOutputType(outputType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000109{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +0000110 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000111 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000112
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000113 mUsesFragColor = false;
114 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000115 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000116 mUsesFragCoord = false;
117 mUsesPointCoord = false;
118 mUsesFrontFacing = false;
119 mUsesPointSize = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400120 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000121 mUsesXor = false;
122 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000123 mUsesMod2v = false;
124 mUsesMod2f = false;
125 mUsesMod3v = false;
126 mUsesMod3f = false;
127 mUsesMod4v = false;
128 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000129 mUsesFaceforward1 = false;
130 mUsesFaceforward2 = false;
131 mUsesFaceforward3 = false;
132 mUsesFaceforward4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000133 mUsesAtan2_1 = false;
134 mUsesAtan2_2 = false;
135 mUsesAtan2_3 = false;
136 mUsesAtan2_4 = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500137 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400138 mUsesNestedBreak = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000139
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000140 mNumRenderTargets = resources.EXT_draw_buffers ? resources.MaxDrawBuffers : 1;
141
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000142 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000143
144 mContainsLoopDiscontinuity = false;
145 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000146 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400147 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000148
149 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000150
Jamie Madill8daaba12014-06-13 10:04:33 -0400151 mStructureHLSL = new StructureHLSL;
152
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000153 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000154 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000155 if (mContext.shaderType == SH_FRAGMENT_SHADER)
156 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000157 mUniformRegister = 3; // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000158 }
159 else
160 {
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000161 mUniformRegister = 2; // Reserve registers for dx_DepthRange and dx_ViewAdjust
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000162 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000163 }
164 else
165 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000166 mUniformRegister = 0;
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000167 }
168
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000169 mSamplerRegister = 0;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000170 mInterfaceBlockRegister = 2; // Reserve registers for the default uniform block and driver constants
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000171}
172
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000173OutputHLSL::~OutputHLSL()
174{
Jamie Madill8daaba12014-06-13 10:04:33 -0400175 SafeDelete(mUnfoldShortCircuit);
176 SafeDelete(mStructureHLSL);
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000177}
178
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000179void OutputHLSL::output()
180{
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +0000181 mContainsLoopDiscontinuity = mContext.shaderType == SH_FRAGMENT_SHADER && containsLoopDiscontinuity(mContext.treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400182 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
183 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000184
Jamie Madille53c98b2014-02-03 11:57:13 -0500185 // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
186 // use a vertex attribute as a condition, and some related computation in the else block.
187 if (mOutputType == SH_HLSL9_OUTPUT && mContext.shaderType == SH_VERTEX_SHADER)
188 {
189 RewriteElseBlocks(mContext.treeRoot);
190 }
191
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000192 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 +0000193 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000194
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000195 mContext.infoSink().obj << mHeader.c_str();
196 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000197}
198
Jamie Madill570e04d2013-06-21 09:15:33 -0400199void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
200{
201 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
202 {
203 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
204
205 // This will mark the necessary block elements as referenced
206 flaggedNode->traverse(this);
207 TString structName(mBody.c_str());
208 mBody.erase();
209
210 mFlaggedStructOriginalNames[flaggedNode] = structName;
211
212 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
213 {
214 structName.erase(pos, 1);
215 }
216
217 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
218 }
219}
220
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000221TInfoSinkBase &OutputHLSL::getBodyStream()
222{
223 return mBody;
224}
225
Jamie Madill834e8b72014-04-11 13:33:58 -0400226const std::vector<gl::Uniform> &OutputHLSL::getUniforms()
daniel@transgaming.com043da132012-12-20 21:12:22 +0000227{
228 return mActiveUniforms;
229}
230
Jamie Madill834e8b72014-04-11 13:33:58 -0400231const std::vector<gl::InterfaceBlock> &OutputHLSL::getInterfaceBlocks() const
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000232{
233 return mActiveInterfaceBlocks;
234}
235
Jamie Madill834e8b72014-04-11 13:33:58 -0400236const std::vector<gl::Attribute> &OutputHLSL::getOutputVariables() const
Jamie Madill46131a32013-06-20 11:55:50 -0400237{
238 return mActiveOutputVariables;
239}
240
Jamie Madill834e8b72014-04-11 13:33:58 -0400241const std::vector<gl::Attribute> &OutputHLSL::getAttributes() const
Jamie Madilldefb6742013-06-20 11:55:51 -0400242{
243 return mActiveAttributes;
244}
245
Jamie Madill834e8b72014-04-11 13:33:58 -0400246const std::vector<gl::Varying> &OutputHLSL::getVaryings() const
Jamie Madill47fdd132013-08-30 13:21:04 -0400247{
248 return mActiveVaryings;
249}
250
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000251int OutputHLSL::vectorSize(const TType &type) const
252{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000253 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000254 int arraySize = type.isArray() ? type.getArraySize() : 1;
255
256 return elementSize * arraySize;
257}
258
Jamie Madill98493dd2013-07-08 14:39:03 -0400259TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, const TField &field)
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000260{
Jamie Madill98493dd2013-07-08 14:39:03 -0400261 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000262 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400263 return interfaceBlock.name() + "." + field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000264 }
265 else
266 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400267 return field.name();
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000268 }
269}
270
Jamie Madill98493dd2013-07-08 14:39:03 -0400271TString OutputHLSL::interfaceBlockStructNameString(const TInterfaceBlock &interfaceBlock)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000272{
Jamie Madill033dae62014-06-18 12:56:28 -0400273 return DecoratePrivate(interfaceBlock.name()) + "_type";
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000274}
275
Jamie Madill98493dd2013-07-08 14:39:03 -0400276TString OutputHLSL::interfaceBlockInstanceString(const TInterfaceBlock& interfaceBlock, unsigned int arrayIndex)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000277{
Jamie Madill98493dd2013-07-08 14:39:03 -0400278 if (!interfaceBlock.hasInstanceName())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000279 {
280 return "";
281 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400282 else if (interfaceBlock.isArray())
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000283 {
Jamie Madill033dae62014-06-18 12:56:28 -0400284 return DecoratePrivate(interfaceBlock.instanceName()) + "_" + str(arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000285 }
286 else
287 {
Jamie Madill033dae62014-06-18 12:56:28 -0400288 return Decorate(interfaceBlock.instanceName());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000289 }
290}
291
Jamie Madill98493dd2013-07-08 14:39:03 -0400292TString OutputHLSL::interfaceBlockFieldTypeString(const TField &field, TLayoutBlockStorage blockStorage)
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000293{
Jamie Madill98493dd2013-07-08 14:39:03 -0400294 const TType &fieldType = *field.type();
295 const TLayoutMatrixPacking matrixPacking = fieldType.getLayoutQualifier().matrixPacking;
Jamie Madill529077d2013-06-20 11:55:54 -0400296 ASSERT(matrixPacking != EmpUnspecified);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000297
Jamie Madill98493dd2013-07-08 14:39:03 -0400298 if (fieldType.isMatrix())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000299 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400300 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill529077d2013-06-20 11:55:54 -0400301 const TString &matrixPackString = (matrixPacking == EmpRowMajor ? "column_major" : "row_major");
Jamie Madill033dae62014-06-18 12:56:28 -0400302 return matrixPackString + " " + TypeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000303 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400304 else if (fieldType.getStruct())
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000305 {
Jamie Madill9cf6c072013-06-20 11:55:53 -0400306 // Use HLSL row-major packing for GLSL column-major matrices
Jamie Madill033dae62014-06-18 12:56:28 -0400307 return QualifiedStructNameString(*fieldType.getStruct(), matrixPacking == EmpColumnMajor,
308 blockStorage == EbsStd140);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000309 }
310 else
311 {
Jamie Madill033dae62014-06-18 12:56:28 -0400312 return TypeString(fieldType);
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000313 }
314}
315
Jamie Madill98493dd2013-07-08 14:39:03 -0400316TString OutputHLSL::interfaceBlockFieldString(const TInterfaceBlock &interfaceBlock, TLayoutBlockStorage blockStorage)
317{
318 TString hlsl;
319
Jamie Madill8daaba12014-06-13 10:04:33 -0400320 Std140PaddingHelper padHelper = mStructureHLSL->getPaddingHelper();
Jamie Madill98493dd2013-07-08 14:39:03 -0400321
322 for (unsigned int typeIndex = 0; typeIndex < interfaceBlock.fields().size(); typeIndex++)
323 {
324 const TField &field = *interfaceBlock.fields()[typeIndex];
325 const TType &fieldType = *field.type();
326
327 if (blockStorage == EbsStd140)
328 {
329 // 2 and 3 component vector types in some cases need pre-padding
Jamie Madill3891fd22014-06-13 10:04:30 -0400330 hlsl += padHelper.prePaddingString(fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -0400331 }
332
333 hlsl += " " + interfaceBlockFieldTypeString(field, blockStorage) +
Jamie Madill033dae62014-06-18 12:56:28 -0400334 " " + Decorate(field.name()) + ArrayString(fieldType) + ";\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400335
336 // must pad out after matrices and arrays, where HLSL usually allows itself room to pack stuff
337 if (blockStorage == EbsStd140)
338 {
339 const bool useHLSLRowMajorPacking = (fieldType.getLayoutQualifier().matrixPacking == EmpColumnMajor);
Jamie Madill3891fd22014-06-13 10:04:30 -0400340 hlsl += padHelper.postPaddingString(fieldType, useHLSLRowMajorPacking);
Jamie Madill98493dd2013-07-08 14:39:03 -0400341 }
342 }
343
344 return hlsl;
345}
346
347TString OutputHLSL::interfaceBlockStructString(const TInterfaceBlock &interfaceBlock)
348{
349 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
350
351 return "struct " + interfaceBlockStructNameString(interfaceBlock) + "\n"
352 "{\n" +
353 interfaceBlockFieldString(interfaceBlock, blockStorage) +
354 "};\n\n";
355}
356
357TString OutputHLSL::interfaceBlockString(const TInterfaceBlock &interfaceBlock, unsigned int registerIndex, unsigned int arrayIndex)
358{
Jamie Madill033dae62014-06-18 12:56:28 -0400359 const TString &arrayIndexString = (arrayIndex != GL_INVALID_INDEX ? Decorate(str(arrayIndex)) : "");
Jamie Madill98493dd2013-07-08 14:39:03 -0400360 const TString &blockName = interfaceBlock.name() + arrayIndexString;
361 TString hlsl;
362
363 hlsl += "cbuffer " + blockName + " : register(b" + str(registerIndex) + ")\n"
364 "{\n";
365
366 if (interfaceBlock.hasInstanceName())
367 {
368 hlsl += " " + interfaceBlockStructNameString(interfaceBlock) + " " + interfaceBlockInstanceString(interfaceBlock, arrayIndex) + ";\n";
369 }
370 else
371 {
372 const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
373 hlsl += interfaceBlockFieldString(interfaceBlock, blockStorage);
374 }
375
376 hlsl += "};\n\n";
377
378 return hlsl;
379}
380
Jamie Madill440dc742013-06-20 11:55:55 -0400381// Use the same layout for packed and shared
Jamie Madill834e8b72014-04-11 13:33:58 -0400382void setBlockLayout(gl::InterfaceBlock *interfaceBlock, gl::BlockLayoutType newLayout)
Jamie Madill440dc742013-06-20 11:55:55 -0400383{
384 interfaceBlock->layout = newLayout;
385 interfaceBlock->blockInfo.clear();
386
387 switch (newLayout)
388 {
Jamie Madill834e8b72014-04-11 13:33:58 -0400389 case gl::BLOCKLAYOUT_SHARED:
390 case gl::BLOCKLAYOUT_PACKED:
Jamie Madill440dc742013-06-20 11:55:55 -0400391 {
Vladimir Vukicevic24d8d672014-05-27 12:07:51 -0400392 gl::HLSLBlockEncoder hlslEncoder(&interfaceBlock->blockInfo, gl::HLSLBlockEncoder::ENCODE_PACKED);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400393 hlslEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400394 interfaceBlock->dataSize = hlslEncoder.getBlockSize();
395 }
396 break;
397
Jamie Madill834e8b72014-04-11 13:33:58 -0400398 case gl::BLOCKLAYOUT_STANDARD:
Jamie Madill440dc742013-06-20 11:55:55 -0400399 {
Jamie Madill834e8b72014-04-11 13:33:58 -0400400 gl::Std140BlockEncoder stdEncoder(&interfaceBlock->blockInfo);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400401 stdEncoder.encodeInterfaceBlockFields(interfaceBlock->fields);
Jamie Madill440dc742013-06-20 11:55:55 -0400402 interfaceBlock->dataSize = stdEncoder.getBlockSize();
403 }
404 break;
405
406 default:
407 UNREACHABLE();
408 break;
409 }
410}
411
Jamie Madill834e8b72014-04-11 13:33:58 -0400412gl::BlockLayoutType convertBlockLayoutType(TLayoutBlockStorage blockStorage)
Jamie Madill574d9dd2013-06-20 11:55:56 -0400413{
414 switch (blockStorage)
415 {
Jamie Madill834e8b72014-04-11 13:33:58 -0400416 case EbsPacked: return gl::BLOCKLAYOUT_PACKED;
417 case EbsShared: return gl::BLOCKLAYOUT_SHARED;
418 case EbsStd140: return gl::BLOCKLAYOUT_STANDARD;
419 default: UNREACHABLE(); return gl::BLOCKLAYOUT_SHARED;
Jamie Madill574d9dd2013-06-20 11:55:56 -0400420 }
421}
422
Jamie Madill98493dd2013-07-08 14:39:03 -0400423TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400424{
425 TString init;
426
427 TString preIndentString;
428 TString fullIndentString;
429
430 for (int spaces = 0; spaces < (indent * 4); spaces++)
431 {
432 preIndentString += ' ';
433 }
434
435 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
436 {
437 fullIndentString += ' ';
438 }
439
440 init += preIndentString + "{\n";
441
Jamie Madill98493dd2013-07-08 14:39:03 -0400442 const TFieldList &fields = structure.fields();
443 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400444 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400445 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400446 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400447 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400448
Jamie Madill98493dd2013-07-08 14:39:03 -0400449 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400450 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400451 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400452 }
453 else
454 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400455 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400456 }
457 }
458
459 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
460
461 return init;
462}
463
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000464void OutputHLSL::header()
465{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000466 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000467
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000468 TString uniforms;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000469 TString interfaceBlocks;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000470 TString varyings;
471 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400472 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000473
Jamie Madillc2141fb2013-08-30 13:21:08 -0400474 for (ReferencedSymbols::const_iterator uniformIt = mReferencedUniforms.begin(); uniformIt != mReferencedUniforms.end(); uniformIt++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000475 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400476 const TIntermSymbol &uniform = *uniformIt->second;
477 const TType &type = uniform.getType();
478 const TString &name = uniform.getSymbol();
479
480 int registerIndex = declareUniformAndAssignRegister(type, name);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000481
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000482 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) // Also declare the texture
483 {
Jamie Madill033dae62014-06-18 12:56:28 -0400484 uniforms += "uniform " + SamplerString(type) + " sampler_" + DecorateUniform(name, type) + ArrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400485 " : register(s" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000486
Jamie Madill033dae62014-06-18 12:56:28 -0400487 uniforms += "uniform " + TextureString(type) + " texture_" + DecorateUniform(name, type) + ArrayString(type) +
Jamie Madillc2141fb2013-08-30 13:21:08 -0400488 " : register(t" + str(registerIndex) + ");\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000489 }
490 else
491 {
Jamie Madillc2141fb2013-08-30 13:21:08 -0400492 const TStructure *structure = type.getStruct();
Jamie Madill033dae62014-06-18 12:56:28 -0400493 const TString &typeName = (structure ? QualifiedStructNameString(*structure, false, false) : TypeString(type));
Jamie Madillc2141fb2013-08-30 13:21:08 -0400494
495 const TString &registerString = TString("register(") + RegisterPrefix(type) + str(registerIndex) + ")";
496
Jamie Madill033dae62014-06-18 12:56:28 -0400497 uniforms += "uniform " + typeName + " " + DecorateUniform(name, type) + ArrayString(type) + " : " + registerString + ";\n";
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000498 }
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000499 }
500
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000501 for (ReferencedSymbols::const_iterator interfaceBlockIt = mReferencedInterfaceBlocks.begin(); interfaceBlockIt != mReferencedInterfaceBlocks.end(); interfaceBlockIt++)
502 {
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000503 const TType &nodeType = interfaceBlockIt->second->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -0400504 const TInterfaceBlock &interfaceBlock = *nodeType.getInterfaceBlock();
505 const TFieldList &fieldList = interfaceBlock.fields();
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000506
Jamie Madill98493dd2013-07-08 14:39:03 -0400507 unsigned int arraySize = static_cast<unsigned int>(interfaceBlock.arraySize());
Jamie Madill834e8b72014-04-11 13:33:58 -0400508 gl::InterfaceBlock activeBlock(interfaceBlock.name().c_str(), arraySize, mInterfaceBlockRegister);
Jamie Madill98493dd2013-07-08 14:39:03 -0400509 for (unsigned int typeIndex = 0; typeIndex < fieldList.size(); typeIndex++)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000510 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400511 const TField &field = *fieldList[typeIndex];
512 const TString &fullUniformName = interfaceBlockFieldString(interfaceBlock, field);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400513 declareInterfaceBlockField(*field.type(), fullUniformName, activeBlock.fields);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000514 }
515
Jamie Madill98493dd2013-07-08 14:39:03 -0400516 mInterfaceBlockRegister += std::max(1u, arraySize);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000517
Jamie Madill834e8b72014-04-11 13:33:58 -0400518 gl::BlockLayoutType blockLayoutType = convertBlockLayoutType(interfaceBlock.blockStorage());
Jamie Madill98493dd2013-07-08 14:39:03 -0400519 setBlockLayout(&activeBlock, blockLayoutType);
Jamie Madill9060a4e2013-08-12 16:22:57 -0700520
521 if (interfaceBlock.matrixPacking() == EmpRowMajor)
522 {
523 activeBlock.isRowMajorLayout = true;
524 }
525
Jamie Madill98493dd2013-07-08 14:39:03 -0400526 mActiveInterfaceBlocks.push_back(activeBlock);
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000527
Jamie Madill98493dd2013-07-08 14:39:03 -0400528 if (interfaceBlock.hasInstanceName())
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000529 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400530 interfaceBlocks += interfaceBlockStructString(interfaceBlock);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000531 }
532
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000533 if (arraySize > 0)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000534 {
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000535 for (unsigned int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
536 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400537 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex + arrayIndex, arrayIndex);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000538 }
539 }
540 else
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000541 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400542 interfaceBlocks += interfaceBlockString(interfaceBlock, activeBlock.registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000543 }
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000544 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000545
Jamie Madill829f59e2013-11-13 19:40:54 -0500546 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400547 {
548 TIntermTyped *structNode = flaggedStructIt->first;
549 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400550 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400551 const TString &originalName = mFlaggedStructOriginalNames[structNode];
552
Jamie Madill033dae62014-06-18 12:56:28 -0400553 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400554 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400555 flaggedStructs += "\n";
556 }
557
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000558 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
559 {
560 const TType &type = varying->second->getType();
561 const TString &name = varying->second->getSymbol();
562
563 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400564 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
565 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madill47fdd132013-08-30 13:21:04 -0400566
Jamie Madill94599662013-08-30 13:21:10 -0400567 declareVaryingToList(type, type.getQualifier(), name, mActiveVaryings);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000568 }
569
570 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
571 {
572 const TType &type = attribute->second->getType();
573 const TString &name = attribute->second->getSymbol();
574
Jamie Madill033dae62014-06-18 12:56:28 -0400575 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
Jamie Madilldefb6742013-06-20 11:55:51 -0400576
Jamie Madill033dae62014-06-18 12:56:28 -0400577 gl::Attribute attributeVar(GLVariableType(type), GLVariablePrecision(type), name.c_str(),
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400578 (unsigned int)type.getArraySize(), type.getLayoutQualifier().location);
579 mActiveAttributes.push_back(attributeVar);
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000580 }
581
Jamie Madill8daaba12014-06-13 10:04:33 -0400582 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400583
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500584 if (mUsesDiscardRewriting)
585 {
586 out << "#define ANGLE_USES_DISCARD_REWRITING" << "\n";
587 }
588
Nicolas Capens655fe362014-04-11 13:12:34 -0400589 if (mUsesNestedBreak)
590 {
591 out << "#define ANGLE_USES_NESTED_BREAK" << "\n";
592 }
593
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400594 if (mContext.shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000595 {
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000596 TExtensionBehavior::const_iterator iter = mContext.extensionBehavior().find("GL_EXT_draw_buffers");
shannon.woods%transgaming.com@gtempaccount.com99ab6eb2013-04-13 03:42:00 +0000597 const bool usingMRTExtension = (iter != mContext.extensionBehavior().end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000598
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000599 out << "// Varyings\n";
600 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400601 out << "\n";
602
603 if (mContext.getShaderVersion() >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000604 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500605 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000606 {
Jamie Madill46131a32013-06-20 11:55:50 -0400607 const TString &variableName = outputVariableIt->first;
608 const TType &variableType = outputVariableIt->second->getType();
609 const TLayoutQualifier &layoutQualifier = variableType.getLayoutQualifier();
610
Jamie Madill033dae62014-06-18 12:56:28 -0400611 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400612 " = " + initializer(variableType) + ";\n";
613
Jamie Madill033dae62014-06-18 12:56:28 -0400614 gl::Attribute outputVar(GLVariableType(variableType), GLVariablePrecision(variableType), variableName.c_str(),
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400615 (unsigned int)variableType.getArraySize(), layoutQualifier.location);
Jamie Madill46131a32013-06-20 11:55:50 -0400616 mActiveOutputVariables.push_back(outputVar);
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000617 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000618 }
Jamie Madill46131a32013-06-20 11:55:50 -0400619 else
620 {
621 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
622
623 out << "static float4 gl_Color[" << numColorValues << "] =\n"
624 "{\n";
625 for (unsigned int i = 0; i < numColorValues; i++)
626 {
627 out << " float4(0, 0, 0, 0)";
628 if (i + 1 != numColorValues)
629 {
630 out << ",";
631 }
632 out << "\n";
633 }
634
635 out << "};\n";
636 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000637
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400638 if (mUsesFragDepth)
639 {
640 out << "static float gl_Depth = 0.0;\n";
641 }
642
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000643 if (mUsesFragCoord)
644 {
645 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
646 }
647
648 if (mUsesPointCoord)
649 {
650 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
651 }
652
653 if (mUsesFrontFacing)
654 {
655 out << "static bool gl_FrontFacing = false;\n";
656 }
657
658 out << "\n";
659
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000660 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000661 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000662 out << "struct gl_DepthRangeParameters\n"
663 "{\n"
664 " float near;\n"
665 " float far;\n"
666 " float diff;\n"
667 "};\n"
668 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000669 }
670
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000671 if (mOutputType == SH_HLSL11_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000672 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000673 out << "cbuffer DriverConstants : register(b1)\n"
674 "{\n";
675
676 if (mUsesDepthRange)
677 {
678 out << " float3 dx_DepthRange : packoffset(c0);\n";
679 }
680
681 if (mUsesFragCoord)
682 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000683 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000684 }
685
686 if (mUsesFragCoord || mUsesFrontFacing)
687 {
688 out << " float3 dx_DepthFront : packoffset(c2);\n";
689 }
690
691 out << "};\n";
692 }
693 else
694 {
695 if (mUsesDepthRange)
696 {
697 out << "uniform float3 dx_DepthRange : register(c0);";
698 }
699
700 if (mUsesFragCoord)
701 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000702 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000703 }
704
705 if (mUsesFragCoord || mUsesFrontFacing)
706 {
707 out << "uniform float3 dx_DepthFront : register(c2);\n";
708 }
709 }
710
711 out << "\n";
712
713 if (mUsesDepthRange)
714 {
715 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
716 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000717 }
718
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000719 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000720 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000721
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000722 if (!interfaceBlocks.empty())
723 {
724 out << interfaceBlocks;
725 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400726
727 if (!flaggedStructs.empty())
728 {
729 out << "// Std140 Structures accessed by value\n";
730 out << "\n";
731 out << flaggedStructs;
732 out << "\n";
733 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000734 }
735
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000736 if (usingMRTExtension && mNumRenderTargets > 1)
737 {
738 out << "#define GL_USES_MRT\n";
739 }
740
741 if (mUsesFragColor)
742 {
743 out << "#define GL_USES_FRAG_COLOR\n";
744 }
745
746 if (mUsesFragData)
747 {
748 out << "#define GL_USES_FRAG_DATA\n";
749 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000750 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000751 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000752 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000753 out << "// Attributes\n";
754 out << attributes;
755 out << "\n"
756 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
757
758 if (mUsesPointSize)
759 {
760 out << "static float gl_PointSize = float(1);\n";
761 }
762
763 out << "\n"
764 "// Varyings\n";
765 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000766 out << "\n";
767
768 if (mUsesDepthRange)
769 {
770 out << "struct gl_DepthRangeParameters\n"
771 "{\n"
772 " float near;\n"
773 " float far;\n"
774 " float diff;\n"
775 "};\n"
776 "\n";
777 }
778
779 if (mOutputType == SH_HLSL11_OUTPUT)
780 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000781 if (mUsesDepthRange)
782 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000783 out << "cbuffer DriverConstants : register(b1)\n"
784 "{\n"
785 " float3 dx_DepthRange : packoffset(c0);\n"
786 "};\n"
787 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000788 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000789 }
790 else
791 {
792 if (mUsesDepthRange)
793 {
794 out << "uniform float3 dx_DepthRange : register(c0);\n";
795 }
796
shannon.woods@transgaming.com42832a62013-02-28 23:18:38 +0000797 out << "uniform float4 dx_ViewAdjust : register(c1);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000798 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000799 }
800
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000801 if (mUsesDepthRange)
802 {
803 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
804 "\n";
805 }
806
807 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000808 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000809
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000810 if (!interfaceBlocks.empty())
811 {
812 out << interfaceBlocks;
813 out << "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400814
815 if (!flaggedStructs.empty())
816 {
817 out << "// Std140 Structures accessed by value\n";
818 out << "\n";
819 out << flaggedStructs;
820 out << "\n";
821 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000822 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400823 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000824
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400825 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
826 {
827 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400828 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000829 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400830 switch(textureFunction->sampler)
831 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400832 case EbtSampler2D: out << "int2 "; break;
833 case EbtSampler3D: out << "int3 "; break;
834 case EbtSamplerCube: out << "int2 "; break;
835 case EbtSampler2DArray: out << "int3 "; break;
836 case EbtISampler2D: out << "int2 "; break;
837 case EbtISampler3D: out << "int3 "; break;
838 case EbtISamplerCube: out << "int2 "; break;
839 case EbtISampler2DArray: out << "int3 "; break;
840 case EbtUSampler2D: out << "int2 "; break;
841 case EbtUSampler3D: out << "int3 "; break;
842 case EbtUSamplerCube: out << "int2 "; break;
843 case EbtUSampler2DArray: out << "int3 "; break;
844 case EbtSampler2DShadow: out << "int2 "; break;
845 case EbtSamplerCubeShadow: out << "int2 "; break;
846 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400847 default: UNREACHABLE();
848 }
849 }
850 else // Sampling function
851 {
852 switch(textureFunction->sampler)
853 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400854 case EbtSampler2D: out << "float4 "; break;
855 case EbtSampler3D: out << "float4 "; break;
856 case EbtSamplerCube: out << "float4 "; break;
857 case EbtSampler2DArray: out << "float4 "; break;
858 case EbtISampler2D: out << "int4 "; break;
859 case EbtISampler3D: out << "int4 "; break;
860 case EbtISamplerCube: out << "int4 "; break;
861 case EbtISampler2DArray: out << "int4 "; break;
862 case EbtUSampler2D: out << "uint4 "; break;
863 case EbtUSampler3D: out << "uint4 "; break;
864 case EbtUSamplerCube: out << "uint4 "; break;
865 case EbtUSampler2DArray: out << "uint4 "; break;
866 case EbtSampler2DShadow: out << "float "; break;
867 case EbtSamplerCubeShadow: out << "float "; break;
868 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400869 default: UNREACHABLE();
870 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000871 }
872
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400873 // Function name
874 out << textureFunction->name();
875
876 // Argument list
877 int hlslCoords = 4;
878
879 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000880 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400881 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000882 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400883 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
884 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
885 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000886 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400887
Nicolas Capens75fb4752013-07-10 15:14:47 -0400888 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000889 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400890 case TextureFunction::IMPLICIT: break;
891 case TextureFunction::BIAS: hlslCoords = 4; break;
892 case TextureFunction::LOD: hlslCoords = 4; break;
893 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400894 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400895 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000896 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400897 }
898 else if (mOutputType == SH_HLSL11_OUTPUT)
899 {
900 switch(textureFunction->sampler)
901 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400902 case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break;
903 case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break;
904 case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break;
905 case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break;
906 case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break;
907 case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500908 case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400909 case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break;
910 case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break;
911 case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capens0027fa92014-02-20 14:26:42 -0500912 case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -0400913 case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break;
914 case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break;
915 case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break;
916 case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400917 default: UNREACHABLE();
918 }
919 }
920 else UNREACHABLE();
921
Nicolas Capensfc014542014-02-18 14:47:13 -0500922 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400923 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500924 switch(textureFunction->coords)
925 {
926 case 2: out << ", int2 t"; break;
927 case 3: out << ", int3 t"; break;
928 default: UNREACHABLE();
929 }
930 }
931 else // Floating-point coordinates (except textureSize)
932 {
933 switch(textureFunction->coords)
934 {
935 case 1: out << ", int lod"; break; // textureSize()
936 case 2: out << ", float2 t"; break;
937 case 3: out << ", float3 t"; break;
938 case 4: out << ", float4 t"; break;
939 default: UNREACHABLE();
940 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000941 }
942
Nicolas Capensd11d5492014-02-19 17:06:10 -0500943 if (textureFunction->method == TextureFunction::GRAD)
944 {
945 switch(textureFunction->sampler)
946 {
947 case EbtSampler2D:
948 case EbtISampler2D:
949 case EbtUSampler2D:
950 case EbtSampler2DArray:
951 case EbtISampler2DArray:
952 case EbtUSampler2DArray:
953 case EbtSampler2DShadow:
954 case EbtSampler2DArrayShadow:
955 out << ", float2 ddx, float2 ddy";
956 break;
957 case EbtSampler3D:
958 case EbtISampler3D:
959 case EbtUSampler3D:
960 case EbtSamplerCube:
961 case EbtISamplerCube:
962 case EbtUSamplerCube:
963 case EbtSamplerCubeShadow:
964 out << ", float3 ddx, float3 ddy";
965 break;
966 default: UNREACHABLE();
967 }
968 }
969
Nicolas Capens75fb4752013-07-10 15:14:47 -0400970 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000971 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400972 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400973 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400974 case TextureFunction::LOD: out << ", float lod"; break;
975 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400976 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400977 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500978 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500979 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400980 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000981 }
982
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500983 if (textureFunction->offset)
984 {
985 switch(textureFunction->sampler)
986 {
987 case EbtSampler2D: out << ", int2 offset"; break;
988 case EbtSampler3D: out << ", int3 offset"; break;
989 case EbtSampler2DArray: out << ", int2 offset"; break;
990 case EbtISampler2D: out << ", int2 offset"; break;
991 case EbtISampler3D: out << ", int3 offset"; break;
992 case EbtISampler2DArray: out << ", int2 offset"; break;
993 case EbtUSampler2D: out << ", int2 offset"; break;
994 case EbtUSampler3D: out << ", int3 offset"; break;
995 case EbtUSampler2DArray: out << ", int2 offset"; break;
996 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500997 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500998 default: UNREACHABLE();
999 }
1000 }
1001
Nicolas Capens84cfa122014-04-14 13:48:45 -04001002 if (textureFunction->method == TextureFunction::BIAS ||
1003 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001004 {
1005 out << ", float bias";
1006 }
1007
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001008 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001009 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001010
Nicolas Capens75fb4752013-07-10 15:14:47 -04001011 if (textureFunction->method == TextureFunction::SIZE)
1012 {
1013 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
1014 {
1015 if (IsSamplerArray(textureFunction->sampler))
1016 {
1017 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
1018 " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n";
1019 }
1020 else
1021 {
1022 out << " uint width; uint height; uint numberOfLevels;\n"
1023 " x.GetDimensions(lod, width, height, numberOfLevels);\n";
1024 }
1025 }
1026 else if (IsSampler3D(textureFunction->sampler))
1027 {
1028 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
1029 " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n";
1030 }
1031 else UNREACHABLE();
1032
1033 switch(textureFunction->sampler)
1034 {
Nicolas Capenscb127d32013-07-15 17:26:18 -04001035 case EbtSampler2D: out << " return int2(width, height);"; break;
1036 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
1037 case EbtSamplerCube: out << " return int2(width, height);"; break;
1038 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
1039 case EbtISampler2D: out << " return int2(width, height);"; break;
1040 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
1041 case EbtISamplerCube: out << " return int2(width, height);"; break;
1042 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
1043 case EbtUSampler2D: out << " return int2(width, height);"; break;
1044 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
1045 case EbtUSamplerCube: out << " return int2(width, height);"; break;
1046 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
1047 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
1048 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
1049 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -04001050 default: UNREACHABLE();
1051 }
1052 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001053 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001054 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001055 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1056 {
1057 out << " float width; float height; float layers; float levels;\n";
1058
1059 out << " uint mip = 0;\n";
1060
1061 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
1062
1063 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
1064 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
1065 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
1066 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
1067
1068 // FACE_POSITIVE_X = 000b
1069 // FACE_NEGATIVE_X = 001b
1070 // FACE_POSITIVE_Y = 010b
1071 // FACE_NEGATIVE_Y = 011b
1072 // FACE_POSITIVE_Z = 100b
1073 // FACE_NEGATIVE_Z = 101b
1074 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
1075
1076 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
1077 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
1078 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
1079
1080 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
1081 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
1082 }
1083 else if (IsIntegerSampler(textureFunction->sampler) &&
1084 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001085 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001086 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001087 {
Nicolas Capens93e50de2013-07-09 13:46:28 -04001088 if (IsSamplerArray(textureFunction->sampler))
1089 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001090 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001091
Nicolas Capens9edebd62013-08-06 10:59:10 -04001092 if (textureFunction->method == TextureFunction::LOD0)
1093 {
1094 out << " uint mip = 0;\n";
1095 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001096 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1097 {
1098 out << " uint mip = bias;\n";
1099 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001100 else
1101 {
1102 if (textureFunction->method == TextureFunction::IMPLICIT ||
1103 textureFunction->method == TextureFunction::BIAS)
1104 {
1105 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1106 " float2 tSized = float2(t.x * width, t.y * height);\n"
1107 " float dx = length(ddx(tSized));\n"
1108 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001109 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001110
1111 if (textureFunction->method == TextureFunction::BIAS)
1112 {
1113 out << " lod += bias;\n";
1114 }
1115 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001116 else if (textureFunction->method == TextureFunction::GRAD)
1117 {
1118 out << " x.GetDimensions(0, width, height, layers, levels);\n"
1119 " float lod = log2(max(length(ddx), length(ddy)));\n";
1120 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001121
1122 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1123 }
1124
1125 out << " x.GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001126 }
1127 else
1128 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001129 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001130
Nicolas Capens9edebd62013-08-06 10:59:10 -04001131 if (textureFunction->method == TextureFunction::LOD0)
1132 {
1133 out << " uint mip = 0;\n";
1134 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001135 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1136 {
1137 out << " uint mip = bias;\n";
1138 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001139 else
1140 {
1141 if (textureFunction->method == TextureFunction::IMPLICIT ||
1142 textureFunction->method == TextureFunction::BIAS)
1143 {
1144 out << " x.GetDimensions(0, width, height, levels);\n"
1145 " float2 tSized = float2(t.x * width, t.y * height);\n"
1146 " float dx = length(ddx(tSized));\n"
1147 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001148 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001149
1150 if (textureFunction->method == TextureFunction::BIAS)
1151 {
1152 out << " lod += bias;\n";
1153 }
1154 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05001155 else if (textureFunction->method == TextureFunction::LOD)
1156 {
1157 out << " x.GetDimensions(0, width, height, levels);\n";
1158 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001159 else if (textureFunction->method == TextureFunction::GRAD)
1160 {
1161 out << " x.GetDimensions(0, width, height, levels);\n"
1162 " float lod = log2(max(length(ddx), length(ddy)));\n";
1163 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001164
1165 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1166 }
1167
1168 out << " x.GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001169 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001170 }
1171 else if (IsSampler3D(textureFunction->sampler))
1172 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001173 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001174
Nicolas Capens9edebd62013-08-06 10:59:10 -04001175 if (textureFunction->method == TextureFunction::LOD0)
1176 {
1177 out << " uint mip = 0;\n";
1178 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001179 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1180 {
1181 out << " uint mip = bias;\n";
1182 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001183 else
1184 {
1185 if (textureFunction->method == TextureFunction::IMPLICIT ||
1186 textureFunction->method == TextureFunction::BIAS)
1187 {
1188 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1189 " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n"
1190 " float dx = length(ddx(tSized));\n"
1191 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001192 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001193
1194 if (textureFunction->method == TextureFunction::BIAS)
1195 {
1196 out << " lod += bias;\n";
1197 }
1198 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001199 else if (textureFunction->method == TextureFunction::GRAD)
1200 {
1201 out << " x.GetDimensions(0, width, height, depth, levels);\n"
1202 " float lod = log2(max(length(ddx), length(ddy)));\n";
1203 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001204
1205 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1206 }
1207
1208 out << " x.GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001209 }
1210 else UNREACHABLE();
1211 }
1212
1213 out << " return ";
1214
1215 // HLSL intrinsic
1216 if (mOutputType == SH_HLSL9_OUTPUT)
1217 {
1218 switch(textureFunction->sampler)
1219 {
1220 case EbtSampler2D: out << "tex2D"; break;
1221 case EbtSamplerCube: out << "texCUBE"; break;
1222 default: UNREACHABLE();
1223 }
1224
Nicolas Capens75fb4752013-07-10 15:14:47 -04001225 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001226 {
1227 case TextureFunction::IMPLICIT: out << "(s, "; break;
1228 case TextureFunction::BIAS: out << "bias(s, "; break;
1229 case TextureFunction::LOD: out << "lod(s, "; break;
1230 case TextureFunction::LOD0: out << "lod(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001231 case TextureFunction::LOD0BIAS: out << "lod(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001232 default: UNREACHABLE();
1233 }
1234 }
1235 else if (mOutputType == SH_HLSL11_OUTPUT)
1236 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001237 if (textureFunction->method == TextureFunction::GRAD)
1238 {
1239 if (IsIntegerSampler(textureFunction->sampler))
1240 {
1241 out << "x.Load(";
1242 }
1243 else if (IsShadowSampler(textureFunction->sampler))
1244 {
1245 out << "x.SampleCmpLevelZero(s, ";
1246 }
1247 else
1248 {
1249 out << "x.SampleGrad(s, ";
1250 }
1251 }
1252 else if (IsIntegerSampler(textureFunction->sampler) ||
1253 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001254 {
1255 out << "x.Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001256 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001257 else if (IsShadowSampler(textureFunction->sampler))
1258 {
1259 out << "x.SampleCmp(s, ";
1260 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001261 else
1262 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001263 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001264 {
1265 case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break;
1266 case TextureFunction::BIAS: out << "x.SampleBias(s, "; break;
1267 case TextureFunction::LOD: out << "x.SampleLevel(s, "; break;
1268 case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001269 case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001270 default: UNREACHABLE();
1271 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001272 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001273 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001274 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001275
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001276 // Integer sampling requires integer addresses
1277 TString addressx = "";
1278 TString addressy = "";
1279 TString addressz = "";
1280 TString close = "";
1281
Nicolas Capensfc014542014-02-18 14:47:13 -05001282 if (IsIntegerSampler(textureFunction->sampler) ||
1283 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001284 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001285 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001286 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001287 case 2: out << "int3("; break;
1288 case 3: out << "int4("; break;
1289 default: UNREACHABLE();
1290 }
1291
Nicolas Capensfc014542014-02-18 14:47:13 -05001292 // Convert from normalized floating-point to integer
1293 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001294 {
Nicolas Capensfc014542014-02-18 14:47:13 -05001295 addressx = "int(floor(width * frac((";
1296 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001297
Nicolas Capensfc014542014-02-18 14:47:13 -05001298 if (IsSamplerArray(textureFunction->sampler))
1299 {
1300 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1301 }
Nicolas Capens0027fa92014-02-20 14:26:42 -05001302 else if (IsSamplerCube(textureFunction->sampler))
1303 {
1304 addressz = "((((";
1305 }
Nicolas Capensfc014542014-02-18 14:47:13 -05001306 else
1307 {
1308 addressz = "int(floor(depth * frac((";
1309 }
1310
1311 close = "))))";
1312 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001313 }
1314 else
1315 {
1316 switch(hlslCoords)
1317 {
1318 case 2: out << "float2("; break;
1319 case 3: out << "float3("; break;
1320 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001321 default: UNREACHABLE();
1322 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001323 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001324
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001325 TString proj = ""; // Only used for projected textures
1326
1327 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001328 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001329 switch(textureFunction->coords)
1330 {
1331 case 3: proj = " / t.z"; break;
1332 case 4: proj = " / t.w"; break;
1333 default: UNREACHABLE();
1334 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001335 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001336
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001337 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001338
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001339 if (mOutputType == SH_HLSL9_OUTPUT)
1340 {
1341 if (hlslCoords >= 3)
1342 {
1343 if (textureFunction->coords < 3)
1344 {
1345 out << ", 0";
1346 }
1347 else
1348 {
1349 out << ", t.z" + proj;
1350 }
1351 }
1352
1353 if (hlslCoords == 4)
1354 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001355 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001356 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001357 case TextureFunction::BIAS: out << ", bias"; break;
1358 case TextureFunction::LOD: out << ", lod"; break;
1359 case TextureFunction::LOD0: out << ", 0"; break;
1360 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001361 default: UNREACHABLE();
1362 }
1363 }
1364
1365 out << "));\n";
1366 }
1367 else if (mOutputType == SH_HLSL11_OUTPUT)
1368 {
1369 if (hlslCoords >= 3)
1370 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001371 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1372 {
1373 out << ", face";
1374 }
1375 else
1376 {
1377 out << ", " + addressz + ("t.z" + proj) + close;
1378 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001379 }
1380
Nicolas Capensd11d5492014-02-19 17:06:10 -05001381 if (textureFunction->method == TextureFunction::GRAD)
1382 {
1383 if (IsIntegerSampler(textureFunction->sampler))
1384 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001385 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001386 }
1387 else if (IsShadowSampler(textureFunction->sampler))
1388 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001389 // Compare value
Nicolas Capensd11d5492014-02-19 17:06:10 -05001390 switch(textureFunction->coords)
1391 {
1392 case 3: out << "), t.z"; break;
1393 case 4: out << "), t.w"; break;
1394 default: UNREACHABLE();
1395 }
1396 }
1397 else
1398 {
1399 out << "), ddx, ddy";
1400 }
1401 }
1402 else if (IsIntegerSampler(textureFunction->sampler) ||
1403 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001404 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001405 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001406 }
1407 else if (IsShadowSampler(textureFunction->sampler))
1408 {
1409 // Compare value
1410 switch(textureFunction->coords)
1411 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001412 case 3: out << "), t.z"; break;
1413 case 4: out << "), t.w"; break;
Nicolas Capenscb127d32013-07-15 17:26:18 -04001414 default: UNREACHABLE();
1415 }
1416 }
1417 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001418 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001419 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001420 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001421 case TextureFunction::IMPLICIT: out << ")"; break;
1422 case TextureFunction::BIAS: out << "), bias"; break;
1423 case TextureFunction::LOD: out << "), lod"; break;
1424 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001425 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001426 default: UNREACHABLE();
1427 }
1428 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001429
1430 if (textureFunction->offset)
1431 {
1432 out << ", offset";
1433 }
1434
1435 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001436 }
1437 else UNREACHABLE();
1438 }
1439
1440 out << "\n"
1441 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001442 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001443 }
1444
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001445 if (mUsesFragCoord)
1446 {
1447 out << "#define GL_USES_FRAG_COORD\n";
1448 }
1449
1450 if (mUsesPointCoord)
1451 {
1452 out << "#define GL_USES_POINT_COORD\n";
1453 }
1454
1455 if (mUsesFrontFacing)
1456 {
1457 out << "#define GL_USES_FRONT_FACING\n";
1458 }
1459
1460 if (mUsesPointSize)
1461 {
1462 out << "#define GL_USES_POINT_SIZE\n";
1463 }
1464
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001465 if (mUsesFragDepth)
1466 {
1467 out << "#define GL_USES_FRAG_DEPTH\n";
1468 }
1469
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001470 if (mUsesDepthRange)
1471 {
1472 out << "#define GL_USES_DEPTH_RANGE\n";
1473 }
1474
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001475 if (mUsesXor)
1476 {
1477 out << "bool xor(bool p, bool q)\n"
1478 "{\n"
1479 " return (p || q) && !(p && q);\n"
1480 "}\n"
1481 "\n";
1482 }
1483
1484 if (mUsesMod1)
1485 {
1486 out << "float mod(float x, float y)\n"
1487 "{\n"
1488 " return x - y * floor(x / y);\n"
1489 "}\n"
1490 "\n";
1491 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001492
1493 if (mUsesMod2v)
1494 {
1495 out << "float2 mod(float2 x, float2 y)\n"
1496 "{\n"
1497 " return x - y * floor(x / y);\n"
1498 "}\n"
1499 "\n";
1500 }
1501
1502 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001503 {
1504 out << "float2 mod(float2 x, float y)\n"
1505 "{\n"
1506 " return x - y * floor(x / y);\n"
1507 "}\n"
1508 "\n";
1509 }
1510
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001511 if (mUsesMod3v)
1512 {
1513 out << "float3 mod(float3 x, float3 y)\n"
1514 "{\n"
1515 " return x - y * floor(x / y);\n"
1516 "}\n"
1517 "\n";
1518 }
1519
1520 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001521 {
1522 out << "float3 mod(float3 x, float y)\n"
1523 "{\n"
1524 " return x - y * floor(x / y);\n"
1525 "}\n"
1526 "\n";
1527 }
1528
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001529 if (mUsesMod4v)
1530 {
1531 out << "float4 mod(float4 x, float4 y)\n"
1532 "{\n"
1533 " return x - y * floor(x / y);\n"
1534 "}\n"
1535 "\n";
1536 }
1537
1538 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001539 {
1540 out << "float4 mod(float4 x, float y)\n"
1541 "{\n"
1542 " return x - y * floor(x / y);\n"
1543 "}\n"
1544 "\n";
1545 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001546
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001547 if (mUsesFaceforward1)
1548 {
1549 out << "float faceforward(float N, float I, float Nref)\n"
1550 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001551 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001552 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001553 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001554 " }\n"
1555 " else\n"
1556 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001557 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001558 " }\n"
1559 "}\n"
1560 "\n";
1561 }
1562
1563 if (mUsesFaceforward2)
1564 {
1565 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
1566 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001567 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001568 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001569 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001570 " }\n"
1571 " else\n"
1572 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001573 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001574 " }\n"
1575 "}\n"
1576 "\n";
1577 }
1578
1579 if (mUsesFaceforward3)
1580 {
1581 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
1582 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001583 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001584 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001585 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001586 " }\n"
1587 " else\n"
1588 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001589 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001590 " }\n"
1591 "}\n"
1592 "\n";
1593 }
1594
1595 if (mUsesFaceforward4)
1596 {
1597 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
1598 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001599 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001600 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001601 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001602 " }\n"
1603 " else\n"
1604 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +00001605 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001606 " }\n"
1607 "}\n"
1608 "\n";
1609 }
1610
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001611 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001612 {
1613 out << "float atanyx(float y, float x)\n"
1614 "{\n"
1615 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
1616 " return atan2(y, x);\n"
1617 "}\n";
1618 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001619
1620 if (mUsesAtan2_2)
1621 {
1622 out << "float2 atanyx(float2 y, float2 x)\n"
1623 "{\n"
1624 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1625 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1626 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
1627 "}\n";
1628 }
1629
1630 if (mUsesAtan2_3)
1631 {
1632 out << "float3 atanyx(float3 y, float3 x)\n"
1633 "{\n"
1634 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1635 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1636 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1637 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
1638 "}\n";
1639 }
1640
1641 if (mUsesAtan2_4)
1642 {
1643 out << "float4 atanyx(float4 y, float4 x)\n"
1644 "{\n"
1645 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
1646 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
1647 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
1648 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
1649 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
1650 "}\n";
1651 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001652}
1653
1654void OutputHLSL::visitSymbol(TIntermSymbol *node)
1655{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001656 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001657
Jamie Madill570e04d2013-06-21 09:15:33 -04001658 // Handle accessing std140 structs by value
1659 if (mFlaggedStructMappedNames.count(node) > 0)
1660 {
1661 out << mFlaggedStructMappedNames[node];
1662 return;
1663 }
1664
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001665 TString name = node->getSymbol();
1666
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001667 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001668 {
1669 mUsesDepthRange = true;
1670 out << name;
1671 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001672 else
1673 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001674 TQualifier qualifier = node->getQualifier();
1675
1676 if (qualifier == EvqUniform)
1677 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001678 const TType& nodeType = node->getType();
1679 const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock();
1680
1681 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001682 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001683 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001684 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001685 else
1686 {
1687 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001688 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001689
Jamie Madill033dae62014-06-18 12:56:28 -04001690 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001691 }
Jamie Madill19571812013-08-12 15:26:34 -07001692 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001693 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001694 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001695 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001696 }
Jamie Madill033dae62014-06-18 12:56:28 -04001697 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001698 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001699 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001700 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001701 }
Jamie Madill19571812013-08-12 15:26:34 -07001702 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001703 {
1704 mReferencedOutputVariables[name] = node;
1705 out << "out_" << name;
1706 }
1707 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001708 {
1709 out << "gl_Color[0]";
1710 mUsesFragColor = true;
1711 }
1712 else if (qualifier == EvqFragData)
1713 {
1714 out << "gl_Color";
1715 mUsesFragData = true;
1716 }
1717 else if (qualifier == EvqFragCoord)
1718 {
1719 mUsesFragCoord = true;
1720 out << name;
1721 }
1722 else if (qualifier == EvqPointCoord)
1723 {
1724 mUsesPointCoord = true;
1725 out << name;
1726 }
1727 else if (qualifier == EvqFrontFacing)
1728 {
1729 mUsesFrontFacing = true;
1730 out << name;
1731 }
1732 else if (qualifier == EvqPointSize)
1733 {
1734 mUsesPointSize = true;
1735 out << name;
1736 }
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001737 else if (name == "gl_FragDepthEXT")
1738 {
1739 mUsesFragDepth = true;
1740 out << "gl_Depth";
1741 }
Jamie Madille53c98b2014-02-03 11:57:13 -05001742 else if (qualifier == EvqInternal)
1743 {
1744 out << name;
1745 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001746 else
1747 {
Jamie Madill033dae62014-06-18 12:56:28 -04001748 out << Decorate(name);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001749 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001750 }
1751}
1752
1753bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1754{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001755 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001756
Jamie Madill570e04d2013-06-21 09:15:33 -04001757 // Handle accessing std140 structs by value
1758 if (mFlaggedStructMappedNames.count(node) > 0)
1759 {
1760 out << mFlaggedStructMappedNames[node];
1761 return false;
1762 }
1763
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001764 switch (node->getOp())
1765 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001766 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001767 case EOpInitialize:
1768 if (visit == PreVisit)
1769 {
1770 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1771 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1772 // new variable is created before the assignment is evaluated), so we need to convert
1773 // this to "float t = x, x = t;".
1774
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001775 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1776 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001777
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001778 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
1779 expression->traverse(&searchSymbol);
1780 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001781
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001782 if (sameSymbol)
1783 {
1784 // Type already printed
1785 out << "t" + str(mUniqueIndex) + " = ";
1786 expression->traverse(this);
1787 out << ", ";
1788 symbolNode->traverse(this);
1789 out << " = t" + str(mUniqueIndex);
1790
1791 mUniqueIndex++;
1792 return false;
1793 }
1794 }
1795 else if (visit == InVisit)
1796 {
1797 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001798 }
1799 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001800 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
1801 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
1802 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1803 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1804 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
1805 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001806 if (visit == PreVisit)
1807 {
1808 out << "(";
1809 }
1810 else if (visit == InVisit)
1811 {
1812 out << " = mul(";
1813 node->getLeft()->traverse(this);
1814 out << ", transpose(";
1815 }
1816 else
1817 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001818 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001819 }
1820 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001821 case EOpMatrixTimesMatrixAssign:
1822 if (visit == PreVisit)
1823 {
1824 out << "(";
1825 }
1826 else if (visit == InVisit)
1827 {
1828 out << " = mul(";
1829 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001830 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001831 }
1832 else
1833 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001834 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001835 }
1836 break;
1837 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001838 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001839 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001840 const TType& leftType = node->getLeft()->getType();
1841 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001842 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001843 if (visit == PreVisit)
1844 {
1845 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1846 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
1847
1848 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
1849 out << interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
1850
1851 return false;
1852 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001853 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001854 else
1855 {
1856 outputTriplet(visit, "", "[", "]");
1857 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001858 }
1859 break;
1860 case EOpIndexIndirect:
1861 // We do not currently support indirect references to interface blocks
1862 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1863 outputTriplet(visit, "", "[", "]");
1864 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001865 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001866 if (visit == InVisit)
1867 {
1868 const TStructure* structure = node->getLeft()->getType().getStruct();
1869 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1870 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001871 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001872
1873 return false;
1874 }
1875 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001876 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001877 if (visit == InVisit)
1878 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001879 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1880 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1881 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001882 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001883
1884 return false;
1885 }
1886 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001887 case EOpVectorSwizzle:
1888 if (visit == InVisit)
1889 {
1890 out << ".";
1891
1892 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1893
1894 if (swizzle)
1895 {
1896 TIntermSequence &sequence = swizzle->getSequence();
1897
1898 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
1899 {
1900 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1901
1902 if (element)
1903 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001904 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001905
1906 switch (i)
1907 {
1908 case 0: out << "x"; break;
1909 case 1: out << "y"; break;
1910 case 2: out << "z"; break;
1911 case 3: out << "w"; break;
1912 default: UNREACHABLE();
1913 }
1914 }
1915 else UNREACHABLE();
1916 }
1917 }
1918 else UNREACHABLE();
1919
1920 return false; // Fully processed
1921 }
1922 break;
1923 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1924 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1925 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1926 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001927 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001928 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001929 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001930 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001931 if (node->getOp() == EOpEqual)
1932 {
1933 outputTriplet(visit, "(", " == ", ")");
1934 }
1935 else
1936 {
1937 outputTriplet(visit, "(", " != ", ")");
1938 }
1939 }
1940 else if (node->getLeft()->getBasicType() == EbtStruct)
1941 {
1942 if (node->getOp() == EOpEqual)
1943 {
1944 out << "(";
1945 }
1946 else
1947 {
1948 out << "!(";
1949 }
1950
Jamie Madill98493dd2013-07-08 14:39:03 -04001951 const TStructure &structure = *node->getLeft()->getType().getStruct();
1952 const TFieldList &fields = structure.fields();
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001953
Jamie Madill98493dd2013-07-08 14:39:03 -04001954 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001955 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001956 const TField *field = fields[i];
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001957
1958 node->getLeft()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001959 out << "." + DecorateField(field->name(), structure) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001960 node->getRight()->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04001961 out << "." + DecorateField(field->name(), structure);
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001962
Jamie Madill98493dd2013-07-08 14:39:03 -04001963 if (i < fields.size() - 1)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001964 {
1965 out << " && ";
1966 }
1967 }
1968
1969 out << ")";
1970
1971 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001972 }
1973 else
1974 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001975 ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001976
1977 if (node->getOp() == EOpEqual)
1978 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001979 outputTriplet(visit, "all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001980 }
1981 else
1982 {
Jamie Madill0b20c942013-07-19 16:36:56 -04001983 outputTriplet(visit, "!all(", " == ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001984 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001985 }
1986 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001987 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1988 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1989 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1990 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1991 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001992 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001993 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1994 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001995 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001996 case EOpLogicalOr:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001997 if (node->getRight()->hasSideEffects())
1998 {
1999 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
2000 return false;
2001 }
2002 else
2003 {
2004 outputTriplet(visit, "(", " || ", ")");
2005 return true;
2006 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002007 case EOpLogicalXor:
2008 mUsesXor = true;
2009 outputTriplet(visit, "xor(", ", ", ")");
2010 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00002011 case EOpLogicalAnd:
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002012 if (node->getRight()->hasSideEffects())
2013 {
2014 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
2015 return false;
2016 }
2017 else
2018 {
2019 outputTriplet(visit, "(", " && ", ")");
2020 return true;
2021 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002022 default: UNREACHABLE();
2023 }
2024
2025 return true;
2026}
2027
2028bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
2029{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002030 switch (node->getOp())
2031 {
Nicolas Capens16004fc2014-06-11 11:29:11 -04002032 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
2033 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
2034 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
2035 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
2036 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
2037 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
2038 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002039 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
2040 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
2041 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
2042 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
2043 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
2044 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
2045 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
2046 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
2047 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
2048 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
2049 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
2050 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
2051 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
2052 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
2053 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
2054 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
2055 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
2056 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
2057 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
2058 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
2059 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002060 case EOpDFdx:
2061 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2062 {
2063 outputTriplet(visit, "(", "", ", 0.0)");
2064 }
2065 else
2066 {
2067 outputTriplet(visit, "ddx(", "", ")");
2068 }
2069 break;
2070 case EOpDFdy:
2071 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2072 {
2073 outputTriplet(visit, "(", "", ", 0.0)");
2074 }
2075 else
2076 {
apatrick@chromium.org9616e582012-06-22 18:27:01 +00002077 outputTriplet(visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002078 }
2079 break;
2080 case EOpFwidth:
2081 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2082 {
2083 outputTriplet(visit, "(", "", ", 0.0)");
2084 }
2085 else
2086 {
2087 outputTriplet(visit, "fwidth(", "", ")");
2088 }
2089 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002090 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
2091 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002092 default: UNREACHABLE();
2093 }
2094
2095 return true;
2096}
2097
2098bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2099{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002100 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002101
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002102 switch (node->getOp())
2103 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002104 case EOpSequence:
2105 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002106 if (mInsideFunction)
2107 {
Jamie Madill075edd82013-07-08 13:30:19 -04002108 outputLineDirective(node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002109 out << "{\n";
2110 }
2111
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002112 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
2113 {
Jamie Madill075edd82013-07-08 13:30:19 -04002114 outputLineDirective((*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002115
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002116 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002117
2118 out << ";\n";
2119 }
2120
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002121 if (mInsideFunction)
2122 {
Jamie Madill075edd82013-07-08 13:30:19 -04002123 outputLineDirective(node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002124 out << "}\n";
2125 }
2126
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002127 return false;
2128 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002129 case EOpDeclaration:
2130 if (visit == PreVisit)
2131 {
2132 TIntermSequence &sequence = node->getSequence();
2133 TIntermTyped *variable = sequence[0]->getAsTyped();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002134
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00002135 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002136 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002137 TStructure *structure = variable->getType().getStruct();
2138
2139 if (structure)
daniel@transgaming.comead23042010-04-29 03:35:36 +00002140 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002141 mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00002142 }
2143
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002144 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002145 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00002146 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00002147 {
2148 out << "static ";
2149 }
2150
Jamie Madill033dae62014-06-18 12:56:28 -04002151 out << TypeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002152
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002153 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002154 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002155 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002156
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002157 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002158 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002159 symbol->traverse(this);
Jamie Madill033dae62014-06-18 12:56:28 -04002160 out << ArrayString(symbol->getType());
Jamie Madill79bb0d92013-12-09 16:20:28 -05002161 out << " = " + initializer(symbol->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002162 }
2163 else
2164 {
2165 (*sit)->traverse(this);
2166 }
2167
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002168 if (*sit != sequence.back())
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002169 {
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002170 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002171 }
2172 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002173 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002174 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2175 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002176 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002177 }
2178 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002179 }
Jamie Madill033dae62014-06-18 12:56:28 -04002180 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002181 {
2182 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
2183 {
2184 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2185
2186 if (symbol)
2187 {
2188 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2189 mReferencedVaryings[symbol->getSymbol()] = symbol;
2190 }
2191 else
2192 {
2193 (*sit)->traverse(this);
2194 }
2195 }
2196 }
2197
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002198 return false;
2199 }
2200 else if (visit == InVisit)
2201 {
2202 out << ", ";
2203 }
2204 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002205 case EOpPrototype:
2206 if (visit == PreVisit)
2207 {
Jamie Madill033dae62014-06-18 12:56:28 -04002208 out << TypeString(node->getType()) << " " << Decorate(node->getName()) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002209
2210 TIntermSequence &arguments = node->getSequence();
2211
2212 for (unsigned int i = 0; i < arguments.size(); i++)
2213 {
2214 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2215
2216 if (symbol)
2217 {
2218 out << argumentString(symbol);
2219
2220 if (i < arguments.size() - 1)
2221 {
2222 out << ", ";
2223 }
2224 }
2225 else UNREACHABLE();
2226 }
2227
2228 out << ");\n";
2229
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002230 // Also prototype the Lod0 variant if needed
2231 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2232 {
2233 mOutputLod0Function = true;
2234 node->traverse(this);
2235 mOutputLod0Function = false;
2236 }
2237
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002238 return false;
2239 }
2240 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00002241 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002242 case EOpFunction:
2243 {
alokp@chromium.org43884872010-03-30 00:08:52 +00002244 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002245
Jamie Madill033dae62014-06-18 12:56:28 -04002246 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002247
2248 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002249 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002250 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002251 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002252 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002253 {
Jamie Madill033dae62014-06-18 12:56:28 -04002254 out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002255 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002256
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002257 TIntermSequence &sequence = node->getSequence();
2258 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
2259
2260 for (unsigned int i = 0; i < arguments.size(); i++)
2261 {
2262 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
2263
2264 if (symbol)
2265 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002266 TStructure *structure = symbol->getType().getStruct();
2267
2268 if (structure)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002269 {
Jamie Madill8daaba12014-06-13 10:04:33 -04002270 mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL);
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002271 }
2272
2273 out << argumentString(symbol);
2274
2275 if (i < arguments.size() - 1)
2276 {
2277 out << ", ";
2278 }
2279 }
2280 else UNREACHABLE();
2281 }
2282
2283 out << ")\n"
2284 "{\n";
2285
2286 if (sequence.size() > 1)
2287 {
2288 mInsideFunction = true;
2289 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002290 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002291 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002292
2293 out << "}\n";
2294
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002295 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
2296 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00002297 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002298 {
2299 mOutputLod0Function = true;
2300 node->traverse(this);
2301 mOutputLod0Function = false;
2302 }
2303 }
2304
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002305 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002306 }
2307 break;
2308 case EOpFunctionCall:
2309 {
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002310 TString name = TFunction::unmangleName(node->getName());
2311 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002312 TIntermSequence &arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002313
2314 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002315 {
Jamie Madill033dae62014-06-18 12:56:28 -04002316 out << Decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002317 }
2318 else
2319 {
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002320 TBasicType samplerType = arguments[0]->getAsTyped()->getType().getBasicType();
2321
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002322 TextureFunction textureFunction;
2323 textureFunction.sampler = samplerType;
2324 textureFunction.coords = arguments[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002325 textureFunction.method = TextureFunction::IMPLICIT;
2326 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002327 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002328
2329 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002330 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002331 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002332 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002333 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002334 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002335 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002336 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002337 }
Nicolas Capens46485082014-04-15 13:12:50 -04002338 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2339 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002340 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002341 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002342 }
Nicolas Capens46485082014-04-15 13:12:50 -04002343 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002344 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002345 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002346 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002347 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002348 else if (name == "textureSize")
2349 {
2350 textureFunction.method = TextureFunction::SIZE;
2351 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002352 else if (name == "textureOffset")
2353 {
2354 textureFunction.method = TextureFunction::IMPLICIT;
2355 textureFunction.offset = true;
2356 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002357 else if (name == "textureProjOffset")
2358 {
2359 textureFunction.method = TextureFunction::IMPLICIT;
2360 textureFunction.offset = true;
2361 textureFunction.proj = true;
2362 }
2363 else if (name == "textureLodOffset")
2364 {
2365 textureFunction.method = TextureFunction::LOD;
2366 textureFunction.offset = true;
2367 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002368 else if (name == "textureProjLodOffset")
2369 {
2370 textureFunction.method = TextureFunction::LOD;
2371 textureFunction.proj = true;
2372 textureFunction.offset = true;
2373 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002374 else if (name == "texelFetch")
2375 {
2376 textureFunction.method = TextureFunction::FETCH;
2377 }
2378 else if (name == "texelFetchOffset")
2379 {
2380 textureFunction.method = TextureFunction::FETCH;
2381 textureFunction.offset = true;
2382 }
Nicolas Capens46485082014-04-15 13:12:50 -04002383 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002384 {
2385 textureFunction.method = TextureFunction::GRAD;
2386 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002387 else if (name == "textureGradOffset")
2388 {
2389 textureFunction.method = TextureFunction::GRAD;
2390 textureFunction.offset = true;
2391 }
Nicolas Capens46485082014-04-15 13:12:50 -04002392 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002393 {
2394 textureFunction.method = TextureFunction::GRAD;
2395 textureFunction.proj = true;
2396 }
2397 else if (name == "textureProjGradOffset")
2398 {
2399 textureFunction.method = TextureFunction::GRAD;
2400 textureFunction.proj = true;
2401 textureFunction.offset = true;
2402 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002403 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002404
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002405 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002406 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002407 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2408
2409 if (textureFunction.offset)
2410 {
2411 mandatoryArgumentCount++;
2412 }
2413
2414 bool bias = (arguments.size() > mandatoryArgumentCount); // Bias argument is optional
2415
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002416 if (lod0 || mContext.shaderType == SH_VERTEX_SHADER)
2417 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002418 if (bias)
2419 {
2420 textureFunction.method = TextureFunction::LOD0BIAS;
2421 }
2422 else
2423 {
2424 textureFunction.method = TextureFunction::LOD0;
2425 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002426 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002427 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002428 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002429 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002430 }
2431 }
2432
2433 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002434
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002435 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002436 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002437
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002438 for (TIntermSequence::iterator arg = arguments.begin(); arg != arguments.end(); arg++)
2439 {
2440 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType()))
2441 {
2442 out << "texture_";
2443 (*arg)->traverse(this);
2444 out << ", sampler_";
2445 }
2446
2447 (*arg)->traverse(this);
2448
2449 if (arg < arguments.end() - 1)
2450 {
2451 out << ", ";
2452 }
2453 }
2454
2455 out << ")";
2456
2457 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002458 }
2459 break;
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002460 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
2461 case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", &node->getSequence()); break;
2462 case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", &node->getSequence()); break;
2463 case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", &node->getSequence()); break;
2464 case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", &node->getSequence()); break;
2465 case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", &node->getSequence()); break;
2466 case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", &node->getSequence()); break;
2467 case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", &node->getSequence()); break;
2468 case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", &node->getSequence()); break;
2469 case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", &node->getSequence()); break;
2470 case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", &node->getSequence()); break;
2471 case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", &node->getSequence()); break;
2472 case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", &node->getSequence()); break;
2473 case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", &node->getSequence()); break;
2474 case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", &node->getSequence()); break;
2475 case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", &node->getSequence()); break;
2476 case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", &node->getSequence()); break;
2477 case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", &node->getSequence()); break;
2478 case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", &node->getSequence()); break;
2479 case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", &node->getSequence()); break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002480 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002481 {
Jamie Madill033dae62014-06-18 12:56:28 -04002482 const TString &structName = StructNameString(*node->getType().getStruct());
Jamie Madill8daaba12014-06-13 10:04:33 -04002483 mStructureHLSL->addConstructor(node->getType(), structName, &node->getSequence());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002484 outputTriplet(visit, structName + "_ctor(", ", ", ")");
2485 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002486 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002487 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
2488 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
2489 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
2490 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
2491 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
2492 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002493 case EOpMod:
2494 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002495 // We need to look at the number of components in both arguments
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002496 const int modValue = node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
2497 + node->getSequence()[1]->getAsTyped()->getNominalSize();
2498 switch (modValue)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002499 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00002500 case 11: mUsesMod1 = true; break;
2501 case 22: mUsesMod2v = true; break;
2502 case 21: mUsesMod2f = true; break;
2503 case 33: mUsesMod3v = true; break;
2504 case 31: mUsesMod3f = true; break;
2505 case 44: mUsesMod4v = true; break;
2506 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002507 default: UNREACHABLE();
2508 }
2509
2510 outputTriplet(visit, "mod(", ", ", ")");
2511 }
2512 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002513 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002514 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002515 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002516 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
2517 {
2518 case 1: mUsesAtan2_1 = true; break;
2519 case 2: mUsesAtan2_2 = true; break;
2520 case 3: mUsesAtan2_3 = true; break;
2521 case 4: mUsesAtan2_4 = true; break;
2522 default: UNREACHABLE();
2523 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00002524 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002525 break;
2526 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
2527 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
2528 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
2529 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
2530 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
2531 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
2532 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
2533 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
2534 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002535 case EOpFaceForward:
2536 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002537 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002538 {
2539 case 1: mUsesFaceforward1 = true; break;
2540 case 2: mUsesFaceforward2 = true; break;
2541 case 3: mUsesFaceforward3 = true; break;
2542 case 4: mUsesFaceforward4 = true; break;
2543 default: UNREACHABLE();
2544 }
2545
2546 outputTriplet(visit, "faceforward(", ", ", ")");
2547 }
2548 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002549 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
2550 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
2551 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002552 default: UNREACHABLE();
2553 }
2554
2555 return true;
2556}
2557
2558bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2559{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002560 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002561
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002562 if (node->usesTernaryOperator())
2563 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002564 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00002565 }
2566 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002567 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002568 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002569
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002570 out << "if (";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002571
2572 node->getCondition()->traverse(this);
2573
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002574 out << ")\n";
2575
Jamie Madill075edd82013-07-08 13:30:19 -04002576 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002577 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002578
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002579 bool discard = false;
2580
daniel@transgaming.combb885322010-04-15 20:45:24 +00002581 if (node->getTrueBlock())
2582 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002583 traverseStatements(node->getTrueBlock());
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002584
2585 // Detect true discard
2586 discard = (discard || FindDiscard::search(node->getTrueBlock()));
daniel@transgaming.combb885322010-04-15 20:45:24 +00002587 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002588
Jamie Madill075edd82013-07-08 13:30:19 -04002589 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002590 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002591
2592 if (node->getFalseBlock())
2593 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002594 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002595
Jamie Madill075edd82013-07-08 13:30:19 -04002596 outputLineDirective(node->getFalseBlock()->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002597 out << "{\n";
2598
Jamie Madill075edd82013-07-08 13:30:19 -04002599 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002600 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002601
Jamie Madill075edd82013-07-08 13:30:19 -04002602 outputLineDirective(node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002603 out << ";\n}\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002604
2605 // Detect false discard
2606 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2607 }
2608
2609 // ANGLE issue 486: Detect problematic conditional discard
2610 if (discard && FindSideEffectRewriting::search(node))
2611 {
2612 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002613 }
2614 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002615
2616 return false;
2617}
2618
2619void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2620{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002621 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002622}
2623
2624bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2625{
Nicolas Capens655fe362014-04-11 13:12:34 -04002626 mNestedLoopDepth++;
2627
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002628 bool wasDiscontinuous = mInsideDiscontinuousLoop;
2629
shannon.woods@transgaming.come91615c2013-01-25 21:56:03 +00002630 if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop)
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002631 {
2632 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
2633 }
2634
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002635 if (mOutputType == SH_HLSL9_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002636 {
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002637 if (handleExcessiveLoop(node))
2638 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002639 mInsideDiscontinuousLoop = wasDiscontinuous;
2640 mNestedLoopDepth--;
2641
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002642 return false;
2643 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002644 }
2645
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002646 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002647
alokp@chromium.org52813552010-11-16 18:36:09 +00002648 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002649 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002650 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002651
Jamie Madill075edd82013-07-08 13:30:19 -04002652 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002653 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002654 }
2655 else
2656 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00002657 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002658
2659 if (node->getInit())
2660 {
2661 node->getInit()->traverse(this);
2662 }
2663
2664 out << "; ";
2665
alokp@chromium.org52813552010-11-16 18:36:09 +00002666 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002667 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002668 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002669 }
2670
2671 out << "; ";
2672
alokp@chromium.org52813552010-11-16 18:36:09 +00002673 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002674 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002675 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002676 }
2677
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002678 out << ")\n";
2679
Jamie Madill075edd82013-07-08 13:30:19 -04002680 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002681 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002682 }
2683
2684 if (node->getBody())
2685 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002686 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002687 }
2688
Jamie Madill075edd82013-07-08 13:30:19 -04002689 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002690 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002691
alokp@chromium.org52813552010-11-16 18:36:09 +00002692 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002693 {
Jamie Madill075edd82013-07-08 13:30:19 -04002694 outputLineDirective(node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002695 out << "while(\n";
2696
alokp@chromium.org52813552010-11-16 18:36:09 +00002697 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002698
daniel@transgaming.com73536982012-03-21 20:45:49 +00002699 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002700 }
2701
daniel@transgaming.com73536982012-03-21 20:45:49 +00002702 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002703
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002704 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002705 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002706
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002707 return false;
2708}
2709
2710bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2711{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002712 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002713
2714 switch (node->getFlowOp())
2715 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002716 case EOpKill:
2717 outputTriplet(visit, "discard;\n", "", "");
2718 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002719 case EOpBreak:
2720 if (visit == PreVisit)
2721 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002722 if (mNestedLoopDepth > 1)
2723 {
2724 mUsesNestedBreak = true;
2725 }
2726
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002727 if (mExcessiveLoopIndex)
2728 {
2729 out << "{Break";
2730 mExcessiveLoopIndex->traverse(this);
2731 out << " = true; break;}\n";
2732 }
2733 else
2734 {
2735 out << "break;\n";
2736 }
2737 }
2738 break;
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00002739 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002740 case EOpReturn:
2741 if (visit == PreVisit)
2742 {
2743 if (node->getExpression())
2744 {
2745 out << "return ";
2746 }
2747 else
2748 {
2749 out << "return;\n";
2750 }
2751 }
2752 else if (visit == PostVisit)
2753 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002754 if (node->getExpression())
2755 {
2756 out << ";\n";
2757 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002758 }
2759 break;
2760 default: UNREACHABLE();
2761 }
2762
2763 return true;
2764}
2765
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002766void OutputHLSL::traverseStatements(TIntermNode *node)
2767{
2768 if (isSingleStatement(node))
2769 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00002770 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00002771 }
2772
2773 node->traverse(this);
2774}
2775
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002776bool OutputHLSL::isSingleStatement(TIntermNode *node)
2777{
2778 TIntermAggregate *aggregate = node->getAsAggregate();
2779
2780 if (aggregate)
2781 {
2782 if (aggregate->getOp() == EOpSequence)
2783 {
2784 return false;
2785 }
2786 else
2787 {
2788 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
2789 {
2790 if (!isSingleStatement(*sit))
2791 {
2792 return false;
2793 }
2794 }
2795
2796 return true;
2797 }
2798 }
2799
2800 return true;
2801}
2802
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002803// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2804// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002805bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
2806{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002807 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002808 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002809
2810 // Parse loops of the form:
2811 // for(int index = initial; index [comparator] limit; index += increment)
2812 TIntermSymbol *index = NULL;
2813 TOperator comparator = EOpNull;
2814 int initial = 0;
2815 int limit = 0;
2816 int increment = 0;
2817
2818 // Parse index name and intial value
2819 if (node->getInit())
2820 {
2821 TIntermAggregate *init = node->getInit()->getAsAggregate();
2822
2823 if (init)
2824 {
2825 TIntermSequence &sequence = init->getSequence();
2826 TIntermTyped *variable = sequence[0]->getAsTyped();
2827
2828 if (variable && variable->getQualifier() == EvqTemporary)
2829 {
2830 TIntermBinary *assign = variable->getAsBinaryNode();
2831
2832 if (assign->getOp() == EOpInitialize)
2833 {
2834 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2835 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2836
2837 if (symbol && constant)
2838 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002839 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002840 {
2841 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002842 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002843 }
2844 }
2845 }
2846 }
2847 }
2848 }
2849
2850 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002851 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002852 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002853 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002854
2855 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2856 {
2857 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2858
2859 if (constant)
2860 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002861 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002862 {
2863 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002864 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002865 }
2866 }
2867 }
2868 }
2869
2870 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002871 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002872 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002873 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2874 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002875
2876 if (binaryTerminal)
2877 {
2878 TOperator op = binaryTerminal->getOp();
2879 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2880
2881 if (constant)
2882 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002883 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002884 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002885 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002886
2887 switch (op)
2888 {
2889 case EOpAddAssign: increment = value; break;
2890 case EOpSubAssign: increment = -value; break;
2891 default: UNIMPLEMENTED();
2892 }
2893 }
2894 }
2895 }
2896 else if (unaryTerminal)
2897 {
2898 TOperator op = unaryTerminal->getOp();
2899
2900 switch (op)
2901 {
2902 case EOpPostIncrement: increment = 1; break;
2903 case EOpPostDecrement: increment = -1; break;
2904 case EOpPreIncrement: increment = 1; break;
2905 case EOpPreDecrement: increment = -1; break;
2906 default: UNIMPLEMENTED();
2907 }
2908 }
2909 }
2910
2911 if (index != NULL && comparator != EOpNull && increment != 0)
2912 {
2913 if (comparator == EOpLessThanEqual)
2914 {
2915 comparator = EOpLessThan;
2916 limit += 1;
2917 }
2918
2919 if (comparator == EOpLessThan)
2920 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002921 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002922
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002923 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002924 {
2925 return false; // Not an excessive loop
2926 }
2927
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002928 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2929 mExcessiveLoopIndex = index;
2930
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002931 out << "{int ";
2932 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002933 out << ";\n"
2934 "bool Break";
2935 index->traverse(this);
2936 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002937
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002938 bool firstLoopFragment = true;
2939
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002940 while (iterations > 0)
2941 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002942 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002943
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002944 if (!firstLoopFragment)
2945 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002946 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002947 index->traverse(this);
2948 out << ") {\n";
2949 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002950
2951 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2952 {
2953 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2954 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002955
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002956 // for(int index = initial; index < clampedLimit; index += increment)
2957
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002958 out << "for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002959 index->traverse(this);
2960 out << " = ";
2961 out << initial;
2962
2963 out << "; ";
2964 index->traverse(this);
2965 out << " < ";
2966 out << clampedLimit;
2967
2968 out << "; ";
2969 index->traverse(this);
2970 out << " += ";
2971 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002972 out << ")\n";
2973
Jamie Madill075edd82013-07-08 13:30:19 -04002974 outputLineDirective(node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002975 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002976
2977 if (node->getBody())
2978 {
2979 node->getBody()->traverse(this);
2980 }
2981
Jamie Madill075edd82013-07-08 13:30:19 -04002982 outputLineDirective(node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002983 out << ";}\n";
2984
2985 if (!firstLoopFragment)
2986 {
2987 out << "}\n";
2988 }
2989
2990 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002991
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002992 initial += MAX_LOOP_ITERATIONS * increment;
2993 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002994 }
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002995
2996 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002997
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002998 mExcessiveLoopIndex = restoreIndex;
2999
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003000 return true;
3001 }
3002 else UNIMPLEMENTED();
3003 }
3004
3005 return false; // Not handled as an excessive loop
3006}
3007
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003008void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003009{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00003010 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003011
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003012 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003013 {
3014 out << preString;
3015 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003016 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003017 {
3018 out << inString;
3019 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003020 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003021 {
3022 out << postString;
3023 }
3024}
3025
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003026void OutputHLSL::outputLineDirective(int line)
3027{
3028 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
3029 {
baustin@google.com8ab69842011-06-02 21:53:45 +00003030 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003031 mBody << "#line " << line;
3032
3033 if (mContext.sourcePath)
3034 {
3035 mBody << " \"" << mContext.sourcePath << "\"";
3036 }
3037
3038 mBody << "\n";
3039 }
3040}
3041
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003042TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3043{
3044 TQualifier qualifier = symbol->getQualifier();
3045 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003046 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003047
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003048 if (name.empty()) // HLSL demands named arguments, also for prototypes
3049 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00003050 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003051 }
3052 else
3053 {
Jamie Madill033dae62014-06-18 12:56:28 -04003054 name = Decorate(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003055 }
3056
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003057 if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType()))
3058 {
Jamie Madill033dae62014-06-18 12:56:28 -04003059 return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " +
3060 QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type);
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003061 }
3062
Jamie Madill033dae62014-06-18 12:56:28 -04003063 return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003064}
3065
3066TString OutputHLSL::initializer(const TType &type)
3067{
3068 TString string;
3069
Jamie Madill94bf7f22013-07-08 13:31:15 -04003070 size_t size = type.getObjectSize();
3071 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003072 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003073 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003074
Jamie Madill94bf7f22013-07-08 13:31:15 -04003075 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003076 {
3077 string += ", ";
3078 }
3079 }
3080
daniel@transgaming.comead23042010-04-29 03:35:36 +00003081 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003082}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003083
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003084void OutputHLSL::outputConstructor(Visit visit, const TType &type, const TString &name, const TIntermSequence *parameters)
3085{
3086 TInfoSinkBase &out = mBody;
3087
3088 if (visit == PreVisit)
3089 {
Jamie Madill8daaba12014-06-13 10:04:33 -04003090 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003091
3092 out << name + "(";
3093 }
3094 else if (visit == InVisit)
3095 {
3096 out << ", ";
3097 }
3098 else if (visit == PostVisit)
3099 {
3100 out << ")";
3101 }
3102}
3103
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003104const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
3105{
3106 TInfoSinkBase &out = mBody;
3107
Jamie Madill98493dd2013-07-08 14:39:03 -04003108 const TStructure* structure = type.getStruct();
3109 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003110 {
Jamie Madill033dae62014-06-18 12:56:28 -04003111 out << StructNameString(*structure) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003112
Jamie Madill98493dd2013-07-08 14:39:03 -04003113 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003114
Jamie Madill98493dd2013-07-08 14:39:03 -04003115 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003116 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003117 const TType *fieldType = fields[i]->type();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003118
3119 constUnion = writeConstantUnion(*fieldType, constUnion);
3120
Jamie Madill98493dd2013-07-08 14:39:03 -04003121 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003122 {
3123 out << ", ";
3124 }
3125 }
3126
3127 out << ")";
3128 }
3129 else
3130 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003131 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003132 bool writeType = size > 1;
3133
3134 if (writeType)
3135 {
Jamie Madill033dae62014-06-18 12:56:28 -04003136 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003137 }
3138
Jamie Madill94bf7f22013-07-08 13:31:15 -04003139 for (size_t i = 0; i < size; i++, constUnion++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003140 {
3141 switch (constUnion->getType())
3142 {
daniel@transgaming.com6c1203f2013-01-11 04:12:43 +00003143 case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003144 case EbtInt: out << constUnion->getIConst(); break;
Nicolas Capensc0f7c612013-06-05 11:46:09 -04003145 case EbtUInt: out << constUnion->getUConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00003146 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003147 default: UNREACHABLE();
3148 }
3149
3150 if (i != size - 1)
3151 {
3152 out << ", ";
3153 }
3154 }
3155
3156 if (writeType)
3157 {
3158 out << ")";
3159 }
3160 }
3161
3162 return constUnion;
3163}
3164
Jamie Madill834e8b72014-04-11 13:33:58 -04003165void OutputHLSL::declareInterfaceBlockField(const TType &type, const TString &name, std::vector<gl::InterfaceBlockField>& output)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003166{
Jamie Madill98493dd2013-07-08 14:39:03 -04003167 const TStructure *structure = type.getStruct();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003168
3169 if (!structure)
3170 {
Jamie Madill010fffa2013-06-20 11:55:53 -04003171 const bool isRowMajorMatrix = (type.isMatrix() && type.getLayoutQualifier().matrixPacking == EmpRowMajor);
Jamie Madill033dae62014-06-18 12:56:28 -04003172 gl::InterfaceBlockField field(GLVariableType(type), GLVariablePrecision(type), name.c_str(),
Jamie Madill834e8b72014-04-11 13:33:58 -04003173 (unsigned int)type.getArraySize(), isRowMajorMatrix);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003174 output.push_back(field);
3175 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003176 else
3177 {
Jamie Madill834e8b72014-04-11 13:33:58 -04003178 gl::InterfaceBlockField structField(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), false);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003179
3180 const TFieldList &fields = structure->fields();
3181
3182 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3183 {
3184 TField *field = fields[fieldIndex];
3185 TType *fieldType = field->type();
3186
3187 // make sure to copy matrix packing information
3188 fieldType->setLayoutQualifier(type.getLayoutQualifier());
3189
3190 declareInterfaceBlockField(*fieldType, field->name(), structField.fields);
3191 }
3192
3193 output.push_back(structField);
3194 }
3195}
3196
Jamie Madill834e8b72014-04-11 13:33:58 -04003197gl::Uniform OutputHLSL::declareUniformToList(const TType &type, const TString &name, int registerIndex, std::vector<gl::Uniform>& output)
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003198{
3199 const TStructure *structure = type.getStruct();
3200
3201 if (!structure)
3202 {
Jamie Madill033dae62014-06-18 12:56:28 -04003203 gl::Uniform uniform(GLVariableType(type), GLVariablePrecision(type), name.c_str(),
Jamie Madill834e8b72014-04-11 13:33:58 -04003204 (unsigned int)type.getArraySize(), (unsigned int)registerIndex, 0);
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003205 output.push_back(uniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003206
3207 return uniform;
Jamie Madill9d2ffb12013-08-30 13:21:04 -04003208 }
3209 else
3210 {
Jamie Madill834e8b72014-04-11 13:33:58 -04003211 gl::Uniform structUniform(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(),
3212 (unsigned int)registerIndex, GL_INVALID_INDEX);
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003213
Jamie Madill98493dd2013-07-08 14:39:03 -04003214 const TFieldList &fields = structure->fields();
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003215
Jamie Madill98493dd2013-07-08 14:39:03 -04003216 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003217 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003218 TField *field = fields[fieldIndex];
3219 TType *fieldType = field->type();
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003220
Jamie Madill56093782013-08-30 13:21:11 -04003221 declareUniformToList(*fieldType, field->name(), GL_INVALID_INDEX, structUniform.fields);
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003222 }
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003223
Jamie Madill56093782013-08-30 13:21:11 -04003224 // assign register offset information -- this will override the information in any sub-structures.
Vladimir Vukicevic24d8d672014-05-27 12:07:51 -04003225 HLSLVariableGetRegisterInfo(registerIndex, &structUniform, mOutputType);
Jamie Madill56093782013-08-30 13:21:11 -04003226
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003227 output.push_back(structUniform);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003228
3229 return structUniform;
daniel@transgaming.comf4d9fef2012-12-20 21:12:13 +00003230 }
3231}
3232
Jamie Madill834e8b72014-04-11 13:33:58 -04003233void OutputHLSL::declareVaryingToList(const TType &type, TQualifier baseTypeQualifier, const TString &name, std::vector<gl::Varying>& fieldsOut)
Jamie Madill47fdd132013-08-30 13:21:04 -04003234{
3235 const TStructure *structure = type.getStruct();
3236
Jamie Madill033dae62014-06-18 12:56:28 -04003237 gl::InterpolationType interpolation = GetInterpolationType(baseTypeQualifier);
Jamie Madill47fdd132013-08-30 13:21:04 -04003238 if (!structure)
3239 {
Jamie Madill033dae62014-06-18 12:56:28 -04003240 gl::Varying varying(GLVariableType(type), GLVariablePrecision(type), name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003241 fieldsOut.push_back(varying);
3242 }
3243 else
3244 {
Jamie Madill834e8b72014-04-11 13:33:58 -04003245 gl::Varying structVarying(GL_STRUCT_ANGLEX, GL_NONE, name.c_str(), (unsigned int)type.getArraySize(), interpolation);
Jamie Madill47fdd132013-08-30 13:21:04 -04003246 const TFieldList &fields = structure->fields();
3247
Jamie Madill28167c62013-08-30 13:21:10 -04003248 structVarying.structName = structure->name().c_str();
3249
Jamie Madill47fdd132013-08-30 13:21:04 -04003250 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
3251 {
3252 const TField &field = *fields[fieldIndex];
Jamie Madill94599662013-08-30 13:21:10 -04003253 declareVaryingToList(*field.type(), baseTypeQualifier, field.name(), structVarying.fields);
Jamie Madill47fdd132013-08-30 13:21:04 -04003254 }
3255
3256 fieldsOut.push_back(structVarying);
3257 }
3258}
3259
Jamie Madillc2141fb2013-08-30 13:21:08 -04003260int OutputHLSL::declareUniformAndAssignRegister(const TType &type, const TString &name)
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003261{
Jamie Madillc2141fb2013-08-30 13:21:08 -04003262 int registerIndex = (IsSampler(type.getBasicType()) ? mSamplerRegister : mUniformRegister);
3263
Jamie Madill834e8b72014-04-11 13:33:58 -04003264 const gl::Uniform &uniform = declareUniformToList(type, name, registerIndex, mActiveUniforms);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003265
3266 if (IsSampler(type.getBasicType()))
3267 {
Vladimir Vukicevic24d8d672014-05-27 12:07:51 -04003268 mSamplerRegister += gl::HLSLVariableRegisterCount(uniform, mOutputType);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003269 }
3270 else
3271 {
Vladimir Vukicevic24d8d672014-05-27 12:07:51 -04003272 mUniformRegister += gl::HLSLVariableRegisterCount(uniform, mOutputType);
Jamie Madillc2141fb2013-08-30 13:21:08 -04003273 }
3274
3275 return registerIndex;
shannonwoods@chromium.org7923dd22013-05-30 00:11:04 +00003276}
3277
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003278}