blob: 4a8ad7f41a8ea6cdc53263af6556631f3353f4a8 [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
daniel@transgaming.com0e3358a2010-04-05 20:32:42 +000053 // Program linking depends on this exact format
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000054 varyingInput += " " + typeString(type) + " " + name + arrayString(type) + semantic + ";\n";
55 varyingGlobals += "static " + typeString(type) + " " + name + arrayString(type) + " = " + initializer(type) + ";\n";
56 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +000057 else if (qualifier == EvqGlobal)
58 {
59 // Globals are declared and intialized as an aggregate node
60 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +000061 else if (qualifier == EvqConst)
62 {
63 // Constants are repeated as literals where used
64 }
65 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000066 }
67 }
68
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +000069 out << "uniform float4 gl_Window;\n"
70 "uniform float2 gl_Depth;\n"
daniel@transgaming.com79b820b2010-03-16 05:48:57 +000071 "uniform bool __frontCCW;\n"
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +000072 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000073 out << uniforms;
daniel@transgaming.com86487c22010-03-11 19:41:43 +000074 out << "\n"
75 "struct PS_INPUT\n" // FIXME: Prevent name clashes
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000076 "{\n";
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +000077 out << varyingInput;
daniel@transgaming.com0e3358a2010-04-05 20:32:42 +000078 out << " float4 gl_FragCoord : TEXCOORD" << semanticIndex << ";\n";
daniel@transgaming.com79b820b2010-03-16 05:48:57 +000079 out << " float __vFace : VFACE;\n"
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +000080 "};\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000081 "\n";
82 out << varyingGlobals;
83 out << "\n"
84 "struct PS_OUTPUT\n" // FIXME: Prevent name clashes
85 "{\n"
86 " float4 gl_Color[1] : COLOR;\n"
87 "};\n"
88 "\n"
89 "static float4 gl_Color[1] = {float4(0, 0, 0, 0)};\n"
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +000090 "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n"
daniel@transgaming.comccad59f2010-03-26 04:08:39 +000091 "static float2 gl_PointCoord = float2(0.5, 0.5);\n"
daniel@transgaming.com79b820b2010-03-16 05:48:57 +000092 "static bool gl_FrontFacing = false;\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000093 "\n"
94 "float4 gl_texture2D(sampler2D s, float2 t)\n"
95 "{\n"
96 " return tex2D(s, t);\n"
97 "}\n"
98 "\n"
99 "float4 gl_texture2DProj(sampler2D s, float3 t)\n"
100 "{\n"
101 " return tex2Dproj(s, float4(t.x, t.y, 0, t.z));\n"
102 "}\n"
103 "float4 gl_texture2DBias(sampler2D s, float2 t, float bias)\n"
104 "{\n"
105 " return tex2Dbias(s, float4(t.x, t.y, 0, bias));\n"
106 "}\n"
107 "\n"
108 "float4 gl_textureCube(samplerCUBE s, float3 t)\n"
109 "{\n"
110 " return texCUBE(s, t);\n"
111 "}\n"
112 "\n";
113 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000114 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000115 {
116 TString uniforms;
117 TString attributeInput;
118 TString attributeGlobals;
119 TString varyingOutput;
120 TString varyingGlobals;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000121
122 TSymbolTableLevel *symbols = context.symbolTable.getGlobalLevel();
123 int semanticIndex = 0;
124
125 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
126 {
127 const TSymbol *symbol = (*namedSymbol).second;
128 const TString &name = symbol->getName();
129
130 if (symbol->isVariable())
131 {
132 const TVariable *variable = static_cast<const TVariable*>(symbol);
133 const TType &type = variable->getType();
134 TQualifier qualifier = type.getQualifier();
135
136 if (qualifier == EvqUniform)
137 {
138 uniforms += "uniform " + typeString(type) + " " + name + arrayString(type) + ";\n";
139 }
140 else if (qualifier == EvqAttribute)
141 {
142 char semantic[100];
143 sprintf(semantic, " : TEXCOORD%d", semanticIndex);
144 semanticIndex += type.isArray() ? type.getArraySize() : 1;
145
146 attributeInput += " " + typeString(type) + " " + name + arrayString(type) + semantic + ";\n";
147 attributeGlobals += "static " + typeString(type) + " " + name + arrayString(type) + " = " + initializer(type) + ";\n";
148 }
149 else if (qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut)
150 {
daniel@transgaming.com0e3358a2010-04-05 20:32:42 +0000151 // Program linking depends on this exact format
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000152 varyingOutput += " " + typeString(type) + " " + name + arrayString(type) + " : TEXCOORD0;\n"; // Actual semantic index assigned during link
153 varyingGlobals += "static " + typeString(type) + " " + name + arrayString(type) + " = " + initializer(type) + ";\n";
154 }
155 else if (qualifier == EvqGlobal)
156 {
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000157 // Globals are declared and intialized as an aggregate node
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000158 }
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000159 else if (qualifier == EvqConst)
160 {
161 // Constants are repeated as literals where used
162 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000163 else UNREACHABLE();
164 }
165 }
166
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +0000167 out << "uniform float2 gl_HalfPixelSize;\n"
168 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000169 out << uniforms;
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +0000170 out << "\n"
171 "struct VS_INPUT\n" // FIXME: Prevent name clashes
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000172 "{\n";
173 out << attributeInput;
174 out << "};\n"
175 "\n";
176 out << attributeGlobals;
177 out << "\n"
178 "struct VS_OUTPUT\n" // FIXME: Prevent name clashes
179 "{\n"
180 " float4 gl_Position : POSITION;\n"
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +0000181 " float gl_PointSize : PSIZE;\n"
daniel@transgaming.com0e3358a2010-04-05 20:32:42 +0000182 " float4 gl_FragCoord : TEXCOORD0;\n"; // Actual semantic index assigned during link
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +0000183 out << varyingOutput;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000184 out << "};\n"
185 "\n"
186 "static float4 gl_Position = float4(0, 0, 0, 0);\n"
daniel@transgaming.comccad59f2010-03-26 04:08:39 +0000187 "static float gl_PointSize = float(1);\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000188 out << varyingGlobals;
189 out << "\n";
190 }
191
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000192 out << "struct gl_DepthRangeParameters\n"
193 "{\n"
194 " float near;\n"
195 " float far;\n"
196 " float diff;\n"
197 "};\n"
198 "\n"
199 "uniform gl_DepthRangeParameters gl_DepthRange;\n"
200 "\n"
201 "float vec1(float x)\n" // FIXME: Prevent name clashes
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000202 "{\n"
203 " return x;\n"
204 "}\n"
205 "\n"
206 "float vec1(float2 xy)\n" // FIXME: Prevent name clashes
207 "{\n"
208 " return xy[0];\n"
209 "}\n"
210 "\n"
211 "float vec1(float3 xyz)\n" // FIXME: Prevent name clashes
212 "{\n"
213 " return xyz[0];\n"
214 "}\n"
215 "\n"
216 "float vec1(float4 xyzw)\n" // FIXME: Prevent name clashes
217 "{\n"
218 " return xyzw[0];\n"
219 "}\n"
220 "\n"
221 "float2 vec2(float x)\n" // FIXME: Prevent name clashes
222 "{\n"
223 " return float2(x, x);\n"
224 "}\n"
225 "\n"
226 "float2 vec2(float x, float y)\n" // FIXME: Prevent name clashes
227 "{\n"
228 " return float2(x, y);\n"
229 "}\n"
230 "\n"
231 "float2 vec2(float2 xy)\n" // FIXME: Prevent name clashes
232 "{\n"
233 " return xy;\n"
234 "}\n"
235 "\n"
236 "float2 vec2(float3 xyz)\n" // FIXME: Prevent name clashes
237 "{\n"
238 " return float2(xyz[0], xyz[1]);\n"
239 "}\n"
240 "\n"
241 "float2 vec2(float4 xyzw)\n" // FIXME: Prevent name clashes
242 "{\n"
243 " return float2(xyzw[0], xyzw[1]);\n"
244 "}\n"
245 "\n"
246 "float3 vec3(float x)\n" // FIXME: Prevent name clashes
247 "{\n"
248 " return float3(x, x, x);\n"
249 "}\n"
250 "\n"
251 "float3 vec3(float x, float y, float z)\n" // FIXME: Prevent name clashes
252 "{\n"
253 " return float3(x, y, z);\n"
254 "}\n"
255 "\n"
256 "float3 vec3(float2 xy, float z)\n" // FIXME: Prevent name clashes
257 "{\n"
258 " return float3(xy[0], xy[1], z);\n"
259 "}\n"
260 "\n"
261 "float3 vec3(float x, float2 yz)\n" // FIXME: Prevent name clashes
262 "{\n"
263 " return float3(x, yz[0], yz[1]);\n"
264 "}\n"
265 "\n"
266 "float3 vec3(float3 xyz)\n" // FIXME: Prevent name clashes
267 "{\n"
268 " return xyz;\n"
269 "}\n"
270 "\n"
271 "float3 vec3(float4 xyzw)\n" // FIXME: Prevent name clashes
272 "{\n"
273 " return float3(xyzw[0], xyzw[1], xyzw[2]);\n"
274 "}\n"
275 "\n"
276 "float4 vec4(float x)\n" // FIXME: Prevent name clashes
277 "{\n"
278 " return float4(x, x, x, x);\n"
279 "}\n"
280 "\n"
281 "float4 vec4(float x, float y, float z, float w)\n" // FIXME: Prevent name clashes
282 "{\n"
283 " return float4(x, y, z, w);\n"
284 "}\n"
285 "\n"
286 "float4 vec4(float2 xy, float z, float w)\n" // FIXME: Prevent name clashes
287 "{\n"
288 " return float4(xy[0], xy[1], z, w);\n"
289 "}\n"
290 "\n"
291 "float4 vec4(float x, float2 yz, float w)\n" // FIXME: Prevent name clashes
292 "{\n"
293 " return float4(x, yz[0], yz[1], w);\n"
294 "}\n"
295 "\n"
296 "float4 vec4(float x, float y, float2 zw)\n" // FIXME: Prevent name clashes
297 "{\n"
298 " return float4(x, y, zw[0], zw[1]);\n"
299 "}\n"
300 "\n"
301 "float4 vec4(float2 xy, float2 zw)\n" // FIXME: Prevent name clashes
302 "{\n"
303 " return float4(xy[0], xy[1], zw[0], zw[1]);\n"
304 "}\n"
305 "\n"
306 "float4 vec4(float3 xyz, float w)\n" // FIXME: Prevent name clashes
307 "{\n"
308 " return float4(xyz[0], xyz[1], xyz[2], w);\n"
309 "}\n"
310 "\n"
311 "float4 vec4(float x, float3 yzw)\n" // FIXME: Prevent name clashes
312 "{\n"
313 " return float4(x, yzw[0], yzw[1], yzw[2]);\n"
314 "}\n"
315 "\n"
316 "float4 vec4(float4 xyzw)\n" // FIXME: Prevent name clashes
317 "{\n"
318 " return xyzw;\n"
319 "}\n"
320 "\n"
321 "bool xor(bool p, bool q)\n" // FIXME: Prevent name clashes
322 "{\n"
323 " return (p || q) && !(p && q);\n"
324 "}\n"
325 "\n"
326 "float mod(float x, float y)\n" // FIXME: Prevent name clashes
327 "{\n"
328 " return x - y * floor(x / y);\n"
329 "}\n"
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000330 "\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000331 "float2 mod(float2 x, float y)\n" // FIXME: Prevent name clashes
332 "{\n"
333 " return x - y * floor(x / y);\n"
334 "}\n"
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000335 "\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000336 "float3 mod(float3 x, float y)\n" // FIXME: Prevent name clashes
337 "{\n"
338 " return x - y * floor(x / y);\n"
339 "}\n"
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000340 "\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000341 "float4 mod(float4 x, float y)\n" // FIXME: Prevent name clashes
342 "{\n"
343 " return x - y * floor(x / y);\n"
344 "}\n"
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000345 "\n"
346 "float faceforward(float N, float I, float Nref)\n" // FIXME: Prevent name clashes
347 "{\n"
348 " if(dot(Nref, I) < 0)\n"
349 " {\n"
350 " return N;\n"
351 " }\n"
352 " else\n"
353 " {\n"
354 " return -N;\n"
355 " }\n"
356 "}\n"
357 "\n"
358 "float2 faceforward(float2 N, float2 I, float2 Nref)\n" // FIXME: Prevent name clashes
359 "{\n"
360 " if(dot(Nref, I) < 0)\n"
361 " {\n"
362 " return N;\n"
363 " }\n"
364 " else\n"
365 " {\n"
366 " return -N;\n"
367 " }\n"
368 "}\n"
369 "\n"
370 "float3 faceforward(float3 N, float3 I, float3 Nref)\n" // FIXME: Prevent name clashes
371 "{\n"
372 " if(dot(Nref, I) < 0)\n"
373 " {\n"
374 " return N;\n"
375 " }\n"
376 " else\n"
377 " {\n"
378 " return -N;\n"
379 " }\n"
380 "}\n"
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000381 "\n"
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000382 "float4 faceforward(float4 N, float4 I, float4 Nref)\n" // FIXME: Prevent name clashes
383 "{\n"
384 " if(dot(Nref, I) < 0)\n"
385 " {\n"
386 " return N;\n"
387 " }\n"
388 " else\n"
389 " {\n"
390 " return -N;\n"
391 " }\n"
392 "}\n"
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000393 "\n"
394 "bool __equal(float2x2 m, float2x2 n)\n"
395 "{\n"
396 " return m[0][0] == n[0][0] && m[0][1] == n[0][1] &&\n"
397 " m[1][0] == n[1][0] && m[1][1] == n[1][1];\n"
398 "}\n"
399 "\n"
400 "bool __equal(float3x3 m, float3x3 n)\n"
401 "{\n"
402 " return m[0][0] == n[0][0] && m[0][1] == n[0][1] && m[0][2] == n[0][2] &&\n"
403 " m[1][0] == n[1][0] && m[1][1] == n[1][1] && m[1][2] == n[1][2] &&\n"
404 " m[2][0] == n[2][0] && m[2][1] == n[2][1] && m[2][2] == n[2][2];\n"
405 "}\n"
406 "\n"
407 "bool __equal(float4x4 m, float4x4 n)\n"
408 "{\n"
409 " 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"
410 " 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"
411 " 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"
412 " 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"
413 "}\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000414 "\n";
415}
416
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000417void OutputHLSL::footer()
418{
419 EShLanguage language = context.language;
420 TInfoSinkBase &out = context.infoSink.obj;
421 TSymbolTableLevel *symbols = context.symbolTable.getGlobalLevel();
422
423 if (language == EShLangFragment)
424 {
425 out << "PS_OUTPUT main(PS_INPUT input)\n" // FIXME: Prevent name clashes
426 "{\n"
427 " float rhw = 1.0 / input.gl_FragCoord.w;\n"
428 " gl_FragCoord.x = (input.gl_FragCoord.x * rhw) * gl_Window.x + gl_Window.z;\n"
429 " gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * gl_Window.y + gl_Window.w;\n"
430 " gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * gl_Depth.x + gl_Depth.y;\n"
431 " gl_FragCoord.w = rhw;\n"
432 " gl_FrontFacing = __frontCCW ? (input.__vFace >= 0.0) : (input.__vFace <= 0.0);\n";
433
434 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
435 {
436 const TSymbol *symbol = (*namedSymbol).second;
437 const TString &name = symbol->getName();
438
439 if (symbol->isVariable())
440 {
441 const TVariable *variable = static_cast<const TVariable*>(symbol);
442 const TType &type = variable->getType();
443 TQualifier qualifier = type.getQualifier();
444
445 if (qualifier == EvqVaryingIn)
446 {
447 out << " " + name + " = input." + name + ";\n"; // FIXME: Prevent name clashes
448 }
449 }
450 }
451
452 out << "\n"
453 " gl_main();\n"
454 "\n"
455 " PS_OUTPUT output;\n" // FIXME: Prevent name clashes
456 " output.gl_Color[0] = gl_Color[0];\n"; // FIXME: Prevent name clashes
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000457 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000458 else // Vertex shader
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000459 {
460 out << "VS_OUTPUT main(VS_INPUT input)\n" // FIXME: Prevent name clashes
461 "{\n";
462
463 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
464 {
465 const TSymbol *symbol = (*namedSymbol).second;
466 const TString &name = symbol->getName();
467
468 if (symbol->isVariable())
469 {
470 const TVariable *variable = static_cast<const TVariable*>(symbol);
471 const TType &type = variable->getType();
472 TQualifier qualifier = type.getQualifier();
473
474 if (qualifier == EvqAttribute)
475 {
476 out << " " + name + " = input." + name + ";\n"; // FIXME: Prevent name clashes
477 }
478 }
479 }
480
481 out << "\n"
482 " gl_main();\n"
483 "\n"
484 " VS_OUTPUT output;\n" // FIXME: Prevent name clashes
485 " output.gl_Position.x = gl_Position.x - gl_HalfPixelSize.x * gl_Position.w;\n"
486 " output.gl_Position.y = -(gl_Position.y - gl_HalfPixelSize.y * gl_Position.w);\n"
487 " output.gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5;\n"
488 " output.gl_Position.w = gl_Position.w;\n"
489 " output.gl_PointSize = gl_PointSize;\n"
490 " output.gl_FragCoord = gl_Position;\n";
491
492 TSymbolTableLevel *symbols = context.symbolTable.getGlobalLevel();
493
494 for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
495 {
496 const TSymbol *symbol = (*namedSymbol).second;
497 const TString &name = symbol->getName();
498
499 if (symbol->isVariable())
500 {
501 const TVariable *variable = static_cast<const TVariable*>(symbol);
502 TQualifier qualifier = variable->getType().getQualifier();
503
504 if (qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut)
505 {
daniel@transgaming.com0e3358a2010-04-05 20:32:42 +0000506 // Program linking depends on this exact format
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000507 out << " output." + name + " = " + name + ";\n"; // FIXME: Prevent name clashes
508 }
509 }
510 }
511 }
512
513 out << " return output;\n" // FIXME: Prevent name clashes
514 "}\n";
515}
516
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000517void OutputHLSL::visitSymbol(TIntermSymbol *node)
518{
519 TInfoSinkBase &out = context.infoSink.obj;
520
521 TString name = node->getSymbol();
522
523 if (name == "gl_FragColor")
524 {
525 out << "gl_Color[0]";
526 }
527 else if (name == "gl_FragData")
528 {
529 out << "gl_Color";
530 }
531 else
532 {
533 out << name;
534 }
535}
536
537bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
538{
539 TInfoSinkBase &out = context.infoSink.obj;
540
541 switch (node->getOp())
542 {
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000543 case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
544 case EOpInitialize: outputTriplet(visit, NULL, " = ", NULL); break;
545 case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
546 case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
547 case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
548 case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
549 case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break;
550 case EOpVectorTimesMatrixAssign:
551 case EOpMatrixTimesMatrixAssign:
552 if (visit == PreVisit)
553 {
554 out << "(";
555 }
556 else if (visit == InVisit)
557 {
558 out << " = mul(";
daniel@transgaming.com279e38a2010-04-03 20:56:13 +0000559
560 if (node->getOp() == EOpMatrixTimesMatrixAssign)
561 {
562 out << "transpose(";
563 }
564
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000565 node->getLeft()->traverse(this);
daniel@transgaming.com279e38a2010-04-03 20:56:13 +0000566
567 if (node->getOp() == EOpMatrixTimesMatrixAssign)
568 {
569 out << ")";
570 }
571
572 out << ", transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000573 }
574 else
575 {
daniel@transgaming.com279e38a2010-04-03 20:56:13 +0000576 out << ")))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000577 }
578 break;
579 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
580 case EOpIndexDirect: outputTriplet(visit, NULL, "[", "]"); break;
581 case EOpIndexIndirect: outputTriplet(visit, NULL, "[", "]"); break;
582 case EOpIndexDirectStruct: outputTriplet(visit, NULL, ".", NULL); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000583 case EOpVectorSwizzle:
584 if (visit == InVisit)
585 {
586 out << ".";
587
588 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
589
590 if (swizzle)
591 {
592 TIntermSequence &sequence = swizzle->getSequence();
593
594 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
595 {
596 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
597
598 if (element)
599 {
600 int i = element->getUnionArrayPointer()[0].getIConst();
601
602 switch (i)
603 {
604 case 0: out << "x"; break;
605 case 1: out << "y"; break;
606 case 2: out << "z"; break;
607 case 3: out << "w"; break;
608 default: UNREACHABLE();
609 }
610 }
611 else UNREACHABLE();
612 }
613 }
614 else UNREACHABLE();
615
616 return false; // Fully processed
617 }
618 break;
619 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
620 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
621 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
622 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000623 case EOpEqual:
624 if (!node->getLeft()->isMatrix())
625 {
626 outputTriplet(visit, "(", " == ", ")");
627 }
628 else
629 {
630 outputTriplet(visit, "__equal(", ", ", ")");
631 }
632 break;
633 case EOpNotEqual:
634 if (!node->getLeft()->isMatrix())
635 {
636 outputTriplet(visit, "(", " != ", ")");
637 }
638 else
639 {
640 outputTriplet(visit, "!__equal(", ", ", ")");
641 }
642 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000643 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
644 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
645 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
646 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
647 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000648 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com279e38a2010-04-03 20:56:13 +0000649 case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break;
650 case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break;
651 case EOpMatrixTimesMatrix: outputTriplet(visit, "mul(transpose(", "), transpose(", "))"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000652 case EOpLogicalOr: outputTriplet(visit, "(", " || ", ")"); break;
653 case EOpLogicalXor: outputTriplet(visit, "xor(", ", ", ")"); break; // FIXME: Prevent name clashes
654 case EOpLogicalAnd: outputTriplet(visit, "(", " && ", ")"); break;
655 default: UNREACHABLE();
656 }
657
658 return true;
659}
660
661bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
662{
663 TInfoSinkBase &out = context.infoSink.obj;
664
665 switch (node->getOp())
666 {
667 case EOpNegative: outputTriplet(visit, "(-", NULL, ")"); break;
668 case EOpVectorLogicalNot: outputTriplet(visit, "(!", NULL, ")"); break;
669 case EOpLogicalNot: outputTriplet(visit, "(!", NULL, ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000670 case EOpPostIncrement: outputTriplet(visit, "(", NULL, "++)"); break;
671 case EOpPostDecrement: outputTriplet(visit, "(", NULL, "--)"); break;
672 case EOpPreIncrement: outputTriplet(visit, "(++", NULL, ")"); break;
673 case EOpPreDecrement: outputTriplet(visit, "(--", NULL, ")"); break;
674 case EOpConvIntToBool:
675 case EOpConvFloatToBool:
676 switch (node->getOperand()->getType().getNominalSize())
677 {
678 case 1: outputTriplet(visit, "bool(", NULL, ")"); break;
679 case 2: outputTriplet(visit, "bool2(", NULL, ")"); break;
680 case 3: outputTriplet(visit, "bool3(", NULL, ")"); break;
681 case 4: outputTriplet(visit, "bool4(", NULL, ")"); break;
682 default: UNREACHABLE();
683 }
684 break;
685 case EOpConvBoolToFloat:
686 case EOpConvIntToFloat:
687 switch (node->getOperand()->getType().getNominalSize())
688 {
689 case 1: outputTriplet(visit, "float(", NULL, ")"); break;
690 case 2: outputTriplet(visit, "float2(", NULL, ")"); break;
691 case 3: outputTriplet(visit, "float3(", NULL, ")"); break;
692 case 4: outputTriplet(visit, "float4(", NULL, ")"); break;
693 default: UNREACHABLE();
694 }
695 break;
696 case EOpConvFloatToInt:
697 case EOpConvBoolToInt:
698 switch (node->getOperand()->getType().getNominalSize())
699 {
700 case 1: outputTriplet(visit, "int(", NULL, ")"); break;
701 case 2: outputTriplet(visit, "int2(", NULL, ")"); break;
702 case 3: outputTriplet(visit, "int3(", NULL, ")"); break;
703 case 4: outputTriplet(visit, "int4(", NULL, ")"); break;
704 default: UNREACHABLE();
705 }
706 break;
707 case EOpRadians: outputTriplet(visit, "radians(", NULL, ")"); break;
708 case EOpDegrees: outputTriplet(visit, "degrees(", NULL, ")"); break;
709 case EOpSin: outputTriplet(visit, "sin(", NULL, ")"); break;
710 case EOpCos: outputTriplet(visit, "cos(", NULL, ")"); break;
711 case EOpTan: outputTriplet(visit, "tan(", NULL, ")"); break;
712 case EOpAsin: outputTriplet(visit, "asin(", NULL, ")"); break;
713 case EOpAcos: outputTriplet(visit, "acos(", NULL, ")"); break;
714 case EOpAtan: outputTriplet(visit, "atan(", NULL, ")"); break;
715 case EOpExp: outputTriplet(visit, "exp(", NULL, ")"); break;
716 case EOpLog: outputTriplet(visit, "log(", NULL, ")"); break;
717 case EOpExp2: outputTriplet(visit, "exp2(", NULL, ")"); break;
718 case EOpLog2: outputTriplet(visit, "log2(", NULL, ")"); break;
719 case EOpSqrt: outputTriplet(visit, "sqrt(", NULL, ")"); break;
720 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", NULL, ")"); break;
721 case EOpAbs: outputTriplet(visit, "abs(", NULL, ")"); break;
722 case EOpSign: outputTriplet(visit, "sign(", NULL, ")"); break;
723 case EOpFloor: outputTriplet(visit, "floor(", NULL, ")"); break;
724 case EOpCeil: outputTriplet(visit, "ceil(", NULL, ")"); break;
725 case EOpFract: outputTriplet(visit, "frac(", NULL, ")"); break;
726 case EOpLength: outputTriplet(visit, "length(", NULL, ")"); break;
727 case EOpNormalize: outputTriplet(visit, "normalize(", NULL, ")"); break;
daniel@transgaming.com45d03582010-03-11 19:41:29 +0000728// case EOpDPdx: outputTriplet(visit, "ddx(", NULL, ")"); break;
729// case EOpDPdy: outputTriplet(visit, "ddy(", NULL, ")"); break;
730// case EOpFwidth: outputTriplet(visit, "fwidth(", NULL, ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000731 case EOpAny: outputTriplet(visit, "any(", NULL, ")"); break;
732 case EOpAll: outputTriplet(visit, "all(", NULL, ")"); break;
733 default: UNREACHABLE();
734 }
735
736 return true;
737}
738
739bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
740{
741 EShLanguage language = context.language;
742 TInfoSinkBase &out = context.infoSink.obj;
743
744 if (node->getOp() == EOpNull)
745 {
746 out.message(EPrefixError, "node is still EOpNull!");
747 return true;
748 }
749
750 switch (node->getOp())
751 {
752 case EOpSequence: outputTriplet(visit, NULL, ";\n", ";\n"); break;
753 case EOpDeclaration:
754 if (visit == PreVisit)
755 {
756 TIntermSequence &sequence = node->getSequence();
757 TIntermTyped *variable = sequence[0]->getAsTyped();
758 bool visit = true;
759
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000760 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000761 {
762 out << typeString(variable->getType()) + " ";
763
764 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
765 {
766 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
767
768 if (symbol)
769 {
770 symbol->traverse(this);
771
772 out << arrayString(symbol->getType());
773 }
774 else
775 {
776 (*sit)->traverse(this);
777 }
778
779 if (visit && this->inVisit)
780 {
781 if (*sit != sequence.back())
782 {
783 visit = this->visitAggregate(InVisit, node);
784 }
785 }
786 }
787
788 if (visit && this->postVisit)
789 {
790 this->visitAggregate(PostVisit, node);
791 }
792 }
793
794 return false;
795 }
796 else if (visit == InVisit)
797 {
798 out << ", ";
799 }
800 break;
801 case EOpComma: UNIMPLEMENTED(); /* FIXME */ out << "Comma\n"; return true;
802 case EOpFunction:
803 {
alokp@chromium.org43884872010-03-30 00:08:52 +0000804 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000805
806 if (visit == PreVisit)
807 {
808 if (name == "main")
809 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000810 name = "gl_main";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000811 }
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000812
813 out << typeString(node->getType()) << " " << name << "(";
814
815 TIntermSequence &sequence = node->getSequence();
816 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
817
818 for (unsigned int i = 0; i < arguments.size(); i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000819 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000820 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000821
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000822 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000823 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000824 const TType &type = symbol->getType();
825 const TString &name = symbol->getSymbol();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000826
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000827 out << typeString(type) + " " + name;
828
829 if (i < arguments.size() - 1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000830 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000831 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000832 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000833 }
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000834 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000835 }
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000836
837 sequence.erase(sequence.begin());
838
839 out << ")\n"
840 "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000841 }
842 else if (visit == PostVisit)
843 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000844 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000845 }
846 }
847 break;
848 case EOpFunctionCall:
849 {
850 if (visit == PreVisit)
851 {
alokp@chromium.org43884872010-03-30 00:08:52 +0000852 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853
854 if (node->isUserDefined())
855 {
856 out << name << "(";
857 }
858 else
859 {
860 if (name == "texture2D")
861 {
862 if (node->getSequence().size() == 2)
863 {
864 out << "gl_texture2D(";
865 }
866 else if (node->getSequence().size() == 3)
867 {
868 out << "gl_texture2DBias(";
869 }
870 else UNREACHABLE();
871 }
872 else if (name == "texture2DProj")
873 {
874 out << "gl_texture2DProj(";
875 }
876 else if (name == "texture2DLod")
877 {
878 out << "gl_texture2DLod(";
879 UNIMPLEMENTED(); // FIXME: Move lod to last texture coordinate component
880 }
881 else if (name == "texture2DProjLod")
882 {
883 out << "gl_texture2DProjLod(";
884 UNIMPLEMENTED(); // FIXME: Move lod to last texture coordinate component
885 }
886 else if (name == "textureCube")
887 {
888 out << "gl_textureCube("; // FIXME: Incorrect sampling location
889 }
890 else
891 {
892 UNIMPLEMENTED(); // FIXME
893 }
894 }
895 }
896 else if (visit == InVisit)
897 {
898 out << ", ";
899 }
900 else
901 {
902 out << ")";
903 }
904 }
905 break;
906 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
907 case EOpConstructFloat: outputTriplet(visit, "vec1(", NULL, ")"); break;
908 case EOpConstructVec2: outputTriplet(visit, "vec2(", ", ", ")"); break;
909 case EOpConstructVec3: outputTriplet(visit, "vec3(", ", ", ")"); break;
910 case EOpConstructVec4: outputTriplet(visit, "vec4(", ", ", ")"); break;
911 case EOpConstructBool: UNIMPLEMENTED(); /* FIXME */ out << "Construct bool"; break;
912 case EOpConstructBVec2: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec2"; break;
913 case EOpConstructBVec3: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec3"; break;
914 case EOpConstructBVec4: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec4"; break;
915 case EOpConstructInt: UNIMPLEMENTED(); /* FIXME */ out << "Construct int"; break;
916 case EOpConstructIVec2: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec2"; break;
917 case EOpConstructIVec3: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec3"; break;
918 case EOpConstructIVec4: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec4"; break;
919 case EOpConstructMat2: outputTriplet(visit, "float2x2(", ", ", ")"); break;
920 case EOpConstructMat3: outputTriplet(visit, "float3x3(", ", ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000921 case EOpConstructMat4: outputTriplet(visit, "float4x4(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000922 case EOpConstructStruct: UNIMPLEMENTED(); /* FIXME */ out << "Construct structure"; break;
923 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
924 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
925 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
926 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
927 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
928 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
929 case EOpMod: outputTriplet(visit, "mod(", ", ", ")"); break; // FIXME: Prevent name clashes
930 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
931 case EOpAtan:
932 if (node->getSequence().size() == 1)
933 {
934 outputTriplet(visit, "atan(", ", ", ")");
935 }
936 else if (node->getSequence().size() == 2)
937 {
938 outputTriplet(visit, "atan2(", ", ", ")");
939 }
940 else UNREACHABLE();
941 break;
942 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
943 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
944 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
945 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
946 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
947 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
948 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
949 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
950 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com680553b2010-03-08 21:30:52 +0000951 case EOpFaceForward: outputTriplet(visit, "faceforward(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
953 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
954 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000955 default: UNREACHABLE();
956 }
957
958 return true;
959}
960
961bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
962{
963 TInfoSinkBase &out = context.infoSink.obj;
964
alokp@chromium.org60fe4072010-03-29 20:58:29 +0000965 if (node->usesTernaryOperator())
966 {
967 out << "(";
968 node->getCondition()->traverse(this);
969 out << ") ? (";
970 node->getTrueBlock()->traverse(this);
971 out << ") : (";
972 node->getFalseBlock()->traverse(this);
973 out << ")\n";
974 }
975 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000976 {
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +0000977 out << "if(";
978
979 node->getCondition()->traverse(this);
980
981 out << ")\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000982 "{\n";
983
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +0000984 node->getTrueBlock()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000985
986 out << ";}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +0000987
988 if (node->getFalseBlock())
989 {
990 out << "else\n"
991 "{\n";
992
993 node->getFalseBlock()->traverse(this);
994
995 out << ";}\n";
996 }
997 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000998
999 return false;
1000}
1001
1002void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
1003{
1004 TInfoSinkBase &out = context.infoSink.obj;
1005
alokp@chromium.orgdd037b22010-03-30 18:47:20 +00001006 const TType &type = node->getType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001007
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001008 if (type.isField())
1009 {
1010 out << type.getFieldName();
1011 }
1012 else
1013 {
1014 int size = type.getObjectSize();
1015 bool matrix = type.isMatrix();
1016 TBasicType basicType = node->getUnionArrayPointer()[0].getType();
1017
1018 switch (basicType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001019 {
1020 case EbtBool:
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001021 if (!matrix)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001022 {
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001023 switch (size)
1024 {
1025 case 1: out << "bool("; break;
1026 case 2: out << "bool2("; break;
1027 case 3: out << "bool3("; break;
1028 case 4: out << "bool4("; break;
1029 default: UNREACHABLE();
1030 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001031 }
1032 else
1033 {
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001034 UNIMPLEMENTED();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001035 }
1036 break;
1037 case EbtFloat:
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001038 if (!matrix)
1039 {
1040 switch (size)
1041 {
1042 case 1: out << "float("; break;
1043 case 2: out << "float2("; break;
1044 case 3: out << "float3("; break;
1045 case 4: out << "float4("; break;
1046 default: UNREACHABLE();
1047 }
1048 }
1049 else
1050 {
1051 switch (size)
1052 {
1053 case 4: out << "float2x2("; break;
1054 case 9: out << "float3x3("; break;
1055 case 16: out << "float4x4("; break;
1056 default: UNREACHABLE();
1057 }
1058 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001059 break;
1060 case EbtInt:
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001061 if (!matrix)
1062 {
1063 switch (size)
1064 {
1065 case 1: out << "int("; break;
1066 case 2: out << "int2("; break;
1067 case 3: out << "int3("; break;
1068 case 4: out << "int4("; break;
1069 default: UNREACHABLE();
1070 }
1071 }
1072 else
1073 {
1074 UNIMPLEMENTED();
1075 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001076 break;
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001077 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001078 UNIMPLEMENTED(); // FIXME
1079 }
1080
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001081 for (int i = 0; i < size; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001082 {
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001083 switch (basicType)
1084 {
1085 case EbtBool:
1086 if (node->getUnionArrayPointer()[i].getBConst())
1087 {
1088 out << "true";
1089 }
1090 else
1091 {
1092 out << "false";
1093 }
1094 break;
1095 case EbtFloat:
1096 out << node->getUnionArrayPointer()[i].getFConst();
1097 break;
1098 case EbtInt:
1099 out << node->getUnionArrayPointer()[i].getIConst();
1100 break;
1101 default:
1102 UNIMPLEMENTED(); // FIXME
1103 }
1104
1105 if (i != size - 1)
1106 {
1107 out << ", ";
1108 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001109 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001110
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001111 out << ")";
1112 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001113}
1114
1115bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
1116{
1117 TInfoSinkBase &out = context.infoSink.obj;
1118
1119 if (!node->testFirst())
1120 {
1121 out << "do\n"
1122 "{\n";
1123 }
1124 else
1125 {
1126 out << "for(";
1127
1128 if (node->getInit())
1129 {
1130 node->getInit()->traverse(this);
1131 }
1132
1133 out << "; ";
1134
1135 if (node->getTest())
1136 {
1137 node->getTest()->traverse(this);
1138 }
1139
1140 out << "; ";
1141
1142 if (node->getTerminal())
1143 {
1144 node->getTerminal()->traverse(this);
1145 }
1146
1147 out << ")\n"
1148 "{\n";
1149 }
1150
1151 if (node->getBody())
1152 {
1153 node->getBody()->traverse(this);
1154 }
1155
1156 out << "}\n";
1157
1158 if (!node->testFirst())
1159 {
1160 out << "while(\n";
1161
1162 node->getTest()->traverse(this);
1163
1164 out << ")";
1165 }
1166
1167 out << ";\n";
1168
1169 return false;
1170}
1171
1172bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
1173{
1174 TInfoSinkBase &out = context.infoSink.obj;
1175
1176 switch (node->getFlowOp())
1177 {
daniel@transgaming.comf67f82e2010-03-17 03:58:54 +00001178 case EOpKill: outputTriplet(visit, "discard", NULL, NULL); break;
1179 case EOpBreak: outputTriplet(visit, "break", NULL, NULL); break;
1180 case EOpContinue: outputTriplet(visit, "continue", NULL, NULL); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001181 case EOpReturn:
1182 if (visit == PreVisit)
1183 {
1184 if (node->getExpression())
1185 {
1186 out << "return ";
1187 }
1188 else
1189 {
1190 out << "return;\n";
1191 }
1192 }
1193 else if (visit == PostVisit)
1194 {
1195 out << ";\n";
1196 }
1197 break;
1198 default: UNREACHABLE();
1199 }
1200
1201 return true;
1202}
1203
1204void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
1205{
1206 TInfoSinkBase &out = context.infoSink.obj;
1207
1208 if (visit == PreVisit && preString)
1209 {
1210 out << preString;
1211 }
1212 else if (visit == InVisit && inString)
1213 {
1214 out << inString;
1215 }
1216 else if (visit == PostVisit && postString)
1217 {
1218 out << postString;
1219 }
1220}
1221
1222TString OutputHLSL::typeString(const TType &type)
1223{
1224 if (type.isMatrix())
1225 {
1226 switch (type.getNominalSize())
1227 {
1228 case 2: return "float2x2";
1229 case 3: return "float3x3";
1230 case 4: return "float4x4";
1231 }
1232 }
1233 else
1234 {
1235 switch (type.getBasicType())
1236 {
1237 case EbtFloat:
1238 switch (type.getNominalSize())
1239 {
1240 case 1: return "float";
1241 case 2: return "float2";
1242 case 3: return "float3";
1243 case 4: return "float4";
1244 }
1245 case EbtInt:
1246 switch (type.getNominalSize())
1247 {
1248 case 1: return "int";
1249 case 2: return "int2";
1250 case 3: return "int3";
1251 case 4: return "int4";
1252 }
1253 case EbtBool:
1254 switch (type.getNominalSize())
1255 {
1256 case 1: return "bool";
1257 case 2: return "bool2";
1258 case 3: return "bool3";
1259 case 4: return "bool4";
1260 }
1261 case EbtVoid:
1262 return "void";
1263 case EbtSampler2D:
1264 return "sampler2D";
1265 case EbtSamplerCube:
1266 return "samplerCUBE";
1267 }
1268 }
1269
1270 UNIMPLEMENTED(); // FIXME
1271 return "<unknown type>";
1272}
1273
1274TString OutputHLSL::arrayString(const TType &type)
1275{
1276 if (!type.isArray())
1277 {
1278 return "";
1279 }
1280
1281 char buffer[100];
1282 sprintf(buffer, "[%d]", type.getArraySize());
1283
1284 return buffer;
1285}
1286
1287TString OutputHLSL::initializer(const TType &type)
1288{
1289 TString string;
1290
1291 int arraySize = type.isArray() ? type.getArraySize() : 1;
1292
1293 if (type.isArray())
1294 {
1295 string += "{";
1296 }
1297
1298 for (int element = 0; element < arraySize; element++)
1299 {
1300 string += typeString(type) + "(";
1301
1302 for (int component = 0; component < type.getNominalSize(); component++)
1303 {
1304 string += "0";
1305
1306 if (component < type.getNominalSize() - 1)
1307 {
1308 string += ", ";
1309 }
1310 }
1311
1312 string += ")";
1313
1314 if (element < arraySize - 1)
1315 {
1316 string += ", ";
1317 }
1318 }
1319
1320 if (type.isArray())
1321 {
1322 string += "}";
1323 }
1324
1325 return string;
1326}
1327}