blob: 7e00cb621c5743dd969b2322b3a1720d6685cb1e [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00002// Copyright (c) 2002-2012 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
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00007#include "compiler/OutputHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
alokp@chromium.org79fb1012012-04-26 21:07:39 +00009#include "common/angleutils.h"
alokp@chromium.org91b72322010-06-02 15:50:56 +000010#include "compiler/debug.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011#include "compiler/InfoSink.h"
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000012#include "compiler/UnfoldShortCircuit.h"
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +000013#include "compiler/SearchSymbol.h"
daniel@transgaming.com89431aa2012-05-31 01:20:29 +000014#include "compiler/DetectDiscontinuity.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000015
apatrick@chromium.orgbad6c2a2010-07-20 20:19:51 +000016#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000017#include <algorithm>
18
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000019namespace sh
20{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000021// Integer to TString conversion
22TString str(int i)
23{
24 char buffer[20];
kbr@chromium.orgddb6e8e2012-04-25 00:48:13 +000025 snprintf(buffer, sizeof(buffer), "%d", i);
daniel@transgaming.com005c7392010-04-15 20:45:27 +000026 return buffer;
27}
28
daniel@transgaming.com950f9932010-04-13 03:26:14 +000029OutputHLSL::OutputHLSL(TParseContext &context) : TIntermTraverser(true, true, true), mContext(context)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000031 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +000032 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000033
daniel@transgaming.com5024cc42010-04-20 18:52:04 +000034 mUsesTexture2D = false;
35 mUsesTexture2D_bias = false;
36 mUsesTexture2DProj = false;
37 mUsesTexture2DProj_bias = false;
daniel@transgaming.com15795192011-05-11 15:36:20 +000038 mUsesTexture2DProjLod = false;
39 mUsesTexture2DLod = false;
daniel@transgaming.com5024cc42010-04-20 18:52:04 +000040 mUsesTextureCube = false;
41 mUsesTextureCube_bias = false;
daniel@transgaming.com15795192011-05-11 15:36:20 +000042 mUsesTextureCubeLod = false;
daniel@transgaming.coma54f5182012-05-31 01:20:16 +000043 mUsesTexture2DLod0 = false;
44 mUsesTexture2DLod0_bias = false;
45 mUsesTexture2DProjLod0 = false;
46 mUsesTexture2DProjLod0_bias = false;
47 mUsesTextureCubeLod0 = false;
48 mUsesTextureCubeLod0_bias = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +000049 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +000050 mUsesFragCoord = false;
51 mUsesPointCoord = false;
52 mUsesFrontFacing = false;
53 mUsesPointSize = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +000054 mUsesXor = false;
55 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +000056 mUsesMod2v = false;
57 mUsesMod2f = false;
58 mUsesMod3v = false;
59 mUsesMod3f = false;
60 mUsesMod4v = false;
61 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +000062 mUsesFaceforward1 = false;
63 mUsesFaceforward2 = false;
64 mUsesFaceforward3 = false;
65 mUsesFaceforward4 = false;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +000066 mUsesEqualMat2 = false;
67 mUsesEqualMat3 = false;
68 mUsesEqualMat4 = false;
69 mUsesEqualVec2 = false;
70 mUsesEqualVec3 = false;
71 mUsesEqualVec4 = false;
72 mUsesEqualIVec2 = false;
73 mUsesEqualIVec3 = false;
74 mUsesEqualIVec4 = false;
75 mUsesEqualBVec2 = false;
76 mUsesEqualBVec3 = false;
77 mUsesEqualBVec4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +000078 mUsesAtan2_1 = false;
79 mUsesAtan2_2 = false;
80 mUsesAtan2_3 = false;
81 mUsesAtan2_4 = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +000082
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +000083 mScopeDepth = 0;
84
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +000085 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +000086
87 mContainsLoopDiscontinuity = false;
88 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +000089 mInsideDiscontinuousLoop = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000090}
91
daniel@transgaming.comb5875982010-04-15 20:44:53 +000092OutputHLSL::~OutputHLSL()
93{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000094 delete mUnfoldShortCircuit;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000095}
96
daniel@transgaming.com950f9932010-04-13 03:26:14 +000097void OutputHLSL::output()
98{
daniel@transgaming.com89431aa2012-05-31 01:20:29 +000099 mContainsLoopDiscontinuity = containsLoopDiscontinuity(mContext.treeRoot);
100
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000101 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 +0000102 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000103
alokp@chromium.org646ea1e2012-06-15 17:36:31 +0000104 mContext.infoSink().obj << mHeader.c_str();
105 mContext.infoSink().obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000106}
107
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000108TInfoSinkBase &OutputHLSL::getBodyStream()
109{
110 return mBody;
111}
112
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000113int OutputHLSL::vectorSize(const TType &type) const
114{
115 int elementSize = type.isMatrix() ? type.getNominalSize() : 1;
116 int arraySize = type.isArray() ? type.getArraySize() : 1;
117
118 return elementSize * arraySize;
119}
120
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000121void OutputHLSL::header()
122{
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000123 ShShaderType shaderType = mContext.shaderType;
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000124 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000125
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000126 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
daniel@transgaming.com51d0dc22010-04-29 03:39:11 +0000127 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000128 out << *structDeclaration;
daniel@transgaming.com51d0dc22010-04-29 03:39:11 +0000129 }
130
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000131 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
daniel@transgaming.com51d0dc22010-04-29 03:39:11 +0000132 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000133 out << *constructor;
daniel@transgaming.com51d0dc22010-04-29 03:39:11 +0000134 }
135
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000136 if (shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000137 {
138 TString uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000139 TString varyings;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000140
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000141 TSymbolTableLevel *symbols = mContext.symbolTable.getGlobalLevel();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000142 int semanticIndex = 0;
143
144 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
145 {
146 const TSymbol *symbol = (*namedSymbol).second;
147 const TString &name = symbol->getName();
148
149 if (symbol->isVariable())
150 {
151 const TVariable *variable = static_cast<const TVariable*>(symbol);
152 const TType &type = variable->getType();
153 TQualifier qualifier = type.getQualifier();
154
155 if (qualifier == EvqUniform)
156 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000157 if (mReferencedUniforms.find(name.c_str()) != mReferencedUniforms.end())
158 {
apatrick@chromium.org65756022012-01-17 21:45:38 +0000159 uniforms += "uniform " + typeString(type) + " " + decorateUniform(name, type) + arrayString(type) + ";\n";
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000160 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000161 }
162 else if (qualifier == EvqVaryingIn || qualifier == EvqInvariantVaryingIn)
163 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000164 if (mReferencedVaryings.find(name.c_str()) != mReferencedVaryings.end())
165 {
166 // Program linking depends on this exact format
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000167 varyings += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000168
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000169 semanticIndex += type.isArray() ? type.getArraySize() : 1;
170 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000171 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +0000172 else if (qualifier == EvqGlobal || qualifier == EvqTemporary)
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000173 {
174 // Globals are declared and intialized as an aggregate node
175 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000176 else if (qualifier == EvqConst)
177 {
178 // Constants are repeated as literals where used
179 }
180 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181 }
182 }
183
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000184 out << "// Varyings\n";
185 out << varyings;
186 out << "\n"
187 "static float4 gl_Color[1] = {float4(0, 0, 0, 0)};\n";
188
189 if (mUsesFragCoord)
190 {
191 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
192 }
193
194 if (mUsesPointCoord)
195 {
196 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
197 }
198
199 if (mUsesFrontFacing)
200 {
201 out << "static bool gl_FrontFacing = false;\n";
202 }
203
204 out << "\n";
205
206 if (mUsesFragCoord)
207 {
daniel@transgaming.comd9a54f92011-12-22 18:32:53 +0000208 out << "uniform float4 dx_Coord;\n"
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000209 "uniform float2 dx_Depth;\n";
210 }
211
212 if (mUsesFrontFacing)
213 {
214 out << "uniform bool dx_PointsOrLines;\n"
215 "uniform bool dx_FrontCCW;\n";
216 }
217
218 out << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000219 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000220 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000221
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000222 // The texture fetch functions "flip" the Y coordinate in one way or another. This is because textures are stored
223 // according to the OpenGL convention, i.e. (0, 0) is "bottom left", rather than the D3D convention where (0, 0)
224 // is "top left". Since the HLSL texture fetch functions expect textures to be stored according to the D3D
225 // convention, the Y coordinate passed to these functions is adjusted to compensate.
226 //
227 // The simplest case is texture2D where the mapping is Y -> 1-Y, which maps [0, 1] -> [1, 0].
228 //
229 // The texture2DProj functions are more complicated because the projection divides by either Z or W. For the vec3
230 // case, the mapping is Y -> Z-Y or Y/Z -> 1-Y/Z, which again maps [0, 1] -> [1, 0].
231 //
232 // For cube textures the mapping is Y -> -Y, which maps [-1, 1] -> [1, -1]. This is not sufficient on its own for the
233 // +Y and -Y faces, which are now on the "wrong sides" of the cube. This is compensated for by exchanging the
234 // +Y and -Y faces everywhere else throughout the code.
235
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000236 if (mUsesTexture2D)
237 {
238 out << "float4 gl_texture2D(sampler2D s, float2 t)\n"
239 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000240 " return tex2D(s, float2(t.x, 1 - t.y));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000241 "}\n"
242 "\n";
243 }
244
245 if (mUsesTexture2D_bias)
246 {
247 out << "float4 gl_texture2D(sampler2D s, float2 t, float bias)\n"
248 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000249 " return tex2Dbias(s, float4(t.x, 1 - t.y, 0, bias));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000250 "}\n"
251 "\n";
252 }
253
254 if (mUsesTexture2DProj)
255 {
256 out << "float4 gl_texture2DProj(sampler2D s, float3 t)\n"
257 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000258 " return tex2Dproj(s, float4(t.x, t.z - t.y, 0, t.z));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000259 "}\n"
260 "\n"
261 "float4 gl_texture2DProj(sampler2D s, float4 t)\n"
262 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000263 " return tex2Dproj(s, float4(t.x, t.w - t.y, t.z, t.w));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000264 "}\n"
265 "\n";
266 }
267
268 if (mUsesTexture2DProj_bias)
269 {
270 out << "float4 gl_texture2DProj(sampler2D s, float3 t, float bias)\n"
271 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000272 " return tex2Dbias(s, float4(t.x / t.z, 1 - (t.y / t.z), 0, bias));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000273 "}\n"
274 "\n"
275 "float4 gl_texture2DProj(sampler2D s, float4 t, float bias)\n"
276 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000277 " return tex2Dbias(s, float4(t.x / t.w, 1 - (t.y / t.w), 0, bias));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000278 "}\n"
279 "\n";
280 }
281
282 if (mUsesTextureCube)
283 {
284 out << "float4 gl_textureCube(samplerCUBE s, float3 t)\n"
285 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000286 " return texCUBE(s, float3(t.x, -t.y, t.z));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000287 "}\n"
288 "\n";
289 }
290
291 if (mUsesTextureCube_bias)
292 {
293 out << "float4 gl_textureCube(samplerCUBE s, float3 t, float bias)\n"
294 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000295 " return texCUBEbias(s, float4(t.x, -t.y, t.z, bias));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000296 "}\n"
297 "\n";
298 }
daniel@transgaming.coma54f5182012-05-31 01:20:16 +0000299
300 // These *Lod0 intrinsics are not available in GL fragment shaders.
301 // They are used to sample using discontinuous texture coordinates.
302 if (mUsesTexture2DLod0)
303 {
304 out << "float4 gl_texture2DLod0(sampler2D s, float2 t)\n"
305 "{\n"
306 " return tex2Dlod(s, float4(t.x, 1 - t.y, 0, 0));\n"
307 "}\n"
308 "\n";
309 }
310
311 if (mUsesTexture2DLod0_bias)
312 {
313 out << "float4 gl_texture2DLod0(sampler2D s, float2 t, float bias)\n"
314 "{\n"
315 " return tex2Dlod(s, float4(t.x, 1 - t.y, 0, 0));\n"
316 "}\n"
317 "\n";
318 }
319
320 if (mUsesTexture2DProjLod0)
321 {
322 out << "float4 gl_texture2DProjLod0(sampler2D s, float3 t)\n"
323 "{\n"
324 " return tex2Dlod(s, float4(t.x / t.z, 1 - t.y / t.z, 0, 0));\n"
325 "}\n"
326 "\n"
327 "float4 gl_texture2DProjLod(sampler2D s, float4 t)\n"
328 "{\n"
329 " return tex2Dlod(s, float4(t.x / t.w, 1 - t.y / t.w, 0, 0));\n"
330 "}\n"
331 "\n";
332 }
333
334 if (mUsesTexture2DProjLod0_bias)
335 {
336 out << "float4 gl_texture2DProjLod0_bias(sampler2D s, float3 t, float bias)\n"
337 "{\n"
338 " return tex2Dlod(s, float4(t.x / t.z, 1 - t.y / t.z, 0, 0));\n"
339 "}\n"
340 "\n"
341 "float4 gl_texture2DProjLod_bias(sampler2D s, float4 t, float bias)\n"
342 "{\n"
343 " return tex2Dlod(s, float4(t.x / t.w, 1 - t.y / t.w, 0, 0));\n"
344 "}\n"
345 "\n";
346 }
347
348 if (mUsesTextureCubeLod0)
349 {
350 out << "float4 gl_textureCubeLod0(samplerCUBE s, float3 t)\n"
351 "{\n"
352 " return texCUBElod(s, float4(t.x, -t.y, t.z, 0));\n"
353 "}\n"
354 "\n";
355 }
356
357 if (mUsesTextureCubeLod0_bias)
358 {
359 out << "float4 gl_textureCubeLod0(samplerCUBE s, float3 t, float bias)\n"
360 "{\n"
361 " return texCUBElod(s, float4(t.x, -t.y, t.z, 0));\n"
362 "}\n"
363 "\n";
364 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000365 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000366 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000367 {
368 TString uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000369 TString attributes;
370 TString varyings;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000371
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000372 TSymbolTableLevel *symbols = mContext.symbolTable.getGlobalLevel();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000373
374 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
375 {
376 const TSymbol *symbol = (*namedSymbol).second;
377 const TString &name = symbol->getName();
378
379 if (symbol->isVariable())
380 {
381 const TVariable *variable = static_cast<const TVariable*>(symbol);
382 const TType &type = variable->getType();
383 TQualifier qualifier = type.getQualifier();
384
385 if (qualifier == EvqUniform)
386 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000387 if (mReferencedUniforms.find(name.c_str()) != mReferencedUniforms.end())
388 {
apatrick@chromium.org65756022012-01-17 21:45:38 +0000389 uniforms += "uniform " + typeString(type) + " " + decorateUniform(name, type) + arrayString(type) + ";\n";
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000390 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000391 }
392 else if (qualifier == EvqAttribute)
393 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000394 if (mReferencedAttributes.find(name.c_str()) != mReferencedAttributes.end())
395 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000396 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000397 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000398 }
399 else if (qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut)
400 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000401 if (mReferencedVaryings.find(name.c_str()) != mReferencedVaryings.end())
402 {
403 // Program linking depends on this exact format
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000404 varyings += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000405 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000406 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +0000407 else if (qualifier == EvqGlobal || qualifier == EvqTemporary)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000408 {
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000409 // Globals are declared and intialized as an aggregate node
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000410 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000411 else if (qualifier == EvqConst)
412 {
413 // Constants are repeated as literals where used
414 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000415 else UNREACHABLE();
416 }
417 }
418
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000419 out << "// Attributes\n";
420 out << attributes;
421 out << "\n"
422 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
423
424 if (mUsesPointSize)
425 {
426 out << "static float gl_PointSize = float(1);\n";
427 }
428
429 out << "\n"
430 "// Varyings\n";
431 out << varyings;
432 out << "\n"
433 "uniform float2 dx_HalfPixelSize;\n"
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +0000434 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000435 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000436 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000437
438 // The texture fetch functions "flip" the Y coordinate in one way or another. This is because textures are stored
439 // according to the OpenGL convention, i.e. (0, 0) is "bottom left", rather than the D3D convention where (0, 0)
440 // is "top left". Since the HLSL texture fetch functions expect textures to be stored according to the D3D
441 // convention, the Y coordinate passed to these functions is adjusted to compensate.
442 //
443 // The simplest case is texture2D where the mapping is Y -> 1-Y, which maps [0, 1] -> [1, 0].
444 //
445 // The texture2DProj functions are more complicated because the projection divides by either Z or W. For the vec3
446 // case, the mapping is Y -> Z-Y or Y/Z -> 1-Y/Z, which again maps [0, 1] -> [1, 0].
447 //
448 // For cube textures the mapping is Y -> -Y, which maps [-1, 1] -> [1, -1]. This is not sufficient on its own for the
449 // +Y and -Y faces, which are now on the "wrong sides" of the cube. This is compensated for by exchanging the
450 // +Y and -Y faces everywhere else throughout the code.
451
452 if (mUsesTexture2D)
453 {
454 out << "float4 gl_texture2D(sampler2D s, float2 t)\n"
455 "{\n"
456 " return tex2Dlod(s, float4(t.x, 1 - t.y, 0, 0));\n"
457 "}\n"
458 "\n";
459 }
460
461 if (mUsesTexture2DLod)
462 {
463 out << "float4 gl_texture2DLod(sampler2D s, float2 t, float lod)\n"
464 "{\n"
465 " return tex2Dlod(s, float4(t.x, 1 - t.y, 0, lod));\n"
466 "}\n"
467 "\n";
468 }
469
470 if (mUsesTexture2DProj)
471 {
472 out << "float4 gl_texture2DProj(sampler2D s, float3 t)\n"
473 "{\n"
474 " return tex2Dlod(s, float4(t.x / t.z, 1 - t.y / t.z, 0, 0));\n"
475 "}\n"
476 "\n"
477 "float4 gl_texture2DProj(sampler2D s, float4 t)\n"
478 "{\n"
479 " return tex2Dlod(s, float4(t.x / t.w, 1 - t.y / t.w, 0, 0));\n"
480 "}\n"
481 "\n";
482 }
483
484 if (mUsesTexture2DProjLod)
485 {
486 out << "float4 gl_texture2DProjLod(sampler2D s, float3 t, float lod)\n"
487 "{\n"
488 " return tex2Dlod(s, float4(t.x / t.z, 1 - t.y / t.z, 0, lod));\n"
489 "}\n"
490 "\n"
491 "float4 gl_texture2DProjLod(sampler2D s, float4 t, float lod)\n"
492 "{\n"
493 " return tex2Dlod(s, float4(t.x / t.w, 1 - t.y / t.w, 0, lod));\n"
494 "}\n"
495 "\n";
496 }
497
498 if (mUsesTextureCube)
499 {
500 out << "float4 gl_textureCube(samplerCUBE s, float3 t)\n"
501 "{\n"
502 " return texCUBElod(s, float4(t.x, -t.y, t.z, 0));\n"
503 "}\n"
504 "\n";
505 }
506
507 if (mUsesTextureCubeLod)
508 {
509 out << "float4 gl_textureCubeLod(samplerCUBE s, float3 t, float lod)\n"
510 "{\n"
511 " return texCUBElod(s, float4(t.x, -t.y, t.z, lod));\n"
512 "}\n"
513 "\n";
514 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000515 }
516
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000517 if (mUsesFragCoord)
518 {
519 out << "#define GL_USES_FRAG_COORD\n";
520 }
521
522 if (mUsesPointCoord)
523 {
524 out << "#define GL_USES_POINT_COORD\n";
525 }
526
527 if (mUsesFrontFacing)
528 {
529 out << "#define GL_USES_FRONT_FACING\n";
530 }
531
532 if (mUsesPointSize)
533 {
534 out << "#define GL_USES_POINT_SIZE\n";
535 }
536
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000537 if (mUsesDepthRange)
538 {
539 out << "struct gl_DepthRangeParameters\n"
540 "{\n"
541 " float near;\n"
542 " float far;\n"
543 " float diff;\n"
544 "};\n"
545 "\n"
daniel@transgaming.com31754962010-11-28 02:02:52 +0000546 "uniform float3 dx_DepthRange;"
547 "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000548 "\n";
549 }
550
551 if (mUsesXor)
552 {
553 out << "bool xor(bool p, bool q)\n"
554 "{\n"
555 " return (p || q) && !(p && q);\n"
556 "}\n"
557 "\n";
558 }
559
560 if (mUsesMod1)
561 {
562 out << "float mod(float x, float y)\n"
563 "{\n"
564 " return x - y * floor(x / y);\n"
565 "}\n"
566 "\n";
567 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000568
569 if (mUsesMod2v)
570 {
571 out << "float2 mod(float2 x, float2 y)\n"
572 "{\n"
573 " return x - y * floor(x / y);\n"
574 "}\n"
575 "\n";
576 }
577
578 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000579 {
580 out << "float2 mod(float2 x, float y)\n"
581 "{\n"
582 " return x - y * floor(x / y);\n"
583 "}\n"
584 "\n";
585 }
586
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000587 if (mUsesMod3v)
588 {
589 out << "float3 mod(float3 x, float3 y)\n"
590 "{\n"
591 " return x - y * floor(x / y);\n"
592 "}\n"
593 "\n";
594 }
595
596 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000597 {
598 out << "float3 mod(float3 x, float y)\n"
599 "{\n"
600 " return x - y * floor(x / y);\n"
601 "}\n"
602 "\n";
603 }
604
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000605 if (mUsesMod4v)
606 {
607 out << "float4 mod(float4 x, float4 y)\n"
608 "{\n"
609 " return x - y * floor(x / y);\n"
610 "}\n"
611 "\n";
612 }
613
614 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000615 {
616 out << "float4 mod(float4 x, float y)\n"
617 "{\n"
618 " return x - y * floor(x / y);\n"
619 "}\n"
620 "\n";
621 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000622
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000623 if (mUsesFaceforward1)
624 {
625 out << "float faceforward(float N, float I, float Nref)\n"
626 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000627 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000628 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000629 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000630 " }\n"
631 " else\n"
632 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000633 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000634 " }\n"
635 "}\n"
636 "\n";
637 }
638
639 if (mUsesFaceforward2)
640 {
641 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
642 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000643 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000644 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000645 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000646 " }\n"
647 " else\n"
648 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000649 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000650 " }\n"
651 "}\n"
652 "\n";
653 }
654
655 if (mUsesFaceforward3)
656 {
657 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
658 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000659 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000660 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000661 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000662 " }\n"
663 " else\n"
664 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000665 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000666 " }\n"
667 "}\n"
668 "\n";
669 }
670
671 if (mUsesFaceforward4)
672 {
673 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
674 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000675 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000676 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000677 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000678 " }\n"
679 " else\n"
680 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000681 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000682 " }\n"
683 "}\n"
684 "\n";
685 }
686
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000687 if (mUsesEqualMat2)
688 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000689 out << "bool equal(float2x2 m, float2x2 n)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000690 "{\n"
691 " return m[0][0] == n[0][0] && m[0][1] == n[0][1] &&\n"
692 " m[1][0] == n[1][0] && m[1][1] == n[1][1];\n"
693 "}\n";
694 }
695
696 if (mUsesEqualMat3)
697 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000698 out << "bool equal(float3x3 m, float3x3 n)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000699 "{\n"
700 " return m[0][0] == n[0][0] && m[0][1] == n[0][1] && m[0][2] == n[0][2] &&\n"
701 " m[1][0] == n[1][0] && m[1][1] == n[1][1] && m[1][2] == n[1][2] &&\n"
702 " m[2][0] == n[2][0] && m[2][1] == n[2][1] && m[2][2] == n[2][2];\n"
703 "}\n";
704 }
705
706 if (mUsesEqualMat4)
707 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000708 out << "bool equal(float4x4 m, float4x4 n)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000709 "{\n"
710 " return m[0][0] == n[0][0] && m[0][1] == n[0][1] && m[0][2] == n[0][2] && m[0][3] == n[0][3] &&\n"
711 " m[1][0] == n[1][0] && m[1][1] == n[1][1] && m[1][2] == n[1][2] && m[1][3] == n[1][3] &&\n"
712 " m[2][0] == n[2][0] && m[2][1] == n[2][1] && m[2][2] == n[2][2] && m[2][3] == n[2][3] &&\n"
713 " m[3][0] == n[3][0] && m[3][1] == n[3][1] && m[3][2] == n[3][2] && m[3][3] == n[3][3];\n"
714 "}\n";
715 }
716
717 if (mUsesEqualVec2)
718 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000719 out << "bool equal(float2 v, float2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000720 "{\n"
721 " return v.x == u.x && v.y == u.y;\n"
722 "}\n";
723 }
724
725 if (mUsesEqualVec3)
726 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000727 out << "bool equal(float3 v, float3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000728 "{\n"
729 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
730 "}\n";
731 }
732
733 if (mUsesEqualVec4)
734 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000735 out << "bool equal(float4 v, float4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000736 "{\n"
737 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
738 "}\n";
739 }
740
741 if (mUsesEqualIVec2)
742 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000743 out << "bool equal(int2 v, int2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000744 "{\n"
745 " return v.x == u.x && v.y == u.y;\n"
746 "}\n";
747 }
748
749 if (mUsesEqualIVec3)
750 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000751 out << "bool equal(int3 v, int3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000752 "{\n"
753 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
754 "}\n";
755 }
756
757 if (mUsesEqualIVec4)
758 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000759 out << "bool equal(int4 v, int4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000760 "{\n"
761 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
762 "}\n";
763 }
764
765 if (mUsesEqualBVec2)
766 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000767 out << "bool equal(bool2 v, bool2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000768 "{\n"
769 " return v.x == u.x && v.y == u.y;\n"
770 "}\n";
771 }
772
773 if (mUsesEqualBVec3)
774 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000775 out << "bool equal(bool3 v, bool3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000776 "{\n"
777 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
778 "}\n";
779 }
780
781 if (mUsesEqualBVec4)
782 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000783 out << "bool equal(bool4 v, bool4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000784 "{\n"
785 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
786 "}\n";
787 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +0000788
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000789 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +0000790 {
791 out << "float atanyx(float y, float x)\n"
792 "{\n"
793 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
794 " return atan2(y, x);\n"
795 "}\n";
796 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000797
798 if (mUsesAtan2_2)
799 {
800 out << "float2 atanyx(float2 y, float2 x)\n"
801 "{\n"
802 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
803 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
804 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
805 "}\n";
806 }
807
808 if (mUsesAtan2_3)
809 {
810 out << "float3 atanyx(float3 y, float3 x)\n"
811 "{\n"
812 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
813 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
814 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
815 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
816 "}\n";
817 }
818
819 if (mUsesAtan2_4)
820 {
821 out << "float4 atanyx(float4 y, float4 x)\n"
822 "{\n"
823 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
824 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
825 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
826 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
827 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
828 "}\n";
829 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000830}
831
832void OutputHLSL::visitSymbol(TIntermSymbol *node)
833{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000834 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000835
836 TString name = node->getSymbol();
837
838 if (name == "gl_FragColor")
839 {
840 out << "gl_Color[0]";
841 }
842 else if (name == "gl_FragData")
843 {
844 out << "gl_Color";
845 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000846 else if (name == "gl_DepthRange")
847 {
848 mUsesDepthRange = true;
849 out << name;
850 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000851 else if (name == "gl_FragCoord")
852 {
853 mUsesFragCoord = true;
854 out << name;
855 }
856 else if (name == "gl_PointCoord")
857 {
858 mUsesPointCoord = true;
859 out << name;
860 }
861 else if (name == "gl_FrontFacing")
862 {
863 mUsesFrontFacing = true;
864 out << name;
865 }
866 else if (name == "gl_PointSize")
867 {
868 mUsesPointSize = true;
869 out << name;
870 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000871 else
872 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000873 TQualifier qualifier = node->getQualifier();
874
875 if (qualifier == EvqUniform)
876 {
877 mReferencedUniforms.insert(name.c_str());
apatrick@chromium.org65756022012-01-17 21:45:38 +0000878 out << decorateUniform(name, node->getType());
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000879 }
880 else if (qualifier == EvqAttribute)
881 {
882 mReferencedAttributes.insert(name.c_str());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000883 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000884 }
885 else if (qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut || qualifier == EvqVaryingIn || qualifier == EvqInvariantVaryingIn)
886 {
887 mReferencedVaryings.insert(name.c_str());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000888 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000889 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000890 else
891 {
892 out << decorate(name);
893 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000894 }
895}
896
897bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
898{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000899 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000900
901 switch (node->getOp())
902 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000903 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000904 case EOpInitialize:
905 if (visit == PreVisit)
906 {
907 // GLSL allows to write things like "float x = x;" where a new variable x is defined
908 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
909 // new variable is created before the assignment is evaluated), so we need to convert
910 // this to "float t = x, x = t;".
911
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000912 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
913 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000914
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000915 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
916 expression->traverse(&searchSymbol);
917 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000918
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000919 if (sameSymbol)
920 {
921 // Type already printed
922 out << "t" + str(mUniqueIndex) + " = ";
923 expression->traverse(this);
924 out << ", ";
925 symbolNode->traverse(this);
926 out << " = t" + str(mUniqueIndex);
927
928 mUniqueIndex++;
929 return false;
930 }
931 }
932 else if (visit == InVisit)
933 {
934 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000935 }
936 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000937 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
938 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
939 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
940 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
941 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
942 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000943 if (visit == PreVisit)
944 {
945 out << "(";
946 }
947 else if (visit == InVisit)
948 {
949 out << " = mul(";
950 node->getLeft()->traverse(this);
951 out << ", transpose(";
952 }
953 else
954 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +0000955 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000956 }
957 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000958 case EOpMatrixTimesMatrixAssign:
959 if (visit == PreVisit)
960 {
961 out << "(";
962 }
963 else if (visit == InVisit)
964 {
965 out << " = mul(";
966 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000967 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000968 }
969 else
970 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +0000971 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000972 }
973 break;
974 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +0000975 case EOpIndexDirect: outputTriplet(visit, "", "[", "]"); break;
976 case EOpIndexIndirect: outputTriplet(visit, "", "[", "]"); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +0000977 case EOpIndexDirectStruct:
978 if (visit == InVisit)
979 {
daniel@transgaming.com2e793f02012-04-11 19:41:35 +0000980 out << "." + decorateField(node->getType().getFieldName(), node->getLeft()->getType());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +0000981
982 return false;
983 }
984 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000985 case EOpVectorSwizzle:
986 if (visit == InVisit)
987 {
988 out << ".";
989
990 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
991
992 if (swizzle)
993 {
994 TIntermSequence &sequence = swizzle->getSequence();
995
996 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
997 {
998 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
999
1000 if (element)
1001 {
1002 int i = element->getUnionArrayPointer()[0].getIConst();
1003
1004 switch (i)
1005 {
1006 case 0: out << "x"; break;
1007 case 1: out << "y"; break;
1008 case 2: out << "z"; break;
1009 case 3: out << "w"; break;
1010 default: UNREACHABLE();
1011 }
1012 }
1013 else UNREACHABLE();
1014 }
1015 }
1016 else UNREACHABLE();
1017
1018 return false; // Fully processed
1019 }
1020 break;
1021 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1022 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1023 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1024 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001025 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001026 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001027 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001028 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001029 if (node->getOp() == EOpEqual)
1030 {
1031 outputTriplet(visit, "(", " == ", ")");
1032 }
1033 else
1034 {
1035 outputTriplet(visit, "(", " != ", ")");
1036 }
1037 }
1038 else if (node->getLeft()->getBasicType() == EbtStruct)
1039 {
1040 if (node->getOp() == EOpEqual)
1041 {
1042 out << "(";
1043 }
1044 else
1045 {
1046 out << "!(";
1047 }
1048
1049 const TTypeList *fields = node->getLeft()->getType().getStruct();
1050
1051 for (size_t i = 0; i < fields->size(); i++)
1052 {
1053 const TType *fieldType = (*fields)[i].type;
1054
1055 node->getLeft()->traverse(this);
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00001056 out << "." + decorateField(fieldType->getFieldName(), node->getLeft()->getType()) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001057 node->getRight()->traverse(this);
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00001058 out << "." + decorateField(fieldType->getFieldName(), node->getLeft()->getType());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001059
1060 if (i < fields->size() - 1)
1061 {
1062 out << " && ";
1063 }
1064 }
1065
1066 out << ")";
1067
1068 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001069 }
1070 else
1071 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001072 if (node->getLeft()->isMatrix())
1073 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001074 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001075 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001076 case 2: mUsesEqualMat2 = true; break;
1077 case 3: mUsesEqualMat3 = true; break;
1078 case 4: mUsesEqualMat4 = true; break;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001079 default: UNREACHABLE();
1080 }
1081 }
1082 else if (node->getLeft()->isVector())
1083 {
1084 switch (node->getLeft()->getBasicType())
1085 {
1086 case EbtFloat:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001087 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001088 {
1089 case 2: mUsesEqualVec2 = true; break;
1090 case 3: mUsesEqualVec3 = true; break;
1091 case 4: mUsesEqualVec4 = true; break;
1092 default: UNREACHABLE();
1093 }
1094 break;
1095 case EbtInt:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001096 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001097 {
1098 case 2: mUsesEqualIVec2 = true; break;
1099 case 3: mUsesEqualIVec3 = true; break;
1100 case 4: mUsesEqualIVec4 = true; break;
1101 default: UNREACHABLE();
1102 }
1103 break;
1104 case EbtBool:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001105 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001106 {
1107 case 2: mUsesEqualBVec2 = true; break;
1108 case 3: mUsesEqualBVec3 = true; break;
1109 case 4: mUsesEqualBVec4 = true; break;
1110 default: UNREACHABLE();
1111 }
1112 break;
1113 default: UNREACHABLE();
1114 }
1115 }
1116 else UNREACHABLE();
1117
1118 if (node->getOp() == EOpEqual)
1119 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001120 outputTriplet(visit, "equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001121 }
1122 else
1123 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001124 outputTriplet(visit, "!equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001125 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001126 }
1127 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001128 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1129 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1130 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1131 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1132 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001133 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001134 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1135 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001136 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001137 case EOpLogicalOr:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001138 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001139 return false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001140 case EOpLogicalXor:
1141 mUsesXor = true;
1142 outputTriplet(visit, "xor(", ", ", ")");
1143 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001144 case EOpLogicalAnd:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001145 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001146 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001147 default: UNREACHABLE();
1148 }
1149
1150 return true;
1151}
1152
1153bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1154{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001155 switch (node->getOp())
1156 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001157 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1158 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1159 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1160 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1161 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1162 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1163 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001164 case EOpConvIntToBool:
1165 case EOpConvFloatToBool:
1166 switch (node->getOperand()->getType().getNominalSize())
1167 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001168 case 1: outputTriplet(visit, "bool(", "", ")"); break;
1169 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
1170 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
1171 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001172 default: UNREACHABLE();
1173 }
1174 break;
1175 case EOpConvBoolToFloat:
1176 case EOpConvIntToFloat:
1177 switch (node->getOperand()->getType().getNominalSize())
1178 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001179 case 1: outputTriplet(visit, "float(", "", ")"); break;
1180 case 2: outputTriplet(visit, "float2(", "", ")"); break;
1181 case 3: outputTriplet(visit, "float3(", "", ")"); break;
1182 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001183 default: UNREACHABLE();
1184 }
1185 break;
1186 case EOpConvFloatToInt:
1187 case EOpConvBoolToInt:
1188 switch (node->getOperand()->getType().getNominalSize())
1189 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001190 case 1: outputTriplet(visit, "int(", "", ")"); break;
1191 case 2: outputTriplet(visit, "int2(", "", ")"); break;
1192 case 3: outputTriplet(visit, "int3(", "", ")"); break;
1193 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001194 default: UNREACHABLE();
1195 }
1196 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001197 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1198 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1199 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1200 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1201 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1202 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1203 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1204 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1205 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1206 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1207 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1208 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1209 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1210 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1211 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1212 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1213 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1214 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1215 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1216 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1217 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001218 case EOpDFdx:
1219 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1220 {
1221 outputTriplet(visit, "(", "", ", 0.0)");
1222 }
1223 else
1224 {
1225 outputTriplet(visit, "ddx(", "", ")");
1226 }
1227 break;
1228 case EOpDFdy:
1229 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1230 {
1231 outputTriplet(visit, "(", "", ", 0.0)");
1232 }
1233 else
1234 {
1235 outputTriplet(visit, "(-ddy(", "", "))");
1236 }
1237 break;
1238 case EOpFwidth:
1239 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1240 {
1241 outputTriplet(visit, "(", "", ", 0.0)");
1242 }
1243 else
1244 {
1245 outputTriplet(visit, "fwidth(", "", ")");
1246 }
1247 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001248 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1249 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001250 default: UNREACHABLE();
1251 }
1252
1253 return true;
1254}
1255
1256bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1257{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001258 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001259
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001260 switch (node->getOp())
1261 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001262 case EOpSequence:
1263 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001264 if (mInsideFunction)
1265 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001266 outputLineDirective(node->getLine());
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001267 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001268
1269 mScopeDepth++;
1270
1271 if (mScopeBracket.size() < mScopeDepth)
1272 {
1273 mScopeBracket.push_back(0); // New scope level
1274 }
1275 else
1276 {
1277 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
1278 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001279 }
1280
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001281 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
1282 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001283 outputLineDirective((*sit)->getLine());
1284
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001285 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001286
1287 out << ";\n";
1288 }
1289
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001290 if (mInsideFunction)
1291 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001292 outputLineDirective(node->getEndLine());
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001293 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001294
1295 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001296 }
1297
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001298 return false;
1299 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001300 case EOpDeclaration:
1301 if (visit == PreVisit)
1302 {
1303 TIntermSequence &sequence = node->getSequence();
1304 TIntermTyped *variable = sequence[0]->getAsTyped();
1305 bool visit = true;
1306
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001307 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001308 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001309 if (variable->getType().getStruct())
1310 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001311 addConstructor(variable->getType(), scopedStruct(variable->getType().getTypeName()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001312 }
1313
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001314 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001315 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00001316 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001317 {
1318 out << "static ";
1319 }
1320
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001321 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001322
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001323 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001324 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001325 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001326
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001327 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001328 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001329 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001330 out << arrayString(symbol->getType());
daniel@transgaming.com7127f202010-04-15 20:45:22 +00001331 out << " = " + initializer(variable->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001332 }
1333 else
1334 {
1335 (*sit)->traverse(this);
1336 }
1337
1338 if (visit && this->inVisit)
1339 {
1340 if (*sit != sequence.back())
1341 {
1342 visit = this->visitAggregate(InVisit, node);
1343 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001344 }
1345 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001346
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001347 if (visit && this->postVisit)
1348 {
1349 this->visitAggregate(PostVisit, node);
1350 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001351 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001352 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1353 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001354 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001355 }
1356 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001357 }
1358
1359 return false;
1360 }
1361 else if (visit == InVisit)
1362 {
1363 out << ", ";
1364 }
1365 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001366 case EOpPrototype:
1367 if (visit == PreVisit)
1368 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001369 out << typeString(node->getType()) << " " << decorate(node->getName()) << "(";
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001370
1371 TIntermSequence &arguments = node->getSequence();
1372
1373 for (unsigned int i = 0; i < arguments.size(); i++)
1374 {
1375 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
1376
1377 if (symbol)
1378 {
1379 out << argumentString(symbol);
1380
1381 if (i < arguments.size() - 1)
1382 {
1383 out << ", ";
1384 }
1385 }
1386 else UNREACHABLE();
1387 }
1388
1389 out << ");\n";
1390
1391 return false;
1392 }
1393 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001394 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001395 case EOpFunction:
1396 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001397 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001398
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001399 out << typeString(node->getType()) << " ";
1400
1401 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001402 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001403 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001404 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001405 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001406 {
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001407 out << decorate(name) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001408 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001409
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001410 TIntermSequence &sequence = node->getSequence();
1411 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
1412
1413 for (unsigned int i = 0; i < arguments.size(); i++)
1414 {
1415 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
1416
1417 if (symbol)
1418 {
1419 if (symbol->getType().getStruct())
1420 {
1421 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getTypeName()), NULL);
1422 }
1423
1424 out << argumentString(symbol);
1425
1426 if (i < arguments.size() - 1)
1427 {
1428 out << ", ";
1429 }
1430 }
1431 else UNREACHABLE();
1432 }
1433
1434 out << ")\n"
1435 "{\n";
1436
1437 if (sequence.size() > 1)
1438 {
1439 mInsideFunction = true;
1440 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001441 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001442 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001443
1444 out << "}\n";
1445
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001446 if (mContainsLoopDiscontinuity && !mOutputLod0Function)
1447 {
daniel@transgaming.comecdf44a2012-06-01 01:45:15 +00001448 if (name != "main")
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001449 {
1450 mOutputLod0Function = true;
1451 node->traverse(this);
1452 mOutputLod0Function = false;
1453 }
1454 }
1455
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001456 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001457 }
1458 break;
1459 case EOpFunctionCall:
1460 {
1461 if (visit == PreVisit)
1462 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001463 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.come11100c2012-05-31 01:20:32 +00001464 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001465
1466 if (node->isUserDefined())
1467 {
daniel@transgaming.come11100c2012-05-31 01:20:32 +00001468 out << decorate(name) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001469 }
1470 else
1471 {
1472 if (name == "texture2D")
1473 {
daniel@transgaming.come11100c2012-05-31 01:20:32 +00001474 if (!lod0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001475 {
daniel@transgaming.come11100c2012-05-31 01:20:32 +00001476 if (node->getSequence().size() == 2)
1477 {
1478 mUsesTexture2D = true;
1479 }
1480 else if (node->getSequence().size() == 3)
1481 {
1482 mUsesTexture2D_bias = true;
1483 }
1484 else UNREACHABLE();
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001485
daniel@transgaming.come11100c2012-05-31 01:20:32 +00001486 out << "gl_texture2D(";
1487 }
1488 else
1489 {
1490 if (node->getSequence().size() == 2)
1491 {
1492 mUsesTexture2DLod0 = true;
1493 }
1494 else if (node->getSequence().size() == 3)
1495 {
1496 mUsesTexture2DLod0_bias = true;
1497 }
1498 else UNREACHABLE();
1499
1500 out << "gl_texture2DLod0(";
1501 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001502 }
1503 else if (name == "texture2DProj")
1504 {
daniel@transgaming.come11100c2012-05-31 01:20:32 +00001505 if (!lod0)
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001506 {
daniel@transgaming.come11100c2012-05-31 01:20:32 +00001507 if (node->getSequence().size() == 2)
1508 {
1509 mUsesTexture2DProj = true;
1510 }
1511 else if (node->getSequence().size() == 3)
1512 {
1513 mUsesTexture2DProj_bias = true;
1514 }
1515 else UNREACHABLE();
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001516
daniel@transgaming.come11100c2012-05-31 01:20:32 +00001517 out << "gl_texture2DProj(";
1518 }
1519 else
1520 {
1521 if (node->getSequence().size() == 2)
1522 {
1523 mUsesTexture2DProjLod0 = true;
1524 }
1525 else if (node->getSequence().size() == 3)
1526 {
1527 mUsesTexture2DProjLod0_bias = true;
1528 }
1529 else UNREACHABLE();
1530
1531 out << "gl_texture2DProjLod0(";
1532 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001533 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001534 else if (name == "textureCube")
1535 {
daniel@transgaming.come11100c2012-05-31 01:20:32 +00001536 if (!lod0)
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001537 {
daniel@transgaming.come11100c2012-05-31 01:20:32 +00001538 if (node->getSequence().size() == 2)
1539 {
1540 mUsesTextureCube = true;
1541 }
1542 else if (node->getSequence().size() == 3)
1543 {
1544 mUsesTextureCube_bias = true;
1545 }
1546 else UNREACHABLE();
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001547
daniel@transgaming.come11100c2012-05-31 01:20:32 +00001548 out << "gl_textureCube(";
1549 }
1550 else
1551 {
1552 if (node->getSequence().size() == 2)
1553 {
1554 mUsesTextureCubeLod0 = true;
1555 }
1556 else if (node->getSequence().size() == 3)
1557 {
1558 mUsesTextureCubeLod0_bias = true;
1559 }
1560 else UNREACHABLE();
1561
1562 out << "gl_textureCubeLod0(";
1563 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001564 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001565 else if (name == "texture2DLod")
1566 {
daniel@transgaming.com15795192011-05-11 15:36:20 +00001567 if (node->getSequence().size() == 3)
1568 {
1569 mUsesTexture2DLod = true;
1570 }
1571 else UNREACHABLE();
1572
1573 out << "gl_texture2DLod(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001574 }
1575 else if (name == "texture2DProjLod")
1576 {
daniel@transgaming.com15795192011-05-11 15:36:20 +00001577 if (node->getSequence().size() == 3)
1578 {
1579 mUsesTexture2DProjLod = true;
1580 }
1581 else UNREACHABLE();
1582
1583 out << "gl_texture2DProjLod(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001584 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001585 else if (name == "textureCubeLod")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001586 {
daniel@transgaming.com15795192011-05-11 15:36:20 +00001587 if (node->getSequence().size() == 3)
1588 {
1589 mUsesTextureCubeLod = true;
1590 }
1591 else UNREACHABLE();
1592
1593 out << "gl_textureCubeLod(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001594 }
daniel@transgaming.comec55d292010-04-15 20:44:49 +00001595 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001596 }
1597 }
1598 else if (visit == InVisit)
1599 {
1600 out << ", ";
1601 }
1602 else
1603 {
1604 out << ")";
1605 }
1606 }
1607 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001608 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001609 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001610 addConstructor(node->getType(), "vec1", &node->getSequence());
1611 outputTriplet(visit, "vec1(", "", ")");
1612 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001613 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001614 addConstructor(node->getType(), "vec2", &node->getSequence());
1615 outputTriplet(visit, "vec2(", ", ", ")");
1616 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001617 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001618 addConstructor(node->getType(), "vec3", &node->getSequence());
1619 outputTriplet(visit, "vec3(", ", ", ")");
1620 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001621 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001622 addConstructor(node->getType(), "vec4", &node->getSequence());
1623 outputTriplet(visit, "vec4(", ", ", ")");
1624 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001625 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001626 addConstructor(node->getType(), "bvec1", &node->getSequence());
1627 outputTriplet(visit, "bvec1(", "", ")");
1628 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001629 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001630 addConstructor(node->getType(), "bvec2", &node->getSequence());
1631 outputTriplet(visit, "bvec2(", ", ", ")");
1632 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001633 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001634 addConstructor(node->getType(), "bvec3", &node->getSequence());
1635 outputTriplet(visit, "bvec3(", ", ", ")");
1636 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001637 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001638 addConstructor(node->getType(), "bvec4", &node->getSequence());
1639 outputTriplet(visit, "bvec4(", ", ", ")");
1640 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001641 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001642 addConstructor(node->getType(), "ivec1", &node->getSequence());
1643 outputTriplet(visit, "ivec1(", "", ")");
1644 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001645 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001646 addConstructor(node->getType(), "ivec2", &node->getSequence());
1647 outputTriplet(visit, "ivec2(", ", ", ")");
1648 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001649 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001650 addConstructor(node->getType(), "ivec3", &node->getSequence());
1651 outputTriplet(visit, "ivec3(", ", ", ")");
1652 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001653 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001654 addConstructor(node->getType(), "ivec4", &node->getSequence());
1655 outputTriplet(visit, "ivec4(", ", ", ")");
1656 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001657 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001658 addConstructor(node->getType(), "mat2", &node->getSequence());
1659 outputTriplet(visit, "mat2(", ", ", ")");
1660 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001661 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001662 addConstructor(node->getType(), "mat3", &node->getSequence());
1663 outputTriplet(visit, "mat3(", ", ", ")");
1664 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001665 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001666 addConstructor(node->getType(), "mat4", &node->getSequence());
1667 outputTriplet(visit, "mat4(", ", ", ")");
1668 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001669 case EOpConstructStruct:
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001670 addConstructor(node->getType(), scopedStruct(node->getType().getTypeName()), &node->getSequence());
1671 outputTriplet(visit, structLookup(node->getType().getTypeName()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001672 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001673 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1674 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1675 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1676 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1677 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
1678 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001679 case EOpMod:
1680 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001681 // We need to look at the number of components in both arguments
1682 switch (node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
1683 + node->getSequence()[1]->getAsTyped()->getNominalSize())
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001684 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001685 case 11: mUsesMod1 = true; break;
1686 case 22: mUsesMod2v = true; break;
1687 case 21: mUsesMod2f = true; break;
1688 case 33: mUsesMod3v = true; break;
1689 case 31: mUsesMod3f = true; break;
1690 case 44: mUsesMod4v = true; break;
1691 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001692 default: UNREACHABLE();
1693 }
1694
1695 outputTriplet(visit, "mod(", ", ", ")");
1696 }
1697 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001698 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001699 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001700 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001701 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
1702 {
1703 case 1: mUsesAtan2_1 = true; break;
1704 case 2: mUsesAtan2_2 = true; break;
1705 case 3: mUsesAtan2_3 = true; break;
1706 case 4: mUsesAtan2_4 = true; break;
1707 default: UNREACHABLE();
1708 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001709 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001710 break;
1711 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
1712 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
1713 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
1714 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
1715 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
1716 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
1717 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
1718 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
1719 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001720 case EOpFaceForward:
1721 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001722 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001723 {
1724 case 1: mUsesFaceforward1 = true; break;
1725 case 2: mUsesFaceforward2 = true; break;
1726 case 3: mUsesFaceforward3 = true; break;
1727 case 4: mUsesFaceforward4 = true; break;
1728 default: UNREACHABLE();
1729 }
1730
1731 outputTriplet(visit, "faceforward(", ", ", ")");
1732 }
1733 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001734 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
1735 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
1736 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001737 default: UNREACHABLE();
1738 }
1739
1740 return true;
1741}
1742
1743bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
1744{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001745 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001746
alokp@chromium.org60fe4072010-03-29 20:58:29 +00001747 if (node->usesTernaryOperator())
1748 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001749 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00001750 }
1751 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001752 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001753 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001754
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001755 out << "if(";
1756
1757 node->getCondition()->traverse(this);
1758
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001759 out << ")\n";
1760
1761 outputLineDirective(node->getLine());
1762 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001763
daniel@transgaming.combb885322010-04-15 20:45:24 +00001764 if (node->getTrueBlock())
1765 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001766 traverseStatements(node->getTrueBlock());
daniel@transgaming.combb885322010-04-15 20:45:24 +00001767 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001768
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001769 outputLineDirective(node->getLine());
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001770 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001771
1772 if (node->getFalseBlock())
1773 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001774 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001775
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001776 outputLineDirective(node->getFalseBlock()->getLine());
1777 out << "{\n";
1778
1779 outputLineDirective(node->getFalseBlock()->getLine());
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001780 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001781
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001782 outputLineDirective(node->getFalseBlock()->getLine());
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001783 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001784 }
1785 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001786
1787 return false;
1788}
1789
1790void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
1791{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001792 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001793}
1794
1795bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
1796{
daniel@transgaming.come11100c2012-05-31 01:20:32 +00001797 bool wasDiscontinuous = mInsideDiscontinuousLoop;
1798
1799 if (!mInsideDiscontinuousLoop)
1800 {
1801 mInsideDiscontinuousLoop = containsLoopDiscontinuity(node);
1802 }
1803
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001804 if (handleExcessiveLoop(node))
1805 {
1806 return false;
1807 }
1808
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001809 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001810
alokp@chromium.org52813552010-11-16 18:36:09 +00001811 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001812 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00001813 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001814
1815 outputLineDirective(node->getLine());
1816 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001817 }
1818 else
1819 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00001820 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001821
1822 if (node->getInit())
1823 {
1824 node->getInit()->traverse(this);
1825 }
1826
1827 out << "; ";
1828
alokp@chromium.org52813552010-11-16 18:36:09 +00001829 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001830 {
alokp@chromium.org52813552010-11-16 18:36:09 +00001831 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001832 }
1833
1834 out << "; ";
1835
alokp@chromium.org52813552010-11-16 18:36:09 +00001836 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001837 {
alokp@chromium.org52813552010-11-16 18:36:09 +00001838 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001839 }
1840
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001841 out << ")\n";
1842
1843 outputLineDirective(node->getLine());
1844 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001845 }
1846
1847 if (node->getBody())
1848 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001849 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001850 }
1851
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001852 outputLineDirective(node->getLine());
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00001853 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001854
alokp@chromium.org52813552010-11-16 18:36:09 +00001855 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001856 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001857 outputLineDirective(node->getCondition()->getLine());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001858 out << "while(\n";
1859
alokp@chromium.org52813552010-11-16 18:36:09 +00001860 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001861
daniel@transgaming.com73536982012-03-21 20:45:49 +00001862 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001863 }
1864
daniel@transgaming.com73536982012-03-21 20:45:49 +00001865 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001866
daniel@transgaming.come11100c2012-05-31 01:20:32 +00001867 mInsideDiscontinuousLoop = wasDiscontinuous;
1868
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001869 return false;
1870}
1871
1872bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
1873{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001874 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001875
1876 switch (node->getFlowOp())
1877 {
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00001878 case EOpKill: outputTriplet(visit, "discard;\n", "", ""); break;
1879 case EOpBreak: outputTriplet(visit, "break;\n", "", ""); break;
1880 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001881 case EOpReturn:
1882 if (visit == PreVisit)
1883 {
1884 if (node->getExpression())
1885 {
1886 out << "return ";
1887 }
1888 else
1889 {
1890 out << "return;\n";
1891 }
1892 }
1893 else if (visit == PostVisit)
1894 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001895 if (node->getExpression())
1896 {
1897 out << ";\n";
1898 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001899 }
1900 break;
1901 default: UNREACHABLE();
1902 }
1903
1904 return true;
1905}
1906
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001907void OutputHLSL::traverseStatements(TIntermNode *node)
1908{
1909 if (isSingleStatement(node))
1910 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001911 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001912 }
1913
1914 node->traverse(this);
1915}
1916
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001917bool OutputHLSL::isSingleStatement(TIntermNode *node)
1918{
1919 TIntermAggregate *aggregate = node->getAsAggregate();
1920
1921 if (aggregate)
1922 {
1923 if (aggregate->getOp() == EOpSequence)
1924 {
1925 return false;
1926 }
1927 else
1928 {
1929 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
1930 {
1931 if (!isSingleStatement(*sit))
1932 {
1933 return false;
1934 }
1935 }
1936
1937 return true;
1938 }
1939 }
1940
1941 return true;
1942}
1943
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00001944// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
1945// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001946bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
1947{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00001948 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001949 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001950
1951 // Parse loops of the form:
1952 // for(int index = initial; index [comparator] limit; index += increment)
1953 TIntermSymbol *index = NULL;
1954 TOperator comparator = EOpNull;
1955 int initial = 0;
1956 int limit = 0;
1957 int increment = 0;
1958
1959 // Parse index name and intial value
1960 if (node->getInit())
1961 {
1962 TIntermAggregate *init = node->getInit()->getAsAggregate();
1963
1964 if (init)
1965 {
1966 TIntermSequence &sequence = init->getSequence();
1967 TIntermTyped *variable = sequence[0]->getAsTyped();
1968
1969 if (variable && variable->getQualifier() == EvqTemporary)
1970 {
1971 TIntermBinary *assign = variable->getAsBinaryNode();
1972
1973 if (assign->getOp() == EOpInitialize)
1974 {
1975 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
1976 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
1977
1978 if (symbol && constant)
1979 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001980 if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001981 {
1982 index = symbol;
1983 initial = constant->getUnionArrayPointer()[0].getIConst();
1984 }
1985 }
1986 }
1987 }
1988 }
1989 }
1990
1991 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00001992 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001993 {
alokp@chromium.org52813552010-11-16 18:36:09 +00001994 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001995
1996 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
1997 {
1998 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
1999
2000 if (constant)
2001 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002002 if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002003 {
2004 comparator = test->getOp();
2005 limit = constant->getUnionArrayPointer()[0].getIConst();
2006 }
2007 }
2008 }
2009 }
2010
2011 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002012 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002013 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002014 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2015 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002016
2017 if (binaryTerminal)
2018 {
2019 TOperator op = binaryTerminal->getOp();
2020 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2021
2022 if (constant)
2023 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00002024 if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002025 {
2026 int value = constant->getUnionArrayPointer()[0].getIConst();
2027
2028 switch (op)
2029 {
2030 case EOpAddAssign: increment = value; break;
2031 case EOpSubAssign: increment = -value; break;
2032 default: UNIMPLEMENTED();
2033 }
2034 }
2035 }
2036 }
2037 else if (unaryTerminal)
2038 {
2039 TOperator op = unaryTerminal->getOp();
2040
2041 switch (op)
2042 {
2043 case EOpPostIncrement: increment = 1; break;
2044 case EOpPostDecrement: increment = -1; break;
2045 case EOpPreIncrement: increment = 1; break;
2046 case EOpPreDecrement: increment = -1; break;
2047 default: UNIMPLEMENTED();
2048 }
2049 }
2050 }
2051
2052 if (index != NULL && comparator != EOpNull && increment != 0)
2053 {
2054 if (comparator == EOpLessThanEqual)
2055 {
2056 comparator = EOpLessThan;
2057 limit += 1;
2058 }
2059
2060 if (comparator == EOpLessThan)
2061 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002062 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002063
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002064 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002065 {
2066 return false; // Not an excessive loop
2067 }
2068
2069 while (iterations > 0)
2070 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002071 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002072
2073 // for(int index = initial; index < clampedLimit; index += increment)
2074
2075 out << "for(int ";
2076 index->traverse(this);
2077 out << " = ";
2078 out << initial;
2079
2080 out << "; ";
2081 index->traverse(this);
2082 out << " < ";
2083 out << clampedLimit;
2084
2085 out << "; ";
2086 index->traverse(this);
2087 out << " += ";
2088 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002089 out << ")\n";
2090
2091 outputLineDirective(node->getLine());
2092 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002093
2094 if (node->getBody())
2095 {
2096 node->getBody()->traverse(this);
2097 }
2098
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002099 outputLineDirective(node->getLine());
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00002100 out << ";}\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002101
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002102 initial += MAX_LOOP_ITERATIONS * increment;
2103 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002104 }
2105
2106 return true;
2107 }
2108 else UNIMPLEMENTED();
2109 }
2110
2111 return false; // Not handled as an excessive loop
2112}
2113
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002114void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002115{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002116 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002117
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002118 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002119 {
2120 out << preString;
2121 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002122 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002123 {
2124 out << inString;
2125 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002126 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002127 {
2128 out << postString;
2129 }
2130}
2131
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002132void OutputHLSL::outputLineDirective(int line)
2133{
2134 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2135 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002136 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002137 mBody << "#line " << line;
2138
2139 if (mContext.sourcePath)
2140 {
2141 mBody << " \"" << mContext.sourcePath << "\"";
2142 }
2143
2144 mBody << "\n";
2145 }
2146}
2147
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002148TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2149{
2150 TQualifier qualifier = symbol->getQualifier();
2151 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002152 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002153
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002154 if (name.empty()) // HLSL demands named arguments, also for prototypes
2155 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002156 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002157 }
2158 else
2159 {
2160 name = decorate(name);
2161 }
2162
2163 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002164}
2165
2166TString OutputHLSL::qualifierString(TQualifier qualifier)
2167{
2168 switch(qualifier)
2169 {
2170 case EvqIn: return "in";
2171 case EvqOut: return "out";
2172 case EvqInOut: return "inout";
2173 case EvqConstReadOnly: return "const";
2174 default: UNREACHABLE();
2175 }
2176
2177 return "";
2178}
2179
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002180TString OutputHLSL::typeString(const TType &type)
2181{
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002182 if (type.getBasicType() == EbtStruct)
2183 {
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002184 if (type.getTypeName() != "")
2185 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002186 return structLookup(type.getTypeName());
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002187 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00002188 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002189 {
2190 const TTypeList &fields = *type.getStruct();
2191
2192 TString string = "struct\n"
2193 "{\n";
2194
2195 for (unsigned int i = 0; i < fields.size(); i++)
2196 {
2197 const TType &field = *fields[i].type;
2198
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00002199 string += " " + typeString(field) + " " + decorate(field.getFieldName()) + arrayString(field) + ";\n";
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002200 }
2201
2202 string += "} ";
2203
2204 return string;
2205 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002206 }
2207 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002208 {
2209 switch (type.getNominalSize())
2210 {
2211 case 2: return "float2x2";
2212 case 3: return "float3x3";
2213 case 4: return "float4x4";
2214 }
2215 }
2216 else
2217 {
2218 switch (type.getBasicType())
2219 {
2220 case EbtFloat:
2221 switch (type.getNominalSize())
2222 {
2223 case 1: return "float";
2224 case 2: return "float2";
2225 case 3: return "float3";
2226 case 4: return "float4";
2227 }
2228 case EbtInt:
2229 switch (type.getNominalSize())
2230 {
2231 case 1: return "int";
2232 case 2: return "int2";
2233 case 3: return "int3";
2234 case 4: return "int4";
2235 }
2236 case EbtBool:
2237 switch (type.getNominalSize())
2238 {
2239 case 1: return "bool";
2240 case 2: return "bool2";
2241 case 3: return "bool3";
2242 case 4: return "bool4";
2243 }
2244 case EbtVoid:
2245 return "void";
2246 case EbtSampler2D:
2247 return "sampler2D";
2248 case EbtSamplerCube:
2249 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00002250 case EbtSamplerExternalOES:
2251 return "sampler2D";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002252 }
2253 }
2254
2255 UNIMPLEMENTED(); // FIXME
2256 return "<unknown type>";
2257}
2258
2259TString OutputHLSL::arrayString(const TType &type)
2260{
2261 if (!type.isArray())
2262 {
2263 return "";
2264 }
2265
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002266 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002267}
2268
2269TString OutputHLSL::initializer(const TType &type)
2270{
2271 TString string;
2272
daniel@transgaming.comead23042010-04-29 03:35:36 +00002273 for (int component = 0; component < type.getObjectSize(); component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002274 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002275 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002276
daniel@transgaming.comead23042010-04-29 03:35:36 +00002277 if (component < type.getObjectSize() - 1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002278 {
2279 string += ", ";
2280 }
2281 }
2282
daniel@transgaming.comead23042010-04-29 03:35:36 +00002283 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002284}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002285
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002286void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00002287{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002288 if (name == "")
2289 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00002290 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002291 }
2292
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00002293 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
2294 {
2295 return; // Already added
2296 }
2297
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002298 TType ctorType = type;
2299 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00002300 ctorType.setPrecision(EbpHigh);
2301 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00002302
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002303 TString ctorName = type.getStruct() ? decorate(name) : name;
2304
2305 typedef std::vector<TType> ParameterArray;
2306 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002307
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00002308 if (type.getStruct())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002309 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002310 mStructNames.insert(decorate(name));
2311
2312 TString structure;
2313 structure += "struct " + decorate(name) + "\n"
2314 "{\n";
2315
2316 const TTypeList &fields = *type.getStruct();
2317
2318 for (unsigned int i = 0; i < fields.size(); i++)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002319 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002320 const TType &field = *fields[i].type;
2321
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00002322 structure += " " + typeString(field) + " " + decorateField(field.getFieldName(), type) + arrayString(field) + ";\n";
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002323 }
2324
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002325 structure += "};\n";
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002326
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002327 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structure) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002328 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002329 mStructDeclarations.push_back(structure);
2330 }
2331
2332 for (unsigned int i = 0; i < fields.size(); i++)
2333 {
2334 ctorParameters.push_back(*fields[i].type);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002335 }
2336 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00002337 else if (parameters)
2338 {
2339 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
2340 {
2341 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
2342 }
2343 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002344 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00002345
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002346 TString constructor;
2347
2348 if (ctorType.getStruct())
2349 {
2350 constructor += ctorName + " " + ctorName + "_ctor(";
2351 }
2352 else // Built-in type
2353 {
2354 constructor += typeString(ctorType) + " " + ctorName + "(";
2355 }
2356
2357 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
2358 {
2359 const TType &type = ctorParameters[parameter];
2360
2361 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
2362
2363 if (parameter < ctorParameters.size() - 1)
2364 {
2365 constructor += ", ";
2366 }
2367 }
2368
2369 constructor += ")\n"
2370 "{\n";
2371
2372 if (ctorType.getStruct())
2373 {
2374 constructor += " " + ctorName + " structure = {";
2375 }
2376 else
2377 {
2378 constructor += " return " + typeString(ctorType) + "(";
2379 }
2380
2381 if (ctorType.isMatrix() && ctorParameters.size() == 1)
2382 {
2383 int dim = ctorType.getNominalSize();
2384 const TType &parameter = ctorParameters[0];
2385
2386 if (parameter.isScalar())
2387 {
2388 for (int row = 0; row < dim; row++)
2389 {
2390 for (int col = 0; col < dim; col++)
2391 {
2392 constructor += TString((row == col) ? "x0" : "0.0");
2393
2394 if (row < dim - 1 || col < dim - 1)
2395 {
2396 constructor += ", ";
2397 }
2398 }
2399 }
2400 }
2401 else if (parameter.isMatrix())
2402 {
2403 for (int row = 0; row < dim; row++)
2404 {
2405 for (int col = 0; col < dim; col++)
2406 {
2407 if (row < parameter.getNominalSize() && col < parameter.getNominalSize())
2408 {
2409 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
2410 }
2411 else
2412 {
2413 constructor += TString((row == col) ? "1.0" : "0.0");
2414 }
2415
2416 if (row < dim - 1 || col < dim - 1)
2417 {
2418 constructor += ", ";
2419 }
2420 }
2421 }
2422 }
2423 else UNREACHABLE();
2424 }
2425 else
2426 {
2427 int remainingComponents = ctorType.getObjectSize();
2428 int parameterIndex = 0;
2429
2430 while (remainingComponents > 0)
2431 {
2432 const TType &parameter = ctorParameters[parameterIndex];
2433 bool moreParameters = parameterIndex < (int)ctorParameters.size() - 1;
2434
2435 constructor += "x" + str(parameterIndex);
2436
2437 if (parameter.isScalar())
2438 {
2439 remainingComponents -= parameter.getObjectSize();
2440 }
2441 else if (parameter.isVector())
2442 {
2443 if (remainingComponents == parameter.getObjectSize() || moreParameters)
2444 {
2445 remainingComponents -= parameter.getObjectSize();
2446 }
2447 else if (remainingComponents < parameter.getNominalSize())
2448 {
2449 switch (remainingComponents)
2450 {
2451 case 1: constructor += ".x"; break;
2452 case 2: constructor += ".xy"; break;
2453 case 3: constructor += ".xyz"; break;
2454 case 4: constructor += ".xyzw"; break;
2455 default: UNREACHABLE();
2456 }
2457
2458 remainingComponents = 0;
2459 }
2460 else UNREACHABLE();
2461 }
2462 else if (parameter.isMatrix() || parameter.getStruct())
2463 {
2464 ASSERT(remainingComponents == parameter.getObjectSize() || moreParameters);
2465
2466 remainingComponents -= parameter.getObjectSize();
2467 }
2468 else UNREACHABLE();
2469
2470 if (moreParameters)
2471 {
2472 parameterIndex++;
2473 }
2474
2475 if (remainingComponents)
2476 {
2477 constructor += ", ";
2478 }
2479 }
2480 }
2481
2482 if (ctorType.getStruct())
2483 {
2484 constructor += "};\n"
2485 " return structure;\n"
2486 "}\n";
2487 }
2488 else
2489 {
2490 constructor += ");\n"
2491 "}\n";
2492 }
2493
daniel@transgaming.com63691862010-04-29 03:32:42 +00002494 mConstructors.insert(constructor);
2495}
2496
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002497const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2498{
2499 TInfoSinkBase &out = mBody;
2500
2501 if (type.getBasicType() == EbtStruct)
2502 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002503 out << structLookup(type.getTypeName()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002504
2505 const TTypeList *structure = type.getStruct();
2506
2507 for (size_t i = 0; i < structure->size(); i++)
2508 {
2509 const TType *fieldType = (*structure)[i].type;
2510
2511 constUnion = writeConstantUnion(*fieldType, constUnion);
2512
2513 if (i != structure->size() - 1)
2514 {
2515 out << ", ";
2516 }
2517 }
2518
2519 out << ")";
2520 }
2521 else
2522 {
2523 int size = type.getObjectSize();
2524 bool writeType = size > 1;
2525
2526 if (writeType)
2527 {
2528 out << typeString(type) << "(";
2529 }
2530
2531 for (int i = 0; i < size; i++, constUnion++)
2532 {
2533 switch (constUnion->getType())
2534 {
2535 case EbtFloat: out << constUnion->getFConst(); break;
2536 case EbtInt: out << constUnion->getIConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002537 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002538 default: UNREACHABLE();
2539 }
2540
2541 if (i != size - 1)
2542 {
2543 out << ", ";
2544 }
2545 }
2546
2547 if (writeType)
2548 {
2549 out << ")";
2550 }
2551 }
2552
2553 return constUnion;
2554}
2555
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002556TString OutputHLSL::scopeString(unsigned int depthLimit)
2557{
2558 TString string;
2559
2560 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
2561 {
2562 string += "_" + str(i);
2563 }
2564
2565 return string;
2566}
2567
2568TString OutputHLSL::scopedStruct(const TString &typeName)
2569{
2570 if (typeName == "")
2571 {
2572 return typeName;
2573 }
2574
2575 return typeName + scopeString(mScopeDepth);
2576}
2577
2578TString OutputHLSL::structLookup(const TString &typeName)
2579{
2580 for (int depth = mScopeDepth; depth >= 0; depth--)
2581 {
2582 TString scopedName = decorate(typeName + scopeString(depth));
2583
2584 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
2585 {
2586 if (*structName == scopedName)
2587 {
2588 return scopedName;
2589 }
2590 }
2591 }
2592
2593 UNREACHABLE(); // Should have found a matching constructor
2594
2595 return typeName;
2596}
2597
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002598TString OutputHLSL::decorate(const TString &string)
2599{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00002600 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002601 {
2602 return "_" + string;
2603 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00002604
2605 return string;
2606}
2607
apatrick@chromium.org65756022012-01-17 21:45:38 +00002608TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00002609{
apatrick@chromium.org65756022012-01-17 21:45:38 +00002610 if (type.isArray())
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002611 {
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00002612 return "ar_" + string; // Allows identifying arrays of size 1
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002613 }
apatrick@chromium.org65756022012-01-17 21:45:38 +00002614 else if (type.getBasicType() == EbtSamplerExternalOES)
2615 {
2616 return "ex_" + string;
2617 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00002618
2619 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002620}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00002621
2622TString OutputHLSL::decorateField(const TString &string, const TType &structure)
2623{
2624 if (structure.getTypeName().compare(0, 3, "gl_") != 0)
2625 {
2626 return decorate(string);
2627 }
2628
2629 return string;
2630}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002631}