blob: 5da0996b9042f521104bd8d1c3f985f091e07cb3 [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
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +00009#include "common/debug.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000010#include "InfoSink.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000011
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 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +000056 else if (qualifier == EvqConst)
57 {
58 // Constants are repeated as literals where used
59 }
60 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000061 }
62 }
63
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +000064 out << "uniform float4 gl_Window;\n"
65 "uniform float2 gl_Depth;\n"
daniel@transgaming.com79b820b2010-03-16 05:48:57 +000066 "uniform bool __frontCCW;\n"
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +000067 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000068 out << uniforms;
daniel@transgaming.com86487c22010-03-11 19:41:43 +000069 out << "\n"
70 "struct PS_INPUT\n" // FIXME: Prevent name clashes
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000071 "{\n";
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +000072 out << varyingInput;
daniel@transgaming.com79b820b2010-03-16 05:48:57 +000073 out << " float4 gl_FragCoord : TEXCOORD" << HLSL_FRAG_COORD_SEMANTIC << ";\n";
74 out << " float __vFace : VFACE;\n"
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +000075 "};\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000076 "\n";
77 out << varyingGlobals;
78 out << "\n"
79 "struct PS_OUTPUT\n" // FIXME: Prevent name clashes
80 "{\n"
81 " float4 gl_Color[1] : COLOR;\n"
82 "};\n"
83 "\n"
84 "static float4 gl_Color[1] = {float4(0, 0, 0, 0)};\n"
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +000085 "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n"
daniel@transgaming.comccad59f2010-03-26 04:08:39 +000086 "static float2 gl_PointCoord = float2(0.5, 0.5);\n"
daniel@transgaming.com79b820b2010-03-16 05:48:57 +000087 "static bool gl_FrontFacing = false;\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000088 "\n"
89 "float4 gl_texture2D(sampler2D s, float2 t)\n"
90 "{\n"
91 " return tex2D(s, t);\n"
92 "}\n"
93 "\n"
94 "float4 gl_texture2DProj(sampler2D s, float3 t)\n"
95 "{\n"
96 " return tex2Dproj(s, float4(t.x, t.y, 0, t.z));\n"
97 "}\n"
98 "float4 gl_texture2DBias(sampler2D s, float2 t, float bias)\n"
99 "{\n"
100 " return tex2Dbias(s, float4(t.x, t.y, 0, bias));\n"
101 "}\n"
102 "\n"
103 "float4 gl_textureCube(samplerCUBE s, float3 t)\n"
104 "{\n"
105 " return texCUBE(s, t);\n"
106 "}\n"
107 "\n";
108 }
109 else
110 {
111 TString uniforms;
112 TString attributeInput;
113 TString attributeGlobals;
114 TString varyingOutput;
115 TString varyingGlobals;
116 TString globals;
117
118 TSymbolTableLevel *symbols = context.symbolTable.getGlobalLevel();
119 int semanticIndex = 0;
120
121 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
122 {
123 const TSymbol *symbol = (*namedSymbol).second;
124 const TString &name = symbol->getName();
125
126 if (symbol->isVariable())
127 {
128 const TVariable *variable = static_cast<const TVariable*>(symbol);
129 const TType &type = variable->getType();
130 TQualifier qualifier = type.getQualifier();
131
132 if (qualifier == EvqUniform)
133 {
134 uniforms += "uniform " + typeString(type) + " " + name + arrayString(type) + ";\n";
135 }
136 else if (qualifier == EvqAttribute)
137 {
138 char semantic[100];
139 sprintf(semantic, " : TEXCOORD%d", semanticIndex);
140 semanticIndex += type.isArray() ? type.getArraySize() : 1;
141
142 attributeInput += " " + typeString(type) + " " + name + arrayString(type) + semantic + ";\n";
143 attributeGlobals += "static " + typeString(type) + " " + name + arrayString(type) + " = " + initializer(type) + ";\n";
144 }
145 else if (qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut)
146 {
147 varyingOutput += " " + typeString(type) + " " + name + arrayString(type) + " : TEXCOORD0;\n"; // Actual semantic index assigned during link
148 varyingGlobals += "static " + typeString(type) + " " + name + arrayString(type) + " = " + initializer(type) + ";\n";
149 }
150 else if (qualifier == EvqGlobal)
151 {
152 globals += typeString(type) + " " + name + arrayString(type) + ";\n";
153 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000154 else if (qualifier == EvqConst)
155 {
156 // Constants are repeated as literals where used
157 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000158 else UNREACHABLE();
159 }
160 }
161
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +0000162 out << "uniform float2 gl_HalfPixelSize;\n"
163 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000164 out << uniforms;
165 out << "\n";
166 out << globals;
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +0000167 out << "\n"
168 "struct VS_INPUT\n" // FIXME: Prevent name clashes
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000169 "{\n";
170 out << attributeInput;
171 out << "};\n"
172 "\n";
173 out << attributeGlobals;
174 out << "\n"
175 "struct VS_OUTPUT\n" // FIXME: Prevent name clashes
176 "{\n"
177 " float4 gl_Position : POSITION;\n"
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +0000178 " float gl_PointSize : PSIZE;\n"
179 " float4 gl_FragCoord : TEXCOORD" << HLSL_FRAG_COORD_SEMANTIC << ";\n";
180 out << varyingOutput;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181 out << "};\n"
182 "\n"
183 "static float4 gl_Position = float4(0, 0, 0, 0);\n"
daniel@transgaming.comccad59f2010-03-26 04:08:39 +0000184 "static float gl_PointSize = float(1);\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000185 out << varyingGlobals;
186 out << "\n";
187 }
188
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000189 out << "struct gl_DepthRangeParameters\n"
190 "{\n"
191 " float near;\n"
192 " float far;\n"
193 " float diff;\n"
194 "};\n"
195 "\n"
196 "uniform gl_DepthRangeParameters gl_DepthRange;\n"
197 "\n"
198 "float vec1(float x)\n" // FIXME: Prevent name clashes
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000199 "{\n"
200 " return x;\n"
201 "}\n"
202 "\n"
203 "float vec1(float2 xy)\n" // FIXME: Prevent name clashes
204 "{\n"
205 " return xy[0];\n"
206 "}\n"
207 "\n"
208 "float vec1(float3 xyz)\n" // FIXME: Prevent name clashes
209 "{\n"
210 " return xyz[0];\n"
211 "}\n"
212 "\n"
213 "float vec1(float4 xyzw)\n" // FIXME: Prevent name clashes
214 "{\n"
215 " return xyzw[0];\n"
216 "}\n"
217 "\n"
218 "float2 vec2(float x)\n" // FIXME: Prevent name clashes
219 "{\n"
220 " return float2(x, x);\n"
221 "}\n"
222 "\n"
223 "float2 vec2(float x, float y)\n" // FIXME: Prevent name clashes
224 "{\n"
225 " return float2(x, y);\n"
226 "}\n"
227 "\n"
228 "float2 vec2(float2 xy)\n" // FIXME: Prevent name clashes
229 "{\n"
230 " return xy;\n"
231 "}\n"
232 "\n"
233 "float2 vec2(float3 xyz)\n" // FIXME: Prevent name clashes
234 "{\n"
235 " return float2(xyz[0], xyz[1]);\n"
236 "}\n"
237 "\n"
238 "float2 vec2(float4 xyzw)\n" // FIXME: Prevent name clashes
239 "{\n"
240 " return float2(xyzw[0], xyzw[1]);\n"
241 "}\n"
242 "\n"
243 "float3 vec3(float x)\n" // FIXME: Prevent name clashes
244 "{\n"
245 " return float3(x, x, x);\n"
246 "}\n"
247 "\n"
248 "float3 vec3(float x, float y, float z)\n" // FIXME: Prevent name clashes
249 "{\n"
250 " return float3(x, y, z);\n"
251 "}\n"
252 "\n"
253 "float3 vec3(float2 xy, float z)\n" // FIXME: Prevent name clashes
254 "{\n"
255 " return float3(xy[0], xy[1], z);\n"
256 "}\n"
257 "\n"
258 "float3 vec3(float x, float2 yz)\n" // FIXME: Prevent name clashes
259 "{\n"
260 " return float3(x, yz[0], yz[1]);\n"
261 "}\n"
262 "\n"
263 "float3 vec3(float3 xyz)\n" // FIXME: Prevent name clashes
264 "{\n"
265 " return xyz;\n"
266 "}\n"
267 "\n"
268 "float3 vec3(float4 xyzw)\n" // FIXME: Prevent name clashes
269 "{\n"
270 " return float3(xyzw[0], xyzw[1], xyzw[2]);\n"
271 "}\n"
272 "\n"
273 "float4 vec4(float x)\n" // FIXME: Prevent name clashes
274 "{\n"
275 " return float4(x, x, x, x);\n"
276 "}\n"
277 "\n"
278 "float4 vec4(float x, float y, float z, float w)\n" // FIXME: Prevent name clashes
279 "{\n"
280 " return float4(x, y, z, w);\n"
281 "}\n"
282 "\n"
283 "float4 vec4(float2 xy, float z, float w)\n" // FIXME: Prevent name clashes
284 "{\n"
285 " return float4(xy[0], xy[1], z, w);\n"
286 "}\n"
287 "\n"
288 "float4 vec4(float x, float2 yz, float w)\n" // FIXME: Prevent name clashes
289 "{\n"
290 " return float4(x, yz[0], yz[1], w);\n"
291 "}\n"
292 "\n"
293 "float4 vec4(float x, float y, float2 zw)\n" // FIXME: Prevent name clashes
294 "{\n"
295 " return float4(x, y, zw[0], zw[1]);\n"
296 "}\n"
297 "\n"
298 "float4 vec4(float2 xy, float2 zw)\n" // FIXME: Prevent name clashes
299 "{\n"
300 " return float4(xy[0], xy[1], zw[0], zw[1]);\n"
301 "}\n"
302 "\n"
303 "float4 vec4(float3 xyz, float w)\n" // FIXME: Prevent name clashes
304 "{\n"
305 " return float4(xyz[0], xyz[1], xyz[2], w);\n"
306 "}\n"
307 "\n"
308 "float4 vec4(float x, float3 yzw)\n" // FIXME: Prevent name clashes
309 "{\n"
310 " return float4(x, yzw[0], yzw[1], yzw[2]);\n"
311 "}\n"
312 "\n"
313 "float4 vec4(float4 xyzw)\n" // FIXME: Prevent name clashes
314 "{\n"
315 " return xyzw;\n"
316 "}\n"
317 "\n"
318 "bool xor(bool p, bool q)\n" // FIXME: Prevent name clashes
319 "{\n"
320 " return (p || q) && !(p && q);\n"
321 "}\n"
322 "\n"
323 "float mod(float x, float y)\n" // FIXME: Prevent name clashes
324 "{\n"
325 " return x - y * floor(x / y);\n"
326 "}\n"
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000327 "\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000328 "float2 mod(float2 x, float y)\n" // FIXME: Prevent name clashes
329 "{\n"
330 " return x - y * floor(x / y);\n"
331 "}\n"
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000332 "\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000333 "float3 mod(float3 x, float y)\n" // FIXME: Prevent name clashes
334 "{\n"
335 " return x - y * floor(x / y);\n"
336 "}\n"
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000337 "\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000338 "float4 mod(float4 x, float y)\n" // FIXME: Prevent name clashes
339 "{\n"
340 " return x - y * floor(x / y);\n"
341 "}\n"
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000342 "\n"
343 "float faceforward(float N, float I, float Nref)\n" // FIXME: Prevent name clashes
344 "{\n"
345 " if(dot(Nref, I) < 0)\n"
346 " {\n"
347 " return N;\n"
348 " }\n"
349 " else\n"
350 " {\n"
351 " return -N;\n"
352 " }\n"
353 "}\n"
354 "\n"
355 "float2 faceforward(float2 N, float2 I, float2 Nref)\n" // FIXME: Prevent name clashes
356 "{\n"
357 " if(dot(Nref, I) < 0)\n"
358 " {\n"
359 " return N;\n"
360 " }\n"
361 " else\n"
362 " {\n"
363 " return -N;\n"
364 " }\n"
365 "}\n"
366 "\n"
367 "float3 faceforward(float3 N, float3 I, float3 Nref)\n" // FIXME: Prevent name clashes
368 "{\n"
369 " if(dot(Nref, I) < 0)\n"
370 " {\n"
371 " return N;\n"
372 " }\n"
373 " else\n"
374 " {\n"
375 " return -N;\n"
376 " }\n"
377 "}\n"
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000378 "\n"
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000379 "float4 faceforward(float4 N, float4 I, float4 Nref)\n" // FIXME: Prevent name clashes
380 "{\n"
381 " if(dot(Nref, I) < 0)\n"
382 " {\n"
383 " return N;\n"
384 " }\n"
385 " else\n"
386 " {\n"
387 " return -N;\n"
388 " }\n"
389 "}\n"
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000390 "\n"
391 "bool __equal(float2x2 m, float2x2 n)\n"
392 "{\n"
393 " return m[0][0] == n[0][0] && m[0][1] == n[0][1] &&\n"
394 " m[1][0] == n[1][0] && m[1][1] == n[1][1];\n"
395 "}\n"
396 "\n"
397 "bool __equal(float3x3 m, float3x3 n)\n"
398 "{\n"
399 " return m[0][0] == n[0][0] && m[0][1] == n[0][1] && m[0][2] == n[0][2] &&\n"
400 " m[1][0] == n[1][0] && m[1][1] == n[1][1] && m[1][2] == n[1][2] &&\n"
401 " m[2][0] == n[2][0] && m[2][1] == n[2][1] && m[2][2] == n[2][2];\n"
402 "}\n"
403 "\n"
404 "bool __equal(float4x4 m, float4x4 n)\n"
405 "{\n"
406 " return m[0][0] == n[0][0] && m[0][1] == n[0][1] && m[0][2] == n[0][2] && m[0][3] == n[0][3] &&\n"
407 " m[1][0] == n[1][0] && m[1][1] == n[1][1] && m[1][2] == n[1][2] && m[1][3] == n[1][3] &&\n"
408 " m[2][0] == n[2][0] && m[2][1] == n[2][1] && m[2][2] == n[2][2] && m[2][3] == n[2][3] &&\n"
409 " m[3][0] == n[3][0] && m[3][1] == n[3][1] && m[3][2] == n[3][2] && m[3][3] == n[3][3];\n"
410 "}\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000411 "\n";
412}
413
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000414void OutputHLSL::footer()
415{
416 EShLanguage language = context.language;
417 TInfoSinkBase &out = context.infoSink.obj;
418 TSymbolTableLevel *symbols = context.symbolTable.getGlobalLevel();
419
420 if (language == EShLangFragment)
421 {
422 out << "PS_OUTPUT main(PS_INPUT input)\n" // FIXME: Prevent name clashes
423 "{\n"
424 " float rhw = 1.0 / input.gl_FragCoord.w;\n"
425 " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * gl_Window.x + gl_Window.z;\n"
426 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * gl_Window.y + gl_Window.w;\n"
427 " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * gl_Depth.x + gl_Depth.y;\n"
428 " gl_FragCoord.w = rhw;\n"
429 " gl_FrontFacing = __frontCCW ? (input.__vFace >= 0.0) : (input.__vFace <= 0.0);\n";
430
431 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
432 {
433 const TSymbol *symbol = (*namedSymbol).second;
434 const TString &name = symbol->getName();
435
436 if (symbol->isVariable())
437 {
438 const TVariable *variable = static_cast<const TVariable*>(symbol);
439 const TType &type = variable->getType();
440 TQualifier qualifier = type.getQualifier();
441
442 if (qualifier == EvqVaryingIn)
443 {
444 out << " " + name + " = input." + name + ";\n"; // FIXME: Prevent name clashes
445 }
446 }
447 }
448
449 out << "\n"
450 " gl_main();\n"
451 "\n"
452 " PS_OUTPUT output;\n" // FIXME: Prevent name clashes
453 " output.gl_Color[0] = gl_Color[0];\n"; // FIXME: Prevent name clashes
454
455 TSymbolTableLevel *symbols = context.symbolTable.getGlobalLevel();
456
457 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
458 {
459 const TSymbol *symbol = (*namedSymbol).second;
460 const TString &name = symbol->getName();
461 }
462 }
463 else
464 {
465 out << "VS_OUTPUT main(VS_INPUT input)\n" // FIXME: Prevent name clashes
466 "{\n";
467
468 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
469 {
470 const TSymbol *symbol = (*namedSymbol).second;
471 const TString &name = symbol->getName();
472
473 if (symbol->isVariable())
474 {
475 const TVariable *variable = static_cast<const TVariable*>(symbol);
476 const TType &type = variable->getType();
477 TQualifier qualifier = type.getQualifier();
478
479 if (qualifier == EvqAttribute)
480 {
481 out << " " + name + " = input." + name + ";\n"; // FIXME: Prevent name clashes
482 }
483 }
484 }
485
486 out << "\n"
487 " gl_main();\n"
488 "\n"
489 " VS_OUTPUT output;\n" // FIXME: Prevent name clashes
490 " output.gl_Position.x = gl_Position.x - gl_HalfPixelSize.x * gl_Position.w;\n"
491 " output.gl_Position.y = -(gl_Position.y - gl_HalfPixelSize.y * gl_Position.w);\n"
492 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
493 " output.gl_Position.w = gl_Position.w;\n"
494 " output.gl_PointSize = gl_PointSize;\n"
495 " output.gl_FragCoord = gl_Position;\n";
496
497 TSymbolTableLevel *symbols = context.symbolTable.getGlobalLevel();
498
499 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
500 {
501 const TSymbol *symbol = (*namedSymbol).second;
502 const TString &name = symbol->getName();
503
504 if (symbol->isVariable())
505 {
506 const TVariable *variable = static_cast<const TVariable*>(symbol);
507 TQualifier qualifier = variable->getType().getQualifier();
508
509 if (qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut)
510 {
511 out << " output." + name + " = " + name + ";\n"; // FIXME: Prevent name clashes
512 }
513 }
514 }
515 }
516
517 out << " return output;\n" // FIXME: Prevent name clashes
518 "}\n";
519}
520
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000521void OutputHLSL::visitSymbol(TIntermSymbol *node)
522{
523 TInfoSinkBase &out = context.infoSink.obj;
524
525 TString name = node->getSymbol();
526
527 if (name == "gl_FragColor")
528 {
529 out << "gl_Color[0]";
530 }
531 else if (name == "gl_FragData")
532 {
533 out << "gl_Color";
534 }
535 else
536 {
537 out << name;
538 }
539}
540
541bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
542{
543 TInfoSinkBase &out = context.infoSink.obj;
544
545 switch (node->getOp())
546 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000547 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
548 case EOpInitialize: outputTriplet(visit, NULL, " = ", NULL); break;
549 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
550 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
551 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
552 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
553 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
554 case EOpVectorTimesMatrixAssign:
555 case EOpMatrixTimesMatrixAssign:
556 if (visit == PreVisit)
557 {
558 out << "(";
559 }
560 else if (visit == InVisit)
561 {
562 out << " = mul(";
563 node->getLeft()->traverse(this);
564 out << ", ";
565 }
566 else
567 {
568 out << "))";
569 }
570 break;
571 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
572 case EOpIndexDirect: outputTriplet(visit, NULL, "[", "]"); break;
573 case EOpIndexIndirect: outputTriplet(visit, NULL, "[", "]"); break;
574 case EOpIndexDirectStruct: outputTriplet(visit, NULL, ".", NULL); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000575 case EOpVectorSwizzle:
576 if (visit == InVisit)
577 {
578 out << ".";
579
580 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
581
582 if (swizzle)
583 {
584 TIntermSequence &sequence = swizzle->getSequence();
585
586 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
587 {
588 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
589
590 if (element)
591 {
592 int i = element->getUnionArrayPointer()[0].getIConst();
593
594 switch (i)
595 {
596 case 0: out << "x"; break;
597 case 1: out << "y"; break;
598 case 2: out << "z"; break;
599 case 3: out << "w"; break;
600 default: UNREACHABLE();
601 }
602 }
603 else UNREACHABLE();
604 }
605 }
606 else UNREACHABLE();
607
608 return false; // Fully processed
609 }
610 break;
611 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
612 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
613 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
614 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000615 case EOpEqual:
616 if (!node->getLeft()->isMatrix())
617 {
618 outputTriplet(visit, "(", " == ", ")");
619 }
620 else
621 {
622 outputTriplet(visit, "__equal(", ", ", ")");
623 }
624 break;
625 case EOpNotEqual:
626 if (!node->getLeft()->isMatrix())
627 {
628 outputTriplet(visit, "(", " != ", ")");
629 }
630 else
631 {
632 outputTriplet(visit, "!__equal(", ", ", ")");
633 }
634 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000635 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
636 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
637 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
638 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
639 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000640 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
641 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000642 case EOpMatrixTimesVector: outputTriplet(visit, "mul(", ", ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000643 case EOpMatrixTimesMatrix: outputTriplet(visit, "mul(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000644 case EOpLogicalOr: outputTriplet(visit, "(", " || ", ")"); break;
645 case EOpLogicalXor: outputTriplet(visit, "xor(", ", ", ")"); break; // FIXME: Prevent name clashes
646 case EOpLogicalAnd: outputTriplet(visit, "(", " && ", ")"); break;
647 default: UNREACHABLE();
648 }
649
650 return true;
651}
652
653bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
654{
655 TInfoSinkBase &out = context.infoSink.obj;
656
657 switch (node->getOp())
658 {
659 case EOpNegative: outputTriplet(visit, "(-", NULL, ")"); break;
660 case EOpVectorLogicalNot: outputTriplet(visit, "(!", NULL, ")"); break;
661 case EOpLogicalNot: outputTriplet(visit, "(!", NULL, ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000662 case EOpPostIncrement: outputTriplet(visit, "(", NULL, "++)"); break;
663 case EOpPostDecrement: outputTriplet(visit, "(", NULL, "--)"); break;
664 case EOpPreIncrement: outputTriplet(visit, "(++", NULL, ")"); break;
665 case EOpPreDecrement: outputTriplet(visit, "(--", NULL, ")"); break;
666 case EOpConvIntToBool:
667 case EOpConvFloatToBool:
668 switch (node->getOperand()->getType().getNominalSize())
669 {
670 case 1: outputTriplet(visit, "bool(", NULL, ")"); break;
671 case 2: outputTriplet(visit, "bool2(", NULL, ")"); break;
672 case 3: outputTriplet(visit, "bool3(", NULL, ")"); break;
673 case 4: outputTriplet(visit, "bool4(", NULL, ")"); break;
674 default: UNREACHABLE();
675 }
676 break;
677 case EOpConvBoolToFloat:
678 case EOpConvIntToFloat:
679 switch (node->getOperand()->getType().getNominalSize())
680 {
681 case 1: outputTriplet(visit, "float(", NULL, ")"); break;
682 case 2: outputTriplet(visit, "float2(", NULL, ")"); break;
683 case 3: outputTriplet(visit, "float3(", NULL, ")"); break;
684 case 4: outputTriplet(visit, "float4(", NULL, ")"); break;
685 default: UNREACHABLE();
686 }
687 break;
688 case EOpConvFloatToInt:
689 case EOpConvBoolToInt:
690 switch (node->getOperand()->getType().getNominalSize())
691 {
692 case 1: outputTriplet(visit, "int(", NULL, ")"); break;
693 case 2: outputTriplet(visit, "int2(", NULL, ")"); break;
694 case 3: outputTriplet(visit, "int3(", NULL, ")"); break;
695 case 4: outputTriplet(visit, "int4(", NULL, ")"); break;
696 default: UNREACHABLE();
697 }
698 break;
699 case EOpRadians: outputTriplet(visit, "radians(", NULL, ")"); break;
700 case EOpDegrees: outputTriplet(visit, "degrees(", NULL, ")"); break;
701 case EOpSin: outputTriplet(visit, "sin(", NULL, ")"); break;
702 case EOpCos: outputTriplet(visit, "cos(", NULL, ")"); break;
703 case EOpTan: outputTriplet(visit, "tan(", NULL, ")"); break;
704 case EOpAsin: outputTriplet(visit, "asin(", NULL, ")"); break;
705 case EOpAcos: outputTriplet(visit, "acos(", NULL, ")"); break;
706 case EOpAtan: outputTriplet(visit, "atan(", NULL, ")"); break;
707 case EOpExp: outputTriplet(visit, "exp(", NULL, ")"); break;
708 case EOpLog: outputTriplet(visit, "log(", NULL, ")"); break;
709 case EOpExp2: outputTriplet(visit, "exp2(", NULL, ")"); break;
710 case EOpLog2: outputTriplet(visit, "log2(", NULL, ")"); break;
711 case EOpSqrt: outputTriplet(visit, "sqrt(", NULL, ")"); break;
712 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", NULL, ")"); break;
713 case EOpAbs: outputTriplet(visit, "abs(", NULL, ")"); break;
714 case EOpSign: outputTriplet(visit, "sign(", NULL, ")"); break;
715 case EOpFloor: outputTriplet(visit, "floor(", NULL, ")"); break;
716 case EOpCeil: outputTriplet(visit, "ceil(", NULL, ")"); break;
717 case EOpFract: outputTriplet(visit, "frac(", NULL, ")"); break;
718 case EOpLength: outputTriplet(visit, "length(", NULL, ")"); break;
719 case EOpNormalize: outputTriplet(visit, "normalize(", NULL, ")"); break;
daniel@transgaming.com45d03582010-03-11 19:41:29 +0000720// case EOpDPdx: outputTriplet(visit, "ddx(", NULL, ")"); break;
721// case EOpDPdy: outputTriplet(visit, "ddy(", NULL, ")"); break;
722// case EOpFwidth: outputTriplet(visit, "fwidth(", NULL, ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000723 case EOpAny: outputTriplet(visit, "any(", NULL, ")"); break;
724 case EOpAll: outputTriplet(visit, "all(", NULL, ")"); break;
725 default: UNREACHABLE();
726 }
727
728 return true;
729}
730
731bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
732{
733 EShLanguage language = context.language;
734 TInfoSinkBase &out = context.infoSink.obj;
735
736 if (node->getOp() == EOpNull)
737 {
738 out.message(EPrefixError, "node is still EOpNull!");
739 return true;
740 }
741
742 switch (node->getOp())
743 {
744 case EOpSequence: outputTriplet(visit, NULL, ";\n", ";\n"); break;
745 case EOpDeclaration:
746 if (visit == PreVisit)
747 {
748 TIntermSequence &sequence = node->getSequence();
749 TIntermTyped *variable = sequence[0]->getAsTyped();
750 bool visit = true;
751
752 if (variable && variable->getQualifier() == EvqTemporary)
753 {
754 out << typeString(variable->getType()) + " ";
755
756 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
757 {
758 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
759
760 if (symbol)
761 {
762 symbol->traverse(this);
763
764 out << arrayString(symbol->getType());
765 }
766 else
767 {
768 (*sit)->traverse(this);
769 }
770
771 if (visit && this->inVisit)
772 {
773 if (*sit != sequence.back())
774 {
775 visit = this->visitAggregate(InVisit, node);
776 }
777 }
778 }
779
780 if (visit && this->postVisit)
781 {
782 this->visitAggregate(PostVisit, node);
783 }
784 }
785
786 return false;
787 }
788 else if (visit == InVisit)
789 {
790 out << ", ";
791 }
792 break;
793 case EOpComma: UNIMPLEMENTED(); /* FIXME */ out << "Comma\n"; return true;
794 case EOpFunction:
795 {
796 const TString &mangledName = node->getName();
797 TString name = TString(mangledName.c_str(), mangledName.find_first_of('('));
798
799 if (visit == PreVisit)
800 {
801 if (name == "main")
802 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000803 name = "gl_main";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000804 }
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000805
806 out << typeString(node->getType()) << " " << name << "(";
807
808 TIntermSequence &sequence = node->getSequence();
809 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
810
811 for (unsigned int i = 0; i < arguments.size(); i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000812 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000813 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000814
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000815 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000816 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000817 const TType &type = symbol->getType();
818 const TString &name = symbol->getSymbol();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000819
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000820 out << typeString(type) + " " + name;
821
822 if (i < arguments.size() - 1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000823 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000824 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000825 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000826 }
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000827 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828 }
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000829
830 sequence.erase(sequence.begin());
831
832 out << ")\n"
833 "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000834 }
835 else if (visit == PostVisit)
836 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000837 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000838 }
839 }
840 break;
841 case EOpFunctionCall:
842 {
843 if (visit == PreVisit)
844 {
845 const TString &mangledName = node->getName();
846 TString name = TString(mangledName.c_str(), mangledName.find_first_of('('));
847
848 if (node->isUserDefined())
849 {
850 out << name << "(";
851 }
852 else
853 {
854 if (name == "texture2D")
855 {
856 if (node->getSequence().size() == 2)
857 {
858 out << "gl_texture2D(";
859 }
860 else if (node->getSequence().size() == 3)
861 {
862 out << "gl_texture2DBias(";
863 }
864 else UNREACHABLE();
865 }
866 else if (name == "texture2DProj")
867 {
868 out << "gl_texture2DProj(";
869 }
870 else if (name == "texture2DLod")
871 {
872 out << "gl_texture2DLod(";
873 UNIMPLEMENTED(); // FIXME: Move lod to last texture coordinate component
874 }
875 else if (name == "texture2DProjLod")
876 {
877 out << "gl_texture2DProjLod(";
878 UNIMPLEMENTED(); // FIXME: Move lod to last texture coordinate component
879 }
880 else if (name == "textureCube")
881 {
882 out << "gl_textureCube("; // FIXME: Incorrect sampling location
883 }
884 else
885 {
886 UNIMPLEMENTED(); // FIXME
887 }
888 }
889 }
890 else if (visit == InVisit)
891 {
892 out << ", ";
893 }
894 else
895 {
896 out << ")";
897 }
898 }
899 break;
900 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
901 case EOpConstructFloat: outputTriplet(visit, "vec1(", NULL, ")"); break;
902 case EOpConstructVec2: outputTriplet(visit, "vec2(", ", ", ")"); break;
903 case EOpConstructVec3: outputTriplet(visit, "vec3(", ", ", ")"); break;
904 case EOpConstructVec4: outputTriplet(visit, "vec4(", ", ", ")"); break;
905 case EOpConstructBool: UNIMPLEMENTED(); /* FIXME */ out << "Construct bool"; break;
906 case EOpConstructBVec2: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec2"; break;
907 case EOpConstructBVec3: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec3"; break;
908 case EOpConstructBVec4: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec4"; break;
909 case EOpConstructInt: UNIMPLEMENTED(); /* FIXME */ out << "Construct int"; break;
910 case EOpConstructIVec2: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec2"; break;
911 case EOpConstructIVec3: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec3"; break;
912 case EOpConstructIVec4: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec4"; break;
913 case EOpConstructMat2: outputTriplet(visit, "float2x2(", ", ", ")"); break;
914 case EOpConstructMat3: outputTriplet(visit, "float3x3(", ", ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000915 case EOpConstructMat4: outputTriplet(visit, "float4x4(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000916 case EOpConstructStruct: UNIMPLEMENTED(); /* FIXME */ out << "Construct structure"; break;
917 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
918 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
919 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
920 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
921 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
922 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
923 case EOpMod: outputTriplet(visit, "mod(", ", ", ")"); break; // FIXME: Prevent name clashes
924 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
925 case EOpAtan:
926 if (node->getSequence().size() == 1)
927 {
928 outputTriplet(visit, "atan(", ", ", ")");
929 }
930 else if (node->getSequence().size() == 2)
931 {
932 outputTriplet(visit, "atan2(", ", ", ")");
933 }
934 else UNREACHABLE();
935 break;
936 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
937 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
938 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
939 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
940 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
941 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
942 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
943 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
944 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000945 case EOpFaceForward: outputTriplet(visit, "faceforward(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000946 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
947 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
948 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000949 default: UNREACHABLE();
950 }
951
952 return true;
953}
954
955bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
956{
957 TInfoSinkBase &out = context.infoSink.obj;
958
alokp@chromium.org60fe4072010-03-29 20:58:29 +0000959 if (node->usesTernaryOperator())
960 {
961 out << "(";
962 node->getCondition()->traverse(this);
963 out << ") ? (";
964 node->getTrueBlock()->traverse(this);
965 out << ") : (";
966 node->getFalseBlock()->traverse(this);
967 out << ")\n";
968 }
969 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970 {
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +0000971 out << "if(";
972
973 node->getCondition()->traverse(this);
974
975 out << ")\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000976 "{\n";
977
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +0000978 node->getTrueBlock()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000979
980 out << ";}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +0000981
982 if (node->getFalseBlock())
983 {
984 out << "else\n"
985 "{\n";
986
987 node->getFalseBlock()->traverse(this);
988
989 out << ";}\n";
990 }
991 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000992
993 return false;
994}
995
996void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
997{
998 TInfoSinkBase &out = context.infoSink.obj;
999
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001000 TType &type = node->getType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001001
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001002 if (type.isField())
1003 {
1004 out << type.getFieldName();
1005 }
1006 else
1007 {
1008 int size = type.getObjectSize();
1009 bool matrix = type.isMatrix();
1010 TBasicType basicType = node->getUnionArrayPointer()[0].getType();
1011
1012 switch (basicType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001013 {
1014 case EbtBool:
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001015 if (!matrix)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001016 {
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001017 switch (size)
1018 {
1019 case 1: out << "bool("; break;
1020 case 2: out << "bool2("; break;
1021 case 3: out << "bool3("; break;
1022 case 4: out << "bool4("; break;
1023 default: UNREACHABLE();
1024 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001025 }
1026 else
1027 {
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001028 UNIMPLEMENTED();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001029 }
1030 break;
1031 case EbtFloat:
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001032 if (!matrix)
1033 {
1034 switch (size)
1035 {
1036 case 1: out << "float("; break;
1037 case 2: out << "float2("; break;
1038 case 3: out << "float3("; break;
1039 case 4: out << "float4("; break;
1040 default: UNREACHABLE();
1041 }
1042 }
1043 else
1044 {
1045 switch (size)
1046 {
1047 case 4: out << "float2x2("; break;
1048 case 9: out << "float3x3("; break;
1049 case 16: out << "float4x4("; break;
1050 default: UNREACHABLE();
1051 }
1052 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001053 break;
1054 case EbtInt:
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001055 if (!matrix)
1056 {
1057 switch (size)
1058 {
1059 case 1: out << "int("; break;
1060 case 2: out << "int2("; break;
1061 case 3: out << "int3("; break;
1062 case 4: out << "int4("; break;
1063 default: UNREACHABLE();
1064 }
1065 }
1066 else
1067 {
1068 UNIMPLEMENTED();
1069 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070 break;
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001071 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001072 UNIMPLEMENTED(); // FIXME
1073 }
1074
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001075 for (int i = 0; i < size; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001076 {
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001077 switch (basicType)
1078 {
1079 case EbtBool:
1080 if (node->getUnionArrayPointer()[i].getBConst())
1081 {
1082 out << "true";
1083 }
1084 else
1085 {
1086 out << "false";
1087 }
1088 break;
1089 case EbtFloat:
1090 out << node->getUnionArrayPointer()[i].getFConst();
1091 break;
1092 case EbtInt:
1093 out << node->getUnionArrayPointer()[i].getIConst();
1094 break;
1095 default:
1096 UNIMPLEMENTED(); // FIXME
1097 }
1098
1099 if (i != size - 1)
1100 {
1101 out << ", ";
1102 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001103 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001104
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001105 out << ")";
1106 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001107}
1108
1109bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
1110{
1111 TInfoSinkBase &out = context.infoSink.obj;
1112
1113 if (!node->testFirst())
1114 {
1115 out << "do\n"
1116 "{\n";
1117 }
1118 else
1119 {
1120 out << "for(";
1121
1122 if (node->getInit())
1123 {
1124 node->getInit()->traverse(this);
1125 }
1126
1127 out << "; ";
1128
1129 if (node->getTest())
1130 {
1131 node->getTest()->traverse(this);
1132 }
1133
1134 out << "; ";
1135
1136 if (node->getTerminal())
1137 {
1138 node->getTerminal()->traverse(this);
1139 }
1140
1141 out << ")\n"
1142 "{\n";
1143 }
1144
1145 if (node->getBody())
1146 {
1147 node->getBody()->traverse(this);
1148 }
1149
1150 out << "}\n";
1151
1152 if (!node->testFirst())
1153 {
1154 out << "while(\n";
1155
1156 node->getTest()->traverse(this);
1157
1158 out << ")";
1159 }
1160
1161 out << ";\n";
1162
1163 return false;
1164}
1165
1166bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
1167{
1168 TInfoSinkBase &out = context.infoSink.obj;
1169
1170 switch (node->getFlowOp())
1171 {
daniel@transgaming.comf67f82e2010-03-17 03:58:54 +00001172 case EOpKill: outputTriplet(visit, "discard", NULL, NULL); break;
1173 case EOpBreak: outputTriplet(visit, "break", NULL, NULL); break;
1174 case EOpContinue: outputTriplet(visit, "continue", NULL, NULL); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001175 case EOpReturn:
1176 if (visit == PreVisit)
1177 {
1178 if (node->getExpression())
1179 {
1180 out << "return ";
1181 }
1182 else
1183 {
1184 out << "return;\n";
1185 }
1186 }
1187 else if (visit == PostVisit)
1188 {
1189 out << ";\n";
1190 }
1191 break;
1192 default: UNREACHABLE();
1193 }
1194
1195 return true;
1196}
1197
1198void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
1199{
1200 TInfoSinkBase &out = context.infoSink.obj;
1201
1202 if (visit == PreVisit && preString)
1203 {
1204 out << preString;
1205 }
1206 else if (visit == InVisit && inString)
1207 {
1208 out << inString;
1209 }
1210 else if (visit == PostVisit && postString)
1211 {
1212 out << postString;
1213 }
1214}
1215
1216TString OutputHLSL::typeString(const TType &type)
1217{
1218 if (type.isMatrix())
1219 {
1220 switch (type.getNominalSize())
1221 {
1222 case 2: return "float2x2";
1223 case 3: return "float3x3";
1224 case 4: return "float4x4";
1225 }
1226 }
1227 else
1228 {
1229 switch (type.getBasicType())
1230 {
1231 case EbtFloat:
1232 switch (type.getNominalSize())
1233 {
1234 case 1: return "float";
1235 case 2: return "float2";
1236 case 3: return "float3";
1237 case 4: return "float4";
1238 }
1239 case EbtInt:
1240 switch (type.getNominalSize())
1241 {
1242 case 1: return "int";
1243 case 2: return "int2";
1244 case 3: return "int3";
1245 case 4: return "int4";
1246 }
1247 case EbtBool:
1248 switch (type.getNominalSize())
1249 {
1250 case 1: return "bool";
1251 case 2: return "bool2";
1252 case 3: return "bool3";
1253 case 4: return "bool4";
1254 }
1255 case EbtVoid:
1256 return "void";
1257 case EbtSampler2D:
1258 return "sampler2D";
1259 case EbtSamplerCube:
1260 return "samplerCUBE";
1261 }
1262 }
1263
1264 UNIMPLEMENTED(); // FIXME
1265 return "<unknown type>";
1266}
1267
1268TString OutputHLSL::arrayString(const TType &type)
1269{
1270 if (!type.isArray())
1271 {
1272 return "";
1273 }
1274
1275 char buffer[100];
1276 sprintf(buffer, "[%d]", type.getArraySize());
1277
1278 return buffer;
1279}
1280
1281TString OutputHLSL::initializer(const TType &type)
1282{
1283 TString string;
1284
1285 int arraySize = type.isArray() ? type.getArraySize() : 1;
1286
1287 if (type.isArray())
1288 {
1289 string += "{";
1290 }
1291
1292 for (int element = 0; element < arraySize; element++)
1293 {
1294 string += typeString(type) + "(";
1295
1296 for (int component = 0; component < type.getNominalSize(); component++)
1297 {
1298 string += "0";
1299
1300 if (component < type.getNominalSize() - 1)
1301 {
1302 string += ", ";
1303 }
1304 }
1305
1306 string += ")";
1307
1308 if (element < arraySize - 1)
1309 {
1310 string += ", ";
1311 }
1312 }
1313
1314 if (type.isArray())
1315 {
1316 string += "}";
1317 }
1318
1319 return string;
1320}
1321}