blob: bf2491d48296009381b25964245308f294cdf2a9 [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"
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000299 "\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000300 "float2 mod(float2 x, float y)\n" // FIXME: Prevent name clashes
301 "{\n"
302 " return x - y * floor(x / y);\n"
303 "}\n"
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000304 "\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305 "float3 mod(float3 x, float y)\n" // FIXME: Prevent name clashes
306 "{\n"
307 " return x - y * floor(x / y);\n"
308 "}\n"
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000309 "\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310 "float4 mod(float4 x, float y)\n" // FIXME: Prevent name clashes
311 "{\n"
312 " return x - y * floor(x / y);\n"
313 "}\n"
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000314 "\n"
315 "float faceforward(float N, float I, float Nref)\n" // FIXME: Prevent name clashes
316 "{\n"
317 " if(dot(Nref, I) < 0)\n"
318 " {\n"
319 " return N;\n"
320 " }\n"
321 " else\n"
322 " {\n"
323 " return -N;\n"
324 " }\n"
325 "}\n"
326 "\n"
327 "float2 faceforward(float2 N, float2 I, float2 Nref)\n" // FIXME: Prevent name clashes
328 "{\n"
329 " if(dot(Nref, I) < 0)\n"
330 " {\n"
331 " return N;\n"
332 " }\n"
333 " else\n"
334 " {\n"
335 " return -N;\n"
336 " }\n"
337 "}\n"
338 "\n"
339 "float3 faceforward(float3 N, float3 I, float3 Nref)\n" // FIXME: Prevent name clashes
340 "{\n"
341 " if(dot(Nref, I) < 0)\n"
342 " {\n"
343 " return N;\n"
344 " }\n"
345 " else\n"
346 " {\n"
347 " return -N;\n"
348 " }\n"
349 "}\n"
350 "float4 faceforward(float4 N, float4 I, float4 Nref)\n" // FIXME: Prevent name clashes
351 "{\n"
352 " if(dot(Nref, I) < 0)\n"
353 " {\n"
354 " return N;\n"
355 " }\n"
356 " else\n"
357 " {\n"
358 " return -N;\n"
359 " }\n"
360 "}\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000361 "\n";
362}
363
364void OutputHLSL::visitSymbol(TIntermSymbol *node)
365{
366 TInfoSinkBase &out = context.infoSink.obj;
367
368 TString name = node->getSymbol();
369
370 if (name == "gl_FragColor")
371 {
372 out << "gl_Color[0]";
373 }
374 else if (name == "gl_FragData")
375 {
376 out << "gl_Color";
377 }
378 else
379 {
380 out << name;
381 }
382}
383
384bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
385{
386 TInfoSinkBase &out = context.infoSink.obj;
387
388 switch (node->getOp())
389 {
390 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
391 case EOpInitialize: outputTriplet(visit, NULL, " = ", NULL); break;
392 case EOpAddAssign: outputTriplet(visit, NULL, " += ", NULL); break;
393 case EOpSubAssign: outputTriplet(visit, NULL, " -= ", NULL); break;
394 case EOpMulAssign: outputTriplet(visit, NULL, " *= ", NULL); break;
395 case EOpVectorTimesMatrixAssign: UNIMPLEMENTED(); /* FIXME */ out << "matrix mult second child into first child"; break;
396 case EOpVectorTimesScalarAssign: outputTriplet(visit, NULL, " *= ", NULL); break;
397 case EOpMatrixTimesScalarAssign: UNIMPLEMENTED(); /* FIXME */ out << "matrix scale second child into first child"; break;
398 case EOpMatrixTimesMatrixAssign: UNIMPLEMENTED(); /* FIXME */ out << "matrix mult second child into first child"; break;
399 case EOpDivAssign: outputTriplet(visit, NULL, " /= ", NULL); break;
400 case EOpModAssign: UNIMPLEMENTED(); /* FIXME */ out << "mod second child into first child"; break;
401 case EOpAndAssign: UNIMPLEMENTED(); /* FIXME */ out << "and second child into first child"; break;
402 case EOpInclusiveOrAssign: UNIMPLEMENTED(); /* FIXME */ out << "or second child into first child"; break;
403 case EOpExclusiveOrAssign: UNIMPLEMENTED(); /* FIXME */ out << "exclusive or second child into first child"; break;
404 case EOpLeftShiftAssign: UNIMPLEMENTED(); /* FIXME */ out << "left shift second child into first child"; break;
405 case EOpRightShiftAssign: UNIMPLEMENTED(); /* FIXME */ out << "right shift second child into first child"; break;
406 case EOpIndexDirect: outputTriplet(visit, NULL, "[", "]"); break;
407 case EOpIndexIndirect: outputTriplet(visit, NULL, "[", "]"); break;
408 case EOpIndexDirectStruct: UNIMPLEMENTED(); /* FIXME */ out << "direct index for structure"; break;
409 case EOpVectorSwizzle:
410 if (visit == InVisit)
411 {
412 out << ".";
413
414 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
415
416 if (swizzle)
417 {
418 TIntermSequence &sequence = swizzle->getSequence();
419
420 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
421 {
422 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
423
424 if (element)
425 {
426 int i = element->getUnionArrayPointer()[0].getIConst();
427
428 switch (i)
429 {
430 case 0: out << "x"; break;
431 case 1: out << "y"; break;
432 case 2: out << "z"; break;
433 case 3: out << "w"; break;
434 default: UNREACHABLE();
435 }
436 }
437 else UNREACHABLE();
438 }
439 }
440 else UNREACHABLE();
441
442 return false; // Fully processed
443 }
444 break;
445 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
446 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
447 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
448 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
449 case EOpMod: UNIMPLEMENTED(); /* FIXME */ out << "mod"; break;
450 case EOpRightShift: UNIMPLEMENTED(); /* FIXME */ out << "right-shift"; break;
451 case EOpLeftShift: UNIMPLEMENTED(); /* FIXME */ out << "left-shift"; break;
452 case EOpAnd: UNIMPLEMENTED(); /* FIXME */ out << "bitwise and"; break;
453 case EOpInclusiveOr: UNIMPLEMENTED(); /* FIXME */ out << "inclusive-or"; break;
454 case EOpExclusiveOr: UNIMPLEMENTED(); /* FIXME */ out << "exclusive-or"; break;
455 case EOpEqual: outputTriplet(visit, "(", " == ", ")"); break;
456 case EOpNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
457 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
458 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
459 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
460 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
461 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
462 case EOpVectorTimesMatrix: UNIMPLEMENTED(); /* FIXME */ out << "vector-times-matrix"; break;
463 case EOpMatrixTimesVector: outputTriplet(visit, "mul(", ", ", ")"); break;
464 case EOpMatrixTimesScalar: UNIMPLEMENTED(); /* FIXME */ out << "matrix-scale"; break;
465 case EOpMatrixTimesMatrix: UNIMPLEMENTED(); /* FIXME */ out << "matrix-multiply"; break;
466 case EOpLogicalOr: outputTriplet(visit, "(", " || ", ")"); break;
467 case EOpLogicalXor: outputTriplet(visit, "xor(", ", ", ")"); break; // FIXME: Prevent name clashes
468 case EOpLogicalAnd: outputTriplet(visit, "(", " && ", ")"); break;
469 default: UNREACHABLE();
470 }
471
472 return true;
473}
474
475bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
476{
477 TInfoSinkBase &out = context.infoSink.obj;
478
479 switch (node->getOp())
480 {
481 case EOpNegative: outputTriplet(visit, "(-", NULL, ")"); break;
482 case EOpVectorLogicalNot: outputTriplet(visit, "(!", NULL, ")"); break;
483 case EOpLogicalNot: outputTriplet(visit, "(!", NULL, ")"); break;
484 case EOpBitwiseNot: outputTriplet(visit, "(~", NULL, ")"); break;
485 case EOpPostIncrement: outputTriplet(visit, "(", NULL, "++)"); break;
486 case EOpPostDecrement: outputTriplet(visit, "(", NULL, "--)"); break;
487 case EOpPreIncrement: outputTriplet(visit, "(++", NULL, ")"); break;
488 case EOpPreDecrement: outputTriplet(visit, "(--", NULL, ")"); break;
489 case EOpConvIntToBool:
490 case EOpConvFloatToBool:
491 switch (node->getOperand()->getType().getNominalSize())
492 {
493 case 1: outputTriplet(visit, "bool(", NULL, ")"); break;
494 case 2: outputTriplet(visit, "bool2(", NULL, ")"); break;
495 case 3: outputTriplet(visit, "bool3(", NULL, ")"); break;
496 case 4: outputTriplet(visit, "bool4(", NULL, ")"); break;
497 default: UNREACHABLE();
498 }
499 break;
500 case EOpConvBoolToFloat:
501 case EOpConvIntToFloat:
502 switch (node->getOperand()->getType().getNominalSize())
503 {
504 case 1: outputTriplet(visit, "float(", NULL, ")"); break;
505 case 2: outputTriplet(visit, "float2(", NULL, ")"); break;
506 case 3: outputTriplet(visit, "float3(", NULL, ")"); break;
507 case 4: outputTriplet(visit, "float4(", NULL, ")"); break;
508 default: UNREACHABLE();
509 }
510 break;
511 case EOpConvFloatToInt:
512 case EOpConvBoolToInt:
513 switch (node->getOperand()->getType().getNominalSize())
514 {
515 case 1: outputTriplet(visit, "int(", NULL, ")"); break;
516 case 2: outputTriplet(visit, "int2(", NULL, ")"); break;
517 case 3: outputTriplet(visit, "int3(", NULL, ")"); break;
518 case 4: outputTriplet(visit, "int4(", NULL, ")"); break;
519 default: UNREACHABLE();
520 }
521 break;
522 case EOpRadians: outputTriplet(visit, "radians(", NULL, ")"); break;
523 case EOpDegrees: outputTriplet(visit, "degrees(", NULL, ")"); break;
524 case EOpSin: outputTriplet(visit, "sin(", NULL, ")"); break;
525 case EOpCos: outputTriplet(visit, "cos(", NULL, ")"); break;
526 case EOpTan: outputTriplet(visit, "tan(", NULL, ")"); break;
527 case EOpAsin: outputTriplet(visit, "asin(", NULL, ")"); break;
528 case EOpAcos: outputTriplet(visit, "acos(", NULL, ")"); break;
529 case EOpAtan: outputTriplet(visit, "atan(", NULL, ")"); break;
530 case EOpExp: outputTriplet(visit, "exp(", NULL, ")"); break;
531 case EOpLog: outputTriplet(visit, "log(", NULL, ")"); break;
532 case EOpExp2: outputTriplet(visit, "exp2(", NULL, ")"); break;
533 case EOpLog2: outputTriplet(visit, "log2(", NULL, ")"); break;
534 case EOpSqrt: outputTriplet(visit, "sqrt(", NULL, ")"); break;
535 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", NULL, ")"); break;
536 case EOpAbs: outputTriplet(visit, "abs(", NULL, ")"); break;
537 case EOpSign: outputTriplet(visit, "sign(", NULL, ")"); break;
538 case EOpFloor: outputTriplet(visit, "floor(", NULL, ")"); break;
539 case EOpCeil: outputTriplet(visit, "ceil(", NULL, ")"); break;
540 case EOpFract: outputTriplet(visit, "frac(", NULL, ")"); break;
541 case EOpLength: outputTriplet(visit, "length(", NULL, ")"); break;
542 case EOpNormalize: outputTriplet(visit, "normalize(", NULL, ")"); break;
543// case EOpDPdx: outputTriplet(visit, "ddx(", NULL, ")"); break;
544// case EOpDPdy: outputTriplet(visit, "ddy(", NULL, ")"); break;
545// case EOpFwidth: outputTriplet(visit, "fwidth(", NULL, ")"); break;
546 case EOpAny: outputTriplet(visit, "any(", NULL, ")"); break;
547 case EOpAll: outputTriplet(visit, "all(", NULL, ")"); break;
548 default: UNREACHABLE();
549 }
550
551 return true;
552}
553
554bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
555{
556 EShLanguage language = context.language;
557 TInfoSinkBase &out = context.infoSink.obj;
558
559 if (node->getOp() == EOpNull)
560 {
561 out.message(EPrefixError, "node is still EOpNull!");
562 return true;
563 }
564
565 switch (node->getOp())
566 {
567 case EOpSequence: outputTriplet(visit, NULL, ";\n", ";\n"); break;
568 case EOpDeclaration:
569 if (visit == PreVisit)
570 {
571 TIntermSequence &sequence = node->getSequence();
572 TIntermTyped *variable = sequence[0]->getAsTyped();
573 bool visit = true;
574
575 if (variable && variable->getQualifier() == EvqTemporary)
576 {
577 out << typeString(variable->getType()) + " ";
578
579 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
580 {
581 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
582
583 if (symbol)
584 {
585 symbol->traverse(this);
586
587 out << arrayString(symbol->getType());
588 }
589 else
590 {
591 (*sit)->traverse(this);
592 }
593
594 if (visit && this->inVisit)
595 {
596 if (*sit != sequence.back())
597 {
598 visit = this->visitAggregate(InVisit, node);
599 }
600 }
601 }
602
603 if (visit && this->postVisit)
604 {
605 this->visitAggregate(PostVisit, node);
606 }
607 }
608
609 return false;
610 }
611 else if (visit == InVisit)
612 {
613 out << ", ";
614 }
615 break;
616 case EOpComma: UNIMPLEMENTED(); /* FIXME */ out << "Comma\n"; return true;
617 case EOpFunction:
618 {
619 const TString &mangledName = node->getName();
620 TString name = TString(mangledName.c_str(), mangledName.find_first_of('('));
621
622 if (visit == PreVisit)
623 {
624 if (name == "main")
625 {
626 TSymbolTableLevel *symbols = context.symbolTable.getGlobalLevel();
627
628 if (language == EShLangFragment)
629 {
630 out << "PS_OUTPUT main(PS_INPUT input)\n" // FIXME: Prevent name clashes
631 "{\n";
632
633 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
634 {
635 const TSymbol *symbol = (*namedSymbol).second;
636 const TString &name = symbol->getName();
637
638 if (symbol->isVariable())
639 {
640 const TVariable *variable = static_cast<const TVariable*>(symbol);
641 const TType &type = variable->getType();
642 TQualifier qualifier = type.getQualifier();
643
644 if(qualifier == EvqVaryingIn)
645 {
646 out << " " + name + " = input." + name + ";\n"; // FIXME: Prevent name clashes
647 }
648 }
649 }
650 }
651 else
652 {
653 out << "VS_OUTPUT main(VS_INPUT input)\n" // FIXME: Prevent name clashes
654 "{\n";
655
656 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
657 {
658 const TSymbol *symbol = (*namedSymbol).second;
659 const TString &name = symbol->getName();
660
661 if (symbol->isVariable())
662 {
663 const TVariable *variable = static_cast<const TVariable*>(symbol);
664 const TType &type = variable->getType();
665 TQualifier qualifier = type.getQualifier();
666
667 if (qualifier == EvqAttribute)
668 {
669 out << " " + name + " = input." + name + ";\n"; // FIXME: Prevent name clashes
670 }
671 }
672 }
673 }
674
675 // Erase the (empty) argument list
676 TIntermSequence &sequence = node->getSequence();
677 sequence.erase(sequence.begin());
678 }
679 else
680 {
681 out << typeString(node->getType()) << " " << name << "(";
682
683 TIntermSequence &sequence = node->getSequence();
684 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
685
686 for (unsigned int i = 0; i < arguments.size(); i++)
687 {
688 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
689
690 if (symbol)
691 {
692 const TType &type = symbol->getType();
693 const TString &name = symbol->getSymbol();
694
695 out << typeString(type) + " " + name;
696
697 if(i < arguments.size() - 1)
698 {
699 out << ", ";
700 }
701 }
702 else UNREACHABLE();
703 }
704
705 sequence.erase(sequence.begin());
706
707 out << ")\n"
708 "{\n";
709 }
710 }
711 else if (visit == PostVisit)
712 {
713 if (name == "main")
714 {
715 if (language == EShLangFragment)
716 {
717 out << " PS_OUTPUT output;\n" // FIXME: Prevent name clashes
718 " output.gl_Color[0] = gl_Color[0];\n"; // FIXME: Prevent name clashes
719
720 TSymbolTableLevel *symbols = context.symbolTable.getGlobalLevel();
721
722 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
723 {
724 const TSymbol *symbol = (*namedSymbol).second;
725 const TString &name = symbol->getName();
726 }
727 }
728 else
729 {
730 out << " VS_OUTPUT output;\n" // FIXME: Prevent name clashes
731 " output.gl_Position.x = gl_Position.x - gl_HalfPixelSize.x * gl_Position.w;\n"
732 " output.gl_Position.y = -(gl_Position.y - gl_HalfPixelSize.y * gl_Position.w);\n"
733 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
734 " output.gl_Position.w = gl_Position.w;\n"
735 " output.gl_PointSize = gl_PointSize;\n";
736
737 TSymbolTableLevel *symbols = context.symbolTable.getGlobalLevel();
738
739 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
740 {
741 const TSymbol *symbol = (*namedSymbol).second;
742 const TString &name = symbol->getName();
743
744 if (symbol->isVariable())
745 {
746 const TVariable *variable = static_cast<const TVariable*>(symbol);
747 TQualifier qualifier = variable->getType().getQualifier();
748
749 if(qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut)
750 {
751 out << " output." + name + " = " + name + ";\n"; // FIXME: Prevent name clashes
752 }
753 }
754 }
755 }
756
757 out << " return output;\n" // FIXME: Prevent name clashes
758 "}\n";
759 }
760 else
761 {
762 out << "}\n";
763 }
764 }
765 }
766 break;
767 case EOpFunctionCall:
768 {
769 if (visit == PreVisit)
770 {
771 const TString &mangledName = node->getName();
772 TString name = TString(mangledName.c_str(), mangledName.find_first_of('('));
773
774 if (node->isUserDefined())
775 {
776 out << name << "(";
777 }
778 else
779 {
780 if (name == "texture2D")
781 {
782 if (node->getSequence().size() == 2)
783 {
784 out << "gl_texture2D(";
785 }
786 else if (node->getSequence().size() == 3)
787 {
788 out << "gl_texture2DBias(";
789 }
790 else UNREACHABLE();
791 }
792 else if (name == "texture2DProj")
793 {
794 out << "gl_texture2DProj(";
795 }
796 else if (name == "texture2DLod")
797 {
798 out << "gl_texture2DLod(";
799 UNIMPLEMENTED(); // FIXME: Move lod to last texture coordinate component
800 }
801 else if (name == "texture2DProjLod")
802 {
803 out << "gl_texture2DProjLod(";
804 UNIMPLEMENTED(); // FIXME: Move lod to last texture coordinate component
805 }
806 else if (name == "textureCube")
807 {
808 out << "gl_textureCube("; // FIXME: Incorrect sampling location
809 }
810 else
811 {
812 UNIMPLEMENTED(); // FIXME
813 }
814 }
815 }
816 else if (visit == InVisit)
817 {
818 out << ", ";
819 }
820 else
821 {
822 out << ")";
823 }
824 }
825 break;
826 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
827 case EOpConstructFloat: outputTriplet(visit, "vec1(", NULL, ")"); break;
828 case EOpConstructVec2: outputTriplet(visit, "vec2(", ", ", ")"); break;
829 case EOpConstructVec3: outputTriplet(visit, "vec3(", ", ", ")"); break;
830 case EOpConstructVec4: outputTriplet(visit, "vec4(", ", ", ")"); break;
831 case EOpConstructBool: UNIMPLEMENTED(); /* FIXME */ out << "Construct bool"; break;
832 case EOpConstructBVec2: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec2"; break;
833 case EOpConstructBVec3: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec3"; break;
834 case EOpConstructBVec4: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec4"; break;
835 case EOpConstructInt: UNIMPLEMENTED(); /* FIXME */ out << "Construct int"; break;
836 case EOpConstructIVec2: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec2"; break;
837 case EOpConstructIVec3: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec3"; break;
838 case EOpConstructIVec4: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec4"; break;
839 case EOpConstructMat2: outputTriplet(visit, "float2x2(", ", ", ")"); break;
840 case EOpConstructMat3: outputTriplet(visit, "float3x3(", ", ", ")"); break;
841 case EOpConstructMat4: UNIMPLEMENTED(); /* FIXME */ out << "Construct mat4"; break;
842 case EOpConstructStruct: UNIMPLEMENTED(); /* FIXME */ out << "Construct structure"; break;
843 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
844 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
845 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
846 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
847 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
848 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
849 case EOpMod: outputTriplet(visit, "mod(", ", ", ")"); break; // FIXME: Prevent name clashes
850 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
851 case EOpAtan:
852 if (node->getSequence().size() == 1)
853 {
854 outputTriplet(visit, "atan(", ", ", ")");
855 }
856 else if (node->getSequence().size() == 2)
857 {
858 outputTriplet(visit, "atan2(", ", ", ")");
859 }
860 else UNREACHABLE();
861 break;
862 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
863 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
864 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
865 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
866 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
867 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
868 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
869 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
870 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000871 case EOpFaceForward: outputTriplet(visit, "faceforward(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000872 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
873 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
874 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
875 case EOpItof: UNIMPLEMENTED(); /* FIXME */ out << "itof"; break;
876 case EOpFtoi: UNIMPLEMENTED(); /* FIXME */ out << "ftoi"; break;
877 case EOpSkipPixels: UNIMPLEMENTED(); /* FIXME */ out << "skipPixels"; break;
878 case EOpReadInput: UNIMPLEMENTED(); /* FIXME */ out << "readInput"; break;
879 case EOpWritePixel: UNIMPLEMENTED(); /* FIXME */ out << "writePixel"; break;
880 case EOpBitmapLsb: UNIMPLEMENTED(); /* FIXME */ out << "bitmapLSB"; break;
881 case EOpBitmapMsb: UNIMPLEMENTED(); /* FIXME */ out << "bitmapMSB"; break;
882 case EOpWriteOutput: UNIMPLEMENTED(); /* FIXME */ out << "writeOutput"; break;
883 case EOpReadPixel: UNIMPLEMENTED(); /* FIXME */ out << "readPixel"; break;
884 default: UNREACHABLE();
885 }
886
887 return true;
888}
889
890bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
891{
892 TInfoSinkBase &out = context.infoSink.obj;
893
894 out << "if(";
895
896 node->getCondition()->traverse(this);
897
898 out << ")\n"
899 "{\n";
900
901 node->getTrueBlock()->traverse(this);
902
903 out << ";}\n";
904
905 if (node->getFalseBlock())
906 {
907 out << "else\n"
908 "{\n";
909
910 node->getFalseBlock()->traverse(this);
911
912 out << ";}\n";
913 }
914
915 return false;
916}
917
918void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
919{
920 TInfoSinkBase &out = context.infoSink.obj;
921
922 int size = node->getType().getObjectSize();
923 bool matrix = node->getType().isMatrix();
924 TBasicType type = node->getUnionArrayPointer()[0].getType();
925
926 switch (type)
927 {
928 case EbtBool:
929 if (!matrix)
930 {
931 switch (size)
932 {
933 case 1: out << "bool("; break;
934 case 2: out << "bool2("; break;
935 case 3: out << "bool3("; break;
936 case 4: out << "bool4("; break;
937 default: UNREACHABLE();
938 }
939 }
940 else
941 {
942 UNIMPLEMENTED();
943 }
944 break;
945 case EbtFloat:
946 if (!matrix)
947 {
948 switch (size)
949 {
950 case 1: out << "float("; break;
951 case 2: out << "float2("; break;
952 case 3: out << "float3("; break;
953 case 4: out << "float4("; break;
954 default: UNREACHABLE();
955 }
956 }
957 else
958 {
959 switch (size)
960 {
961 case 4: out << "float2x2("; break;
962 case 9: out << "float3x3("; break;
963 case 16: out << "float4x4("; break;
964 default: UNREACHABLE();
965 }
966 }
967 break;
968 case EbtInt:
969 if (!matrix)
970 {
971 switch (size)
972 {
973 case 1: out << "int("; break;
974 case 2: out << "int2("; break;
975 case 3: out << "int3("; break;
976 case 4: out << "int4("; break;
977 default: UNREACHABLE();
978 }
979 }
980 else
981 {
982 UNIMPLEMENTED();
983 }
984 break;
985 default:
986 UNIMPLEMENTED(); // FIXME
987 }
988
989 for (int i = 0; i < size; i++)
990 {
991 switch (type)
992 {
993 case EbtBool:
994 if (node->getUnionArrayPointer()[i].getBConst())
995 {
996 out << "true";
997 }
998 else
999 {
1000 out << "false";
1001 }
1002 break;
1003 case EbtFloat:
1004 out << node->getUnionArrayPointer()[i].getFConst();
1005 break;
1006 case EbtInt:
1007 out << node->getUnionArrayPointer()[i].getIConst();
1008 break;
1009 default:
1010 UNIMPLEMENTED(); // FIXME
1011 }
1012
1013 if (i != size - 1)
1014 {
1015 out << ", ";
1016 }
1017 }
1018
1019 out << ")";
1020}
1021
1022bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
1023{
1024 TInfoSinkBase &out = context.infoSink.obj;
1025
1026 if (!node->testFirst())
1027 {
1028 out << "do\n"
1029 "{\n";
1030 }
1031 else
1032 {
1033 out << "for(";
1034
1035 if (node->getInit())
1036 {
1037 node->getInit()->traverse(this);
1038 }
1039
1040 out << "; ";
1041
1042 if (node->getTest())
1043 {
1044 node->getTest()->traverse(this);
1045 }
1046
1047 out << "; ";
1048
1049 if (node->getTerminal())
1050 {
1051 node->getTerminal()->traverse(this);
1052 }
1053
1054 out << ")\n"
1055 "{\n";
1056 }
1057
1058 if (node->getBody())
1059 {
1060 node->getBody()->traverse(this);
1061 }
1062
1063 out << "}\n";
1064
1065 if (!node->testFirst())
1066 {
1067 out << "while(\n";
1068
1069 node->getTest()->traverse(this);
1070
1071 out << ")";
1072 }
1073
1074 out << ";\n";
1075
1076 return false;
1077}
1078
1079bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
1080{
1081 TInfoSinkBase &out = context.infoSink.obj;
1082
1083 switch (node->getFlowOp())
1084 {
1085 case EOpKill: outputTriplet(visit, "discard", NULL, NULL); break;
1086 case EOpBreak: UNIMPLEMENTED(); /* FIXME */ break;
1087 case EOpContinue: UNIMPLEMENTED(); /* FIXME */ break;
1088 case EOpReturn:
1089 if (visit == PreVisit)
1090 {
1091 if (node->getExpression())
1092 {
1093 out << "return ";
1094 }
1095 else
1096 {
1097 out << "return;\n";
1098 }
1099 }
1100 else if (visit == PostVisit)
1101 {
1102 out << ";\n";
1103 }
1104 break;
1105 default: UNREACHABLE();
1106 }
1107
1108 return true;
1109}
1110
1111void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
1112{
1113 TInfoSinkBase &out = context.infoSink.obj;
1114
1115 if (visit == PreVisit && preString)
1116 {
1117 out << preString;
1118 }
1119 else if (visit == InVisit && inString)
1120 {
1121 out << inString;
1122 }
1123 else if (visit == PostVisit && postString)
1124 {
1125 out << postString;
1126 }
1127}
1128
1129TString OutputHLSL::typeString(const TType &type)
1130{
1131 if (type.isMatrix())
1132 {
1133 switch (type.getNominalSize())
1134 {
1135 case 2: return "float2x2";
1136 case 3: return "float3x3";
1137 case 4: return "float4x4";
1138 }
1139 }
1140 else
1141 {
1142 switch (type.getBasicType())
1143 {
1144 case EbtFloat:
1145 switch (type.getNominalSize())
1146 {
1147 case 1: return "float";
1148 case 2: return "float2";
1149 case 3: return "float3";
1150 case 4: return "float4";
1151 }
1152 case EbtInt:
1153 switch (type.getNominalSize())
1154 {
1155 case 1: return "int";
1156 case 2: return "int2";
1157 case 3: return "int3";
1158 case 4: return "int4";
1159 }
1160 case EbtBool:
1161 switch (type.getNominalSize())
1162 {
1163 case 1: return "bool";
1164 case 2: return "bool2";
1165 case 3: return "bool3";
1166 case 4: return "bool4";
1167 }
1168 case EbtVoid:
1169 return "void";
1170 case EbtSampler2D:
1171 return "sampler2D";
1172 case EbtSamplerCube:
1173 return "samplerCUBE";
1174 }
1175 }
1176
1177 UNIMPLEMENTED(); // FIXME
1178 return "<unknown type>";
1179}
1180
1181TString OutputHLSL::arrayString(const TType &type)
1182{
1183 if (!type.isArray())
1184 {
1185 return "";
1186 }
1187
1188 char buffer[100];
1189 sprintf(buffer, "[%d]", type.getArraySize());
1190
1191 return buffer;
1192}
1193
1194TString OutputHLSL::initializer(const TType &type)
1195{
1196 TString string;
1197
1198 int arraySize = type.isArray() ? type.getArraySize() : 1;
1199
1200 if (type.isArray())
1201 {
1202 string += "{";
1203 }
1204
1205 for (int element = 0; element < arraySize; element++)
1206 {
1207 string += typeString(type) + "(";
1208
1209 for (int component = 0; component < type.getNominalSize(); component++)
1210 {
1211 string += "0";
1212
1213 if (component < type.getNominalSize() - 1)
1214 {
1215 string += ", ";
1216 }
1217 }
1218
1219 string += ")";
1220
1221 if (element < arraySize - 1)
1222 {
1223 string += ", ";
1224 }
1225 }
1226
1227 if (type.isArray())
1228 {
1229 string += "}";
1230 }
1231
1232 return string;
1233}
1234}