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