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