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