blob: 5566673c8d98145d9f824c91963a9ca6056e9f35 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7#include "OutputHLSL.h"
8
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +00009#include "common/debug.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000010#include "InfoSink.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000011
12namespace sh
13{
14OutputHLSL::OutputHLSL(TParseContext &context) : TIntermTraverser(true, true, true), context(context)
15{
16}
17
18void OutputHLSL::header()
19{
20 EShLanguage language = context.language;
21 TInfoSinkBase &out = context.infoSink.obj;
22
23 if (language == EShLangFragment)
24 {
25 TString uniforms;
26 TString varyingInput;
27 TString varyingGlobals;
28
29 TSymbolTableLevel *symbols = context.symbolTable.getGlobalLevel();
30 int semanticIndex = 0;
31
32 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
33 {
34 const TSymbol *symbol = (*namedSymbol).second;
35 const TString &name = symbol->getName();
36
37 if (symbol->isVariable())
38 {
39 const TVariable *variable = static_cast<const TVariable*>(symbol);
40 const TType &type = variable->getType();
41 TQualifier qualifier = type.getQualifier();
42
43 if (qualifier == EvqUniform)
44 {
45 uniforms += "uniform " + typeString(type) + " " + name + arrayString(type) + ";\n";
46 }
47 else if (qualifier == EvqVaryingIn || qualifier == EvqInvariantVaryingIn)
48 {
49 char semantic[100];
50 sprintf(semantic, " : TEXCOORD%d", semanticIndex);
51 semanticIndex += type.isArray() ? type.getArraySize() : 1;
52
53 varyingInput += " " + typeString(type) + " " + name + arrayString(type) + semantic + ";\n";
54 varyingGlobals += "static " + typeString(type) + " " + name + arrayString(type) + " = " + initializer(type) + ";\n";
55 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +000056 else if (qualifier == EvqConst)
57 {
58 // Constants are repeated as literals where used
59 }
60 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000061 }
62 }
63
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +000064 out << "uniform float4 gl_Window;\n"
65 "uniform float2 gl_Depth;\n"
daniel@transgaming.com79b820b2010-03-16 05:48:57 +000066 "uniform bool __frontCCW;\n"
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +000067 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000068 out << uniforms;
daniel@transgaming.com86487c22010-03-11 19:41:43 +000069 out << "\n"
70 "struct PS_INPUT\n" // FIXME: Prevent name clashes
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000071 "{\n";
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +000072 out << varyingInput;
daniel@transgaming.com79b820b2010-03-16 05:48:57 +000073 out << " float4 gl_FragCoord : TEXCOORD" << HLSL_FRAG_COORD_SEMANTIC << ";\n";
74 out << " float __vFace : VFACE;\n"
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +000075 "};\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000076 "\n";
77 out << varyingGlobals;
78 out << "\n"
79 "struct PS_OUTPUT\n" // FIXME: Prevent name clashes
80 "{\n"
81 " float4 gl_Color[1] : COLOR;\n"
82 "};\n"
83 "\n"
84 "static float4 gl_Color[1] = {float4(0, 0, 0, 0)};\n"
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +000085 "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n"
daniel@transgaming.comccad59f2010-03-26 04:08:39 +000086 "static float2 gl_PointCoord = float2(0.5, 0.5);\n"
daniel@transgaming.com79b820b2010-03-16 05:48:57 +000087 "static bool gl_FrontFacing = false;\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000088 "\n"
89 "float4 gl_texture2D(sampler2D s, float2 t)\n"
90 "{\n"
91 " return tex2D(s, t);\n"
92 "}\n"
93 "\n"
94 "float4 gl_texture2DProj(sampler2D s, float3 t)\n"
95 "{\n"
96 " return tex2Dproj(s, float4(t.x, t.y, 0, t.z));\n"
97 "}\n"
98 "float4 gl_texture2DBias(sampler2D s, float2 t, float bias)\n"
99 "{\n"
100 " return tex2Dbias(s, float4(t.x, t.y, 0, bias));\n"
101 "}\n"
102 "\n"
103 "float4 gl_textureCube(samplerCUBE s, float3 t)\n"
104 "{\n"
105 " return texCUBE(s, t);\n"
106 "}\n"
107 "\n";
108 }
109 else
110 {
111 TString uniforms;
112 TString attributeInput;
113 TString attributeGlobals;
114 TString varyingOutput;
115 TString varyingGlobals;
116 TString globals;
117
118 TSymbolTableLevel *symbols = context.symbolTable.getGlobalLevel();
119 int semanticIndex = 0;
120
121 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
122 {
123 const TSymbol *symbol = (*namedSymbol).second;
124 const TString &name = symbol->getName();
125
126 if (symbol->isVariable())
127 {
128 const TVariable *variable = static_cast<const TVariable*>(symbol);
129 const TType &type = variable->getType();
130 TQualifier qualifier = type.getQualifier();
131
132 if (qualifier == EvqUniform)
133 {
134 uniforms += "uniform " + typeString(type) + " " + name + arrayString(type) + ";\n";
135 }
136 else if (qualifier == EvqAttribute)
137 {
138 char semantic[100];
139 sprintf(semantic, " : TEXCOORD%d", semanticIndex);
140 semanticIndex += type.isArray() ? type.getArraySize() : 1;
141
142 attributeInput += " " + typeString(type) + " " + name + arrayString(type) + semantic + ";\n";
143 attributeGlobals += "static " + typeString(type) + " " + name + arrayString(type) + " = " + initializer(type) + ";\n";
144 }
145 else if (qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut)
146 {
147 varyingOutput += " " + typeString(type) + " " + name + arrayString(type) + " : TEXCOORD0;\n"; // Actual semantic index assigned during link
148 varyingGlobals += "static " + typeString(type) + " " + name + arrayString(type) + " = " + initializer(type) + ";\n";
149 }
150 else if (qualifier == EvqGlobal)
151 {
152 globals += typeString(type) + " " + name + arrayString(type) + ";\n";
153 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000154 else if (qualifier == EvqConst)
155 {
156 // Constants are repeated as literals where used
157 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000158 else UNREACHABLE();
159 }
160 }
161
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +0000162 out << "uniform float2 gl_HalfPixelSize;\n"
163 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000164 out << uniforms;
165 out << "\n";
166 out << globals;
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +0000167 out << "\n"
168 "struct VS_INPUT\n" // FIXME: Prevent name clashes
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000169 "{\n";
170 out << attributeInput;
171 out << "};\n"
172 "\n";
173 out << attributeGlobals;
174 out << "\n"
175 "struct VS_OUTPUT\n" // FIXME: Prevent name clashes
176 "{\n"
177 " float4 gl_Position : POSITION;\n"
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +0000178 " float gl_PointSize : PSIZE;\n"
179 " float4 gl_FragCoord : TEXCOORD" << HLSL_FRAG_COORD_SEMANTIC << ";\n";
180 out << varyingOutput;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181 out << "};\n"
182 "\n"
183 "static float4 gl_Position = float4(0, 0, 0, 0);\n"
daniel@transgaming.comccad59f2010-03-26 04:08:39 +0000184 "static float gl_PointSize = float(1);\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000185 out << varyingGlobals;
186 out << "\n";
187 }
188
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000189 out << "struct gl_DepthRangeParameters\n"
190 "{\n"
191 " float near;\n"
192 " float far;\n"
193 " float diff;\n"
194 "};\n"
195 "\n"
196 "uniform gl_DepthRangeParameters gl_DepthRange;\n"
197 "\n"
198 "float vec1(float x)\n" // FIXME: Prevent name clashes
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000199 "{\n"
200 " return x;\n"
201 "}\n"
202 "\n"
203 "float vec1(float2 xy)\n" // FIXME: Prevent name clashes
204 "{\n"
205 " return xy[0];\n"
206 "}\n"
207 "\n"
208 "float vec1(float3 xyz)\n" // FIXME: Prevent name clashes
209 "{\n"
210 " return xyz[0];\n"
211 "}\n"
212 "\n"
213 "float vec1(float4 xyzw)\n" // FIXME: Prevent name clashes
214 "{\n"
215 " return xyzw[0];\n"
216 "}\n"
217 "\n"
218 "float2 vec2(float x)\n" // FIXME: Prevent name clashes
219 "{\n"
220 " return float2(x, x);\n"
221 "}\n"
222 "\n"
223 "float2 vec2(float x, float y)\n" // FIXME: Prevent name clashes
224 "{\n"
225 " return float2(x, y);\n"
226 "}\n"
227 "\n"
228 "float2 vec2(float2 xy)\n" // FIXME: Prevent name clashes
229 "{\n"
230 " return xy;\n"
231 "}\n"
232 "\n"
233 "float2 vec2(float3 xyz)\n" // FIXME: Prevent name clashes
234 "{\n"
235 " return float2(xyz[0], xyz[1]);\n"
236 "}\n"
237 "\n"
238 "float2 vec2(float4 xyzw)\n" // FIXME: Prevent name clashes
239 "{\n"
240 " return float2(xyzw[0], xyzw[1]);\n"
241 "}\n"
242 "\n"
243 "float3 vec3(float x)\n" // FIXME: Prevent name clashes
244 "{\n"
245 " return float3(x, x, x);\n"
246 "}\n"
247 "\n"
248 "float3 vec3(float x, float y, float z)\n" // FIXME: Prevent name clashes
249 "{\n"
250 " return float3(x, y, z);\n"
251 "}\n"
252 "\n"
253 "float3 vec3(float2 xy, float z)\n" // FIXME: Prevent name clashes
254 "{\n"
255 " return float3(xy[0], xy[1], z);\n"
256 "}\n"
257 "\n"
258 "float3 vec3(float x, float2 yz)\n" // FIXME: Prevent name clashes
259 "{\n"
260 " return float3(x, yz[0], yz[1]);\n"
261 "}\n"
262 "\n"
263 "float3 vec3(float3 xyz)\n" // FIXME: Prevent name clashes
264 "{\n"
265 " return xyz;\n"
266 "}\n"
267 "\n"
268 "float3 vec3(float4 xyzw)\n" // FIXME: Prevent name clashes
269 "{\n"
270 " return float3(xyzw[0], xyzw[1], xyzw[2]);\n"
271 "}\n"
272 "\n"
273 "float4 vec4(float x)\n" // FIXME: Prevent name clashes
274 "{\n"
275 " return float4(x, x, x, x);\n"
276 "}\n"
277 "\n"
278 "float4 vec4(float x, float y, float z, float w)\n" // FIXME: Prevent name clashes
279 "{\n"
280 " return float4(x, y, z, w);\n"
281 "}\n"
282 "\n"
283 "float4 vec4(float2 xy, float z, float w)\n" // FIXME: Prevent name clashes
284 "{\n"
285 " return float4(xy[0], xy[1], z, w);\n"
286 "}\n"
287 "\n"
288 "float4 vec4(float x, float2 yz, float w)\n" // FIXME: Prevent name clashes
289 "{\n"
290 " return float4(x, yz[0], yz[1], w);\n"
291 "}\n"
292 "\n"
293 "float4 vec4(float x, float y, float2 zw)\n" // FIXME: Prevent name clashes
294 "{\n"
295 " return float4(x, y, zw[0], zw[1]);\n"
296 "}\n"
297 "\n"
298 "float4 vec4(float2 xy, float2 zw)\n" // FIXME: Prevent name clashes
299 "{\n"
300 " return float4(xy[0], xy[1], zw[0], zw[1]);\n"
301 "}\n"
302 "\n"
303 "float4 vec4(float3 xyz, float w)\n" // FIXME: Prevent name clashes
304 "{\n"
305 " return float4(xyz[0], xyz[1], xyz[2], w);\n"
306 "}\n"
307 "\n"
308 "float4 vec4(float x, float3 yzw)\n" // FIXME: Prevent name clashes
309 "{\n"
310 " return float4(x, yzw[0], yzw[1], yzw[2]);\n"
311 "}\n"
312 "\n"
313 "float4 vec4(float4 xyzw)\n" // FIXME: Prevent name clashes
314 "{\n"
315 " return xyzw;\n"
316 "}\n"
317 "\n"
318 "bool xor(bool p, bool q)\n" // FIXME: Prevent name clashes
319 "{\n"
320 " return (p || q) && !(p && q);\n"
321 "}\n"
322 "\n"
323 "float mod(float x, float y)\n" // FIXME: Prevent name clashes
324 "{\n"
325 " return x - y * floor(x / y);\n"
326 "}\n"
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000327 "\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000328 "float2 mod(float2 x, float y)\n" // FIXME: Prevent name clashes
329 "{\n"
330 " return x - y * floor(x / y);\n"
331 "}\n"
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000332 "\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000333 "float3 mod(float3 x, float y)\n" // FIXME: Prevent name clashes
334 "{\n"
335 " return x - y * floor(x / y);\n"
336 "}\n"
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000337 "\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000338 "float4 mod(float4 x, float y)\n" // FIXME: Prevent name clashes
339 "{\n"
340 " return x - y * floor(x / y);\n"
341 "}\n"
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000342 "\n"
343 "float faceforward(float N, float I, float Nref)\n" // FIXME: Prevent name clashes
344 "{\n"
345 " if(dot(Nref, I) < 0)\n"
346 " {\n"
347 " return N;\n"
348 " }\n"
349 " else\n"
350 " {\n"
351 " return -N;\n"
352 " }\n"
353 "}\n"
354 "\n"
355 "float2 faceforward(float2 N, float2 I, float2 Nref)\n" // FIXME: Prevent name clashes
356 "{\n"
357 " if(dot(Nref, I) < 0)\n"
358 " {\n"
359 " return N;\n"
360 " }\n"
361 " else\n"
362 " {\n"
363 " return -N;\n"
364 " }\n"
365 "}\n"
366 "\n"
367 "float3 faceforward(float3 N, float3 I, float3 Nref)\n" // FIXME: Prevent name clashes
368 "{\n"
369 " if(dot(Nref, I) < 0)\n"
370 " {\n"
371 " return N;\n"
372 " }\n"
373 " else\n"
374 " {\n"
375 " return -N;\n"
376 " }\n"
377 "}\n"
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000378 "\n"
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000379 "float4 faceforward(float4 N, float4 I, float4 Nref)\n" // FIXME: Prevent name clashes
380 "{\n"
381 " if(dot(Nref, I) < 0)\n"
382 " {\n"
383 " return N;\n"
384 " }\n"
385 " else\n"
386 " {\n"
387 " return -N;\n"
388 " }\n"
389 "}\n"
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000390 "\n"
391 "bool __equal(float2x2 m, float2x2 n)\n"
392 "{\n"
393 " return m[0][0] == n[0][0] && m[0][1] == n[0][1] &&\n"
394 " m[1][0] == n[1][0] && m[1][1] == n[1][1];\n"
395 "}\n"
396 "\n"
397 "bool __equal(float3x3 m, float3x3 n)\n"
398 "{\n"
399 " return m[0][0] == n[0][0] && m[0][1] == n[0][1] && m[0][2] == n[0][2] &&\n"
400 " m[1][0] == n[1][0] && m[1][1] == n[1][1] && m[1][2] == n[1][2] &&\n"
401 " m[2][0] == n[2][0] && m[2][1] == n[2][1] && m[2][2] == n[2][2];\n"
402 "}\n"
403 "\n"
404 "bool __equal(float4x4 m, float4x4 n)\n"
405 "{\n"
406 " 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"
407 " 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"
408 " 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"
409 " 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"
410 "}\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000411 "\n";
412}
413
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000414void OutputHLSL::footer()
415{
416 EShLanguage language = context.language;
417 TInfoSinkBase &out = context.infoSink.obj;
418 TSymbolTableLevel *symbols = context.symbolTable.getGlobalLevel();
419
420 if (language == EShLangFragment)
421 {
422 out << "PS_OUTPUT main(PS_INPUT input)\n" // FIXME: Prevent name clashes
423 "{\n"
424 " float rhw = 1.0 / input.gl_FragCoord.w;\n"
425 " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * gl_Window.x + gl_Window.z;\n"
426 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * gl_Window.y + gl_Window.w;\n"
427 " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * gl_Depth.x + gl_Depth.y;\n"
428 " gl_FragCoord.w = rhw;\n"
429 " gl_FrontFacing = __frontCCW ? (input.__vFace >= 0.0) : (input.__vFace <= 0.0);\n";
430
431 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
432 {
433 const TSymbol *symbol = (*namedSymbol).second;
434 const TString &name = symbol->getName();
435
436 if (symbol->isVariable())
437 {
438 const TVariable *variable = static_cast<const TVariable*>(symbol);
439 const TType &type = variable->getType();
440 TQualifier qualifier = type.getQualifier();
441
442 if (qualifier == EvqVaryingIn)
443 {
444 out << " " + name + " = input." + name + ";\n"; // FIXME: Prevent name clashes
445 }
446 }
447 }
448
449 out << "\n"
450 " gl_main();\n"
451 "\n"
452 " PS_OUTPUT output;\n" // FIXME: Prevent name clashes
453 " output.gl_Color[0] = gl_Color[0];\n"; // FIXME: Prevent name clashes
454
455 TSymbolTableLevel *symbols = context.symbolTable.getGlobalLevel();
456
457 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
458 {
459 const TSymbol *symbol = (*namedSymbol).second;
460 const TString &name = symbol->getName();
461 }
462 }
463 else
464 {
465 out << "VS_OUTPUT main(VS_INPUT input)\n" // FIXME: Prevent name clashes
466 "{\n";
467
468 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
469 {
470 const TSymbol *symbol = (*namedSymbol).second;
471 const TString &name = symbol->getName();
472
473 if (symbol->isVariable())
474 {
475 const TVariable *variable = static_cast<const TVariable*>(symbol);
476 const TType &type = variable->getType();
477 TQualifier qualifier = type.getQualifier();
478
479 if (qualifier == EvqAttribute)
480 {
481 out << " " + name + " = input." + name + ";\n"; // FIXME: Prevent name clashes
482 }
483 }
484 }
485
486 out << "\n"
487 " gl_main();\n"
488 "\n"
489 " VS_OUTPUT output;\n" // FIXME: Prevent name clashes
490 " output.gl_Position.x = gl_Position.x - gl_HalfPixelSize.x * gl_Position.w;\n"
491 " output.gl_Position.y = -(gl_Position.y - gl_HalfPixelSize.y * gl_Position.w);\n"
492 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
493 " output.gl_Position.w = gl_Position.w;\n"
494 " output.gl_PointSize = gl_PointSize;\n"
495 " output.gl_FragCoord = gl_Position;\n";
496
497 TSymbolTableLevel *symbols = context.symbolTable.getGlobalLevel();
498
499 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
500 {
501 const TSymbol *symbol = (*namedSymbol).second;
502 const TString &name = symbol->getName();
503
504 if (symbol->isVariable())
505 {
506 const TVariable *variable = static_cast<const TVariable*>(symbol);
507 TQualifier qualifier = variable->getType().getQualifier();
508
509 if (qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut)
510 {
511 out << " output." + name + " = " + name + ";\n"; // FIXME: Prevent name clashes
512 }
513 }
514 }
515 }
516
517 out << " return output;\n" // FIXME: Prevent name clashes
518 "}\n";
519}
520
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000521void OutputHLSL::visitSymbol(TIntermSymbol *node)
522{
523 TInfoSinkBase &out = context.infoSink.obj;
524
525 TString name = node->getSymbol();
526
527 if (name == "gl_FragColor")
528 {
529 out << "gl_Color[0]";
530 }
531 else if (name == "gl_FragData")
532 {
533 out << "gl_Color";
534 }
535 else
536 {
537 out << name;
538 }
539}
540
541bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
542{
543 TInfoSinkBase &out = context.infoSink.obj;
544
545 switch (node->getOp())
546 {
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000547 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
548 case EOpInitialize: outputTriplet(visit, NULL, " = ", NULL); break;
549 case EOpAddAssign: outputTriplet(visit, NULL, " += ", NULL); break;
550 case EOpSubAssign: outputTriplet(visit, NULL, " -= ", NULL); break;
daniel@transgaming.com45d03582010-03-11 19:41:29 +0000551 case EOpMulAssign: outputTriplet(visit, NULL, " *= ", NULL); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000552 case EOpVectorTimesMatrixAssign: UNIMPLEMENTED(); /* FIXME */ out << "matrix mult second child into first child"; break;
daniel@transgaming.com45d03582010-03-11 19:41:29 +0000553 case EOpVectorTimesScalarAssign: outputTriplet(visit, NULL, " *= ", NULL); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000554 case EOpMatrixTimesScalarAssign: UNIMPLEMENTED(); /* FIXME */ out << "matrix scale second child into first child"; break;
555 case EOpMatrixTimesMatrixAssign: UNIMPLEMENTED(); /* FIXME */ out << "matrix mult second child into first child"; break;
daniel@transgaming.com45d03582010-03-11 19:41:29 +0000556 case EOpDivAssign: outputTriplet(visit, NULL, " /= ", NULL); break;
daniel@transgaming.com45d03582010-03-11 19:41:29 +0000557 case EOpIndexDirect: outputTriplet(visit, NULL, "[", "]"); break;
558 case EOpIndexIndirect: outputTriplet(visit, NULL, "[", "]"); break;
559 case EOpIndexDirectStruct: outputTriplet(visit, NULL, ".", NULL); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000560 case EOpVectorSwizzle:
561 if (visit == InVisit)
562 {
563 out << ".";
564
565 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
566
567 if (swizzle)
568 {
569 TIntermSequence &sequence = swizzle->getSequence();
570
571 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
572 {
573 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
574
575 if (element)
576 {
577 int i = element->getUnionArrayPointer()[0].getIConst();
578
579 switch (i)
580 {
581 case 0: out << "x"; break;
582 case 1: out << "y"; break;
583 case 2: out << "z"; break;
584 case 3: out << "w"; break;
585 default: UNREACHABLE();
586 }
587 }
588 else UNREACHABLE();
589 }
590 }
591 else UNREACHABLE();
592
593 return false; // Fully processed
594 }
595 break;
596 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
597 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
598 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
599 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000600 case EOpEqual:
601 if (!node->getLeft()->isMatrix())
602 {
603 outputTriplet(visit, "(", " == ", ")");
604 }
605 else
606 {
607 outputTriplet(visit, "__equal(", ", ", ")");
608 }
609 break;
610 case EOpNotEqual:
611 if (!node->getLeft()->isMatrix())
612 {
613 outputTriplet(visit, "(", " != ", ")");
614 }
615 else
616 {
617 outputTriplet(visit, "!__equal(", ", ", ")");
618 }
619 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000620 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
621 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
622 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
623 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
624 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
625 case EOpVectorTimesMatrix: UNIMPLEMENTED(); /* FIXME */ out << "vector-times-matrix"; break;
626 case EOpMatrixTimesVector: outputTriplet(visit, "mul(", ", ", ")"); break;
627 case EOpMatrixTimesScalar: UNIMPLEMENTED(); /* FIXME */ out << "matrix-scale"; break;
628 case EOpMatrixTimesMatrix: UNIMPLEMENTED(); /* FIXME */ out << "matrix-multiply"; break;
629 case EOpLogicalOr: outputTriplet(visit, "(", " || ", ")"); break;
630 case EOpLogicalXor: outputTriplet(visit, "xor(", ", ", ")"); break; // FIXME: Prevent name clashes
631 case EOpLogicalAnd: outputTriplet(visit, "(", " && ", ")"); break;
632 default: UNREACHABLE();
633 }
634
635 return true;
636}
637
638bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
639{
640 TInfoSinkBase &out = context.infoSink.obj;
641
642 switch (node->getOp())
643 {
644 case EOpNegative: outputTriplet(visit, "(-", NULL, ")"); break;
645 case EOpVectorLogicalNot: outputTriplet(visit, "(!", NULL, ")"); break;
646 case EOpLogicalNot: outputTriplet(visit, "(!", NULL, ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647 case EOpPostIncrement: outputTriplet(visit, "(", NULL, "++)"); break;
648 case EOpPostDecrement: outputTriplet(visit, "(", NULL, "--)"); break;
649 case EOpPreIncrement: outputTriplet(visit, "(++", NULL, ")"); break;
650 case EOpPreDecrement: outputTriplet(visit, "(--", NULL, ")"); break;
651 case EOpConvIntToBool:
652 case EOpConvFloatToBool:
653 switch (node->getOperand()->getType().getNominalSize())
654 {
655 case 1: outputTriplet(visit, "bool(", NULL, ")"); break;
656 case 2: outputTriplet(visit, "bool2(", NULL, ")"); break;
657 case 3: outputTriplet(visit, "bool3(", NULL, ")"); break;
658 case 4: outputTriplet(visit, "bool4(", NULL, ")"); break;
659 default: UNREACHABLE();
660 }
661 break;
662 case EOpConvBoolToFloat:
663 case EOpConvIntToFloat:
664 switch (node->getOperand()->getType().getNominalSize())
665 {
666 case 1: outputTriplet(visit, "float(", NULL, ")"); break;
667 case 2: outputTriplet(visit, "float2(", NULL, ")"); break;
668 case 3: outputTriplet(visit, "float3(", NULL, ")"); break;
669 case 4: outputTriplet(visit, "float4(", NULL, ")"); break;
670 default: UNREACHABLE();
671 }
672 break;
673 case EOpConvFloatToInt:
674 case EOpConvBoolToInt:
675 switch (node->getOperand()->getType().getNominalSize())
676 {
677 case 1: outputTriplet(visit, "int(", NULL, ")"); break;
678 case 2: outputTriplet(visit, "int2(", NULL, ")"); break;
679 case 3: outputTriplet(visit, "int3(", NULL, ")"); break;
680 case 4: outputTriplet(visit, "int4(", NULL, ")"); break;
681 default: UNREACHABLE();
682 }
683 break;
684 case EOpRadians: outputTriplet(visit, "radians(", NULL, ")"); break;
685 case EOpDegrees: outputTriplet(visit, "degrees(", NULL, ")"); break;
686 case EOpSin: outputTriplet(visit, "sin(", NULL, ")"); break;
687 case EOpCos: outputTriplet(visit, "cos(", NULL, ")"); break;
688 case EOpTan: outputTriplet(visit, "tan(", NULL, ")"); break;
689 case EOpAsin: outputTriplet(visit, "asin(", NULL, ")"); break;
690 case EOpAcos: outputTriplet(visit, "acos(", NULL, ")"); break;
691 case EOpAtan: outputTriplet(visit, "atan(", NULL, ")"); break;
692 case EOpExp: outputTriplet(visit, "exp(", NULL, ")"); break;
693 case EOpLog: outputTriplet(visit, "log(", NULL, ")"); break;
694 case EOpExp2: outputTriplet(visit, "exp2(", NULL, ")"); break;
695 case EOpLog2: outputTriplet(visit, "log2(", NULL, ")"); break;
696 case EOpSqrt: outputTriplet(visit, "sqrt(", NULL, ")"); break;
697 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", NULL, ")"); break;
698 case EOpAbs: outputTriplet(visit, "abs(", NULL, ")"); break;
699 case EOpSign: outputTriplet(visit, "sign(", NULL, ")"); break;
700 case EOpFloor: outputTriplet(visit, "floor(", NULL, ")"); break;
701 case EOpCeil: outputTriplet(visit, "ceil(", NULL, ")"); break;
702 case EOpFract: outputTriplet(visit, "frac(", NULL, ")"); break;
703 case EOpLength: outputTriplet(visit, "length(", NULL, ")"); break;
704 case EOpNormalize: outputTriplet(visit, "normalize(", NULL, ")"); break;
daniel@transgaming.com45d03582010-03-11 19:41:29 +0000705// case EOpDPdx: outputTriplet(visit, "ddx(", NULL, ")"); break;
706// case EOpDPdy: outputTriplet(visit, "ddy(", NULL, ")"); break;
707// case EOpFwidth: outputTriplet(visit, "fwidth(", NULL, ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000708 case EOpAny: outputTriplet(visit, "any(", NULL, ")"); break;
709 case EOpAll: outputTriplet(visit, "all(", NULL, ")"); break;
710 default: UNREACHABLE();
711 }
712
713 return true;
714}
715
716bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
717{
718 EShLanguage language = context.language;
719 TInfoSinkBase &out = context.infoSink.obj;
720
721 if (node->getOp() == EOpNull)
722 {
723 out.message(EPrefixError, "node is still EOpNull!");
724 return true;
725 }
726
727 switch (node->getOp())
728 {
729 case EOpSequence: outputTriplet(visit, NULL, ";\n", ";\n"); break;
730 case EOpDeclaration:
731 if (visit == PreVisit)
732 {
733 TIntermSequence &sequence = node->getSequence();
734 TIntermTyped *variable = sequence[0]->getAsTyped();
735 bool visit = true;
736
737 if (variable && variable->getQualifier() == EvqTemporary)
738 {
739 out << typeString(variable->getType()) + " ";
740
741 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
742 {
743 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
744
745 if (symbol)
746 {
747 symbol->traverse(this);
748
749 out << arrayString(symbol->getType());
750 }
751 else
752 {
753 (*sit)->traverse(this);
754 }
755
756 if (visit && this->inVisit)
757 {
758 if (*sit != sequence.back())
759 {
760 visit = this->visitAggregate(InVisit, node);
761 }
762 }
763 }
764
765 if (visit && this->postVisit)
766 {
767 this->visitAggregate(PostVisit, node);
768 }
769 }
770
771 return false;
772 }
773 else if (visit == InVisit)
774 {
775 out << ", ";
776 }
777 break;
778 case EOpComma: UNIMPLEMENTED(); /* FIXME */ out << "Comma\n"; return true;
779 case EOpFunction:
780 {
781 const TString &mangledName = node->getName();
782 TString name = TString(mangledName.c_str(), mangledName.find_first_of('('));
783
784 if (visit == PreVisit)
785 {
786 if (name == "main")
787 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000788 name = "gl_main";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000789 }
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000790
791 out << typeString(node->getType()) << " " << name << "(";
792
793 TIntermSequence &sequence = node->getSequence();
794 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
795
796 for (unsigned int i = 0; i < arguments.size(); i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000797 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000798 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000799
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000800 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000801 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000802 const TType &type = symbol->getType();
803 const TString &name = symbol->getSymbol();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000804
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000805 out << typeString(type) + " " + name;
806
807 if (i < arguments.size() - 1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000808 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000809 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000811 }
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000812 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000813 }
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000814
815 sequence.erase(sequence.begin());
816
817 out << ")\n"
818 "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000819 }
820 else if (visit == PostVisit)
821 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000822 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000823 }
824 }
825 break;
826 case EOpFunctionCall:
827 {
828 if (visit == PreVisit)
829 {
830 const TString &mangledName = node->getName();
831 TString name = TString(mangledName.c_str(), mangledName.find_first_of('('));
832
833 if (node->isUserDefined())
834 {
835 out << name << "(";
836 }
837 else
838 {
839 if (name == "texture2D")
840 {
841 if (node->getSequence().size() == 2)
842 {
843 out << "gl_texture2D(";
844 }
845 else if (node->getSequence().size() == 3)
846 {
847 out << "gl_texture2DBias(";
848 }
849 else UNREACHABLE();
850 }
851 else if (name == "texture2DProj")
852 {
853 out << "gl_texture2DProj(";
854 }
855 else if (name == "texture2DLod")
856 {
857 out << "gl_texture2DLod(";
858 UNIMPLEMENTED(); // FIXME: Move lod to last texture coordinate component
859 }
860 else if (name == "texture2DProjLod")
861 {
862 out << "gl_texture2DProjLod(";
863 UNIMPLEMENTED(); // FIXME: Move lod to last texture coordinate component
864 }
865 else if (name == "textureCube")
866 {
867 out << "gl_textureCube("; // FIXME: Incorrect sampling location
868 }
869 else
870 {
871 UNIMPLEMENTED(); // FIXME
872 }
873 }
874 }
875 else if (visit == InVisit)
876 {
877 out << ", ";
878 }
879 else
880 {
881 out << ")";
882 }
883 }
884 break;
885 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
886 case EOpConstructFloat: outputTriplet(visit, "vec1(", NULL, ")"); break;
887 case EOpConstructVec2: outputTriplet(visit, "vec2(", ", ", ")"); break;
888 case EOpConstructVec3: outputTriplet(visit, "vec3(", ", ", ")"); break;
889 case EOpConstructVec4: outputTriplet(visit, "vec4(", ", ", ")"); break;
890 case EOpConstructBool: UNIMPLEMENTED(); /* FIXME */ out << "Construct bool"; break;
891 case EOpConstructBVec2: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec2"; break;
892 case EOpConstructBVec3: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec3"; break;
893 case EOpConstructBVec4: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec4"; break;
894 case EOpConstructInt: UNIMPLEMENTED(); /* FIXME */ out << "Construct int"; break;
895 case EOpConstructIVec2: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec2"; break;
896 case EOpConstructIVec3: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec3"; break;
897 case EOpConstructIVec4: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec4"; break;
898 case EOpConstructMat2: outputTriplet(visit, "float2x2(", ", ", ")"); break;
899 case EOpConstructMat3: outputTriplet(visit, "float3x3(", ", ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000900 case EOpConstructMat4: outputTriplet(visit, "float4x4(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000901 case EOpConstructStruct: UNIMPLEMENTED(); /* FIXME */ out << "Construct structure"; break;
902 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
903 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
904 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
905 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
906 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
907 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
908 case EOpMod: outputTriplet(visit, "mod(", ", ", ")"); break; // FIXME: Prevent name clashes
909 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
910 case EOpAtan:
911 if (node->getSequence().size() == 1)
912 {
913 outputTriplet(visit, "atan(", ", ", ")");
914 }
915 else if (node->getSequence().size() == 2)
916 {
917 outputTriplet(visit, "atan2(", ", ", ")");
918 }
919 else UNREACHABLE();
920 break;
921 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
922 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
923 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
924 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
925 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
926 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
927 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
928 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
929 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000930 case EOpFaceForward: outputTriplet(visit, "faceforward(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000931 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
932 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
933 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000934 default: UNREACHABLE();
935 }
936
937 return true;
938}
939
940bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
941{
942 TInfoSinkBase &out = context.infoSink.obj;
943
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000944 if (node->getType().getBasicType() == EbtVoid) // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000945 {
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +0000946 out << "if(";
947
948 node->getCondition()->traverse(this);
949
950 out << ")\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000951 "{\n";
952
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +0000953 node->getTrueBlock()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954
955 out << ";}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +0000956
957 if (node->getFalseBlock())
958 {
959 out << "else\n"
960 "{\n";
961
962 node->getFalseBlock()->traverse(this);
963
964 out << ";}\n";
965 }
966 }
967 else // Ternary operator expression
968 {
969 out << "(";
970 node->getCondition()->traverse(this);
971 out << ") ? (";
972 node->getTrueBlock()->traverse(this);
973 out << ") : (";
974 node->getFalseBlock()->traverse(this);
975 out << ")\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000976 }
977
978 return false;
979}
980
981void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
982{
983 TInfoSinkBase &out = context.infoSink.obj;
984
daniel@transgaming.com45d03582010-03-11 19:41:29 +0000985 TType &type = node->getType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000986
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000987 if (type.isField())
988 {
989 out << type.getFieldName();
990 }
991 else
992 {
993 int size = type.getObjectSize();
994 bool matrix = type.isMatrix();
995 TBasicType basicType = node->getUnionArrayPointer()[0].getType();
996
997 switch (basicType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000998 {
999 case EbtBool:
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001000 if (!matrix)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001001 {
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001002 switch (size)
1003 {
1004 case 1: out << "bool("; break;
1005 case 2: out << "bool2("; break;
1006 case 3: out << "bool3("; break;
1007 case 4: out << "bool4("; break;
1008 default: UNREACHABLE();
1009 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010 }
1011 else
1012 {
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001013 UNIMPLEMENTED();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001014 }
1015 break;
1016 case EbtFloat:
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001017 if (!matrix)
1018 {
1019 switch (size)
1020 {
1021 case 1: out << "float("; break;
1022 case 2: out << "float2("; break;
1023 case 3: out << "float3("; break;
1024 case 4: out << "float4("; break;
1025 default: UNREACHABLE();
1026 }
1027 }
1028 else
1029 {
1030 switch (size)
1031 {
1032 case 4: out << "float2x2("; break;
1033 case 9: out << "float3x3("; break;
1034 case 16: out << "float4x4("; break;
1035 default: UNREACHABLE();
1036 }
1037 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001038 break;
1039 case EbtInt:
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001040 if (!matrix)
1041 {
1042 switch (size)
1043 {
1044 case 1: out << "int("; break;
1045 case 2: out << "int2("; break;
1046 case 3: out << "int3("; break;
1047 case 4: out << "int4("; break;
1048 default: UNREACHABLE();
1049 }
1050 }
1051 else
1052 {
1053 UNIMPLEMENTED();
1054 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001055 break;
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001056 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001057 UNIMPLEMENTED(); // FIXME
1058 }
1059
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001060 for (int i = 0; i < size; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001061 {
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001062 switch (basicType)
1063 {
1064 case EbtBool:
1065 if (node->getUnionArrayPointer()[i].getBConst())
1066 {
1067 out << "true";
1068 }
1069 else
1070 {
1071 out << "false";
1072 }
1073 break;
1074 case EbtFloat:
1075 out << node->getUnionArrayPointer()[i].getFConst();
1076 break;
1077 case EbtInt:
1078 out << node->getUnionArrayPointer()[i].getIConst();
1079 break;
1080 default:
1081 UNIMPLEMENTED(); // FIXME
1082 }
1083
1084 if (i != size - 1)
1085 {
1086 out << ", ";
1087 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001088 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001089
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001090 out << ")";
1091 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001092}
1093
1094bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
1095{
1096 TInfoSinkBase &out = context.infoSink.obj;
1097
1098 if (!node->testFirst())
1099 {
1100 out << "do\n"
1101 "{\n";
1102 }
1103 else
1104 {
1105 out << "for(";
1106
1107 if (node->getInit())
1108 {
1109 node->getInit()->traverse(this);
1110 }
1111
1112 out << "; ";
1113
1114 if (node->getTest())
1115 {
1116 node->getTest()->traverse(this);
1117 }
1118
1119 out << "; ";
1120
1121 if (node->getTerminal())
1122 {
1123 node->getTerminal()->traverse(this);
1124 }
1125
1126 out << ")\n"
1127 "{\n";
1128 }
1129
1130 if (node->getBody())
1131 {
1132 node->getBody()->traverse(this);
1133 }
1134
1135 out << "}\n";
1136
1137 if (!node->testFirst())
1138 {
1139 out << "while(\n";
1140
1141 node->getTest()->traverse(this);
1142
1143 out << ")";
1144 }
1145
1146 out << ";\n";
1147
1148 return false;
1149}
1150
1151bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
1152{
1153 TInfoSinkBase &out = context.infoSink.obj;
1154
1155 switch (node->getFlowOp())
1156 {
daniel@transgaming.comf67f82e2010-03-17 03:58:54 +00001157 case EOpKill: outputTriplet(visit, "discard", NULL, NULL); break;
1158 case EOpBreak: outputTriplet(visit, "break", NULL, NULL); break;
1159 case EOpContinue: outputTriplet(visit, "continue", NULL, NULL); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001160 case EOpReturn:
1161 if (visit == PreVisit)
1162 {
1163 if (node->getExpression())
1164 {
1165 out << "return ";
1166 }
1167 else
1168 {
1169 out << "return;\n";
1170 }
1171 }
1172 else if (visit == PostVisit)
1173 {
1174 out << ";\n";
1175 }
1176 break;
1177 default: UNREACHABLE();
1178 }
1179
1180 return true;
1181}
1182
1183void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
1184{
1185 TInfoSinkBase &out = context.infoSink.obj;
1186
1187 if (visit == PreVisit && preString)
1188 {
1189 out << preString;
1190 }
1191 else if (visit == InVisit && inString)
1192 {
1193 out << inString;
1194 }
1195 else if (visit == PostVisit && postString)
1196 {
1197 out << postString;
1198 }
1199}
1200
1201TString OutputHLSL::typeString(const TType &type)
1202{
1203 if (type.isMatrix())
1204 {
1205 switch (type.getNominalSize())
1206 {
1207 case 2: return "float2x2";
1208 case 3: return "float3x3";
1209 case 4: return "float4x4";
1210 }
1211 }
1212 else
1213 {
1214 switch (type.getBasicType())
1215 {
1216 case EbtFloat:
1217 switch (type.getNominalSize())
1218 {
1219 case 1: return "float";
1220 case 2: return "float2";
1221 case 3: return "float3";
1222 case 4: return "float4";
1223 }
1224 case EbtInt:
1225 switch (type.getNominalSize())
1226 {
1227 case 1: return "int";
1228 case 2: return "int2";
1229 case 3: return "int3";
1230 case 4: return "int4";
1231 }
1232 case EbtBool:
1233 switch (type.getNominalSize())
1234 {
1235 case 1: return "bool";
1236 case 2: return "bool2";
1237 case 3: return "bool3";
1238 case 4: return "bool4";
1239 }
1240 case EbtVoid:
1241 return "void";
1242 case EbtSampler2D:
1243 return "sampler2D";
1244 case EbtSamplerCube:
1245 return "samplerCUBE";
1246 }
1247 }
1248
1249 UNIMPLEMENTED(); // FIXME
1250 return "<unknown type>";
1251}
1252
1253TString OutputHLSL::arrayString(const TType &type)
1254{
1255 if (!type.isArray())
1256 {
1257 return "";
1258 }
1259
1260 char buffer[100];
1261 sprintf(buffer, "[%d]", type.getArraySize());
1262
1263 return buffer;
1264}
1265
1266TString OutputHLSL::initializer(const TType &type)
1267{
1268 TString string;
1269
1270 int arraySize = type.isArray() ? type.getArraySize() : 1;
1271
1272 if (type.isArray())
1273 {
1274 string += "{";
1275 }
1276
1277 for (int element = 0; element < arraySize; element++)
1278 {
1279 string += typeString(type) + "(";
1280
1281 for (int component = 0; component < type.getNominalSize(); component++)
1282 {
1283 string += "0";
1284
1285 if (component < type.getNominalSize() - 1)
1286 {
1287 string += ", ";
1288 }
1289 }
1290
1291 string += ")";
1292
1293 if (element < arraySize - 1)
1294 {
1295 string += ", ";
1296 }
1297 }
1298
1299 if (type.isArray())
1300 {
1301 string += "}";
1302 }
1303
1304 return string;
1305}
1306}