blob: eaa1589020fadc828477392e2a704583b1ea7116 [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
9#include "InfoSink.h"
10#include "debug.h"
11
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 }
56 }
57 }
58
59 out << uniforms;
60 out << "\n";
61 out << "struct PS_INPUT\n" // FIXME: Prevent name clashes
62 "{\n";
63 out << varyingInput;
64 out << "};\n"
65 "\n";
66 out << varyingGlobals;
67 out << "\n"
68 "struct PS_OUTPUT\n" // FIXME: Prevent name clashes
69 "{\n"
70 " float4 gl_Color[1] : COLOR;\n"
71 "};\n"
72 "\n"
73 "static float4 gl_Color[1] = {float4(0, 0, 0, 0)};\n"
74 "\n"
75 "float4 gl_texture2D(sampler2D s, float2 t)\n"
76 "{\n"
77 " return tex2D(s, t);\n"
78 "}\n"
79 "\n"
80 "float4 gl_texture2DProj(sampler2D s, float3 t)\n"
81 "{\n"
82 " return tex2Dproj(s, float4(t.x, t.y, 0, t.z));\n"
83 "}\n"
84 "float4 gl_texture2DBias(sampler2D s, float2 t, float bias)\n"
85 "{\n"
86 " return tex2Dbias(s, float4(t.x, t.y, 0, bias));\n"
87 "}\n"
88 "\n"
89 "float4 gl_textureCube(samplerCUBE s, float3 t)\n"
90 "{\n"
91 " return texCUBE(s, t);\n"
92 "}\n"
93 "\n";
94 }
95 else
96 {
97 TString uniforms;
98 TString attributeInput;
99 TString attributeGlobals;
100 TString varyingOutput;
101 TString varyingGlobals;
102 TString globals;
103
104 TSymbolTableLevel *symbols = context.symbolTable.getGlobalLevel();
105 int semanticIndex = 0;
106
107 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
108 {
109 const TSymbol *symbol = (*namedSymbol).second;
110 const TString &name = symbol->getName();
111
112 if (symbol->isVariable())
113 {
114 const TVariable *variable = static_cast<const TVariable*>(symbol);
115 const TType &type = variable->getType();
116 TQualifier qualifier = type.getQualifier();
117
118 if (qualifier == EvqUniform)
119 {
120 uniforms += "uniform " + typeString(type) + " " + name + arrayString(type) + ";\n";
121 }
122 else if (qualifier == EvqAttribute)
123 {
124 char semantic[100];
125 sprintf(semantic, " : TEXCOORD%d", semanticIndex);
126 semanticIndex += type.isArray() ? type.getArraySize() : 1;
127
128 attributeInput += " " + typeString(type) + " " + name + arrayString(type) + semantic + ";\n";
129 attributeGlobals += "static " + typeString(type) + " " + name + arrayString(type) + " = " + initializer(type) + ";\n";
130 }
131 else if (qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut)
132 {
133 varyingOutput += " " + typeString(type) + " " + name + arrayString(type) + " : TEXCOORD0;\n"; // Actual semantic index assigned during link
134 varyingGlobals += "static " + typeString(type) + " " + name + arrayString(type) + " = " + initializer(type) + ";\n";
135 }
136 else if (qualifier == EvqGlobal)
137 {
138 globals += typeString(type) + " " + name + arrayString(type) + ";\n";
139 }
140 else UNREACHABLE();
141 }
142 }
143
144 out << "uniform float2 gl_HalfPixelSize;\n";
145 out << "\n";
146 out << uniforms;
147 out << "\n";
148 out << globals;
149 out << "\n";
150 out << "struct VS_INPUT\n" // FIXME: Prevent name clashes
151 "{\n";
152 out << attributeInput;
153 out << "};\n"
154 "\n";
155 out << attributeGlobals;
156 out << "\n"
157 "struct VS_OUTPUT\n" // FIXME: Prevent name clashes
158 "{\n"
159 " float4 gl_Position : POSITION;\n"
160 " float gl_PointSize : PSIZE;\n";
161 out << varyingOutput;
162 out << "};\n"
163 "\n"
164 "static float4 gl_Position = float4(0, 0, 0, 0);\n"
165 "static float gl_PointSize = float(0);\n";
166 out << varyingGlobals;
167 out << "\n";
168 }
169
170 out << "float vec1(float x)\n" // FIXME: Prevent name clashes
171 "{\n"
172 " return x;\n"
173 "}\n"
174 "\n"
175 "float vec1(float2 xy)\n" // FIXME: Prevent name clashes
176 "{\n"
177 " return xy[0];\n"
178 "}\n"
179 "\n"
180 "float vec1(float3 xyz)\n" // FIXME: Prevent name clashes
181 "{\n"
182 " return xyz[0];\n"
183 "}\n"
184 "\n"
185 "float vec1(float4 xyzw)\n" // FIXME: Prevent name clashes
186 "{\n"
187 " return xyzw[0];\n"
188 "}\n"
189 "\n"
190 "float2 vec2(float x)\n" // FIXME: Prevent name clashes
191 "{\n"
192 " return float2(x, x);\n"
193 "}\n"
194 "\n"
195 "float2 vec2(float x, float y)\n" // FIXME: Prevent name clashes
196 "{\n"
197 " return float2(x, y);\n"
198 "}\n"
199 "\n"
200 "float2 vec2(float2 xy)\n" // FIXME: Prevent name clashes
201 "{\n"
202 " return xy;\n"
203 "}\n"
204 "\n"
205 "float2 vec2(float3 xyz)\n" // FIXME: Prevent name clashes
206 "{\n"
207 " return float2(xyz[0], xyz[1]);\n"
208 "}\n"
209 "\n"
210 "float2 vec2(float4 xyzw)\n" // FIXME: Prevent name clashes
211 "{\n"
212 " return float2(xyzw[0], xyzw[1]);\n"
213 "}\n"
214 "\n"
215 "float3 vec3(float x)\n" // FIXME: Prevent name clashes
216 "{\n"
217 " return float3(x, x, x);\n"
218 "}\n"
219 "\n"
220 "float3 vec3(float x, float y, float z)\n" // FIXME: Prevent name clashes
221 "{\n"
222 " return float3(x, y, z);\n"
223 "}\n"
224 "\n"
225 "float3 vec3(float2 xy, float z)\n" // FIXME: Prevent name clashes
226 "{\n"
227 " return float3(xy[0], xy[1], z);\n"
228 "}\n"
229 "\n"
230 "float3 vec3(float x, float2 yz)\n" // FIXME: Prevent name clashes
231 "{\n"
232 " return float3(x, yz[0], yz[1]);\n"
233 "}\n"
234 "\n"
235 "float3 vec3(float3 xyz)\n" // FIXME: Prevent name clashes
236 "{\n"
237 " return xyz;\n"
238 "}\n"
239 "\n"
240 "float3 vec3(float4 xyzw)\n" // FIXME: Prevent name clashes
241 "{\n"
242 " return float3(xyzw[0], xyzw[1], xyzw[2]);\n"
243 "}\n"
244 "\n"
245 "float4 vec4(float x)\n" // FIXME: Prevent name clashes
246 "{\n"
247 " return float4(x, x, x, x);\n"
248 "}\n"
249 "\n"
250 "float4 vec4(float x, float y, float z, float w)\n" // FIXME: Prevent name clashes
251 "{\n"
252 " return float4(x, y, z, w);\n"
253 "}\n"
254 "\n"
255 "float4 vec4(float2 xy, float z, float w)\n" // FIXME: Prevent name clashes
256 "{\n"
257 " return float4(xy[0], xy[1], z, w);\n"
258 "}\n"
259 "\n"
260 "float4 vec4(float x, float2 yz, float w)\n" // FIXME: Prevent name clashes
261 "{\n"
262 " return float4(x, yz[0], yz[1], w);\n"
263 "}\n"
264 "\n"
265 "float4 vec4(float x, float y, float2 zw)\n" // FIXME: Prevent name clashes
266 "{\n"
267 " return float4(x, y, zw[0], zw[1]);\n"
268 "}\n"
269 "\n"
270 "float4 vec4(float2 xy, float2 zw)\n" // FIXME: Prevent name clashes
271 "{\n"
272 " return float4(xy[0], xy[1], zw[0], zw[1]);\n"
273 "}\n"
274 "\n"
275 "float4 vec4(float3 xyz, float w)\n" // FIXME: Prevent name clashes
276 "{\n"
277 " return float4(xyz[0], xyz[1], xyz[2], w);\n"
278 "}\n"
279 "\n"
280 "float4 vec4(float x, float3 yzw)\n" // FIXME: Prevent name clashes
281 "{\n"
282 " return float4(x, yzw[0], yzw[1], yzw[2]);\n"
283 "}\n"
284 "\n"
285 "float4 vec4(float4 xyzw)\n" // FIXME: Prevent name clashes
286 "{\n"
287 " return xyzw;\n"
288 "}\n"
289 "\n"
290 "bool xor(bool p, bool q)\n" // FIXME: Prevent name clashes
291 "{\n"
292 " return (p || q) && !(p && q);\n"
293 "}\n"
294 "\n"
295 "float mod(float x, float y)\n" // FIXME: Prevent name clashes
296 "{\n"
297 " return x - y * floor(x / y);\n"
298 "}\n"
299 "float2 mod(float2 x, float y)\n" // FIXME: Prevent name clashes
300 "{\n"
301 " return x - y * floor(x / y);\n"
302 "}\n"
303 "float3 mod(float3 x, float y)\n" // FIXME: Prevent name clashes
304 "{\n"
305 " return x - y * floor(x / y);\n"
306 "}\n"
307 "float4 mod(float4 x, float y)\n" // FIXME: Prevent name clashes
308 "{\n"
309 " return x - y * floor(x / y);\n"
310 "}\n"
311 "\n";
312}
313
314void OutputHLSL::visitSymbol(TIntermSymbol *node)
315{
316 TInfoSinkBase &out = context.infoSink.obj;
317
318 TString name = node->getSymbol();
319
320 if (name == "gl_FragColor")
321 {
322 out << "gl_Color[0]";
323 }
324 else if (name == "gl_FragData")
325 {
326 out << "gl_Color";
327 }
328 else
329 {
330 out << name;
331 }
332}
333
334bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
335{
336 TInfoSinkBase &out = context.infoSink.obj;
337
338 switch (node->getOp())
339 {
340 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
341 case EOpInitialize: outputTriplet(visit, NULL, " = ", NULL); break;
342 case EOpAddAssign: outputTriplet(visit, NULL, " += ", NULL); break;
343 case EOpSubAssign: outputTriplet(visit, NULL, " -= ", NULL); break;
344 case EOpMulAssign: outputTriplet(visit, NULL, " *= ", NULL); break;
345 case EOpVectorTimesMatrixAssign: UNIMPLEMENTED(); /* FIXME */ out << "matrix mult second child into first child"; break;
346 case EOpVectorTimesScalarAssign: outputTriplet(visit, NULL, " *= ", NULL); break;
347 case EOpMatrixTimesScalarAssign: UNIMPLEMENTED(); /* FIXME */ out << "matrix scale second child into first child"; break;
348 case EOpMatrixTimesMatrixAssign: UNIMPLEMENTED(); /* FIXME */ out << "matrix mult second child into first child"; break;
349 case EOpDivAssign: outputTriplet(visit, NULL, " /= ", NULL); break;
350 case EOpModAssign: UNIMPLEMENTED(); /* FIXME */ out << "mod second child into first child"; break;
351 case EOpAndAssign: UNIMPLEMENTED(); /* FIXME */ out << "and second child into first child"; break;
352 case EOpInclusiveOrAssign: UNIMPLEMENTED(); /* FIXME */ out << "or second child into first child"; break;
353 case EOpExclusiveOrAssign: UNIMPLEMENTED(); /* FIXME */ out << "exclusive or second child into first child"; break;
354 case EOpLeftShiftAssign: UNIMPLEMENTED(); /* FIXME */ out << "left shift second child into first child"; break;
355 case EOpRightShiftAssign: UNIMPLEMENTED(); /* FIXME */ out << "right shift second child into first child"; break;
356 case EOpIndexDirect: outputTriplet(visit, NULL, "[", "]"); break;
357 case EOpIndexIndirect: outputTriplet(visit, NULL, "[", "]"); break;
358 case EOpIndexDirectStruct: UNIMPLEMENTED(); /* FIXME */ out << "direct index for structure"; break;
359 case EOpVectorSwizzle:
360 if (visit == InVisit)
361 {
362 out << ".";
363
364 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
365
366 if (swizzle)
367 {
368 TIntermSequence &sequence = swizzle->getSequence();
369
370 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
371 {
372 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
373
374 if (element)
375 {
376 int i = element->getUnionArrayPointer()[0].getIConst();
377
378 switch (i)
379 {
380 case 0: out << "x"; break;
381 case 1: out << "y"; break;
382 case 2: out << "z"; break;
383 case 3: out << "w"; break;
384 default: UNREACHABLE();
385 }
386 }
387 else UNREACHABLE();
388 }
389 }
390 else UNREACHABLE();
391
392 return false; // Fully processed
393 }
394 break;
395 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
396 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
397 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
398 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
399 case EOpMod: UNIMPLEMENTED(); /* FIXME */ out << "mod"; break;
400 case EOpRightShift: UNIMPLEMENTED(); /* FIXME */ out << "right-shift"; break;
401 case EOpLeftShift: UNIMPLEMENTED(); /* FIXME */ out << "left-shift"; break;
402 case EOpAnd: UNIMPLEMENTED(); /* FIXME */ out << "bitwise and"; break;
403 case EOpInclusiveOr: UNIMPLEMENTED(); /* FIXME */ out << "inclusive-or"; break;
404 case EOpExclusiveOr: UNIMPLEMENTED(); /* FIXME */ out << "exclusive-or"; break;
405 case EOpEqual: outputTriplet(visit, "(", " == ", ")"); break;
406 case EOpNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
407 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
408 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
409 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
410 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
411 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
412 case EOpVectorTimesMatrix: UNIMPLEMENTED(); /* FIXME */ out << "vector-times-matrix"; break;
413 case EOpMatrixTimesVector: outputTriplet(visit, "mul(", ", ", ")"); break;
414 case EOpMatrixTimesScalar: UNIMPLEMENTED(); /* FIXME */ out << "matrix-scale"; break;
415 case EOpMatrixTimesMatrix: UNIMPLEMENTED(); /* FIXME */ out << "matrix-multiply"; break;
416 case EOpLogicalOr: outputTriplet(visit, "(", " || ", ")"); break;
417 case EOpLogicalXor: outputTriplet(visit, "xor(", ", ", ")"); break; // FIXME: Prevent name clashes
418 case EOpLogicalAnd: outputTriplet(visit, "(", " && ", ")"); break;
419 default: UNREACHABLE();
420 }
421
422 return true;
423}
424
425bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
426{
427 TInfoSinkBase &out = context.infoSink.obj;
428
429 switch (node->getOp())
430 {
431 case EOpNegative: outputTriplet(visit, "(-", NULL, ")"); break;
432 case EOpVectorLogicalNot: outputTriplet(visit, "(!", NULL, ")"); break;
433 case EOpLogicalNot: outputTriplet(visit, "(!", NULL, ")"); break;
434 case EOpBitwiseNot: outputTriplet(visit, "(~", NULL, ")"); break;
435 case EOpPostIncrement: outputTriplet(visit, "(", NULL, "++)"); break;
436 case EOpPostDecrement: outputTriplet(visit, "(", NULL, "--)"); break;
437 case EOpPreIncrement: outputTriplet(visit, "(++", NULL, ")"); break;
438 case EOpPreDecrement: outputTriplet(visit, "(--", NULL, ")"); break;
439 case EOpConvIntToBool:
440 case EOpConvFloatToBool:
441 switch (node->getOperand()->getType().getNominalSize())
442 {
443 case 1: outputTriplet(visit, "bool(", NULL, ")"); break;
444 case 2: outputTriplet(visit, "bool2(", NULL, ")"); break;
445 case 3: outputTriplet(visit, "bool3(", NULL, ")"); break;
446 case 4: outputTriplet(visit, "bool4(", NULL, ")"); break;
447 default: UNREACHABLE();
448 }
449 break;
450 case EOpConvBoolToFloat:
451 case EOpConvIntToFloat:
452 switch (node->getOperand()->getType().getNominalSize())
453 {
454 case 1: outputTriplet(visit, "float(", NULL, ")"); break;
455 case 2: outputTriplet(visit, "float2(", NULL, ")"); break;
456 case 3: outputTriplet(visit, "float3(", NULL, ")"); break;
457 case 4: outputTriplet(visit, "float4(", NULL, ")"); break;
458 default: UNREACHABLE();
459 }
460 break;
461 case EOpConvFloatToInt:
462 case EOpConvBoolToInt:
463 switch (node->getOperand()->getType().getNominalSize())
464 {
465 case 1: outputTriplet(visit, "int(", NULL, ")"); break;
466 case 2: outputTriplet(visit, "int2(", NULL, ")"); break;
467 case 3: outputTriplet(visit, "int3(", NULL, ")"); break;
468 case 4: outputTriplet(visit, "int4(", NULL, ")"); break;
469 default: UNREACHABLE();
470 }
471 break;
472 case EOpRadians: outputTriplet(visit, "radians(", NULL, ")"); break;
473 case EOpDegrees: outputTriplet(visit, "degrees(", NULL, ")"); break;
474 case EOpSin: outputTriplet(visit, "sin(", NULL, ")"); break;
475 case EOpCos: outputTriplet(visit, "cos(", NULL, ")"); break;
476 case EOpTan: outputTriplet(visit, "tan(", NULL, ")"); break;
477 case EOpAsin: outputTriplet(visit, "asin(", NULL, ")"); break;
478 case EOpAcos: outputTriplet(visit, "acos(", NULL, ")"); break;
479 case EOpAtan: outputTriplet(visit, "atan(", NULL, ")"); break;
480 case EOpExp: outputTriplet(visit, "exp(", NULL, ")"); break;
481 case EOpLog: outputTriplet(visit, "log(", NULL, ")"); break;
482 case EOpExp2: outputTriplet(visit, "exp2(", NULL, ")"); break;
483 case EOpLog2: outputTriplet(visit, "log2(", NULL, ")"); break;
484 case EOpSqrt: outputTriplet(visit, "sqrt(", NULL, ")"); break;
485 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", NULL, ")"); break;
486 case EOpAbs: outputTriplet(visit, "abs(", NULL, ")"); break;
487 case EOpSign: outputTriplet(visit, "sign(", NULL, ")"); break;
488 case EOpFloor: outputTriplet(visit, "floor(", NULL, ")"); break;
489 case EOpCeil: outputTriplet(visit, "ceil(", NULL, ")"); break;
490 case EOpFract: outputTriplet(visit, "frac(", NULL, ")"); break;
491 case EOpLength: outputTriplet(visit, "length(", NULL, ")"); break;
492 case EOpNormalize: outputTriplet(visit, "normalize(", NULL, ")"); break;
493// case EOpDPdx: outputTriplet(visit, "ddx(", NULL, ")"); break;
494// case EOpDPdy: outputTriplet(visit, "ddy(", NULL, ")"); break;
495// case EOpFwidth: outputTriplet(visit, "fwidth(", NULL, ")"); break;
496 case EOpAny: outputTriplet(visit, "any(", NULL, ")"); break;
497 case EOpAll: outputTriplet(visit, "all(", NULL, ")"); break;
498 default: UNREACHABLE();
499 }
500
501 return true;
502}
503
504bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
505{
506 EShLanguage language = context.language;
507 TInfoSinkBase &out = context.infoSink.obj;
508
509 if (node->getOp() == EOpNull)
510 {
511 out.message(EPrefixError, "node is still EOpNull!");
512 return true;
513 }
514
515 switch (node->getOp())
516 {
517 case EOpSequence: outputTriplet(visit, NULL, ";\n", ";\n"); break;
518 case EOpDeclaration:
519 if (visit == PreVisit)
520 {
521 TIntermSequence &sequence = node->getSequence();
522 TIntermTyped *variable = sequence[0]->getAsTyped();
523 bool visit = true;
524
525 if (variable && variable->getQualifier() == EvqTemporary)
526 {
527 out << typeString(variable->getType()) + " ";
528
529 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
530 {
531 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
532
533 if (symbol)
534 {
535 symbol->traverse(this);
536
537 out << arrayString(symbol->getType());
538 }
539 else
540 {
541 (*sit)->traverse(this);
542 }
543
544 if (visit && this->inVisit)
545 {
546 if (*sit != sequence.back())
547 {
548 visit = this->visitAggregate(InVisit, node);
549 }
550 }
551 }
552
553 if (visit && this->postVisit)
554 {
555 this->visitAggregate(PostVisit, node);
556 }
557 }
558
559 return false;
560 }
561 else if (visit == InVisit)
562 {
563 out << ", ";
564 }
565 break;
566 case EOpComma: UNIMPLEMENTED(); /* FIXME */ out << "Comma\n"; return true;
567 case EOpFunction:
568 {
569 const TString &mangledName = node->getName();
570 TString name = TString(mangledName.c_str(), mangledName.find_first_of('('));
571
572 if (visit == PreVisit)
573 {
574 if (name == "main")
575 {
576 TSymbolTableLevel *symbols = context.symbolTable.getGlobalLevel();
577
578 if (language == EShLangFragment)
579 {
580 out << "PS_OUTPUT main(PS_INPUT input)\n" // FIXME: Prevent name clashes
581 "{\n";
582
583 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
584 {
585 const TSymbol *symbol = (*namedSymbol).second;
586 const TString &name = symbol->getName();
587
588 if (symbol->isVariable())
589 {
590 const TVariable *variable = static_cast<const TVariable*>(symbol);
591 const TType &type = variable->getType();
592 TQualifier qualifier = type.getQualifier();
593
594 if(qualifier == EvqVaryingIn)
595 {
596 out << " " + name + " = input." + name + ";\n"; // FIXME: Prevent name clashes
597 }
598 }
599 }
600 }
601 else
602 {
603 out << "VS_OUTPUT main(VS_INPUT input)\n" // FIXME: Prevent name clashes
604 "{\n";
605
606 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
607 {
608 const TSymbol *symbol = (*namedSymbol).second;
609 const TString &name = symbol->getName();
610
611 if (symbol->isVariable())
612 {
613 const TVariable *variable = static_cast<const TVariable*>(symbol);
614 const TType &type = variable->getType();
615 TQualifier qualifier = type.getQualifier();
616
617 if (qualifier == EvqAttribute)
618 {
619 out << " " + name + " = input." + name + ";\n"; // FIXME: Prevent name clashes
620 }
621 }
622 }
623 }
624
625 // Erase the (empty) argument list
626 TIntermSequence &sequence = node->getSequence();
627 sequence.erase(sequence.begin());
628 }
629 else
630 {
631 out << typeString(node->getType()) << " " << name << "(";
632
633 TIntermSequence &sequence = node->getSequence();
634 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
635
636 for (unsigned int i = 0; i < arguments.size(); i++)
637 {
638 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
639
640 if (symbol)
641 {
642 const TType &type = symbol->getType();
643 const TString &name = symbol->getSymbol();
644
645 out << typeString(type) + " " + name;
646
647 if(i < arguments.size() - 1)
648 {
649 out << ", ";
650 }
651 }
652 else UNREACHABLE();
653 }
654
655 sequence.erase(sequence.begin());
656
657 out << ")\n"
658 "{\n";
659 }
660 }
661 else if (visit == PostVisit)
662 {
663 if (name == "main")
664 {
665 if (language == EShLangFragment)
666 {
667 out << " PS_OUTPUT output;\n" // FIXME: Prevent name clashes
668 " output.gl_Color[0] = gl_Color[0];\n"; // FIXME: Prevent name clashes
669
670 TSymbolTableLevel *symbols = context.symbolTable.getGlobalLevel();
671
672 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
673 {
674 const TSymbol *symbol = (*namedSymbol).second;
675 const TString &name = symbol->getName();
676 }
677 }
678 else
679 {
680 out << " VS_OUTPUT output;\n" // FIXME: Prevent name clashes
681 " output.gl_Position.x = gl_Position.x - gl_HalfPixelSize.x * gl_Position.w;\n"
682 " output.gl_Position.y = -(gl_Position.y - gl_HalfPixelSize.y * gl_Position.w);\n"
683 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
684 " output.gl_Position.w = gl_Position.w;\n"
685 " output.gl_PointSize = gl_PointSize;\n";
686
687 TSymbolTableLevel *symbols = context.symbolTable.getGlobalLevel();
688
689 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
690 {
691 const TSymbol *symbol = (*namedSymbol).second;
692 const TString &name = symbol->getName();
693
694 if (symbol->isVariable())
695 {
696 const TVariable *variable = static_cast<const TVariable*>(symbol);
697 TQualifier qualifier = variable->getType().getQualifier();
698
699 if(qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut)
700 {
701 out << " output." + name + " = " + name + ";\n"; // FIXME: Prevent name clashes
702 }
703 }
704 }
705 }
706
707 out << " return output;\n" // FIXME: Prevent name clashes
708 "}\n";
709 }
710 else
711 {
712 out << "}\n";
713 }
714 }
715 }
716 break;
717 case EOpFunctionCall:
718 {
719 if (visit == PreVisit)
720 {
721 const TString &mangledName = node->getName();
722 TString name = TString(mangledName.c_str(), mangledName.find_first_of('('));
723
724 if (node->isUserDefined())
725 {
726 out << name << "(";
727 }
728 else
729 {
730 if (name == "texture2D")
731 {
732 if (node->getSequence().size() == 2)
733 {
734 out << "gl_texture2D(";
735 }
736 else if (node->getSequence().size() == 3)
737 {
738 out << "gl_texture2DBias(";
739 }
740 else UNREACHABLE();
741 }
742 else if (name == "texture2DProj")
743 {
744 out << "gl_texture2DProj(";
745 }
746 else if (name == "texture2DLod")
747 {
748 out << "gl_texture2DLod(";
749 UNIMPLEMENTED(); // FIXME: Move lod to last texture coordinate component
750 }
751 else if (name == "texture2DProjLod")
752 {
753 out << "gl_texture2DProjLod(";
754 UNIMPLEMENTED(); // FIXME: Move lod to last texture coordinate component
755 }
756 else if (name == "textureCube")
757 {
758 out << "gl_textureCube("; // FIXME: Incorrect sampling location
759 }
760 else
761 {
762 UNIMPLEMENTED(); // FIXME
763 }
764 }
765 }
766 else if (visit == InVisit)
767 {
768 out << ", ";
769 }
770 else
771 {
772 out << ")";
773 }
774 }
775 break;
776 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
777 case EOpConstructFloat: outputTriplet(visit, "vec1(", NULL, ")"); break;
778 case EOpConstructVec2: outputTriplet(visit, "vec2(", ", ", ")"); break;
779 case EOpConstructVec3: outputTriplet(visit, "vec3(", ", ", ")"); break;
780 case EOpConstructVec4: outputTriplet(visit, "vec4(", ", ", ")"); break;
781 case EOpConstructBool: UNIMPLEMENTED(); /* FIXME */ out << "Construct bool"; break;
782 case EOpConstructBVec2: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec2"; break;
783 case EOpConstructBVec3: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec3"; break;
784 case EOpConstructBVec4: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec4"; break;
785 case EOpConstructInt: UNIMPLEMENTED(); /* FIXME */ out << "Construct int"; break;
786 case EOpConstructIVec2: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec2"; break;
787 case EOpConstructIVec3: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec3"; break;
788 case EOpConstructIVec4: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec4"; break;
789 case EOpConstructMat2: outputTriplet(visit, "float2x2(", ", ", ")"); break;
790 case EOpConstructMat3: outputTriplet(visit, "float3x3(", ", ", ")"); break;
791 case EOpConstructMat4: UNIMPLEMENTED(); /* FIXME */ out << "Construct mat4"; break;
792 case EOpConstructStruct: UNIMPLEMENTED(); /* FIXME */ out << "Construct structure"; break;
793 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
794 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
795 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
796 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
797 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
798 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
799 case EOpMod: outputTriplet(visit, "mod(", ", ", ")"); break; // FIXME: Prevent name clashes
800 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
801 case EOpAtan:
802 if (node->getSequence().size() == 1)
803 {
804 outputTriplet(visit, "atan(", ", ", ")");
805 }
806 else if (node->getSequence().size() == 2)
807 {
808 outputTriplet(visit, "atan2(", ", ", ")");
809 }
810 else UNREACHABLE();
811 break;
812 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
813 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
814 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
815 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
816 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
817 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
818 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
819 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
820 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
821 case EOpFaceForward: UNIMPLEMENTED(); /* FIXME */ out << "face-forward"; break;
822 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
823 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
824 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
825 case EOpItof: UNIMPLEMENTED(); /* FIXME */ out << "itof"; break;
826 case EOpFtoi: UNIMPLEMENTED(); /* FIXME */ out << "ftoi"; break;
827 case EOpSkipPixels: UNIMPLEMENTED(); /* FIXME */ out << "skipPixels"; break;
828 case EOpReadInput: UNIMPLEMENTED(); /* FIXME */ out << "readInput"; break;
829 case EOpWritePixel: UNIMPLEMENTED(); /* FIXME */ out << "writePixel"; break;
830 case EOpBitmapLsb: UNIMPLEMENTED(); /* FIXME */ out << "bitmapLSB"; break;
831 case EOpBitmapMsb: UNIMPLEMENTED(); /* FIXME */ out << "bitmapMSB"; break;
832 case EOpWriteOutput: UNIMPLEMENTED(); /* FIXME */ out << "writeOutput"; break;
833 case EOpReadPixel: UNIMPLEMENTED(); /* FIXME */ out << "readPixel"; break;
834 default: UNREACHABLE();
835 }
836
837 return true;
838}
839
840bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
841{
842 TInfoSinkBase &out = context.infoSink.obj;
843
844 out << "if(";
845
846 node->getCondition()->traverse(this);
847
848 out << ")\n"
849 "{\n";
850
851 node->getTrueBlock()->traverse(this);
852
853 out << ";}\n";
854
855 if (node->getFalseBlock())
856 {
857 out << "else\n"
858 "{\n";
859
860 node->getFalseBlock()->traverse(this);
861
862 out << ";}\n";
863 }
864
865 return false;
866}
867
868void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
869{
870 TInfoSinkBase &out = context.infoSink.obj;
871
872 int size = node->getType().getObjectSize();
873 bool matrix = node->getType().isMatrix();
874 TBasicType type = node->getUnionArrayPointer()[0].getType();
875
876 switch (type)
877 {
878 case EbtBool:
879 if (!matrix)
880 {
881 switch (size)
882 {
883 case 1: out << "bool("; break;
884 case 2: out << "bool2("; break;
885 case 3: out << "bool3("; break;
886 case 4: out << "bool4("; break;
887 default: UNREACHABLE();
888 }
889 }
890 else
891 {
892 UNIMPLEMENTED();
893 }
894 break;
895 case EbtFloat:
896 if (!matrix)
897 {
898 switch (size)
899 {
900 case 1: out << "float("; break;
901 case 2: out << "float2("; break;
902 case 3: out << "float3("; break;
903 case 4: out << "float4("; break;
904 default: UNREACHABLE();
905 }
906 }
907 else
908 {
909 switch (size)
910 {
911 case 4: out << "float2x2("; break;
912 case 9: out << "float3x3("; break;
913 case 16: out << "float4x4("; break;
914 default: UNREACHABLE();
915 }
916 }
917 break;
918 case EbtInt:
919 if (!matrix)
920 {
921 switch (size)
922 {
923 case 1: out << "int("; break;
924 case 2: out << "int2("; break;
925 case 3: out << "int3("; break;
926 case 4: out << "int4("; break;
927 default: UNREACHABLE();
928 }
929 }
930 else
931 {
932 UNIMPLEMENTED();
933 }
934 break;
935 default:
936 UNIMPLEMENTED(); // FIXME
937 }
938
939 for (int i = 0; i < size; i++)
940 {
941 switch (type)
942 {
943 case EbtBool:
944 if (node->getUnionArrayPointer()[i].getBConst())
945 {
946 out << "true";
947 }
948 else
949 {
950 out << "false";
951 }
952 break;
953 case EbtFloat:
954 out << node->getUnionArrayPointer()[i].getFConst();
955 break;
956 case EbtInt:
957 out << node->getUnionArrayPointer()[i].getIConst();
958 break;
959 default:
960 UNIMPLEMENTED(); // FIXME
961 }
962
963 if (i != size - 1)
964 {
965 out << ", ";
966 }
967 }
968
969 out << ")";
970}
971
972bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
973{
974 TInfoSinkBase &out = context.infoSink.obj;
975
976 if (!node->testFirst())
977 {
978 out << "do\n"
979 "{\n";
980 }
981 else
982 {
983 out << "for(";
984
985 if (node->getInit())
986 {
987 node->getInit()->traverse(this);
988 }
989
990 out << "; ";
991
992 if (node->getTest())
993 {
994 node->getTest()->traverse(this);
995 }
996
997 out << "; ";
998
999 if (node->getTerminal())
1000 {
1001 node->getTerminal()->traverse(this);
1002 }
1003
1004 out << ")\n"
1005 "{\n";
1006 }
1007
1008 if (node->getBody())
1009 {
1010 node->getBody()->traverse(this);
1011 }
1012
1013 out << "}\n";
1014
1015 if (!node->testFirst())
1016 {
1017 out << "while(\n";
1018
1019 node->getTest()->traverse(this);
1020
1021 out << ")";
1022 }
1023
1024 out << ";\n";
1025
1026 return false;
1027}
1028
1029bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
1030{
1031 TInfoSinkBase &out = context.infoSink.obj;
1032
1033 switch (node->getFlowOp())
1034 {
1035 case EOpKill: outputTriplet(visit, "discard", NULL, NULL); break;
1036 case EOpBreak: UNIMPLEMENTED(); /* FIXME */ break;
1037 case EOpContinue: UNIMPLEMENTED(); /* FIXME */ break;
1038 case EOpReturn:
1039 if (visit == PreVisit)
1040 {
1041 if (node->getExpression())
1042 {
1043 out << "return ";
1044 }
1045 else
1046 {
1047 out << "return;\n";
1048 }
1049 }
1050 else if (visit == PostVisit)
1051 {
1052 out << ";\n";
1053 }
1054 break;
1055 default: UNREACHABLE();
1056 }
1057
1058 return true;
1059}
1060
1061void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
1062{
1063 TInfoSinkBase &out = context.infoSink.obj;
1064
1065 if (visit == PreVisit && preString)
1066 {
1067 out << preString;
1068 }
1069 else if (visit == InVisit && inString)
1070 {
1071 out << inString;
1072 }
1073 else if (visit == PostVisit && postString)
1074 {
1075 out << postString;
1076 }
1077}
1078
1079TString OutputHLSL::typeString(const TType &type)
1080{
1081 if (type.isMatrix())
1082 {
1083 switch (type.getNominalSize())
1084 {
1085 case 2: return "float2x2";
1086 case 3: return "float3x3";
1087 case 4: return "float4x4";
1088 }
1089 }
1090 else
1091 {
1092 switch (type.getBasicType())
1093 {
1094 case EbtFloat:
1095 switch (type.getNominalSize())
1096 {
1097 case 1: return "float";
1098 case 2: return "float2";
1099 case 3: return "float3";
1100 case 4: return "float4";
1101 }
1102 case EbtInt:
1103 switch (type.getNominalSize())
1104 {
1105 case 1: return "int";
1106 case 2: return "int2";
1107 case 3: return "int3";
1108 case 4: return "int4";
1109 }
1110 case EbtBool:
1111 switch (type.getNominalSize())
1112 {
1113 case 1: return "bool";
1114 case 2: return "bool2";
1115 case 3: return "bool3";
1116 case 4: return "bool4";
1117 }
1118 case EbtVoid:
1119 return "void";
1120 case EbtSampler2D:
1121 return "sampler2D";
1122 case EbtSamplerCube:
1123 return "samplerCUBE";
1124 }
1125 }
1126
1127 UNIMPLEMENTED(); // FIXME
1128 return "<unknown type>";
1129}
1130
1131TString OutputHLSL::arrayString(const TType &type)
1132{
1133 if (!type.isArray())
1134 {
1135 return "";
1136 }
1137
1138 char buffer[100];
1139 sprintf(buffer, "[%d]", type.getArraySize());
1140
1141 return buffer;
1142}
1143
1144TString OutputHLSL::initializer(const TType &type)
1145{
1146 TString string;
1147
1148 int arraySize = type.isArray() ? type.getArraySize() : 1;
1149
1150 if (type.isArray())
1151 {
1152 string += "{";
1153 }
1154
1155 for (int element = 0; element < arraySize; element++)
1156 {
1157 string += typeString(type) + "(";
1158
1159 for (int component = 0; component < type.getNominalSize(); component++)
1160 {
1161 string += "0";
1162
1163 if (component < type.getNominalSize() - 1)
1164 {
1165 string += ", ";
1166 }
1167 }
1168
1169 string += ")";
1170
1171 if (element < arraySize - 1)
1172 {
1173 string += ", ";
1174 }
1175 }
1176
1177 if (type.isArray())
1178 {
1179 string += "}";
1180 }
1181
1182 return string;
1183}
1184}