blob: f306e04b80a441744843083e04769af54176f618 [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.com4f39fd92010-03-08 20:26:45 +000014
apatrick@chromium.orgbad6c2a2010-07-20 20:19:51 +000015#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000016#include <algorithm>
17
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000018namespace sh
19{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000020// Integer to TString conversion
21TString str(int i)
22{
23 char buffer[20];
kbr@chromium.orgddb6e8e2012-04-25 00:48:13 +000024 snprintf(buffer, sizeof(buffer), "%d", i);
daniel@transgaming.com005c7392010-04-15 20:45:27 +000025 return buffer;
26}
27
daniel@transgaming.com950f9932010-04-13 03:26:14 +000028OutputHLSL::OutputHLSL(TParseContext &context) : TIntermTraverser(true, true, true), mContext(context)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000029{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000030 mUnfoldShortCircuit = new UnfoldShortCircuit(context, this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +000031 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000032
daniel@transgaming.com5024cc42010-04-20 18:52:04 +000033 mUsesTexture2D = false;
34 mUsesTexture2D_bias = false;
35 mUsesTexture2DProj = false;
36 mUsesTexture2DProj_bias = false;
daniel@transgaming.com15795192011-05-11 15:36:20 +000037 mUsesTexture2DProjLod = false;
38 mUsesTexture2DLod = false;
daniel@transgaming.com5024cc42010-04-20 18:52:04 +000039 mUsesTextureCube = false;
40 mUsesTextureCube_bias = false;
daniel@transgaming.com15795192011-05-11 15:36:20 +000041 mUsesTextureCubeLod = false;
daniel@transgaming.coma54f5182012-05-31 01:20:16 +000042 mUsesTexture2DLod0 = false;
43 mUsesTexture2DLod0_bias = false;
44 mUsesTexture2DProjLod0 = false;
45 mUsesTexture2DProjLod0_bias = false;
46 mUsesTextureCubeLod0 = false;
47 mUsesTextureCubeLod0_bias = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +000048 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +000049 mUsesFragCoord = false;
50 mUsesPointCoord = false;
51 mUsesFrontFacing = false;
52 mUsesPointSize = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +000053 mUsesXor = false;
54 mUsesMod1 = false;
daniel@transgaming.com4229f592011-11-24 22:34:04 +000055 mUsesMod2v = false;
56 mUsesMod2f = false;
57 mUsesMod3v = false;
58 mUsesMod3f = false;
59 mUsesMod4v = false;
60 mUsesMod4f = false;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +000061 mUsesFaceforward1 = false;
62 mUsesFaceforward2 = false;
63 mUsesFaceforward3 = false;
64 mUsesFaceforward4 = false;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +000065 mUsesEqualMat2 = false;
66 mUsesEqualMat3 = false;
67 mUsesEqualMat4 = false;
68 mUsesEqualVec2 = false;
69 mUsesEqualVec3 = false;
70 mUsesEqualVec4 = false;
71 mUsesEqualIVec2 = false;
72 mUsesEqualIVec3 = false;
73 mUsesEqualIVec4 = false;
74 mUsesEqualBVec2 = false;
75 mUsesEqualBVec3 = false;
76 mUsesEqualBVec4 = false;
daniel@transgaming.com35342dc2012-02-28 02:01:22 +000077 mUsesAtan2_1 = false;
78 mUsesAtan2_2 = false;
79 mUsesAtan2_3 = false;
80 mUsesAtan2_4 = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +000081
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +000082 mScopeDepth = 0;
83
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +000084 mUniqueIndex = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000085}
86
daniel@transgaming.comb5875982010-04-15 20:44:53 +000087OutputHLSL::~OutputHLSL()
88{
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +000089 delete mUnfoldShortCircuit;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000090}
91
daniel@transgaming.com950f9932010-04-13 03:26:14 +000092void OutputHLSL::output()
93{
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +000094 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 +000095 header();
daniel@transgaming.com950f9932010-04-13 03:26:14 +000096
97 mContext.infoSink.obj << mHeader.c_str();
98 mContext.infoSink.obj << mBody.c_str();
daniel@transgaming.com950f9932010-04-13 03:26:14 +000099}
100
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000101TInfoSinkBase &OutputHLSL::getBodyStream()
102{
103 return mBody;
104}
105
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000106int OutputHLSL::vectorSize(const TType &type) const
107{
108 int elementSize = type.isMatrix() ? type.getNominalSize() : 1;
109 int arraySize = type.isArray() ? type.getArraySize() : 1;
110
111 return elementSize * arraySize;
112}
113
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000114void OutputHLSL::header()
115{
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000116 ShShaderType shaderType = mContext.shaderType;
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000117 TInfoSinkBase &out = mHeader;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000118
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000119 for (StructDeclarations::iterator structDeclaration = mStructDeclarations.begin(); structDeclaration != mStructDeclarations.end(); structDeclaration++)
daniel@transgaming.com51d0dc22010-04-29 03:39:11 +0000120 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000121 out << *structDeclaration;
daniel@transgaming.com51d0dc22010-04-29 03:39:11 +0000122 }
123
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000124 for (Constructors::iterator constructor = mConstructors.begin(); constructor != mConstructors.end(); constructor++)
daniel@transgaming.com51d0dc22010-04-29 03:39:11 +0000125 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +0000126 out << *constructor;
daniel@transgaming.com51d0dc22010-04-29 03:39:11 +0000127 }
128
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000129 if (shaderType == SH_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000130 {
131 TString uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000132 TString varyings;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000133
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000134 TSymbolTableLevel *symbols = mContext.symbolTable.getGlobalLevel();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135 int semanticIndex = 0;
136
137 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
138 {
139 const TSymbol *symbol = (*namedSymbol).second;
140 const TString &name = symbol->getName();
141
142 if (symbol->isVariable())
143 {
144 const TVariable *variable = static_cast<const TVariable*>(symbol);
145 const TType &type = variable->getType();
146 TQualifier qualifier = type.getQualifier();
147
148 if (qualifier == EvqUniform)
149 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000150 if (mReferencedUniforms.find(name.c_str()) != mReferencedUniforms.end())
151 {
apatrick@chromium.org65756022012-01-17 21:45:38 +0000152 uniforms += "uniform " + typeString(type) + " " + decorateUniform(name, type) + arrayString(type) + ";\n";
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000153 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000154 }
155 else if (qualifier == EvqVaryingIn || qualifier == EvqInvariantVaryingIn)
156 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000157 if (mReferencedVaryings.find(name.c_str()) != mReferencedVaryings.end())
158 {
159 // Program linking depends on this exact format
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000160 varyings += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000161
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000162 semanticIndex += type.isArray() ? type.getArraySize() : 1;
163 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000164 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +0000165 else if (qualifier == EvqGlobal || qualifier == EvqTemporary)
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000166 {
167 // Globals are declared and intialized as an aggregate node
168 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000169 else if (qualifier == EvqConst)
170 {
171 // Constants are repeated as literals where used
172 }
173 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000174 }
175 }
176
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000177 out << "// Varyings\n";
178 out << varyings;
179 out << "\n"
180 "static float4 gl_Color[1] = {float4(0, 0, 0, 0)};\n";
181
182 if (mUsesFragCoord)
183 {
184 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
185 }
186
187 if (mUsesPointCoord)
188 {
189 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
190 }
191
192 if (mUsesFrontFacing)
193 {
194 out << "static bool gl_FrontFacing = false;\n";
195 }
196
197 out << "\n";
198
199 if (mUsesFragCoord)
200 {
daniel@transgaming.comd9a54f92011-12-22 18:32:53 +0000201 out << "uniform float4 dx_Coord;\n"
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000202 "uniform float2 dx_Depth;\n";
203 }
204
205 if (mUsesFrontFacing)
206 {
207 out << "uniform bool dx_PointsOrLines;\n"
208 "uniform bool dx_FrontCCW;\n";
209 }
210
211 out << "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000212 out << uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000213 out << "\n";
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000214
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000215 // The texture fetch functions "flip" the Y coordinate in one way or another. This is because textures are stored
216 // according to the OpenGL convention, i.e. (0, 0) is "bottom left", rather than the D3D convention where (0, 0)
217 // is "top left". Since the HLSL texture fetch functions expect textures to be stored according to the D3D
218 // convention, the Y coordinate passed to these functions is adjusted to compensate.
219 //
220 // The simplest case is texture2D where the mapping is Y -> 1-Y, which maps [0, 1] -> [1, 0].
221 //
222 // The texture2DProj functions are more complicated because the projection divides by either Z or W. For the vec3
223 // case, the mapping is Y -> Z-Y or Y/Z -> 1-Y/Z, which again maps [0, 1] -> [1, 0].
224 //
225 // For cube textures the mapping is Y -> -Y, which maps [-1, 1] -> [1, -1]. This is not sufficient on its own for the
226 // +Y and -Y faces, which are now on the "wrong sides" of the cube. This is compensated for by exchanging the
227 // +Y and -Y faces everywhere else throughout the code.
228
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000229 if (mUsesTexture2D)
230 {
231 out << "float4 gl_texture2D(sampler2D s, float2 t)\n"
232 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000233 " return tex2D(s, float2(t.x, 1 - t.y));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000234 "}\n"
235 "\n";
236 }
237
238 if (mUsesTexture2D_bias)
239 {
240 out << "float4 gl_texture2D(sampler2D s, float2 t, float bias)\n"
241 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000242 " return tex2Dbias(s, float4(t.x, 1 - t.y, 0, bias));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000243 "}\n"
244 "\n";
245 }
246
247 if (mUsesTexture2DProj)
248 {
249 out << "float4 gl_texture2DProj(sampler2D s, float3 t)\n"
250 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000251 " return tex2Dproj(s, float4(t.x, t.z - t.y, 0, t.z));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000252 "}\n"
253 "\n"
254 "float4 gl_texture2DProj(sampler2D s, float4 t)\n"
255 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000256 " return tex2Dproj(s, float4(t.x, t.w - t.y, t.z, t.w));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000257 "}\n"
258 "\n";
259 }
260
261 if (mUsesTexture2DProj_bias)
262 {
263 out << "float4 gl_texture2DProj(sampler2D s, float3 t, float bias)\n"
264 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000265 " return tex2Dbias(s, float4(t.x / t.z, 1 - (t.y / t.z), 0, bias));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000266 "}\n"
267 "\n"
268 "float4 gl_texture2DProj(sampler2D s, float4 t, float bias)\n"
269 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000270 " return tex2Dbias(s, float4(t.x / t.w, 1 - (t.y / t.w), 0, bias));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000271 "}\n"
272 "\n";
273 }
274
275 if (mUsesTextureCube)
276 {
277 out << "float4 gl_textureCube(samplerCUBE s, float3 t)\n"
278 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000279 " return texCUBE(s, float3(t.x, -t.y, t.z));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000280 "}\n"
281 "\n";
282 }
283
284 if (mUsesTextureCube_bias)
285 {
286 out << "float4 gl_textureCube(samplerCUBE s, float3 t, float bias)\n"
287 "{\n"
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000288 " return texCUBEbias(s, float4(t.x, -t.y, t.z, bias));\n"
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000289 "}\n"
290 "\n";
291 }
daniel@transgaming.coma54f5182012-05-31 01:20:16 +0000292
293 // These *Lod0 intrinsics are not available in GL fragment shaders.
294 // They are used to sample using discontinuous texture coordinates.
295 if (mUsesTexture2DLod0)
296 {
297 out << "float4 gl_texture2DLod0(sampler2D s, float2 t)\n"
298 "{\n"
299 " return tex2Dlod(s, float4(t.x, 1 - t.y, 0, 0));\n"
300 "}\n"
301 "\n";
302 }
303
304 if (mUsesTexture2DLod0_bias)
305 {
306 out << "float4 gl_texture2DLod0(sampler2D s, float2 t, float bias)\n"
307 "{\n"
308 " return tex2Dlod(s, float4(t.x, 1 - t.y, 0, 0));\n"
309 "}\n"
310 "\n";
311 }
312
313 if (mUsesTexture2DProjLod0)
314 {
315 out << "float4 gl_texture2DProjLod0(sampler2D s, float3 t)\n"
316 "{\n"
317 " return tex2Dlod(s, float4(t.x / t.z, 1 - t.y / t.z, 0, 0));\n"
318 "}\n"
319 "\n"
320 "float4 gl_texture2DProjLod(sampler2D s, float4 t)\n"
321 "{\n"
322 " return tex2Dlod(s, float4(t.x / t.w, 1 - t.y / t.w, 0, 0));\n"
323 "}\n"
324 "\n";
325 }
326
327 if (mUsesTexture2DProjLod0_bias)
328 {
329 out << "float4 gl_texture2DProjLod0_bias(sampler2D s, float3 t, float bias)\n"
330 "{\n"
331 " return tex2Dlod(s, float4(t.x / t.z, 1 - t.y / t.z, 0, 0));\n"
332 "}\n"
333 "\n"
334 "float4 gl_texture2DProjLod_bias(sampler2D s, float4 t, float bias)\n"
335 "{\n"
336 " return tex2Dlod(s, float4(t.x / t.w, 1 - t.y / t.w, 0, 0));\n"
337 "}\n"
338 "\n";
339 }
340
341 if (mUsesTextureCubeLod0)
342 {
343 out << "float4 gl_textureCubeLod0(samplerCUBE s, float3 t)\n"
344 "{\n"
345 " return texCUBElod(s, float4(t.x, -t.y, t.z, 0));\n"
346 "}\n"
347 "\n";
348 }
349
350 if (mUsesTextureCubeLod0_bias)
351 {
352 out << "float4 gl_textureCubeLod0(samplerCUBE s, float3 t, float bias)\n"
353 "{\n"
354 " return texCUBElod(s, float4(t.x, -t.y, t.z, 0));\n"
355 "}\n"
356 "\n";
357 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000358 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000359 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000360 {
361 TString uniforms;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000362 TString attributes;
363 TString varyings;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000364
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000365 TSymbolTableLevel *symbols = mContext.symbolTable.getGlobalLevel();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000366
367 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
368 {
369 const TSymbol *symbol = (*namedSymbol).second;
370 const TString &name = symbol->getName();
371
372 if (symbol->isVariable())
373 {
374 const TVariable *variable = static_cast<const TVariable*>(symbol);
375 const TType &type = variable->getType();
376 TQualifier qualifier = type.getQualifier();
377
378 if (qualifier == EvqUniform)
379 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000380 if (mReferencedUniforms.find(name.c_str()) != mReferencedUniforms.end())
381 {
apatrick@chromium.org65756022012-01-17 21:45:38 +0000382 uniforms += "uniform " + typeString(type) + " " + decorateUniform(name, type) + arrayString(type) + ";\n";
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000383 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000384 }
385 else if (qualifier == EvqAttribute)
386 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000387 if (mReferencedAttributes.find(name.c_str()) != mReferencedAttributes.end())
388 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000389 attributes += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000390 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000391 }
392 else if (qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut)
393 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000394 if (mReferencedVaryings.find(name.c_str()) != mReferencedVaryings.end())
395 {
396 // Program linking depends on this exact format
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000397 varyings += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000398 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000399 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +0000400 else if (qualifier == EvqGlobal || qualifier == EvqTemporary)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000401 {
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000402 // Globals are declared and intialized as an aggregate node
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000404 else if (qualifier == EvqConst)
405 {
406 // Constants are repeated as literals where used
407 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000408 else UNREACHABLE();
409 }
410 }
411
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000412 out << "// Attributes\n";
413 out << attributes;
414 out << "\n"
415 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
416
417 if (mUsesPointSize)
418 {
419 out << "static float gl_PointSize = float(1);\n";
420 }
421
422 out << "\n"
423 "// Varyings\n";
424 out << varyings;
425 out << "\n"
426 "uniform float2 dx_HalfPixelSize;\n"
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +0000427 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000428 out << uniforms;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000429 out << "\n";
daniel@transgaming.com15795192011-05-11 15:36:20 +0000430
431 // The texture fetch functions "flip" the Y coordinate in one way or another. This is because textures are stored
432 // according to the OpenGL convention, i.e. (0, 0) is "bottom left", rather than the D3D convention where (0, 0)
433 // is "top left". Since the HLSL texture fetch functions expect textures to be stored according to the D3D
434 // convention, the Y coordinate passed to these functions is adjusted to compensate.
435 //
436 // The simplest case is texture2D where the mapping is Y -> 1-Y, which maps [0, 1] -> [1, 0].
437 //
438 // The texture2DProj functions are more complicated because the projection divides by either Z or W. For the vec3
439 // case, the mapping is Y -> Z-Y or Y/Z -> 1-Y/Z, which again maps [0, 1] -> [1, 0].
440 //
441 // For cube textures the mapping is Y -> -Y, which maps [-1, 1] -> [1, -1]. This is not sufficient on its own for the
442 // +Y and -Y faces, which are now on the "wrong sides" of the cube. This is compensated for by exchanging the
443 // +Y and -Y faces everywhere else throughout the code.
444
445 if (mUsesTexture2D)
446 {
447 out << "float4 gl_texture2D(sampler2D s, float2 t)\n"
448 "{\n"
449 " return tex2Dlod(s, float4(t.x, 1 - t.y, 0, 0));\n"
450 "}\n"
451 "\n";
452 }
453
454 if (mUsesTexture2DLod)
455 {
456 out << "float4 gl_texture2DLod(sampler2D s, float2 t, float lod)\n"
457 "{\n"
458 " return tex2Dlod(s, float4(t.x, 1 - t.y, 0, lod));\n"
459 "}\n"
460 "\n";
461 }
462
463 if (mUsesTexture2DProj)
464 {
465 out << "float4 gl_texture2DProj(sampler2D s, float3 t)\n"
466 "{\n"
467 " return tex2Dlod(s, float4(t.x / t.z, 1 - t.y / t.z, 0, 0));\n"
468 "}\n"
469 "\n"
470 "float4 gl_texture2DProj(sampler2D s, float4 t)\n"
471 "{\n"
472 " return tex2Dlod(s, float4(t.x / t.w, 1 - t.y / t.w, 0, 0));\n"
473 "}\n"
474 "\n";
475 }
476
477 if (mUsesTexture2DProjLod)
478 {
479 out << "float4 gl_texture2DProjLod(sampler2D s, float3 t, float lod)\n"
480 "{\n"
481 " return tex2Dlod(s, float4(t.x / t.z, 1 - t.y / t.z, 0, lod));\n"
482 "}\n"
483 "\n"
484 "float4 gl_texture2DProjLod(sampler2D s, float4 t, float lod)\n"
485 "{\n"
486 " return tex2Dlod(s, float4(t.x / t.w, 1 - t.y / t.w, 0, lod));\n"
487 "}\n"
488 "\n";
489 }
490
491 if (mUsesTextureCube)
492 {
493 out << "float4 gl_textureCube(samplerCUBE s, float3 t)\n"
494 "{\n"
495 " return texCUBElod(s, float4(t.x, -t.y, t.z, 0));\n"
496 "}\n"
497 "\n";
498 }
499
500 if (mUsesTextureCubeLod)
501 {
502 out << "float4 gl_textureCubeLod(samplerCUBE s, float3 t, float lod)\n"
503 "{\n"
504 " return texCUBElod(s, float4(t.x, -t.y, t.z, lod));\n"
505 "}\n"
506 "\n";
507 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000508 }
509
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000510 if (mUsesFragCoord)
511 {
512 out << "#define GL_USES_FRAG_COORD\n";
513 }
514
515 if (mUsesPointCoord)
516 {
517 out << "#define GL_USES_POINT_COORD\n";
518 }
519
520 if (mUsesFrontFacing)
521 {
522 out << "#define GL_USES_FRONT_FACING\n";
523 }
524
525 if (mUsesPointSize)
526 {
527 out << "#define GL_USES_POINT_SIZE\n";
528 }
529
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000530 if (mUsesDepthRange)
531 {
532 out << "struct gl_DepthRangeParameters\n"
533 "{\n"
534 " float near;\n"
535 " float far;\n"
536 " float diff;\n"
537 "};\n"
538 "\n"
daniel@transgaming.com31754962010-11-28 02:02:52 +0000539 "uniform float3 dx_DepthRange;"
540 "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000541 "\n";
542 }
543
544 if (mUsesXor)
545 {
546 out << "bool xor(bool p, bool q)\n"
547 "{\n"
548 " return (p || q) && !(p && q);\n"
549 "}\n"
550 "\n";
551 }
552
553 if (mUsesMod1)
554 {
555 out << "float mod(float x, float y)\n"
556 "{\n"
557 " return x - y * floor(x / y);\n"
558 "}\n"
559 "\n";
560 }
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000561
562 if (mUsesMod2v)
563 {
564 out << "float2 mod(float2 x, float2 y)\n"
565 "{\n"
566 " return x - y * floor(x / y);\n"
567 "}\n"
568 "\n";
569 }
570
571 if (mUsesMod2f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000572 {
573 out << "float2 mod(float2 x, float y)\n"
574 "{\n"
575 " return x - y * floor(x / y);\n"
576 "}\n"
577 "\n";
578 }
579
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000580 if (mUsesMod3v)
581 {
582 out << "float3 mod(float3 x, float3 y)\n"
583 "{\n"
584 " return x - y * floor(x / y);\n"
585 "}\n"
586 "\n";
587 }
588
589 if (mUsesMod3f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000590 {
591 out << "float3 mod(float3 x, float y)\n"
592 "{\n"
593 " return x - y * floor(x / y);\n"
594 "}\n"
595 "\n";
596 }
597
daniel@transgaming.com4229f592011-11-24 22:34:04 +0000598 if (mUsesMod4v)
599 {
600 out << "float4 mod(float4 x, float4 y)\n"
601 "{\n"
602 " return x - y * floor(x / y);\n"
603 "}\n"
604 "\n";
605 }
606
607 if (mUsesMod4f)
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000608 {
609 out << "float4 mod(float4 x, float y)\n"
610 "{\n"
611 " return x - y * floor(x / y);\n"
612 "}\n"
613 "\n";
614 }
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000615
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000616 if (mUsesFaceforward1)
617 {
618 out << "float faceforward(float N, float I, float Nref)\n"
619 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000620 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000621 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000622 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000623 " }\n"
624 " else\n"
625 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000626 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000627 " }\n"
628 "}\n"
629 "\n";
630 }
631
632 if (mUsesFaceforward2)
633 {
634 out << "float2 faceforward(float2 N, float2 I, float2 Nref)\n"
635 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000636 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000637 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000638 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000639 " }\n"
640 " else\n"
641 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000642 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000643 " }\n"
644 "}\n"
645 "\n";
646 }
647
648 if (mUsesFaceforward3)
649 {
650 out << "float3 faceforward(float3 N, float3 I, float3 Nref)\n"
651 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000652 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000653 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000654 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000655 " }\n"
656 " else\n"
657 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000658 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000659 " }\n"
660 "}\n"
661 "\n";
662 }
663
664 if (mUsesFaceforward4)
665 {
666 out << "float4 faceforward(float4 N, float4 I, float4 Nref)\n"
667 "{\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000668 " if(dot(Nref, I) >= 0)\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000669 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000670 " return -N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000671 " }\n"
672 " else\n"
673 " {\n"
daniel@transgaming.com3debd2b2010-05-13 02:07:34 +0000674 " return N;\n"
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +0000675 " }\n"
676 "}\n"
677 "\n";
678 }
679
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000680 if (mUsesEqualMat2)
681 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000682 out << "bool equal(float2x2 m, float2x2 n)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000683 "{\n"
684 " return m[0][0] == n[0][0] && m[0][1] == n[0][1] &&\n"
685 " m[1][0] == n[1][0] && m[1][1] == n[1][1];\n"
686 "}\n";
687 }
688
689 if (mUsesEqualMat3)
690 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000691 out << "bool equal(float3x3 m, float3x3 n)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000692 "{\n"
693 " return m[0][0] == n[0][0] && m[0][1] == n[0][1] && m[0][2] == n[0][2] &&\n"
694 " m[1][0] == n[1][0] && m[1][1] == n[1][1] && m[1][2] == n[1][2] &&\n"
695 " m[2][0] == n[2][0] && m[2][1] == n[2][1] && m[2][2] == n[2][2];\n"
696 "}\n";
697 }
698
699 if (mUsesEqualMat4)
700 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000701 out << "bool equal(float4x4 m, float4x4 n)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000702 "{\n"
703 " 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"
704 " 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"
705 " 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"
706 " 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"
707 "}\n";
708 }
709
710 if (mUsesEqualVec2)
711 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000712 out << "bool equal(float2 v, float2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000713 "{\n"
714 " return v.x == u.x && v.y == u.y;\n"
715 "}\n";
716 }
717
718 if (mUsesEqualVec3)
719 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000720 out << "bool equal(float3 v, float3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000721 "{\n"
722 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
723 "}\n";
724 }
725
726 if (mUsesEqualVec4)
727 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000728 out << "bool equal(float4 v, float4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000729 "{\n"
730 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
731 "}\n";
732 }
733
734 if (mUsesEqualIVec2)
735 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000736 out << "bool equal(int2 v, int2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000737 "{\n"
738 " return v.x == u.x && v.y == u.y;\n"
739 "}\n";
740 }
741
742 if (mUsesEqualIVec3)
743 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000744 out << "bool equal(int3 v, int3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000745 "{\n"
746 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
747 "}\n";
748 }
749
750 if (mUsesEqualIVec4)
751 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000752 out << "bool equal(int4 v, int4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000753 "{\n"
754 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
755 "}\n";
756 }
757
758 if (mUsesEqualBVec2)
759 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000760 out << "bool equal(bool2 v, bool2 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000761 "{\n"
762 " return v.x == u.x && v.y == u.y;\n"
763 "}\n";
764 }
765
766 if (mUsesEqualBVec3)
767 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000768 out << "bool equal(bool3 v, bool3 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000769 "{\n"
770 " return v.x == u.x && v.y == u.y && v.z == u.z;\n"
771 "}\n";
772 }
773
774 if (mUsesEqualBVec4)
775 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +0000776 out << "bool equal(bool4 v, bool4 u)\n"
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +0000777 "{\n"
778 " return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
779 "}\n";
780 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +0000781
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000782 if (mUsesAtan2_1)
daniel@transgaming.com0f189612010-05-07 13:03:36 +0000783 {
784 out << "float atanyx(float y, float x)\n"
785 "{\n"
786 " if(x == 0 && y == 0) x = 1;\n" // Avoid producing a NaN
787 " return atan2(y, x);\n"
788 "}\n";
789 }
daniel@transgaming.com35342dc2012-02-28 02:01:22 +0000790
791 if (mUsesAtan2_2)
792 {
793 out << "float2 atanyx(float2 y, float2 x)\n"
794 "{\n"
795 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
796 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
797 " return float2(atan2(y[0], x[0]), atan2(y[1], x[1]));\n"
798 "}\n";
799 }
800
801 if (mUsesAtan2_3)
802 {
803 out << "float3 atanyx(float3 y, float3 x)\n"
804 "{\n"
805 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
806 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
807 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
808 " return float3(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]));\n"
809 "}\n";
810 }
811
812 if (mUsesAtan2_4)
813 {
814 out << "float4 atanyx(float4 y, float4 x)\n"
815 "{\n"
816 " if(x[0] == 0 && y[0] == 0) x[0] = 1;\n"
817 " if(x[1] == 0 && y[1] == 0) x[1] = 1;\n"
818 " if(x[2] == 0 && y[2] == 0) x[2] = 1;\n"
819 " if(x[3] == 0 && y[3] == 0) x[3] = 1;\n"
820 " return float4(atan2(y[0], x[0]), atan2(y[1], x[1]), atan2(y[2], x[2]), atan2(y[3], x[3]));\n"
821 "}\n";
822 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000823}
824
825void OutputHLSL::visitSymbol(TIntermSymbol *node)
826{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000827 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828
829 TString name = node->getSymbol();
830
831 if (name == "gl_FragColor")
832 {
833 out << "gl_Color[0]";
834 }
835 else if (name == "gl_FragData")
836 {
837 out << "gl_Color";
838 }
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000839 else if (name == "gl_DepthRange")
840 {
841 mUsesDepthRange = true;
842 out << name;
843 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000844 else if (name == "gl_FragCoord")
845 {
846 mUsesFragCoord = true;
847 out << name;
848 }
849 else if (name == "gl_PointCoord")
850 {
851 mUsesPointCoord = true;
852 out << name;
853 }
854 else if (name == "gl_FrontFacing")
855 {
856 mUsesFrontFacing = true;
857 out << name;
858 }
859 else if (name == "gl_PointSize")
860 {
861 mUsesPointSize = true;
862 out << name;
863 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000864 else
865 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000866 TQualifier qualifier = node->getQualifier();
867
868 if (qualifier == EvqUniform)
869 {
870 mReferencedUniforms.insert(name.c_str());
apatrick@chromium.org65756022012-01-17 21:45:38 +0000871 out << decorateUniform(name, node->getType());
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000872 }
873 else if (qualifier == EvqAttribute)
874 {
875 mReferencedAttributes.insert(name.c_str());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000876 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000877 }
878 else if (qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut || qualifier == EvqVaryingIn || qualifier == EvqInvariantVaryingIn)
879 {
880 mReferencedVaryings.insert(name.c_str());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000881 out << decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000882 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000883 else
884 {
885 out << decorate(name);
886 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000887 }
888}
889
890bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
891{
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000892 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000893
894 switch (node->getOp())
895 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000896 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000897 case EOpInitialize:
898 if (visit == PreVisit)
899 {
900 // GLSL allows to write things like "float x = x;" where a new variable x is defined
901 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
902 // new variable is created before the assignment is evaluated), so we need to convert
903 // this to "float t = x, x = t;".
904
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000905 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
906 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000907
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000908 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
909 expression->traverse(&searchSymbol);
910 bool sameSymbol = searchSymbol.foundMatch();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000911
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000912 if (sameSymbol)
913 {
914 // Type already printed
915 out << "t" + str(mUniqueIndex) + " = ";
916 expression->traverse(this);
917 out << ", ";
918 symbolNode->traverse(this);
919 out << " = t" + str(mUniqueIndex);
920
921 mUniqueIndex++;
922 return false;
923 }
924 }
925 else if (visit == InVisit)
926 {
927 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000928 }
929 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000930 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
931 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
932 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
933 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
934 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
935 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000936 if (visit == PreVisit)
937 {
938 out << "(";
939 }
940 else if (visit == InVisit)
941 {
942 out << " = mul(";
943 node->getLeft()->traverse(this);
944 out << ", transpose(";
945 }
946 else
947 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +0000948 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000949 }
950 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000951 case EOpMatrixTimesMatrixAssign:
952 if (visit == PreVisit)
953 {
954 out << "(";
955 }
956 else if (visit == InVisit)
957 {
958 out << " = mul(";
959 node->getLeft()->traverse(this);
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000960 out << ", ";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000961 }
962 else
963 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +0000964 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000965 }
966 break;
967 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +0000968 case EOpIndexDirect: outputTriplet(visit, "", "[", "]"); break;
969 case EOpIndexIndirect: outputTriplet(visit, "", "[", "]"); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +0000970 case EOpIndexDirectStruct:
971 if (visit == InVisit)
972 {
daniel@transgaming.com2e793f02012-04-11 19:41:35 +0000973 out << "." + decorateField(node->getType().getFieldName(), node->getLeft()->getType());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +0000974
975 return false;
976 }
977 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000978 case EOpVectorSwizzle:
979 if (visit == InVisit)
980 {
981 out << ".";
982
983 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
984
985 if (swizzle)
986 {
987 TIntermSequence &sequence = swizzle->getSequence();
988
989 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
990 {
991 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
992
993 if (element)
994 {
995 int i = element->getUnionArrayPointer()[0].getIConst();
996
997 switch (i)
998 {
999 case 0: out << "x"; break;
1000 case 1: out << "y"; break;
1001 case 2: out << "z"; break;
1002 case 3: out << "w"; break;
1003 default: UNREACHABLE();
1004 }
1005 }
1006 else UNREACHABLE();
1007 }
1008 }
1009 else UNREACHABLE();
1010
1011 return false; // Fully processed
1012 }
1013 break;
1014 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
1015 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
1016 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
1017 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001018 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001019 case EOpNotEqual:
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001020 if (node->getLeft()->isScalar())
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001021 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001022 if (node->getOp() == EOpEqual)
1023 {
1024 outputTriplet(visit, "(", " == ", ")");
1025 }
1026 else
1027 {
1028 outputTriplet(visit, "(", " != ", ")");
1029 }
1030 }
1031 else if (node->getLeft()->getBasicType() == EbtStruct)
1032 {
1033 if (node->getOp() == EOpEqual)
1034 {
1035 out << "(";
1036 }
1037 else
1038 {
1039 out << "!(";
1040 }
1041
1042 const TTypeList *fields = node->getLeft()->getType().getStruct();
1043
1044 for (size_t i = 0; i < fields->size(); i++)
1045 {
1046 const TType *fieldType = (*fields)[i].type;
1047
1048 node->getLeft()->traverse(this);
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00001049 out << "." + decorateField(fieldType->getFieldName(), node->getLeft()->getType()) + " == ";
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001050 node->getRight()->traverse(this);
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00001051 out << "." + decorateField(fieldType->getFieldName(), node->getLeft()->getType());
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001052
1053 if (i < fields->size() - 1)
1054 {
1055 out << " && ";
1056 }
1057 }
1058
1059 out << ")";
1060
1061 return false;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001062 }
1063 else
1064 {
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001065 if (node->getLeft()->isMatrix())
1066 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001067 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001068 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001069 case 2: mUsesEqualMat2 = true; break;
1070 case 3: mUsesEqualMat3 = true; break;
1071 case 4: mUsesEqualMat4 = true; break;
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001072 default: UNREACHABLE();
1073 }
1074 }
1075 else if (node->getLeft()->isVector())
1076 {
1077 switch (node->getLeft()->getBasicType())
1078 {
1079 case EbtFloat:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001080 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001081 {
1082 case 2: mUsesEqualVec2 = true; break;
1083 case 3: mUsesEqualVec3 = true; break;
1084 case 4: mUsesEqualVec4 = true; break;
1085 default: UNREACHABLE();
1086 }
1087 break;
1088 case EbtInt:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001089 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001090 {
1091 case 2: mUsesEqualIVec2 = true; break;
1092 case 3: mUsesEqualIVec3 = true; break;
1093 case 4: mUsesEqualIVec4 = true; break;
1094 default: UNREACHABLE();
1095 }
1096 break;
1097 case EbtBool:
alokp@chromium.org58e54292010-08-24 21:40:03 +00001098 switch (node->getLeft()->getNominalSize())
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001099 {
1100 case 2: mUsesEqualBVec2 = true; break;
1101 case 3: mUsesEqualBVec3 = true; break;
1102 case 4: mUsesEqualBVec4 = true; break;
1103 default: UNREACHABLE();
1104 }
1105 break;
1106 default: UNREACHABLE();
1107 }
1108 }
1109 else UNREACHABLE();
1110
1111 if (node->getOp() == EOpEqual)
1112 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001113 outputTriplet(visit, "equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001114 }
1115 else
1116 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001117 outputTriplet(visit, "!equal(", ", ", ")");
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001118 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001119 }
1120 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001121 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1122 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1123 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1124 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1125 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001126 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001127 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
1128 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
daniel@transgaming.com69f084b2010-04-23 18:34:46 +00001129 case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001130 case EOpLogicalOr:
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001131 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001132 return false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001133 case EOpLogicalXor:
1134 mUsesXor = true;
1135 outputTriplet(visit, "xor(", ", ", ")");
1136 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001137 case EOpLogicalAnd:
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.com4f39fd92010-03-08 20:26:45 +00001140 default: UNREACHABLE();
1141 }
1142
1143 return true;
1144}
1145
1146bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1147{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001148 switch (node->getOp())
1149 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001150 case EOpNegative: outputTriplet(visit, "(-", "", ")"); break;
1151 case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1152 case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break;
1153 case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break;
1154 case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break;
1155 case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break;
1156 case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001157 case EOpConvIntToBool:
1158 case EOpConvFloatToBool:
1159 switch (node->getOperand()->getType().getNominalSize())
1160 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001161 case 1: outputTriplet(visit, "bool(", "", ")"); break;
1162 case 2: outputTriplet(visit, "bool2(", "", ")"); break;
1163 case 3: outputTriplet(visit, "bool3(", "", ")"); break;
1164 case 4: outputTriplet(visit, "bool4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001165 default: UNREACHABLE();
1166 }
1167 break;
1168 case EOpConvBoolToFloat:
1169 case EOpConvIntToFloat:
1170 switch (node->getOperand()->getType().getNominalSize())
1171 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001172 case 1: outputTriplet(visit, "float(", "", ")"); break;
1173 case 2: outputTriplet(visit, "float2(", "", ")"); break;
1174 case 3: outputTriplet(visit, "float3(", "", ")"); break;
1175 case 4: outputTriplet(visit, "float4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001176 default: UNREACHABLE();
1177 }
1178 break;
1179 case EOpConvFloatToInt:
1180 case EOpConvBoolToInt:
1181 switch (node->getOperand()->getType().getNominalSize())
1182 {
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001183 case 1: outputTriplet(visit, "int(", "", ")"); break;
1184 case 2: outputTriplet(visit, "int2(", "", ")"); break;
1185 case 3: outputTriplet(visit, "int3(", "", ")"); break;
1186 case 4: outputTriplet(visit, "int4(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001187 default: UNREACHABLE();
1188 }
1189 break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001190 case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break;
1191 case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break;
1192 case EOpSin: outputTriplet(visit, "sin(", "", ")"); break;
1193 case EOpCos: outputTriplet(visit, "cos(", "", ")"); break;
1194 case EOpTan: outputTriplet(visit, "tan(", "", ")"); break;
1195 case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break;
1196 case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break;
1197 case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break;
1198 case EOpExp: outputTriplet(visit, "exp(", "", ")"); break;
1199 case EOpLog: outputTriplet(visit, "log(", "", ")"); break;
1200 case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break;
1201 case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break;
1202 case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break;
1203 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break;
1204 case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break;
1205 case EOpSign: outputTriplet(visit, "sign(", "", ")"); break;
1206 case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break;
1207 case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break;
1208 case EOpFract: outputTriplet(visit, "frac(", "", ")"); break;
1209 case EOpLength: outputTriplet(visit, "length(", "", ")"); break;
1210 case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break;
alokp@chromium.org06098892010-08-26 19:36:42 +00001211 case EOpDFdx: outputTriplet(visit, "ddx(", "", ")"); break;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001212 case EOpDFdy: outputTriplet(visit, "(-ddy(", "", "))"); break;
alokp@chromium.org06098892010-08-26 19:36:42 +00001213 case EOpFwidth: outputTriplet(visit, "fwidth(", "", ")"); break;
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001214 case EOpAny: outputTriplet(visit, "any(", "", ")"); break;
1215 case EOpAll: outputTriplet(visit, "all(", "", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001216 default: UNREACHABLE();
1217 }
1218
1219 return true;
1220}
1221
1222bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1223{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001224 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001225
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001226 switch (node->getOp())
1227 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001228 case EOpSequence:
1229 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001230 if (mInsideFunction)
1231 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001232 outputLineDirective(node->getLine());
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001233 out << "{\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001234
1235 mScopeDepth++;
1236
1237 if (mScopeBracket.size() < mScopeDepth)
1238 {
1239 mScopeBracket.push_back(0); // New scope level
1240 }
1241 else
1242 {
1243 mScopeBracket[mScopeDepth - 1]++; // New scope at existing level
1244 }
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001245 }
1246
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001247 for (TIntermSequence::iterator sit = node->getSequence().begin(); sit != node->getSequence().end(); sit++)
1248 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001249 outputLineDirective((*sit)->getLine());
1250
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001251 traverseStatements(*sit);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001252
1253 out << ";\n";
1254 }
1255
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001256 if (mInsideFunction)
1257 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001258 outputLineDirective(node->getEndLine());
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001259 out << "}\n";
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001260
1261 mScopeDepth--;
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001262 }
1263
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001264 return false;
1265 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001266 case EOpDeclaration:
1267 if (visit == PreVisit)
1268 {
1269 TIntermSequence &sequence = node->getSequence();
1270 TIntermTyped *variable = sequence[0]->getAsTyped();
1271 bool visit = true;
1272
daniel@transgaming.comd25ab252010-03-30 03:36:26 +00001273 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001274 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001275 if (variable->getType().getStruct())
1276 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001277 addConstructor(variable->getType(), scopedStruct(variable->getType().getTypeName()), NULL);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001278 }
1279
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001280 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001281 {
daniel@transgaming.comd2cf25d2010-04-22 16:27:35 +00001282 if (!mInsideFunction)
daniel@transgaming.comd91cfe72010-04-13 03:26:17 +00001283 {
1284 out << "static ";
1285 }
1286
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001287 out << typeString(variable->getType()) + " ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001288
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001289 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001290 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001291 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001292
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001293 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001294 {
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001295 symbol->traverse(this);
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001296 out << arrayString(symbol->getType());
daniel@transgaming.com7127f202010-04-15 20:45:22 +00001297 out << " = " + initializer(variable->getType());
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001298 }
1299 else
1300 {
1301 (*sit)->traverse(this);
1302 }
1303
1304 if (visit && this->inVisit)
1305 {
1306 if (*sit != sequence.back())
1307 {
1308 visit = this->visitAggregate(InVisit, node);
1309 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001310 }
1311 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001312
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001313 if (visit && this->postVisit)
1314 {
1315 this->visitAggregate(PostVisit, node);
1316 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001317 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001318 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1319 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001320 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001321 }
1322 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001323 }
1324
1325 return false;
1326 }
1327 else if (visit == InVisit)
1328 {
1329 out << ", ";
1330 }
1331 break;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001332 case EOpPrototype:
1333 if (visit == PreVisit)
1334 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001335 out << typeString(node->getType()) << " " << decorate(node->getName()) << "(";
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001336
1337 TIntermSequence &arguments = node->getSequence();
1338
1339 for (unsigned int i = 0; i < arguments.size(); i++)
1340 {
1341 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
1342
1343 if (symbol)
1344 {
1345 out << argumentString(symbol);
1346
1347 if (i < arguments.size() - 1)
1348 {
1349 out << ", ";
1350 }
1351 }
1352 else UNREACHABLE();
1353 }
1354
1355 out << ");\n";
1356
1357 return false;
1358 }
1359 break;
daniel@transgaming.comed2180d2012-03-26 17:08:54 +00001360 case EOpComma: outputTriplet(visit, "(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001361 case EOpFunction:
1362 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001363 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001364
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001365 out << typeString(node->getType()) << " ";
1366
1367 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001368 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001369 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001370 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001371 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001372 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001373 out << decorate(name) << "(";
1374 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001375
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001376 TIntermSequence &sequence = node->getSequence();
1377 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
1378
1379 for (unsigned int i = 0; i < arguments.size(); i++)
1380 {
1381 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
1382
1383 if (symbol)
1384 {
1385 if (symbol->getType().getStruct())
1386 {
1387 addConstructor(symbol->getType(), scopedStruct(symbol->getType().getTypeName()), NULL);
1388 }
1389
1390 out << argumentString(symbol);
1391
1392 if (i < arguments.size() - 1)
1393 {
1394 out << ", ";
1395 }
1396 }
1397 else UNREACHABLE();
1398 }
1399
1400 out << ")\n"
1401 "{\n";
1402
1403 if (sequence.size() > 1)
1404 {
1405 mInsideFunction = true;
1406 sequence[1]->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001407 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001408 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001409
1410 out << "}\n";
1411
1412 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001413 }
1414 break;
1415 case EOpFunctionCall:
1416 {
1417 if (visit == PreVisit)
1418 {
alokp@chromium.org43884872010-03-30 00:08:52 +00001419 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001420
1421 if (node->isUserDefined())
1422 {
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00001423 out << decorate(name) << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001424 }
1425 else
1426 {
1427 if (name == "texture2D")
1428 {
1429 if (node->getSequence().size() == 2)
1430 {
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001431 mUsesTexture2D = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001432 }
1433 else if (node->getSequence().size() == 3)
1434 {
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001435 mUsesTexture2D_bias = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001436 }
1437 else UNREACHABLE();
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001438
1439 out << "gl_texture2D(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001440 }
1441 else if (name == "texture2DProj")
1442 {
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001443 if (node->getSequence().size() == 2)
1444 {
1445 mUsesTexture2DProj = true;
1446 }
1447 else if (node->getSequence().size() == 3)
1448 {
1449 mUsesTexture2DProj_bias = true;
1450 }
1451 else UNREACHABLE();
1452
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001453 out << "gl_texture2DProj(";
1454 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001455 else if (name == "textureCube")
1456 {
1457 if (node->getSequence().size() == 2)
1458 {
1459 mUsesTextureCube = true;
1460 }
1461 else if (node->getSequence().size() == 3)
1462 {
1463 mUsesTextureCube_bias = true;
1464 }
1465 else UNREACHABLE();
1466
1467 out << "gl_textureCube(";
1468 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001469 else if (name == "texture2DLod")
1470 {
daniel@transgaming.com15795192011-05-11 15:36:20 +00001471 if (node->getSequence().size() == 3)
1472 {
1473 mUsesTexture2DLod = true;
1474 }
1475 else UNREACHABLE();
1476
1477 out << "gl_texture2DLod(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001478 }
1479 else if (name == "texture2DProjLod")
1480 {
daniel@transgaming.com15795192011-05-11 15:36:20 +00001481 if (node->getSequence().size() == 3)
1482 {
1483 mUsesTexture2DProjLod = true;
1484 }
1485 else UNREACHABLE();
1486
1487 out << "gl_texture2DProjLod(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001488 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +00001489 else if (name == "textureCubeLod")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001490 {
daniel@transgaming.com15795192011-05-11 15:36:20 +00001491 if (node->getSequence().size() == 3)
1492 {
1493 mUsesTextureCubeLod = true;
1494 }
1495 else UNREACHABLE();
1496
1497 out << "gl_textureCubeLod(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001498 }
daniel@transgaming.comec55d292010-04-15 20:44:49 +00001499 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001500 }
1501 }
1502 else if (visit == InVisit)
1503 {
1504 out << ", ";
1505 }
1506 else
1507 {
1508 out << ")";
1509 }
1510 }
1511 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001512 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001513 case EOpConstructFloat:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001514 addConstructor(node->getType(), "vec1", &node->getSequence());
1515 outputTriplet(visit, "vec1(", "", ")");
1516 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001517 case EOpConstructVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001518 addConstructor(node->getType(), "vec2", &node->getSequence());
1519 outputTriplet(visit, "vec2(", ", ", ")");
1520 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001521 case EOpConstructVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001522 addConstructor(node->getType(), "vec3", &node->getSequence());
1523 outputTriplet(visit, "vec3(", ", ", ")");
1524 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001525 case EOpConstructVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001526 addConstructor(node->getType(), "vec4", &node->getSequence());
1527 outputTriplet(visit, "vec4(", ", ", ")");
1528 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001529 case EOpConstructBool:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001530 addConstructor(node->getType(), "bvec1", &node->getSequence());
1531 outputTriplet(visit, "bvec1(", "", ")");
1532 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001533 case EOpConstructBVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001534 addConstructor(node->getType(), "bvec2", &node->getSequence());
1535 outputTriplet(visit, "bvec2(", ", ", ")");
1536 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001537 case EOpConstructBVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001538 addConstructor(node->getType(), "bvec3", &node->getSequence());
1539 outputTriplet(visit, "bvec3(", ", ", ")");
1540 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001541 case EOpConstructBVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001542 addConstructor(node->getType(), "bvec4", &node->getSequence());
1543 outputTriplet(visit, "bvec4(", ", ", ")");
1544 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001545 case EOpConstructInt:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001546 addConstructor(node->getType(), "ivec1", &node->getSequence());
1547 outputTriplet(visit, "ivec1(", "", ")");
1548 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001549 case EOpConstructIVec2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001550 addConstructor(node->getType(), "ivec2", &node->getSequence());
1551 outputTriplet(visit, "ivec2(", ", ", ")");
1552 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001553 case EOpConstructIVec3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001554 addConstructor(node->getType(), "ivec3", &node->getSequence());
1555 outputTriplet(visit, "ivec3(", ", ", ")");
1556 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001557 case EOpConstructIVec4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001558 addConstructor(node->getType(), "ivec4", &node->getSequence());
1559 outputTriplet(visit, "ivec4(", ", ", ")");
1560 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001561 case EOpConstructMat2:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001562 addConstructor(node->getType(), "mat2", &node->getSequence());
1563 outputTriplet(visit, "mat2(", ", ", ")");
1564 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001565 case EOpConstructMat3:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001566 addConstructor(node->getType(), "mat3", &node->getSequence());
1567 outputTriplet(visit, "mat3(", ", ", ")");
1568 break;
daniel@transgaming.com63691862010-04-29 03:32:42 +00001569 case EOpConstructMat4:
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00001570 addConstructor(node->getType(), "mat4", &node->getSequence());
1571 outputTriplet(visit, "mat4(", ", ", ")");
1572 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001573 case EOpConstructStruct:
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00001574 addConstructor(node->getType(), scopedStruct(node->getType().getTypeName()), &node->getSequence());
1575 outputTriplet(visit, structLookup(node->getType().getTypeName()) + "_ctor(", ", ", ")");
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001576 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001577 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
1578 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
1579 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
1580 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
1581 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
1582 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001583 case EOpMod:
1584 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001585 // We need to look at the number of components in both arguments
1586 switch (node->getSequence()[0]->getAsTyped()->getNominalSize() * 10
1587 + node->getSequence()[1]->getAsTyped()->getNominalSize())
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001588 {
daniel@transgaming.com4229f592011-11-24 22:34:04 +00001589 case 11: mUsesMod1 = true; break;
1590 case 22: mUsesMod2v = true; break;
1591 case 21: mUsesMod2f = true; break;
1592 case 33: mUsesMod3v = true; break;
1593 case 31: mUsesMod3f = true; break;
1594 case 44: mUsesMod4v = true; break;
1595 case 41: mUsesMod4f = true; break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001596 default: UNREACHABLE();
1597 }
1598
1599 outputTriplet(visit, "mod(", ", ", ")");
1600 }
1601 break;
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001602 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001603 case EOpAtan:
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001604 ASSERT(node->getSequence().size() == 2); // atan(x) is a unary operator
daniel@transgaming.com35342dc2012-02-28 02:01:22 +00001605 switch (node->getSequence()[0]->getAsTyped()->getNominalSize())
1606 {
1607 case 1: mUsesAtan2_1 = true; break;
1608 case 2: mUsesAtan2_2 = true; break;
1609 case 3: mUsesAtan2_3 = true; break;
1610 case 4: mUsesAtan2_4 = true; break;
1611 default: UNREACHABLE();
1612 }
daniel@transgaming.com0f189612010-05-07 13:03:36 +00001613 outputTriplet(visit, "atanyx(", ", ", ")");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001614 break;
1615 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
1616 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
1617 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
1618 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
1619 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
1620 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
1621 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
1622 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
1623 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001624 case EOpFaceForward:
1625 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001626 switch (node->getSequence()[0]->getAsTyped()->getNominalSize()) // Number of components in the first argument
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001627 {
1628 case 1: mUsesFaceforward1 = true; break;
1629 case 2: mUsesFaceforward2 = true; break;
1630 case 3: mUsesFaceforward3 = true; break;
1631 case 4: mUsesFaceforward4 = true; break;
1632 default: UNREACHABLE();
1633 }
1634
1635 outputTriplet(visit, "faceforward(", ", ", ")");
1636 }
1637 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001638 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
1639 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
1640 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001641 default: UNREACHABLE();
1642 }
1643
1644 return true;
1645}
1646
1647bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
1648{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001649 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001650
alokp@chromium.org60fe4072010-03-29 20:58:29 +00001651 if (node->usesTernaryOperator())
1652 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001653 out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex();
alokp@chromium.org60fe4072010-03-29 20:58:29 +00001654 }
1655 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001656 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001657 mUnfoldShortCircuit->traverse(node->getCondition());
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001658
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001659 out << "if(";
1660
1661 node->getCondition()->traverse(this);
1662
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001663 out << ")\n";
1664
1665 outputLineDirective(node->getLine());
1666 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001667
daniel@transgaming.combb885322010-04-15 20:45:24 +00001668 if (node->getTrueBlock())
1669 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001670 traverseStatements(node->getTrueBlock());
daniel@transgaming.combb885322010-04-15 20:45:24 +00001671 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001672
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001673 outputLineDirective(node->getLine());
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001674 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001675
1676 if (node->getFalseBlock())
1677 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001678 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001679
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001680 outputLineDirective(node->getFalseBlock()->getLine());
1681 out << "{\n";
1682
1683 outputLineDirective(node->getFalseBlock()->getLine());
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001684 traverseStatements(node->getFalseBlock());
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001685
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001686 outputLineDirective(node->getFalseBlock()->getLine());
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001687 out << ";\n}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001688 }
1689 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001690
1691 return false;
1692}
1693
1694void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
1695{
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001696 writeConstantUnion(node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001697}
1698
1699bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
1700{
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001701 if (handleExcessiveLoop(node))
1702 {
1703 return false;
1704 }
1705
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001706 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001707
alokp@chromium.org52813552010-11-16 18:36:09 +00001708 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001709 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00001710 out << "{do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001711
1712 outputLineDirective(node->getLine());
1713 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001714 }
1715 else
1716 {
daniel@transgaming.com2a073de2012-03-09 21:56:43 +00001717 out << "{for(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001718
1719 if (node->getInit())
1720 {
1721 node->getInit()->traverse(this);
1722 }
1723
1724 out << "; ";
1725
alokp@chromium.org52813552010-11-16 18:36:09 +00001726 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001727 {
alokp@chromium.org52813552010-11-16 18:36:09 +00001728 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001729 }
1730
1731 out << "; ";
1732
alokp@chromium.org52813552010-11-16 18:36:09 +00001733 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001734 {
alokp@chromium.org52813552010-11-16 18:36:09 +00001735 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001736 }
1737
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001738 out << ")\n";
1739
1740 outputLineDirective(node->getLine());
1741 out << "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001742 }
1743
1744 if (node->getBody())
1745 {
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001746 traverseStatements(node->getBody());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001747 }
1748
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001749 outputLineDirective(node->getLine());
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00001750 out << ";}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001751
alokp@chromium.org52813552010-11-16 18:36:09 +00001752 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001753 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001754 outputLineDirective(node->getCondition()->getLine());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001755 out << "while(\n";
1756
alokp@chromium.org52813552010-11-16 18:36:09 +00001757 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001758
daniel@transgaming.com73536982012-03-21 20:45:49 +00001759 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001760 }
1761
daniel@transgaming.com73536982012-03-21 20:45:49 +00001762 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001763
1764 return false;
1765}
1766
1767bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
1768{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001769 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001770
1771 switch (node->getFlowOp())
1772 {
apatrick@chromium.org05a5d8e2011-02-16 19:07:20 +00001773 case EOpKill: outputTriplet(visit, "discard;\n", "", ""); break;
1774 case EOpBreak: outputTriplet(visit, "break;\n", "", ""); break;
1775 case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001776 case EOpReturn:
1777 if (visit == PreVisit)
1778 {
1779 if (node->getExpression())
1780 {
1781 out << "return ";
1782 }
1783 else
1784 {
1785 out << "return;\n";
1786 }
1787 }
1788 else if (visit == PostVisit)
1789 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001790 if (node->getExpression())
1791 {
1792 out << ";\n";
1793 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001794 }
1795 break;
1796 default: UNREACHABLE();
1797 }
1798
1799 return true;
1800}
1801
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001802void OutputHLSL::traverseStatements(TIntermNode *node)
1803{
1804 if (isSingleStatement(node))
1805 {
daniel@transgaming.comf8f8f362012-04-28 00:35:00 +00001806 mUnfoldShortCircuit->traverse(node);
daniel@transgaming.com44fffee2012-04-28 00:34:20 +00001807 }
1808
1809 node->traverse(this);
1810}
1811
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001812bool OutputHLSL::isSingleStatement(TIntermNode *node)
1813{
1814 TIntermAggregate *aggregate = node->getAsAggregate();
1815
1816 if (aggregate)
1817 {
1818 if (aggregate->getOp() == EOpSequence)
1819 {
1820 return false;
1821 }
1822 else
1823 {
1824 for (TIntermSequence::iterator sit = aggregate->getSequence().begin(); sit != aggregate->getSequence().end(); sit++)
1825 {
1826 if (!isSingleStatement(*sit))
1827 {
1828 return false;
1829 }
1830 }
1831
1832 return true;
1833 }
1834 }
1835
1836 return true;
1837}
1838
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00001839// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
1840// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001841bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node)
1842{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00001843 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com950f9932010-04-13 03:26:14 +00001844 TInfoSinkBase &out = mBody;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001845
1846 // Parse loops of the form:
1847 // for(int index = initial; index [comparator] limit; index += increment)
1848 TIntermSymbol *index = NULL;
1849 TOperator comparator = EOpNull;
1850 int initial = 0;
1851 int limit = 0;
1852 int increment = 0;
1853
1854 // Parse index name and intial value
1855 if (node->getInit())
1856 {
1857 TIntermAggregate *init = node->getInit()->getAsAggregate();
1858
1859 if (init)
1860 {
1861 TIntermSequence &sequence = init->getSequence();
1862 TIntermTyped *variable = sequence[0]->getAsTyped();
1863
1864 if (variable && variable->getQualifier() == EvqTemporary)
1865 {
1866 TIntermBinary *assign = variable->getAsBinaryNode();
1867
1868 if (assign->getOp() == EOpInitialize)
1869 {
1870 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
1871 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
1872
1873 if (symbol && constant)
1874 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001875 if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001876 {
1877 index = symbol;
1878 initial = constant->getUnionArrayPointer()[0].getIConst();
1879 }
1880 }
1881 }
1882 }
1883 }
1884 }
1885
1886 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00001887 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001888 {
alokp@chromium.org52813552010-11-16 18:36:09 +00001889 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001890
1891 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
1892 {
1893 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
1894
1895 if (constant)
1896 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001897 if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001898 {
1899 comparator = test->getOp();
1900 limit = constant->getUnionArrayPointer()[0].getIConst();
1901 }
1902 }
1903 }
1904 }
1905
1906 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00001907 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001908 {
alokp@chromium.org52813552010-11-16 18:36:09 +00001909 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
1910 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001911
1912 if (binaryTerminal)
1913 {
1914 TOperator op = binaryTerminal->getOp();
1915 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
1916
1917 if (constant)
1918 {
alokp@chromium.org58e54292010-08-24 21:40:03 +00001919 if (constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001920 {
1921 int value = constant->getUnionArrayPointer()[0].getIConst();
1922
1923 switch (op)
1924 {
1925 case EOpAddAssign: increment = value; break;
1926 case EOpSubAssign: increment = -value; break;
1927 default: UNIMPLEMENTED();
1928 }
1929 }
1930 }
1931 }
1932 else if (unaryTerminal)
1933 {
1934 TOperator op = unaryTerminal->getOp();
1935
1936 switch (op)
1937 {
1938 case EOpPostIncrement: increment = 1; break;
1939 case EOpPostDecrement: increment = -1; break;
1940 case EOpPreIncrement: increment = 1; break;
1941 case EOpPreDecrement: increment = -1; break;
1942 default: UNIMPLEMENTED();
1943 }
1944 }
1945 }
1946
1947 if (index != NULL && comparator != EOpNull && increment != 0)
1948 {
1949 if (comparator == EOpLessThanEqual)
1950 {
1951 comparator = EOpLessThan;
1952 limit += 1;
1953 }
1954
1955 if (comparator == EOpLessThan)
1956 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00001957 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001958
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00001959 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001960 {
1961 return false; // Not an excessive loop
1962 }
1963
1964 while (iterations > 0)
1965 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00001966 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001967
1968 // for(int index = initial; index < clampedLimit; index += increment)
1969
1970 out << "for(int ";
1971 index->traverse(this);
1972 out << " = ";
1973 out << initial;
1974
1975 out << "; ";
1976 index->traverse(this);
1977 out << " < ";
1978 out << clampedLimit;
1979
1980 out << "; ";
1981 index->traverse(this);
1982 out << " += ";
1983 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001984 out << ")\n";
1985
1986 outputLineDirective(node->getLine());
1987 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001988
1989 if (node->getBody())
1990 {
1991 node->getBody()->traverse(this);
1992 }
1993
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001994 outputLineDirective(node->getLine());
daniel@transgaming.com7fb81e82011-09-23 18:20:46 +00001995 out << ";}\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001996
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00001997 initial += MAX_LOOP_ITERATIONS * increment;
1998 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00001999 }
2000
2001 return true;
2002 }
2003 else UNIMPLEMENTED();
2004 }
2005
2006 return false; // Not handled as an excessive loop
2007}
2008
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002009void OutputHLSL::outputTriplet(Visit visit, const TString &preString, const TString &inString, const TString &postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002010{
daniel@transgaming.com950f9932010-04-13 03:26:14 +00002011 TInfoSinkBase &out = mBody;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002012
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002013 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002014 {
2015 out << preString;
2016 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002017 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002018 {
2019 out << inString;
2020 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002021 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002022 {
2023 out << postString;
2024 }
2025}
2026
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002027void OutputHLSL::outputLineDirective(int line)
2028{
2029 if ((mContext.compileOptions & SH_LINE_DIRECTIVES) && (line > 0))
2030 {
baustin@google.com8ab69842011-06-02 21:53:45 +00002031 mBody << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002032 mBody << "#line " << line;
2033
2034 if (mContext.sourcePath)
2035 {
2036 mBody << " \"" << mContext.sourcePath << "\"";
2037 }
2038
2039 mBody << "\n";
2040 }
2041}
2042
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002043TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2044{
2045 TQualifier qualifier = symbol->getQualifier();
2046 const TType &type = symbol->getType();
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002047 TString name = symbol->getSymbol();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002048
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002049 if (name.empty()) // HLSL demands named arguments, also for prototypes
2050 {
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00002051 name = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002052 }
2053 else
2054 {
2055 name = decorate(name);
2056 }
2057
2058 return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002059}
2060
2061TString OutputHLSL::qualifierString(TQualifier qualifier)
2062{
2063 switch(qualifier)
2064 {
2065 case EvqIn: return "in";
2066 case EvqOut: return "out";
2067 case EvqInOut: return "inout";
2068 case EvqConstReadOnly: return "const";
2069 default: UNREACHABLE();
2070 }
2071
2072 return "";
2073}
2074
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002075TString OutputHLSL::typeString(const TType &type)
2076{
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002077 if (type.getBasicType() == EbtStruct)
2078 {
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002079 if (type.getTypeName() != "")
2080 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002081 return structLookup(type.getTypeName());
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002082 }
daniel@transgaming.com6b998402010-05-04 03:35:07 +00002083 else // Nameless structure, define in place
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002084 {
2085 const TTypeList &fields = *type.getStruct();
2086
2087 TString string = "struct\n"
2088 "{\n";
2089
2090 for (unsigned int i = 0; i < fields.size(); i++)
2091 {
2092 const TType &field = *fields[i].type;
2093
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00002094 string += " " + typeString(field) + " " + decorate(field.getFieldName()) + arrayString(field) + ";\n";
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002095 }
2096
2097 string += "} ";
2098
2099 return string;
2100 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002101 }
2102 else if (type.isMatrix())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002103 {
2104 switch (type.getNominalSize())
2105 {
2106 case 2: return "float2x2";
2107 case 3: return "float3x3";
2108 case 4: return "float4x4";
2109 }
2110 }
2111 else
2112 {
2113 switch (type.getBasicType())
2114 {
2115 case EbtFloat:
2116 switch (type.getNominalSize())
2117 {
2118 case 1: return "float";
2119 case 2: return "float2";
2120 case 3: return "float3";
2121 case 4: return "float4";
2122 }
2123 case EbtInt:
2124 switch (type.getNominalSize())
2125 {
2126 case 1: return "int";
2127 case 2: return "int2";
2128 case 3: return "int3";
2129 case 4: return "int4";
2130 }
2131 case EbtBool:
2132 switch (type.getNominalSize())
2133 {
2134 case 1: return "bool";
2135 case 2: return "bool2";
2136 case 3: return "bool3";
2137 case 4: return "bool4";
2138 }
2139 case EbtVoid:
2140 return "void";
2141 case EbtSampler2D:
2142 return "sampler2D";
2143 case EbtSamplerCube:
2144 return "samplerCUBE";
apatrick@chromium.org65756022012-01-17 21:45:38 +00002145 case EbtSamplerExternalOES:
2146 return "sampler2D";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002147 }
2148 }
2149
2150 UNIMPLEMENTED(); // FIXME
2151 return "<unknown type>";
2152}
2153
2154TString OutputHLSL::arrayString(const TType &type)
2155{
2156 if (!type.isArray())
2157 {
2158 return "";
2159 }
2160
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002161 return "[" + str(type.getArraySize()) + "]";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002162}
2163
2164TString OutputHLSL::initializer(const TType &type)
2165{
2166 TString string;
2167
daniel@transgaming.comead23042010-04-29 03:35:36 +00002168 for (int component = 0; component < type.getObjectSize(); component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002169 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002170 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002171
daniel@transgaming.comead23042010-04-29 03:35:36 +00002172 if (component < type.getObjectSize() - 1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002173 {
2174 string += ", ";
2175 }
2176 }
2177
daniel@transgaming.comead23042010-04-29 03:35:36 +00002178 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002179}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002180
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002181void OutputHLSL::addConstructor(const TType &type, const TString &name, const TIntermSequence *parameters)
daniel@transgaming.com63691862010-04-29 03:32:42 +00002182{
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002183 if (name == "")
2184 {
daniel@transgaming.com6b998402010-05-04 03:35:07 +00002185 return; // Nameless structures don't have constructors
daniel@transgaming.coma637e552010-04-29 03:39:08 +00002186 }
2187
daniel@transgaming.com43eecdc2012-03-20 20:10:33 +00002188 if (type.getStruct() && mStructNames.find(decorate(name)) != mStructNames.end())
2189 {
2190 return; // Already added
2191 }
2192
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002193 TType ctorType = type;
2194 ctorType.clearArrayness();
alokp@chromium.org58e54292010-08-24 21:40:03 +00002195 ctorType.setPrecision(EbpHigh);
2196 ctorType.setQualifier(EvqTemporary);
daniel@transgaming.com63691862010-04-29 03:32:42 +00002197
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002198 TString ctorName = type.getStruct() ? decorate(name) : name;
2199
2200 typedef std::vector<TType> ParameterArray;
2201 ParameterArray ctorParameters;
daniel@transgaming.com63691862010-04-29 03:32:42 +00002202
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00002203 if (type.getStruct())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002204 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002205 mStructNames.insert(decorate(name));
2206
2207 TString structure;
2208 structure += "struct " + decorate(name) + "\n"
2209 "{\n";
2210
2211 const TTypeList &fields = *type.getStruct();
2212
2213 for (unsigned int i = 0; i < fields.size(); i++)
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002214 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002215 const TType &field = *fields[i].type;
2216
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00002217 structure += " " + typeString(field) + " " + decorateField(field.getFieldName(), type) + arrayString(field) + ";\n";
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002218 }
2219
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002220 structure += "};\n";
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002221
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002222 if (std::find(mStructDeclarations.begin(), mStructDeclarations.end(), structure) == mStructDeclarations.end())
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002223 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002224 mStructDeclarations.push_back(structure);
2225 }
2226
2227 for (unsigned int i = 0; i < fields.size(); i++)
2228 {
2229 ctorParameters.push_back(*fields[i].type);
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002230 }
2231 }
daniel@transgaming.com55d48c72011-09-26 18:24:36 +00002232 else if (parameters)
2233 {
2234 for (TIntermSequence::const_iterator parameter = parameters->begin(); parameter != parameters->end(); parameter++)
2235 {
2236 ctorParameters.push_back((*parameter)->getAsTyped()->getType());
2237 }
2238 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002239 else UNREACHABLE();
daniel@transgaming.com63691862010-04-29 03:32:42 +00002240
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002241 TString constructor;
2242
2243 if (ctorType.getStruct())
2244 {
2245 constructor += ctorName + " " + ctorName + "_ctor(";
2246 }
2247 else // Built-in type
2248 {
2249 constructor += typeString(ctorType) + " " + ctorName + "(";
2250 }
2251
2252 for (unsigned int parameter = 0; parameter < ctorParameters.size(); parameter++)
2253 {
2254 const TType &type = ctorParameters[parameter];
2255
2256 constructor += typeString(type) + " x" + str(parameter) + arrayString(type);
2257
2258 if (parameter < ctorParameters.size() - 1)
2259 {
2260 constructor += ", ";
2261 }
2262 }
2263
2264 constructor += ")\n"
2265 "{\n";
2266
2267 if (ctorType.getStruct())
2268 {
2269 constructor += " " + ctorName + " structure = {";
2270 }
2271 else
2272 {
2273 constructor += " return " + typeString(ctorType) + "(";
2274 }
2275
2276 if (ctorType.isMatrix() && ctorParameters.size() == 1)
2277 {
2278 int dim = ctorType.getNominalSize();
2279 const TType &parameter = ctorParameters[0];
2280
2281 if (parameter.isScalar())
2282 {
2283 for (int row = 0; row < dim; row++)
2284 {
2285 for (int col = 0; col < dim; col++)
2286 {
2287 constructor += TString((row == col) ? "x0" : "0.0");
2288
2289 if (row < dim - 1 || col < dim - 1)
2290 {
2291 constructor += ", ";
2292 }
2293 }
2294 }
2295 }
2296 else if (parameter.isMatrix())
2297 {
2298 for (int row = 0; row < dim; row++)
2299 {
2300 for (int col = 0; col < dim; col++)
2301 {
2302 if (row < parameter.getNominalSize() && col < parameter.getNominalSize())
2303 {
2304 constructor += TString("x0") + "[" + str(row) + "]" + "[" + str(col) + "]";
2305 }
2306 else
2307 {
2308 constructor += TString((row == col) ? "1.0" : "0.0");
2309 }
2310
2311 if (row < dim - 1 || col < dim - 1)
2312 {
2313 constructor += ", ";
2314 }
2315 }
2316 }
2317 }
2318 else UNREACHABLE();
2319 }
2320 else
2321 {
2322 int remainingComponents = ctorType.getObjectSize();
2323 int parameterIndex = 0;
2324
2325 while (remainingComponents > 0)
2326 {
2327 const TType &parameter = ctorParameters[parameterIndex];
2328 bool moreParameters = parameterIndex < (int)ctorParameters.size() - 1;
2329
2330 constructor += "x" + str(parameterIndex);
2331
2332 if (parameter.isScalar())
2333 {
2334 remainingComponents -= parameter.getObjectSize();
2335 }
2336 else if (parameter.isVector())
2337 {
2338 if (remainingComponents == parameter.getObjectSize() || moreParameters)
2339 {
2340 remainingComponents -= parameter.getObjectSize();
2341 }
2342 else if (remainingComponents < parameter.getNominalSize())
2343 {
2344 switch (remainingComponents)
2345 {
2346 case 1: constructor += ".x"; break;
2347 case 2: constructor += ".xy"; break;
2348 case 3: constructor += ".xyz"; break;
2349 case 4: constructor += ".xyzw"; break;
2350 default: UNREACHABLE();
2351 }
2352
2353 remainingComponents = 0;
2354 }
2355 else UNREACHABLE();
2356 }
2357 else if (parameter.isMatrix() || parameter.getStruct())
2358 {
2359 ASSERT(remainingComponents == parameter.getObjectSize() || moreParameters);
2360
2361 remainingComponents -= parameter.getObjectSize();
2362 }
2363 else UNREACHABLE();
2364
2365 if (moreParameters)
2366 {
2367 parameterIndex++;
2368 }
2369
2370 if (remainingComponents)
2371 {
2372 constructor += ", ";
2373 }
2374 }
2375 }
2376
2377 if (ctorType.getStruct())
2378 {
2379 constructor += "};\n"
2380 " return structure;\n"
2381 "}\n";
2382 }
2383 else
2384 {
2385 constructor += ");\n"
2386 "}\n";
2387 }
2388
daniel@transgaming.com63691862010-04-29 03:32:42 +00002389 mConstructors.insert(constructor);
2390}
2391
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002392const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion)
2393{
2394 TInfoSinkBase &out = mBody;
2395
2396 if (type.getBasicType() == EbtStruct)
2397 {
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002398 out << structLookup(type.getTypeName()) + "_ctor(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002399
2400 const TTypeList *structure = type.getStruct();
2401
2402 for (size_t i = 0; i < structure->size(); i++)
2403 {
2404 const TType *fieldType = (*structure)[i].type;
2405
2406 constUnion = writeConstantUnion(*fieldType, constUnion);
2407
2408 if (i != structure->size() - 1)
2409 {
2410 out << ", ";
2411 }
2412 }
2413
2414 out << ")";
2415 }
2416 else
2417 {
2418 int size = type.getObjectSize();
2419 bool writeType = size > 1;
2420
2421 if (writeType)
2422 {
2423 out << typeString(type) << "(";
2424 }
2425
2426 for (int i = 0; i < size; i++, constUnion++)
2427 {
2428 switch (constUnion->getType())
2429 {
2430 case EbtFloat: out << constUnion->getFConst(); break;
2431 case EbtInt: out << constUnion->getIConst(); break;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +00002432 case EbtBool: out << constUnion->getBConst(); break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002433 default: UNREACHABLE();
2434 }
2435
2436 if (i != size - 1)
2437 {
2438 out << ", ";
2439 }
2440 }
2441
2442 if (writeType)
2443 {
2444 out << ")";
2445 }
2446 }
2447
2448 return constUnion;
2449}
2450
daniel@transgaming.coma2a95e72010-05-20 19:17:55 +00002451TString OutputHLSL::scopeString(unsigned int depthLimit)
2452{
2453 TString string;
2454
2455 for (unsigned int i = 0; i < mScopeBracket.size() && i < depthLimit; i++)
2456 {
2457 string += "_" + str(i);
2458 }
2459
2460 return string;
2461}
2462
2463TString OutputHLSL::scopedStruct(const TString &typeName)
2464{
2465 if (typeName == "")
2466 {
2467 return typeName;
2468 }
2469
2470 return typeName + scopeString(mScopeDepth);
2471}
2472
2473TString OutputHLSL::structLookup(const TString &typeName)
2474{
2475 for (int depth = mScopeDepth; depth >= 0; depth--)
2476 {
2477 TString scopedName = decorate(typeName + scopeString(depth));
2478
2479 for (StructNames::iterator structName = mStructNames.begin(); structName != mStructNames.end(); structName++)
2480 {
2481 if (*structName == scopedName)
2482 {
2483 return scopedName;
2484 }
2485 }
2486 }
2487
2488 UNREACHABLE(); // Should have found a matching constructor
2489
2490 return typeName;
2491}
2492
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002493TString OutputHLSL::decorate(const TString &string)
2494{
daniel@transgaming.com51db7fb2011-09-20 16:11:06 +00002495 if (string.compare(0, 3, "gl_") != 0 && string.compare(0, 3, "dx_") != 0)
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002496 {
2497 return "_" + string;
2498 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00002499
2500 return string;
2501}
2502
apatrick@chromium.org65756022012-01-17 21:45:38 +00002503TString OutputHLSL::decorateUniform(const TString &string, const TType &type)
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00002504{
apatrick@chromium.org65756022012-01-17 21:45:38 +00002505 if (type.isArray())
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002506 {
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00002507 return "ar_" + string; // Allows identifying arrays of size 1
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002508 }
apatrick@chromium.org65756022012-01-17 21:45:38 +00002509 else if (type.getBasicType() == EbtSamplerExternalOES)
2510 {
2511 return "ex_" + string;
2512 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00002513
2514 return decorate(string);
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002515}
daniel@transgaming.com2e793f02012-04-11 19:41:35 +00002516
2517TString OutputHLSL::decorateField(const TString &string, const TType &structure)
2518{
2519 if (structure.getTypeName().compare(0, 3, "gl_") != 0)
2520 {
2521 return decorate(string);
2522 }
2523
2524 return string;
2525}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002526}