blob: c8a7c980ef87dca7233bd3562b0a7cc0293f31d3 [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
414void OutputHLSL::visitSymbol(TIntermSymbol *node)
415{
416 TInfoSinkBase &out = context.infoSink.obj;
417
418 TString name = node->getSymbol();
419
420 if (name == "gl_FragColor")
421 {
422 out << "gl_Color[0]";
423 }
424 else if (name == "gl_FragData")
425 {
426 out << "gl_Color";
427 }
428 else
429 {
430 out << name;
431 }
432}
433
434bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
435{
436 TInfoSinkBase &out = context.infoSink.obj;
437
438 switch (node->getOp())
439 {
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000440 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
441 case EOpInitialize: outputTriplet(visit, NULL, " = ", NULL); break;
442 case EOpAddAssign: outputTriplet(visit, NULL, " += ", NULL); break;
443 case EOpSubAssign: outputTriplet(visit, NULL, " -= ", NULL); break;
daniel@transgaming.com45d03582010-03-11 19:41:29 +0000444 case EOpMulAssign: outputTriplet(visit, NULL, " *= ", NULL); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000445 case EOpVectorTimesMatrixAssign: UNIMPLEMENTED(); /* FIXME */ out << "matrix mult second child into first child"; break;
daniel@transgaming.com45d03582010-03-11 19:41:29 +0000446 case EOpVectorTimesScalarAssign: outputTriplet(visit, NULL, " *= ", NULL); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000447 case EOpMatrixTimesScalarAssign: UNIMPLEMENTED(); /* FIXME */ out << "matrix scale second child into first child"; break;
448 case EOpMatrixTimesMatrixAssign: UNIMPLEMENTED(); /* FIXME */ out << "matrix mult second child into first child"; break;
daniel@transgaming.com45d03582010-03-11 19:41:29 +0000449 case EOpDivAssign: outputTriplet(visit, NULL, " /= ", NULL); break;
daniel@transgaming.com45d03582010-03-11 19:41:29 +0000450 case EOpIndexDirect: outputTriplet(visit, NULL, "[", "]"); break;
451 case EOpIndexIndirect: outputTriplet(visit, NULL, "[", "]"); break;
452 case EOpIndexDirectStruct: outputTriplet(visit, NULL, ".", NULL); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000453 case EOpVectorSwizzle:
454 if (visit == InVisit)
455 {
456 out << ".";
457
458 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
459
460 if (swizzle)
461 {
462 TIntermSequence &sequence = swizzle->getSequence();
463
464 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
465 {
466 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
467
468 if (element)
469 {
470 int i = element->getUnionArrayPointer()[0].getIConst();
471
472 switch (i)
473 {
474 case 0: out << "x"; break;
475 case 1: out << "y"; break;
476 case 2: out << "z"; break;
477 case 3: out << "w"; break;
478 default: UNREACHABLE();
479 }
480 }
481 else UNREACHABLE();
482 }
483 }
484 else UNREACHABLE();
485
486 return false; // Fully processed
487 }
488 break;
489 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
490 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
491 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
492 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000493 case EOpEqual:
494 if (!node->getLeft()->isMatrix())
495 {
496 outputTriplet(visit, "(", " == ", ")");
497 }
498 else
499 {
500 outputTriplet(visit, "__equal(", ", ", ")");
501 }
502 break;
503 case EOpNotEqual:
504 if (!node->getLeft()->isMatrix())
505 {
506 outputTriplet(visit, "(", " != ", ")");
507 }
508 else
509 {
510 outputTriplet(visit, "!__equal(", ", ", ")");
511 }
512 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000513 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
514 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
515 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
516 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
517 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
518 case EOpVectorTimesMatrix: UNIMPLEMENTED(); /* FIXME */ out << "vector-times-matrix"; break;
519 case EOpMatrixTimesVector: outputTriplet(visit, "mul(", ", ", ")"); break;
520 case EOpMatrixTimesScalar: UNIMPLEMENTED(); /* FIXME */ out << "matrix-scale"; break;
521 case EOpMatrixTimesMatrix: UNIMPLEMENTED(); /* FIXME */ out << "matrix-multiply"; break;
522 case EOpLogicalOr: outputTriplet(visit, "(", " || ", ")"); break;
523 case EOpLogicalXor: outputTriplet(visit, "xor(", ", ", ")"); break; // FIXME: Prevent name clashes
524 case EOpLogicalAnd: outputTriplet(visit, "(", " && ", ")"); break;
525 default: UNREACHABLE();
526 }
527
528 return true;
529}
530
531bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
532{
533 TInfoSinkBase &out = context.infoSink.obj;
534
535 switch (node->getOp())
536 {
537 case EOpNegative: outputTriplet(visit, "(-", NULL, ")"); break;
538 case EOpVectorLogicalNot: outputTriplet(visit, "(!", NULL, ")"); break;
539 case EOpLogicalNot: outputTriplet(visit, "(!", NULL, ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000540 case EOpPostIncrement: outputTriplet(visit, "(", NULL, "++)"); break;
541 case EOpPostDecrement: outputTriplet(visit, "(", NULL, "--)"); break;
542 case EOpPreIncrement: outputTriplet(visit, "(++", NULL, ")"); break;
543 case EOpPreDecrement: outputTriplet(visit, "(--", NULL, ")"); break;
544 case EOpConvIntToBool:
545 case EOpConvFloatToBool:
546 switch (node->getOperand()->getType().getNominalSize())
547 {
548 case 1: outputTriplet(visit, "bool(", NULL, ")"); break;
549 case 2: outputTriplet(visit, "bool2(", NULL, ")"); break;
550 case 3: outputTriplet(visit, "bool3(", NULL, ")"); break;
551 case 4: outputTriplet(visit, "bool4(", NULL, ")"); break;
552 default: UNREACHABLE();
553 }
554 break;
555 case EOpConvBoolToFloat:
556 case EOpConvIntToFloat:
557 switch (node->getOperand()->getType().getNominalSize())
558 {
559 case 1: outputTriplet(visit, "float(", NULL, ")"); break;
560 case 2: outputTriplet(visit, "float2(", NULL, ")"); break;
561 case 3: outputTriplet(visit, "float3(", NULL, ")"); break;
562 case 4: outputTriplet(visit, "float4(", NULL, ")"); break;
563 default: UNREACHABLE();
564 }
565 break;
566 case EOpConvFloatToInt:
567 case EOpConvBoolToInt:
568 switch (node->getOperand()->getType().getNominalSize())
569 {
570 case 1: outputTriplet(visit, "int(", NULL, ")"); break;
571 case 2: outputTriplet(visit, "int2(", NULL, ")"); break;
572 case 3: outputTriplet(visit, "int3(", NULL, ")"); break;
573 case 4: outputTriplet(visit, "int4(", NULL, ")"); break;
574 default: UNREACHABLE();
575 }
576 break;
577 case EOpRadians: outputTriplet(visit, "radians(", NULL, ")"); break;
578 case EOpDegrees: outputTriplet(visit, "degrees(", NULL, ")"); break;
579 case EOpSin: outputTriplet(visit, "sin(", NULL, ")"); break;
580 case EOpCos: outputTriplet(visit, "cos(", NULL, ")"); break;
581 case EOpTan: outputTriplet(visit, "tan(", NULL, ")"); break;
582 case EOpAsin: outputTriplet(visit, "asin(", NULL, ")"); break;
583 case EOpAcos: outputTriplet(visit, "acos(", NULL, ")"); break;
584 case EOpAtan: outputTriplet(visit, "atan(", NULL, ")"); break;
585 case EOpExp: outputTriplet(visit, "exp(", NULL, ")"); break;
586 case EOpLog: outputTriplet(visit, "log(", NULL, ")"); break;
587 case EOpExp2: outputTriplet(visit, "exp2(", NULL, ")"); break;
588 case EOpLog2: outputTriplet(visit, "log2(", NULL, ")"); break;
589 case EOpSqrt: outputTriplet(visit, "sqrt(", NULL, ")"); break;
590 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", NULL, ")"); break;
591 case EOpAbs: outputTriplet(visit, "abs(", NULL, ")"); break;
592 case EOpSign: outputTriplet(visit, "sign(", NULL, ")"); break;
593 case EOpFloor: outputTriplet(visit, "floor(", NULL, ")"); break;
594 case EOpCeil: outputTriplet(visit, "ceil(", NULL, ")"); break;
595 case EOpFract: outputTriplet(visit, "frac(", NULL, ")"); break;
596 case EOpLength: outputTriplet(visit, "length(", NULL, ")"); break;
597 case EOpNormalize: outputTriplet(visit, "normalize(", NULL, ")"); break;
daniel@transgaming.com45d03582010-03-11 19:41:29 +0000598// case EOpDPdx: outputTriplet(visit, "ddx(", NULL, ")"); break;
599// case EOpDPdy: outputTriplet(visit, "ddy(", NULL, ")"); break;
600// case EOpFwidth: outputTriplet(visit, "fwidth(", NULL, ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000601 case EOpAny: outputTriplet(visit, "any(", NULL, ")"); break;
602 case EOpAll: outputTriplet(visit, "all(", NULL, ")"); break;
603 default: UNREACHABLE();
604 }
605
606 return true;
607}
608
609bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
610{
611 EShLanguage language = context.language;
612 TInfoSinkBase &out = context.infoSink.obj;
613
614 if (node->getOp() == EOpNull)
615 {
616 out.message(EPrefixError, "node is still EOpNull!");
617 return true;
618 }
619
620 switch (node->getOp())
621 {
622 case EOpSequence: outputTriplet(visit, NULL, ";\n", ";\n"); break;
623 case EOpDeclaration:
624 if (visit == PreVisit)
625 {
626 TIntermSequence &sequence = node->getSequence();
627 TIntermTyped *variable = sequence[0]->getAsTyped();
628 bool visit = true;
629
630 if (variable && variable->getQualifier() == EvqTemporary)
631 {
632 out << typeString(variable->getType()) + " ";
633
634 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
635 {
636 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
637
638 if (symbol)
639 {
640 symbol->traverse(this);
641
642 out << arrayString(symbol->getType());
643 }
644 else
645 {
646 (*sit)->traverse(this);
647 }
648
649 if (visit && this->inVisit)
650 {
651 if (*sit != sequence.back())
652 {
653 visit = this->visitAggregate(InVisit, node);
654 }
655 }
656 }
657
658 if (visit && this->postVisit)
659 {
660 this->visitAggregate(PostVisit, node);
661 }
662 }
663
664 return false;
665 }
666 else if (visit == InVisit)
667 {
668 out << ", ";
669 }
670 break;
671 case EOpComma: UNIMPLEMENTED(); /* FIXME */ out << "Comma\n"; return true;
672 case EOpFunction:
673 {
674 const TString &mangledName = node->getName();
675 TString name = TString(mangledName.c_str(), mangledName.find_first_of('('));
676
677 if (visit == PreVisit)
678 {
679 if (name == "main")
680 {
681 TSymbolTableLevel *symbols = context.symbolTable.getGlobalLevel();
682
683 if (language == EShLangFragment)
684 {
685 out << "PS_OUTPUT main(PS_INPUT input)\n" // FIXME: Prevent name clashes
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +0000686 "{\n"
687 " float rhw = 1.0 / input.gl_FragCoord.w;\n"
688 " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * gl_Window.x + gl_Window.z;\n"
689 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * gl_Window.y + gl_Window.w;\n"
690 " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * gl_Depth.x + gl_Depth.y;\n"
daniel@transgaming.com79b820b2010-03-16 05:48:57 +0000691 " gl_FragCoord.w = rhw;\n"
692 " gl_FrontFacing = __frontCCW ? (input.__vFace >= 0.0) : (input.__vFace <= 0.0);\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000693
694 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
695 {
696 const TSymbol *symbol = (*namedSymbol).second;
697 const TString &name = symbol->getName();
698
699 if (symbol->isVariable())
700 {
701 const TVariable *variable = static_cast<const TVariable*>(symbol);
702 const TType &type = variable->getType();
703 TQualifier qualifier = type.getQualifier();
704
705 if(qualifier == EvqVaryingIn)
706 {
707 out << " " + name + " = input." + name + ";\n"; // FIXME: Prevent name clashes
708 }
709 }
710 }
711 }
712 else
713 {
714 out << "VS_OUTPUT main(VS_INPUT input)\n" // FIXME: Prevent name clashes
715 "{\n";
716
717 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
718 {
719 const TSymbol *symbol = (*namedSymbol).second;
720 const TString &name = symbol->getName();
721
722 if (symbol->isVariable())
723 {
724 const TVariable *variable = static_cast<const TVariable*>(symbol);
725 const TType &type = variable->getType();
726 TQualifier qualifier = type.getQualifier();
727
728 if (qualifier == EvqAttribute)
729 {
730 out << " " + name + " = input." + name + ";\n"; // FIXME: Prevent name clashes
731 }
732 }
733 }
734 }
735
736 // Erase the (empty) argument list
737 TIntermSequence &sequence = node->getSequence();
738 sequence.erase(sequence.begin());
739 }
740 else
741 {
742 out << typeString(node->getType()) << " " << name << "(";
743
744 TIntermSequence &sequence = node->getSequence();
745 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
746
747 for (unsigned int i = 0; i < arguments.size(); i++)
748 {
749 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
750
751 if (symbol)
752 {
753 const TType &type = symbol->getType();
754 const TString &name = symbol->getSymbol();
755
756 out << typeString(type) + " " + name;
757
758 if(i < arguments.size() - 1)
759 {
760 out << ", ";
761 }
762 }
763 else UNREACHABLE();
764 }
765
766 sequence.erase(sequence.begin());
767
768 out << ")\n"
769 "{\n";
770 }
771 }
772 else if (visit == PostVisit)
773 {
774 if (name == "main")
775 {
776 if (language == EShLangFragment)
777 {
778 out << " PS_OUTPUT output;\n" // FIXME: Prevent name clashes
779 " output.gl_Color[0] = gl_Color[0];\n"; // FIXME: Prevent name clashes
780
781 TSymbolTableLevel *symbols = context.symbolTable.getGlobalLevel();
782
783 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
784 {
785 const TSymbol *symbol = (*namedSymbol).second;
786 const TString &name = symbol->getName();
787 }
788 }
789 else
790 {
791 out << " VS_OUTPUT output;\n" // FIXME: Prevent name clashes
792 " output.gl_Position.x = gl_Position.x - gl_HalfPixelSize.x * gl_Position.w;\n"
793 " output.gl_Position.y = -(gl_Position.y - gl_HalfPixelSize.y * gl_Position.w);\n"
794 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
795 " output.gl_Position.w = gl_Position.w;\n"
daniel@transgaming.comccad59f2010-03-26 04:08:39 +0000796 " output.gl_PointSize = 1.0;\n"
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +0000797 " output.gl_FragCoord = gl_Position;\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000798
799 TSymbolTableLevel *symbols = context.symbolTable.getGlobalLevel();
800
801 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
802 {
803 const TSymbol *symbol = (*namedSymbol).second;
804 const TString &name = symbol->getName();
805
806 if (symbol->isVariable())
807 {
808 const TVariable *variable = static_cast<const TVariable*>(symbol);
809 TQualifier qualifier = variable->getType().getQualifier();
810
811 if(qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut)
812 {
813 out << " output." + name + " = " + name + ";\n"; // FIXME: Prevent name clashes
814 }
815 }
816 }
817 }
818
819 out << " return output;\n" // FIXME: Prevent name clashes
820 "}\n";
821 }
822 else
823 {
824 out << "}\n";
825 }
826 }
827 }
828 break;
829 case EOpFunctionCall:
830 {
831 if (visit == PreVisit)
832 {
833 const TString &mangledName = node->getName();
834 TString name = TString(mangledName.c_str(), mangledName.find_first_of('('));
835
836 if (node->isUserDefined())
837 {
838 out << name << "(";
839 }
840 else
841 {
842 if (name == "texture2D")
843 {
844 if (node->getSequence().size() == 2)
845 {
846 out << "gl_texture2D(";
847 }
848 else if (node->getSequence().size() == 3)
849 {
850 out << "gl_texture2DBias(";
851 }
852 else UNREACHABLE();
853 }
854 else if (name == "texture2DProj")
855 {
856 out << "gl_texture2DProj(";
857 }
858 else if (name == "texture2DLod")
859 {
860 out << "gl_texture2DLod(";
861 UNIMPLEMENTED(); // FIXME: Move lod to last texture coordinate component
862 }
863 else if (name == "texture2DProjLod")
864 {
865 out << "gl_texture2DProjLod(";
866 UNIMPLEMENTED(); // FIXME: Move lod to last texture coordinate component
867 }
868 else if (name == "textureCube")
869 {
870 out << "gl_textureCube("; // FIXME: Incorrect sampling location
871 }
872 else
873 {
874 UNIMPLEMENTED(); // FIXME
875 }
876 }
877 }
878 else if (visit == InVisit)
879 {
880 out << ", ";
881 }
882 else
883 {
884 out << ")";
885 }
886 }
887 break;
888 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
889 case EOpConstructFloat: outputTriplet(visit, "vec1(", NULL, ")"); break;
890 case EOpConstructVec2: outputTriplet(visit, "vec2(", ", ", ")"); break;
891 case EOpConstructVec3: outputTriplet(visit, "vec3(", ", ", ")"); break;
892 case EOpConstructVec4: outputTriplet(visit, "vec4(", ", ", ")"); break;
893 case EOpConstructBool: UNIMPLEMENTED(); /* FIXME */ out << "Construct bool"; break;
894 case EOpConstructBVec2: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec2"; break;
895 case EOpConstructBVec3: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec3"; break;
896 case EOpConstructBVec4: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec4"; break;
897 case EOpConstructInt: UNIMPLEMENTED(); /* FIXME */ out << "Construct int"; break;
898 case EOpConstructIVec2: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec2"; break;
899 case EOpConstructIVec3: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec3"; break;
900 case EOpConstructIVec4: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec4"; break;
901 case EOpConstructMat2: outputTriplet(visit, "float2x2(", ", ", ")"); break;
902 case EOpConstructMat3: outputTriplet(visit, "float3x3(", ", ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000903 case EOpConstructMat4: outputTriplet(visit, "float4x4(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904 case EOpConstructStruct: UNIMPLEMENTED(); /* FIXME */ out << "Construct structure"; break;
905 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
906 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
907 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
908 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
909 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
910 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
911 case EOpMod: outputTriplet(visit, "mod(", ", ", ")"); break; // FIXME: Prevent name clashes
912 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
913 case EOpAtan:
914 if (node->getSequence().size() == 1)
915 {
916 outputTriplet(visit, "atan(", ", ", ")");
917 }
918 else if (node->getSequence().size() == 2)
919 {
920 outputTriplet(visit, "atan2(", ", ", ")");
921 }
922 else UNREACHABLE();
923 break;
924 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
925 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
926 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
927 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
928 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
929 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
930 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
931 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
932 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000933 case EOpFaceForward: outputTriplet(visit, "faceforward(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000934 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
935 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
936 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000937 default: UNREACHABLE();
938 }
939
940 return true;
941}
942
943bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
944{
945 TInfoSinkBase &out = context.infoSink.obj;
946
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +0000947 if(node->getType().getBasicType() == EbtVoid) // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000948 {
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +0000949 out << "if(";
950
951 node->getCondition()->traverse(this);
952
953 out << ")\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954 "{\n";
955
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +0000956 node->getTrueBlock()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957
958 out << ";}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +0000959
960 if (node->getFalseBlock())
961 {
962 out << "else\n"
963 "{\n";
964
965 node->getFalseBlock()->traverse(this);
966
967 out << ";}\n";
968 }
969 }
970 else // Ternary operator expression
971 {
972 out << "(";
973 node->getCondition()->traverse(this);
974 out << ") ? (";
975 node->getTrueBlock()->traverse(this);
976 out << ") : (";
977 node->getFalseBlock()->traverse(this);
978 out << ")\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000979 }
980
981 return false;
982}
983
984void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
985{
986 TInfoSinkBase &out = context.infoSink.obj;
987
daniel@transgaming.com45d03582010-03-11 19:41:29 +0000988 TType &type = node->getType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000989
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000990 if (type.isField())
991 {
992 out << type.getFieldName();
993 }
994 else
995 {
996 int size = type.getObjectSize();
997 bool matrix = type.isMatrix();
998 TBasicType basicType = node->getUnionArrayPointer()[0].getType();
999
1000 switch (basicType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001001 {
1002 case EbtBool:
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001003 if (!matrix)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001004 {
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001005 switch (size)
1006 {
1007 case 1: out << "bool("; break;
1008 case 2: out << "bool2("; break;
1009 case 3: out << "bool3("; break;
1010 case 4: out << "bool4("; break;
1011 default: UNREACHABLE();
1012 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001013 }
1014 else
1015 {
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001016 UNIMPLEMENTED();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001017 }
1018 break;
1019 case EbtFloat:
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001020 if (!matrix)
1021 {
1022 switch (size)
1023 {
1024 case 1: out << "float("; break;
1025 case 2: out << "float2("; break;
1026 case 3: out << "float3("; break;
1027 case 4: out << "float4("; break;
1028 default: UNREACHABLE();
1029 }
1030 }
1031 else
1032 {
1033 switch (size)
1034 {
1035 case 4: out << "float2x2("; break;
1036 case 9: out << "float3x3("; break;
1037 case 16: out << "float4x4("; break;
1038 default: UNREACHABLE();
1039 }
1040 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001041 break;
1042 case EbtInt:
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001043 if (!matrix)
1044 {
1045 switch (size)
1046 {
1047 case 1: out << "int("; break;
1048 case 2: out << "int2("; break;
1049 case 3: out << "int3("; break;
1050 case 4: out << "int4("; break;
1051 default: UNREACHABLE();
1052 }
1053 }
1054 else
1055 {
1056 UNIMPLEMENTED();
1057 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001058 break;
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001059 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001060 UNIMPLEMENTED(); // FIXME
1061 }
1062
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001063 for (int i = 0; i < size; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001064 {
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001065 switch (basicType)
1066 {
1067 case EbtBool:
1068 if (node->getUnionArrayPointer()[i].getBConst())
1069 {
1070 out << "true";
1071 }
1072 else
1073 {
1074 out << "false";
1075 }
1076 break;
1077 case EbtFloat:
1078 out << node->getUnionArrayPointer()[i].getFConst();
1079 break;
1080 case EbtInt:
1081 out << node->getUnionArrayPointer()[i].getIConst();
1082 break;
1083 default:
1084 UNIMPLEMENTED(); // FIXME
1085 }
1086
1087 if (i != size - 1)
1088 {
1089 out << ", ";
1090 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001091 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001092
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001093 out << ")";
1094 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001095}
1096
1097bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
1098{
1099 TInfoSinkBase &out = context.infoSink.obj;
1100
1101 if (!node->testFirst())
1102 {
1103 out << "do\n"
1104 "{\n";
1105 }
1106 else
1107 {
1108 out << "for(";
1109
1110 if (node->getInit())
1111 {
1112 node->getInit()->traverse(this);
1113 }
1114
1115 out << "; ";
1116
1117 if (node->getTest())
1118 {
1119 node->getTest()->traverse(this);
1120 }
1121
1122 out << "; ";
1123
1124 if (node->getTerminal())
1125 {
1126 node->getTerminal()->traverse(this);
1127 }
1128
1129 out << ")\n"
1130 "{\n";
1131 }
1132
1133 if (node->getBody())
1134 {
1135 node->getBody()->traverse(this);
1136 }
1137
1138 out << "}\n";
1139
1140 if (!node->testFirst())
1141 {
1142 out << "while(\n";
1143
1144 node->getTest()->traverse(this);
1145
1146 out << ")";
1147 }
1148
1149 out << ";\n";
1150
1151 return false;
1152}
1153
1154bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
1155{
1156 TInfoSinkBase &out = context.infoSink.obj;
1157
1158 switch (node->getFlowOp())
1159 {
daniel@transgaming.comf67f82e2010-03-17 03:58:54 +00001160 case EOpKill: outputTriplet(visit, "discard", NULL, NULL); break;
1161 case EOpBreak: outputTriplet(visit, "break", NULL, NULL); break;
1162 case EOpContinue: outputTriplet(visit, "continue", NULL, NULL); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001163 case EOpReturn:
1164 if (visit == PreVisit)
1165 {
1166 if (node->getExpression())
1167 {
1168 out << "return ";
1169 }
1170 else
1171 {
1172 out << "return;\n";
1173 }
1174 }
1175 else if (visit == PostVisit)
1176 {
1177 out << ";\n";
1178 }
1179 break;
1180 default: UNREACHABLE();
1181 }
1182
1183 return true;
1184}
1185
1186void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
1187{
1188 TInfoSinkBase &out = context.infoSink.obj;
1189
1190 if (visit == PreVisit && preString)
1191 {
1192 out << preString;
1193 }
1194 else if (visit == InVisit && inString)
1195 {
1196 out << inString;
1197 }
1198 else if (visit == PostVisit && postString)
1199 {
1200 out << postString;
1201 }
1202}
1203
1204TString OutputHLSL::typeString(const TType &type)
1205{
1206 if (type.isMatrix())
1207 {
1208 switch (type.getNominalSize())
1209 {
1210 case 2: return "float2x2";
1211 case 3: return "float3x3";
1212 case 4: return "float4x4";
1213 }
1214 }
1215 else
1216 {
1217 switch (type.getBasicType())
1218 {
1219 case EbtFloat:
1220 switch (type.getNominalSize())
1221 {
1222 case 1: return "float";
1223 case 2: return "float2";
1224 case 3: return "float3";
1225 case 4: return "float4";
1226 }
1227 case EbtInt:
1228 switch (type.getNominalSize())
1229 {
1230 case 1: return "int";
1231 case 2: return "int2";
1232 case 3: return "int3";
1233 case 4: return "int4";
1234 }
1235 case EbtBool:
1236 switch (type.getNominalSize())
1237 {
1238 case 1: return "bool";
1239 case 2: return "bool2";
1240 case 3: return "bool3";
1241 case 4: return "bool4";
1242 }
1243 case EbtVoid:
1244 return "void";
1245 case EbtSampler2D:
1246 return "sampler2D";
1247 case EbtSamplerCube:
1248 return "samplerCUBE";
1249 }
1250 }
1251
1252 UNIMPLEMENTED(); // FIXME
1253 return "<unknown type>";
1254}
1255
1256TString OutputHLSL::arrayString(const TType &type)
1257{
1258 if (!type.isArray())
1259 {
1260 return "";
1261 }
1262
1263 char buffer[100];
1264 sprintf(buffer, "[%d]", type.getArraySize());
1265
1266 return buffer;
1267}
1268
1269TString OutputHLSL::initializer(const TType &type)
1270{
1271 TString string;
1272
1273 int arraySize = type.isArray() ? type.getArraySize() : 1;
1274
1275 if (type.isArray())
1276 {
1277 string += "{";
1278 }
1279
1280 for (int element = 0; element < arraySize; element++)
1281 {
1282 string += typeString(type) + "(";
1283
1284 for (int component = 0; component < type.getNominalSize(); component++)
1285 {
1286 string += "0";
1287
1288 if (component < type.getNominalSize() - 1)
1289 {
1290 string += ", ";
1291 }
1292 }
1293
1294 string += ")";
1295
1296 if (element < arraySize - 1)
1297 {
1298 string += ", ";
1299 }
1300 }
1301
1302 if (type.isArray())
1303 {
1304 string += "}";
1305 }
1306
1307 return string;
1308}
1309}