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