blob: 84cb8ed5d55761b2070ae20df43c1fb2497acd36 [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 {
alokp@chromium.org43884872010-03-30 00:08:52 +0000796 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000797
798 if (visit == PreVisit)
799 {
800 if (name == "main")
801 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000802 name = "gl_main";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000803 }
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000804
805 out << typeString(node->getType()) << " " << name << "(";
806
807 TIntermSequence &sequence = node->getSequence();
808 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
809
810 for (unsigned int i = 0; i < arguments.size(); i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000811 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000812 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000813
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000814 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000815 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000816 const TType &type = symbol->getType();
817 const TString &name = symbol->getSymbol();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000818
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000819 out << typeString(type) + " " + name;
820
821 if (i < arguments.size() - 1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000822 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000823 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000824 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000825 }
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000826 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000827 }
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000828
829 sequence.erase(sequence.begin());
830
831 out << ")\n"
832 "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000833 }
834 else if (visit == PostVisit)
835 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000836 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000837 }
838 }
839 break;
840 case EOpFunctionCall:
841 {
842 if (visit == PreVisit)
843 {
alokp@chromium.org43884872010-03-30 00:08:52 +0000844 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000845
846 if (node->isUserDefined())
847 {
848 out << name << "(";
849 }
850 else
851 {
852 if (name == "texture2D")
853 {
854 if (node->getSequence().size() == 2)
855 {
856 out << "gl_texture2D(";
857 }
858 else if (node->getSequence().size() == 3)
859 {
860 out << "gl_texture2DBias(";
861 }
862 else UNREACHABLE();
863 }
864 else if (name == "texture2DProj")
865 {
866 out << "gl_texture2DProj(";
867 }
868 else if (name == "texture2DLod")
869 {
870 out << "gl_texture2DLod(";
871 UNIMPLEMENTED(); // FIXME: Move lod to last texture coordinate component
872 }
873 else if (name == "texture2DProjLod")
874 {
875 out << "gl_texture2DProjLod(";
876 UNIMPLEMENTED(); // FIXME: Move lod to last texture coordinate component
877 }
878 else if (name == "textureCube")
879 {
880 out << "gl_textureCube("; // FIXME: Incorrect sampling location
881 }
882 else
883 {
884 UNIMPLEMENTED(); // FIXME
885 }
886 }
887 }
888 else if (visit == InVisit)
889 {
890 out << ", ";
891 }
892 else
893 {
894 out << ")";
895 }
896 }
897 break;
898 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
899 case EOpConstructFloat: outputTriplet(visit, "vec1(", NULL, ")"); break;
900 case EOpConstructVec2: outputTriplet(visit, "vec2(", ", ", ")"); break;
901 case EOpConstructVec3: outputTriplet(visit, "vec3(", ", ", ")"); break;
902 case EOpConstructVec4: outputTriplet(visit, "vec4(", ", ", ")"); break;
903 case EOpConstructBool: UNIMPLEMENTED(); /* FIXME */ out << "Construct bool"; break;
904 case EOpConstructBVec2: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec2"; break;
905 case EOpConstructBVec3: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec3"; break;
906 case EOpConstructBVec4: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec4"; break;
907 case EOpConstructInt: UNIMPLEMENTED(); /* FIXME */ out << "Construct int"; break;
908 case EOpConstructIVec2: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec2"; break;
909 case EOpConstructIVec3: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec3"; break;
910 case EOpConstructIVec4: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec4"; break;
911 case EOpConstructMat2: outputTriplet(visit, "float2x2(", ", ", ")"); break;
912 case EOpConstructMat3: outputTriplet(visit, "float3x3(", ", ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000913 case EOpConstructMat4: outputTriplet(visit, "float4x4(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000914 case EOpConstructStruct: UNIMPLEMENTED(); /* FIXME */ out << "Construct structure"; break;
915 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
916 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
917 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
918 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
919 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
920 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
921 case EOpMod: outputTriplet(visit, "mod(", ", ", ")"); break; // FIXME: Prevent name clashes
922 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
923 case EOpAtan:
924 if (node->getSequence().size() == 1)
925 {
926 outputTriplet(visit, "atan(", ", ", ")");
927 }
928 else if (node->getSequence().size() == 2)
929 {
930 outputTriplet(visit, "atan2(", ", ", ")");
931 }
932 else UNREACHABLE();
933 break;
934 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
935 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
936 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
937 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
938 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
939 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
940 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
941 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
942 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000943 case EOpFaceForward: outputTriplet(visit, "faceforward(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000944 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
945 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
946 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000947 default: UNREACHABLE();
948 }
949
950 return true;
951}
952
953bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
954{
955 TInfoSinkBase &out = context.infoSink.obj;
956
alokp@chromium.org60fe4072010-03-29 20:58:29 +0000957 if (node->usesTernaryOperator())
958 {
959 out << "(";
960 node->getCondition()->traverse(this);
961 out << ") ? (";
962 node->getTrueBlock()->traverse(this);
963 out << ") : (";
964 node->getFalseBlock()->traverse(this);
965 out << ")\n";
966 }
967 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000968 {
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +0000969 out << "if(";
970
971 node->getCondition()->traverse(this);
972
973 out << ")\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000974 "{\n";
975
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +0000976 node->getTrueBlock()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000977
978 out << ";}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +0000979
980 if (node->getFalseBlock())
981 {
982 out << "else\n"
983 "{\n";
984
985 node->getFalseBlock()->traverse(this);
986
987 out << ";}\n";
988 }
989 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000990
991 return false;
992}
993
994void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
995{
996 TInfoSinkBase &out = context.infoSink.obj;
997
daniel@transgaming.com45d03582010-03-11 19:41:29 +0000998 TType &type = node->getType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000999
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001000 if (type.isField())
1001 {
1002 out << type.getFieldName();
1003 }
1004 else
1005 {
1006 int size = type.getObjectSize();
1007 bool matrix = type.isMatrix();
1008 TBasicType basicType = node->getUnionArrayPointer()[0].getType();
1009
1010 switch (basicType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001011 {
1012 case EbtBool:
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001013 if (!matrix)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001014 {
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001015 switch (size)
1016 {
1017 case 1: out << "bool("; break;
1018 case 2: out << "bool2("; break;
1019 case 3: out << "bool3("; break;
1020 case 4: out << "bool4("; break;
1021 default: UNREACHABLE();
1022 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001023 }
1024 else
1025 {
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001026 UNIMPLEMENTED();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001027 }
1028 break;
1029 case EbtFloat:
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001030 if (!matrix)
1031 {
1032 switch (size)
1033 {
1034 case 1: out << "float("; break;
1035 case 2: out << "float2("; break;
1036 case 3: out << "float3("; break;
1037 case 4: out << "float4("; break;
1038 default: UNREACHABLE();
1039 }
1040 }
1041 else
1042 {
1043 switch (size)
1044 {
1045 case 4: out << "float2x2("; break;
1046 case 9: out << "float3x3("; break;
1047 case 16: out << "float4x4("; break;
1048 default: UNREACHABLE();
1049 }
1050 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001051 break;
1052 case EbtInt:
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001053 if (!matrix)
1054 {
1055 switch (size)
1056 {
1057 case 1: out << "int("; break;
1058 case 2: out << "int2("; break;
1059 case 3: out << "int3("; break;
1060 case 4: out << "int4("; break;
1061 default: UNREACHABLE();
1062 }
1063 }
1064 else
1065 {
1066 UNIMPLEMENTED();
1067 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001068 break;
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001069 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070 UNIMPLEMENTED(); // FIXME
1071 }
1072
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001073 for (int i = 0; i < size; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001074 {
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001075 switch (basicType)
1076 {
1077 case EbtBool:
1078 if (node->getUnionArrayPointer()[i].getBConst())
1079 {
1080 out << "true";
1081 }
1082 else
1083 {
1084 out << "false";
1085 }
1086 break;
1087 case EbtFloat:
1088 out << node->getUnionArrayPointer()[i].getFConst();
1089 break;
1090 case EbtInt:
1091 out << node->getUnionArrayPointer()[i].getIConst();
1092 break;
1093 default:
1094 UNIMPLEMENTED(); // FIXME
1095 }
1096
1097 if (i != size - 1)
1098 {
1099 out << ", ";
1100 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001101 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001102
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001103 out << ")";
1104 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001105}
1106
1107bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
1108{
1109 TInfoSinkBase &out = context.infoSink.obj;
1110
1111 if (!node->testFirst())
1112 {
1113 out << "do\n"
1114 "{\n";
1115 }
1116 else
1117 {
1118 out << "for(";
1119
1120 if (node->getInit())
1121 {
1122 node->getInit()->traverse(this);
1123 }
1124
1125 out << "; ";
1126
1127 if (node->getTest())
1128 {
1129 node->getTest()->traverse(this);
1130 }
1131
1132 out << "; ";
1133
1134 if (node->getTerminal())
1135 {
1136 node->getTerminal()->traverse(this);
1137 }
1138
1139 out << ")\n"
1140 "{\n";
1141 }
1142
1143 if (node->getBody())
1144 {
1145 node->getBody()->traverse(this);
1146 }
1147
1148 out << "}\n";
1149
1150 if (!node->testFirst())
1151 {
1152 out << "while(\n";
1153
1154 node->getTest()->traverse(this);
1155
1156 out << ")";
1157 }
1158
1159 out << ";\n";
1160
1161 return false;
1162}
1163
1164bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
1165{
1166 TInfoSinkBase &out = context.infoSink.obj;
1167
1168 switch (node->getFlowOp())
1169 {
daniel@transgaming.comf67f82e2010-03-17 03:58:54 +00001170 case EOpKill: outputTriplet(visit, "discard", NULL, NULL); break;
1171 case EOpBreak: outputTriplet(visit, "break", NULL, NULL); break;
1172 case EOpContinue: outputTriplet(visit, "continue", NULL, NULL); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001173 case EOpReturn:
1174 if (visit == PreVisit)
1175 {
1176 if (node->getExpression())
1177 {
1178 out << "return ";
1179 }
1180 else
1181 {
1182 out << "return;\n";
1183 }
1184 }
1185 else if (visit == PostVisit)
1186 {
1187 out << ";\n";
1188 }
1189 break;
1190 default: UNREACHABLE();
1191 }
1192
1193 return true;
1194}
1195
1196void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
1197{
1198 TInfoSinkBase &out = context.infoSink.obj;
1199
1200 if (visit == PreVisit && preString)
1201 {
1202 out << preString;
1203 }
1204 else if (visit == InVisit && inString)
1205 {
1206 out << inString;
1207 }
1208 else if (visit == PostVisit && postString)
1209 {
1210 out << postString;
1211 }
1212}
1213
1214TString OutputHLSL::typeString(const TType &type)
1215{
1216 if (type.isMatrix())
1217 {
1218 switch (type.getNominalSize())
1219 {
1220 case 2: return "float2x2";
1221 case 3: return "float3x3";
1222 case 4: return "float4x4";
1223 }
1224 }
1225 else
1226 {
1227 switch (type.getBasicType())
1228 {
1229 case EbtFloat:
1230 switch (type.getNominalSize())
1231 {
1232 case 1: return "float";
1233 case 2: return "float2";
1234 case 3: return "float3";
1235 case 4: return "float4";
1236 }
1237 case EbtInt:
1238 switch (type.getNominalSize())
1239 {
1240 case 1: return "int";
1241 case 2: return "int2";
1242 case 3: return "int3";
1243 case 4: return "int4";
1244 }
1245 case EbtBool:
1246 switch (type.getNominalSize())
1247 {
1248 case 1: return "bool";
1249 case 2: return "bool2";
1250 case 3: return "bool3";
1251 case 4: return "bool4";
1252 }
1253 case EbtVoid:
1254 return "void";
1255 case EbtSampler2D:
1256 return "sampler2D";
1257 case EbtSamplerCube:
1258 return "samplerCUBE";
1259 }
1260 }
1261
1262 UNIMPLEMENTED(); // FIXME
1263 return "<unknown type>";
1264}
1265
1266TString OutputHLSL::arrayString(const TType &type)
1267{
1268 if (!type.isArray())
1269 {
1270 return "";
1271 }
1272
1273 char buffer[100];
1274 sprintf(buffer, "[%d]", type.getArraySize());
1275
1276 return buffer;
1277}
1278
1279TString OutputHLSL::initializer(const TType &type)
1280{
1281 TString string;
1282
1283 int arraySize = type.isArray() ? type.getArraySize() : 1;
1284
1285 if (type.isArray())
1286 {
1287 string += "{";
1288 }
1289
1290 for (int element = 0; element < arraySize; element++)
1291 {
1292 string += typeString(type) + "(";
1293
1294 for (int component = 0; component < type.getNominalSize(); component++)
1295 {
1296 string += "0";
1297
1298 if (component < type.getNominalSize() - 1)
1299 {
1300 string += ", ";
1301 }
1302 }
1303
1304 string += ")";
1305
1306 if (element < arraySize - 1)
1307 {
1308 string += ", ";
1309 }
1310 }
1311
1312 if (type.isArray())
1313 {
1314 string += "}";
1315 }
1316
1317 return string;
1318}
1319}