blob: 785e18d4c2b5ee28dcbd4c821789e84f1990790e [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(";
daniel@transgaming.com279e38a2010-04-03 20:56:13 +0000556
557 if (node->getOp() == EOpMatrixTimesMatrixAssign)
558 {
559 out << "transpose(";
560 }
561
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000562 node->getLeft()->traverse(this);
daniel@transgaming.com279e38a2010-04-03 20:56:13 +0000563
564 if (node->getOp() == EOpMatrixTimesMatrixAssign)
565 {
566 out << ")";
567 }
568
569 out << ", transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000570 }
571 else
572 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +0000573 out << ")))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000574 }
575 break;
576 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
577 case EOpIndexDirect: outputTriplet(visit, NULL, "[", "]"); break;
578 case EOpIndexIndirect: outputTriplet(visit, NULL, "[", "]"); break;
579 case EOpIndexDirectStruct: outputTriplet(visit, NULL, ".", NULL); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000580 case EOpVectorSwizzle:
581 if (visit == InVisit)
582 {
583 out << ".";
584
585 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
586
587 if (swizzle)
588 {
589 TIntermSequence &sequence = swizzle->getSequence();
590
591 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
592 {
593 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
594
595 if (element)
596 {
597 int i = element->getUnionArrayPointer()[0].getIConst();
598
599 switch (i)
600 {
601 case 0: out << "x"; break;
602 case 1: out << "y"; break;
603 case 2: out << "z"; break;
604 case 3: out << "w"; break;
605 default: UNREACHABLE();
606 }
607 }
608 else UNREACHABLE();
609 }
610 }
611 else UNREACHABLE();
612
613 return false; // Fully processed
614 }
615 break;
616 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
617 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
618 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
619 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000620 case EOpEqual:
621 if (!node->getLeft()->isMatrix())
622 {
623 outputTriplet(visit, "(", " == ", ")");
624 }
625 else
626 {
627 outputTriplet(visit, "__equal(", ", ", ")");
628 }
629 break;
630 case EOpNotEqual:
631 if (!node->getLeft()->isMatrix())
632 {
633 outputTriplet(visit, "(", " != ", ")");
634 }
635 else
636 {
637 outputTriplet(visit, "!__equal(", ", ", ")");
638 }
639 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000640 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
641 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
642 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
643 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
644 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000645 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com279e38a2010-04-03 20:56:13 +0000646 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
647 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
648 case EOpMatrixTimesMatrix: outputTriplet(visit, "mul(transpose(", "), transpose(", "))"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649 case EOpLogicalOr: outputTriplet(visit, "(", " || ", ")"); break;
650 case EOpLogicalXor: outputTriplet(visit, "xor(", ", ", ")"); break; // FIXME: Prevent name clashes
651 case EOpLogicalAnd: outputTriplet(visit, "(", " && ", ")"); break;
652 default: UNREACHABLE();
653 }
654
655 return true;
656}
657
658bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
659{
660 TInfoSinkBase &out = context.infoSink.obj;
661
662 switch (node->getOp())
663 {
664 case EOpNegative: outputTriplet(visit, "(-", NULL, ")"); break;
665 case EOpVectorLogicalNot: outputTriplet(visit, "(!", NULL, ")"); break;
666 case EOpLogicalNot: outputTriplet(visit, "(!", NULL, ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000667 case EOpPostIncrement: outputTriplet(visit, "(", NULL, "++)"); break;
668 case EOpPostDecrement: outputTriplet(visit, "(", NULL, "--)"); break;
669 case EOpPreIncrement: outputTriplet(visit, "(++", NULL, ")"); break;
670 case EOpPreDecrement: outputTriplet(visit, "(--", NULL, ")"); break;
671 case EOpConvIntToBool:
672 case EOpConvFloatToBool:
673 switch (node->getOperand()->getType().getNominalSize())
674 {
675 case 1: outputTriplet(visit, "bool(", NULL, ")"); break;
676 case 2: outputTriplet(visit, "bool2(", NULL, ")"); break;
677 case 3: outputTriplet(visit, "bool3(", NULL, ")"); break;
678 case 4: outputTriplet(visit, "bool4(", NULL, ")"); break;
679 default: UNREACHABLE();
680 }
681 break;
682 case EOpConvBoolToFloat:
683 case EOpConvIntToFloat:
684 switch (node->getOperand()->getType().getNominalSize())
685 {
686 case 1: outputTriplet(visit, "float(", NULL, ")"); break;
687 case 2: outputTriplet(visit, "float2(", NULL, ")"); break;
688 case 3: outputTriplet(visit, "float3(", NULL, ")"); break;
689 case 4: outputTriplet(visit, "float4(", NULL, ")"); break;
690 default: UNREACHABLE();
691 }
692 break;
693 case EOpConvFloatToInt:
694 case EOpConvBoolToInt:
695 switch (node->getOperand()->getType().getNominalSize())
696 {
697 case 1: outputTriplet(visit, "int(", NULL, ")"); break;
698 case 2: outputTriplet(visit, "int2(", NULL, ")"); break;
699 case 3: outputTriplet(visit, "int3(", NULL, ")"); break;
700 case 4: outputTriplet(visit, "int4(", NULL, ")"); break;
701 default: UNREACHABLE();
702 }
703 break;
704 case EOpRadians: outputTriplet(visit, "radians(", NULL, ")"); break;
705 case EOpDegrees: outputTriplet(visit, "degrees(", NULL, ")"); break;
706 case EOpSin: outputTriplet(visit, "sin(", NULL, ")"); break;
707 case EOpCos: outputTriplet(visit, "cos(", NULL, ")"); break;
708 case EOpTan: outputTriplet(visit, "tan(", NULL, ")"); break;
709 case EOpAsin: outputTriplet(visit, "asin(", NULL, ")"); break;
710 case EOpAcos: outputTriplet(visit, "acos(", NULL, ")"); break;
711 case EOpAtan: outputTriplet(visit, "atan(", NULL, ")"); break;
712 case EOpExp: outputTriplet(visit, "exp(", NULL, ")"); break;
713 case EOpLog: outputTriplet(visit, "log(", NULL, ")"); break;
714 case EOpExp2: outputTriplet(visit, "exp2(", NULL, ")"); break;
715 case EOpLog2: outputTriplet(visit, "log2(", NULL, ")"); break;
716 case EOpSqrt: outputTriplet(visit, "sqrt(", NULL, ")"); break;
717 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", NULL, ")"); break;
718 case EOpAbs: outputTriplet(visit, "abs(", NULL, ")"); break;
719 case EOpSign: outputTriplet(visit, "sign(", NULL, ")"); break;
720 case EOpFloor: outputTriplet(visit, "floor(", NULL, ")"); break;
721 case EOpCeil: outputTriplet(visit, "ceil(", NULL, ")"); break;
722 case EOpFract: outputTriplet(visit, "frac(", NULL, ")"); break;
723 case EOpLength: outputTriplet(visit, "length(", NULL, ")"); break;
724 case EOpNormalize: outputTriplet(visit, "normalize(", NULL, ")"); break;
daniel@transgaming.com45d03582010-03-11 19:41:29 +0000725// case EOpDPdx: outputTriplet(visit, "ddx(", NULL, ")"); break;
726// case EOpDPdy: outputTriplet(visit, "ddy(", NULL, ")"); break;
727// case EOpFwidth: outputTriplet(visit, "fwidth(", NULL, ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000728 case EOpAny: outputTriplet(visit, "any(", NULL, ")"); break;
729 case EOpAll: outputTriplet(visit, "all(", NULL, ")"); break;
730 default: UNREACHABLE();
731 }
732
733 return true;
734}
735
736bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
737{
738 EShLanguage language = context.language;
739 TInfoSinkBase &out = context.infoSink.obj;
740
741 if (node->getOp() == EOpNull)
742 {
743 out.message(EPrefixError, "node is still EOpNull!");
744 return true;
745 }
746
747 switch (node->getOp())
748 {
749 case EOpSequence: outputTriplet(visit, NULL, ";\n", ";\n"); break;
750 case EOpDeclaration:
751 if (visit == PreVisit)
752 {
753 TIntermSequence &sequence = node->getSequence();
754 TIntermTyped *variable = sequence[0]->getAsTyped();
755 bool visit = true;
756
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000757 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000758 {
759 out << typeString(variable->getType()) + " ";
760
761 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
762 {
763 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
764
765 if (symbol)
766 {
767 symbol->traverse(this);
768
769 out << arrayString(symbol->getType());
770 }
771 else
772 {
773 (*sit)->traverse(this);
774 }
775
776 if (visit && this->inVisit)
777 {
778 if (*sit != sequence.back())
779 {
780 visit = this->visitAggregate(InVisit, node);
781 }
782 }
783 }
784
785 if (visit && this->postVisit)
786 {
787 this->visitAggregate(PostVisit, node);
788 }
789 }
790
791 return false;
792 }
793 else if (visit == InVisit)
794 {
795 out << ", ";
796 }
797 break;
798 case EOpComma: UNIMPLEMENTED(); /* FIXME */ out << "Comma\n"; return true;
799 case EOpFunction:
800 {
alokp@chromium.org43884872010-03-30 00:08:52 +0000801 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000802
803 if (visit == PreVisit)
804 {
805 if (name == "main")
806 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000807 name = "gl_main";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000808 }
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000809
810 out << typeString(node->getType()) << " " << name << "(";
811
812 TIntermSequence &sequence = node->getSequence();
813 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
814
815 for (unsigned int i = 0; i < arguments.size(); i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000816 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000817 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000818
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000819 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000820 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000821 const TType &type = symbol->getType();
822 const TString &name = symbol->getSymbol();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000823
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000824 out << typeString(type) + " " + name;
825
826 if (i < arguments.size() - 1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000827 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000828 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000829 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000830 }
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000831 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000832 }
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000833
834 sequence.erase(sequence.begin());
835
836 out << ")\n"
837 "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000838 }
839 else if (visit == PostVisit)
840 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000841 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000842 }
843 }
844 break;
845 case EOpFunctionCall:
846 {
847 if (visit == PreVisit)
848 {
alokp@chromium.org43884872010-03-30 00:08:52 +0000849 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000850
851 if (node->isUserDefined())
852 {
853 out << name << "(";
854 }
855 else
856 {
857 if (name == "texture2D")
858 {
859 if (node->getSequence().size() == 2)
860 {
861 out << "gl_texture2D(";
862 }
863 else if (node->getSequence().size() == 3)
864 {
865 out << "gl_texture2DBias(";
866 }
867 else UNREACHABLE();
868 }
869 else if (name == "texture2DProj")
870 {
871 out << "gl_texture2DProj(";
872 }
873 else if (name == "texture2DLod")
874 {
875 out << "gl_texture2DLod(";
876 UNIMPLEMENTED(); // FIXME: Move lod to last texture coordinate component
877 }
878 else if (name == "texture2DProjLod")
879 {
880 out << "gl_texture2DProjLod(";
881 UNIMPLEMENTED(); // FIXME: Move lod to last texture coordinate component
882 }
883 else if (name == "textureCube")
884 {
885 out << "gl_textureCube("; // FIXME: Incorrect sampling location
886 }
887 else
888 {
889 UNIMPLEMENTED(); // FIXME
890 }
891 }
892 }
893 else if (visit == InVisit)
894 {
895 out << ", ";
896 }
897 else
898 {
899 out << ")";
900 }
901 }
902 break;
903 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
904 case EOpConstructFloat: outputTriplet(visit, "vec1(", NULL, ")"); break;
905 case EOpConstructVec2: outputTriplet(visit, "vec2(", ", ", ")"); break;
906 case EOpConstructVec3: outputTriplet(visit, "vec3(", ", ", ")"); break;
907 case EOpConstructVec4: outputTriplet(visit, "vec4(", ", ", ")"); break;
908 case EOpConstructBool: UNIMPLEMENTED(); /* FIXME */ out << "Construct bool"; break;
909 case EOpConstructBVec2: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec2"; break;
910 case EOpConstructBVec3: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec3"; break;
911 case EOpConstructBVec4: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec4"; break;
912 case EOpConstructInt: UNIMPLEMENTED(); /* FIXME */ out << "Construct int"; break;
913 case EOpConstructIVec2: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec2"; break;
914 case EOpConstructIVec3: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec3"; break;
915 case EOpConstructIVec4: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec4"; break;
916 case EOpConstructMat2: outputTriplet(visit, "float2x2(", ", ", ")"); break;
917 case EOpConstructMat3: outputTriplet(visit, "float3x3(", ", ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000918 case EOpConstructMat4: outputTriplet(visit, "float4x4(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000919 case EOpConstructStruct: UNIMPLEMENTED(); /* FIXME */ out << "Construct structure"; break;
920 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
921 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
922 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
923 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
924 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
925 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
926 case EOpMod: outputTriplet(visit, "mod(", ", ", ")"); break; // FIXME: Prevent name clashes
927 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
928 case EOpAtan:
929 if (node->getSequence().size() == 1)
930 {
931 outputTriplet(visit, "atan(", ", ", ")");
932 }
933 else if (node->getSequence().size() == 2)
934 {
935 outputTriplet(visit, "atan2(", ", ", ")");
936 }
937 else UNREACHABLE();
938 break;
939 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
940 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
941 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
942 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
943 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
944 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
945 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
946 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
947 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000948 case EOpFaceForward: outputTriplet(visit, "faceforward(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000949 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
950 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
951 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952 default: UNREACHABLE();
953 }
954
955 return true;
956}
957
958bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
959{
960 TInfoSinkBase &out = context.infoSink.obj;
961
alokp@chromium.org60fe4072010-03-29 20:58:29 +0000962 if (node->usesTernaryOperator())
963 {
964 out << "(";
965 node->getCondition()->traverse(this);
966 out << ") ? (";
967 node->getTrueBlock()->traverse(this);
968 out << ") : (";
969 node->getFalseBlock()->traverse(this);
970 out << ")\n";
971 }
972 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973 {
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +0000974 out << "if(";
975
976 node->getCondition()->traverse(this);
977
978 out << ")\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000979 "{\n";
980
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +0000981 node->getTrueBlock()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000982
983 out << ";}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +0000984
985 if (node->getFalseBlock())
986 {
987 out << "else\n"
988 "{\n";
989
990 node->getFalseBlock()->traverse(this);
991
992 out << ";}\n";
993 }
994 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000995
996 return false;
997}
998
999void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
1000{
1001 TInfoSinkBase &out = context.infoSink.obj;
1002
alokp@chromium.orgdd037b22010-03-30 18:47:20 +00001003 const TType &type = node->getType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001004
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001005 if (type.isField())
1006 {
1007 out << type.getFieldName();
1008 }
1009 else
1010 {
1011 int size = type.getObjectSize();
1012 bool matrix = type.isMatrix();
1013 TBasicType basicType = node->getUnionArrayPointer()[0].getType();
1014
1015 switch (basicType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001016 {
1017 case EbtBool:
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001018 if (!matrix)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001019 {
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001020 switch (size)
1021 {
1022 case 1: out << "bool("; break;
1023 case 2: out << "bool2("; break;
1024 case 3: out << "bool3("; break;
1025 case 4: out << "bool4("; break;
1026 default: UNREACHABLE();
1027 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028 }
1029 else
1030 {
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001031 UNIMPLEMENTED();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001032 }
1033 break;
1034 case EbtFloat:
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001035 if (!matrix)
1036 {
1037 switch (size)
1038 {
1039 case 1: out << "float("; break;
1040 case 2: out << "float2("; break;
1041 case 3: out << "float3("; break;
1042 case 4: out << "float4("; break;
1043 default: UNREACHABLE();
1044 }
1045 }
1046 else
1047 {
1048 switch (size)
1049 {
1050 case 4: out << "float2x2("; break;
1051 case 9: out << "float3x3("; break;
1052 case 16: out << "float4x4("; break;
1053 default: UNREACHABLE();
1054 }
1055 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001056 break;
1057 case EbtInt:
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001058 if (!matrix)
1059 {
1060 switch (size)
1061 {
1062 case 1: out << "int("; break;
1063 case 2: out << "int2("; break;
1064 case 3: out << "int3("; break;
1065 case 4: out << "int4("; break;
1066 default: UNREACHABLE();
1067 }
1068 }
1069 else
1070 {
1071 UNIMPLEMENTED();
1072 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001073 break;
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001074 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001075 UNIMPLEMENTED(); // FIXME
1076 }
1077
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001078 for (int i = 0; i < size; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001079 {
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001080 switch (basicType)
1081 {
1082 case EbtBool:
1083 if (node->getUnionArrayPointer()[i].getBConst())
1084 {
1085 out << "true";
1086 }
1087 else
1088 {
1089 out << "false";
1090 }
1091 break;
1092 case EbtFloat:
1093 out << node->getUnionArrayPointer()[i].getFConst();
1094 break;
1095 case EbtInt:
1096 out << node->getUnionArrayPointer()[i].getIConst();
1097 break;
1098 default:
1099 UNIMPLEMENTED(); // FIXME
1100 }
1101
1102 if (i != size - 1)
1103 {
1104 out << ", ";
1105 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001106 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001107
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001108 out << ")";
1109 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001110}
1111
1112bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
1113{
1114 TInfoSinkBase &out = context.infoSink.obj;
1115
1116 if (!node->testFirst())
1117 {
1118 out << "do\n"
1119 "{\n";
1120 }
1121 else
1122 {
1123 out << "for(";
1124
1125 if (node->getInit())
1126 {
1127 node->getInit()->traverse(this);
1128 }
1129
1130 out << "; ";
1131
1132 if (node->getTest())
1133 {
1134 node->getTest()->traverse(this);
1135 }
1136
1137 out << "; ";
1138
1139 if (node->getTerminal())
1140 {
1141 node->getTerminal()->traverse(this);
1142 }
1143
1144 out << ")\n"
1145 "{\n";
1146 }
1147
1148 if (node->getBody())
1149 {
1150 node->getBody()->traverse(this);
1151 }
1152
1153 out << "}\n";
1154
1155 if (!node->testFirst())
1156 {
1157 out << "while(\n";
1158
1159 node->getTest()->traverse(this);
1160
1161 out << ")";
1162 }
1163
1164 out << ";\n";
1165
1166 return false;
1167}
1168
1169bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
1170{
1171 TInfoSinkBase &out = context.infoSink.obj;
1172
1173 switch (node->getFlowOp())
1174 {
daniel@transgaming.comf67f82e2010-03-17 03:58:54 +00001175 case EOpKill: outputTriplet(visit, "discard", NULL, NULL); break;
1176 case EOpBreak: outputTriplet(visit, "break", NULL, NULL); break;
1177 case EOpContinue: outputTriplet(visit, "continue", NULL, NULL); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001178 case EOpReturn:
1179 if (visit == PreVisit)
1180 {
1181 if (node->getExpression())
1182 {
1183 out << "return ";
1184 }
1185 else
1186 {
1187 out << "return;\n";
1188 }
1189 }
1190 else if (visit == PostVisit)
1191 {
1192 out << ";\n";
1193 }
1194 break;
1195 default: UNREACHABLE();
1196 }
1197
1198 return true;
1199}
1200
1201void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
1202{
1203 TInfoSinkBase &out = context.infoSink.obj;
1204
1205 if (visit == PreVisit && preString)
1206 {
1207 out << preString;
1208 }
1209 else if (visit == InVisit && inString)
1210 {
1211 out << inString;
1212 }
1213 else if (visit == PostVisit && postString)
1214 {
1215 out << postString;
1216 }
1217}
1218
1219TString OutputHLSL::typeString(const TType &type)
1220{
1221 if (type.isMatrix())
1222 {
1223 switch (type.getNominalSize())
1224 {
1225 case 2: return "float2x2";
1226 case 3: return "float3x3";
1227 case 4: return "float4x4";
1228 }
1229 }
1230 else
1231 {
1232 switch (type.getBasicType())
1233 {
1234 case EbtFloat:
1235 switch (type.getNominalSize())
1236 {
1237 case 1: return "float";
1238 case 2: return "float2";
1239 case 3: return "float3";
1240 case 4: return "float4";
1241 }
1242 case EbtInt:
1243 switch (type.getNominalSize())
1244 {
1245 case 1: return "int";
1246 case 2: return "int2";
1247 case 3: return "int3";
1248 case 4: return "int4";
1249 }
1250 case EbtBool:
1251 switch (type.getNominalSize())
1252 {
1253 case 1: return "bool";
1254 case 2: return "bool2";
1255 case 3: return "bool3";
1256 case 4: return "bool4";
1257 }
1258 case EbtVoid:
1259 return "void";
1260 case EbtSampler2D:
1261 return "sampler2D";
1262 case EbtSamplerCube:
1263 return "samplerCUBE";
1264 }
1265 }
1266
1267 UNIMPLEMENTED(); // FIXME
1268 return "<unknown type>";
1269}
1270
1271TString OutputHLSL::arrayString(const TType &type)
1272{
1273 if (!type.isArray())
1274 {
1275 return "";
1276 }
1277
1278 char buffer[100];
1279 sprintf(buffer, "[%d]", type.getArraySize());
1280
1281 return buffer;
1282}
1283
1284TString OutputHLSL::initializer(const TType &type)
1285{
1286 TString string;
1287
1288 int arraySize = type.isArray() ? type.getArraySize() : 1;
1289
1290 if (type.isArray())
1291 {
1292 string += "{";
1293 }
1294
1295 for (int element = 0; element < arraySize; element++)
1296 {
1297 string += typeString(type) + "(";
1298
1299 for (int component = 0; component < type.getNominalSize(); component++)
1300 {
1301 string += "0";
1302
1303 if (component < type.getNominalSize() - 1)
1304 {
1305 string += ", ";
1306 }
1307 }
1308
1309 string += ")";
1310
1311 if (element < arraySize - 1)
1312 {
1313 string += ", ";
1314 }
1315 }
1316
1317 if (type.isArray())
1318 {
1319 string += "}";
1320 }
1321
1322 return string;
1323}
1324}