blob: 94a9341b428283516513b6ad735ce7f7c383f84c [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
daniel@transgaming.com5441d662010-04-07 03:25:11 +0000560 if (node->getLeft()->getQualifier() == EvqUniform)
daniel@transgaming.com279e38a2010-04-03 20:56:13 +0000561 {
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
daniel@transgaming.com5441d662010-04-07 03:25:11 +0000567 if (node->getLeft()->getQualifier() == EvqUniform)
daniel@transgaming.com279e38a2010-04-03 20:56:13 +0000568 {
569 out << ")";
570 }
571
daniel@transgaming.com5441d662010-04-07 03:25:11 +0000572 out << ", ";
573
574 if (node->getRight()->getQualifier() == EvqUniform)
575 {
576 out << "transpose(";
577 }
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000578 }
579 else
580 {
daniel@transgaming.com5441d662010-04-07 03:25:11 +0000581 if (node->getRight()->getQualifier() == EvqUniform)
582 {
583 out << ")";
584 }
585
586 out << "))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000587 }
588 break;
589 case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break;
590 case EOpIndexDirect: outputTriplet(visit, NULL, "[", "]"); break;
591 case EOpIndexIndirect: outputTriplet(visit, NULL, "[", "]"); break;
592 case EOpIndexDirectStruct: outputTriplet(visit, NULL, ".", NULL); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000593 case EOpVectorSwizzle:
594 if (visit == InVisit)
595 {
596 out << ".";
597
598 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
599
600 if (swizzle)
601 {
602 TIntermSequence &sequence = swizzle->getSequence();
603
604 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
605 {
606 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
607
608 if (element)
609 {
610 int i = element->getUnionArrayPointer()[0].getIConst();
611
612 switch (i)
613 {
614 case 0: out << "x"; break;
615 case 1: out << "y"; break;
616 case 2: out << "z"; break;
617 case 3: out << "w"; break;
618 default: UNREACHABLE();
619 }
620 }
621 else UNREACHABLE();
622 }
623 }
624 else UNREACHABLE();
625
626 return false; // Fully processed
627 }
628 break;
629 case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break;
630 case EOpSub: outputTriplet(visit, "(", " - ", ")"); break;
631 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
632 case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000633 case EOpEqual:
634 if (!node->getLeft()->isMatrix())
635 {
636 outputTriplet(visit, "(", " == ", ")");
637 }
638 else
639 {
640 outputTriplet(visit, "__equal(", ", ", ")");
641 }
642 break;
643 case EOpNotEqual:
644 if (!node->getLeft()->isMatrix())
645 {
646 outputTriplet(visit, "(", " != ", ")");
647 }
648 else
649 {
650 outputTriplet(visit, "!__equal(", ", ", ")");
651 }
652 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000653 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
654 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
655 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
656 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
657 case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000658 case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com5441d662010-04-07 03:25:11 +0000659 case EOpVectorTimesMatrix:
660 if (node->getRight()->getQualifier() == EvqUniform)
661 {
662 outputTriplet(visit, "mul(", ", transpose(", "))");
663 }
664 else
665 {
666 outputTriplet(visit, "mul(", ", ", ")");
667 }
668 break;
669 case EOpMatrixTimesVector:
670 if (node->getLeft()->getQualifier() == EvqUniform)
671 {
672 outputTriplet(visit, "mul(transpose(", "), ", ")");
673 }
674 else
675 {
676 outputTriplet(visit, "mul(", ", ", ")");
677 }
678 break;
679 case EOpMatrixTimesMatrix:
680 if (node->getLeft()->getQualifier() == EvqUniform)
681 {
682 if (node->getRight()->getQualifier() == EvqUniform)
683 {
684 outputTriplet(visit, "mul(transpose(", "), transpose(", "))");
685 }
686 else
687 {
688 outputTriplet(visit, "mul(transpose(", "), ", ")");
689 }
690 }
691 else
692 {
693 if (node->getRight()->getQualifier() == EvqUniform)
694 {
695 outputTriplet(visit, "mul(", ", transpose(", "))");
696 }
697 else
698 {
699 outputTriplet(visit, "mul(", ", ", ")");
700 }
701 }
702 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000703 case EOpLogicalOr: outputTriplet(visit, "(", " || ", ")"); break;
704 case EOpLogicalXor: outputTriplet(visit, "xor(", ", ", ")"); break; // FIXME: Prevent name clashes
705 case EOpLogicalAnd: outputTriplet(visit, "(", " && ", ")"); break;
706 default: UNREACHABLE();
707 }
708
709 return true;
710}
711
712bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
713{
714 TInfoSinkBase &out = context.infoSink.obj;
715
716 switch (node->getOp())
717 {
718 case EOpNegative: outputTriplet(visit, "(-", NULL, ")"); break;
719 case EOpVectorLogicalNot: outputTriplet(visit, "(!", NULL, ")"); break;
720 case EOpLogicalNot: outputTriplet(visit, "(!", NULL, ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000721 case EOpPostIncrement: outputTriplet(visit, "(", NULL, "++)"); break;
722 case EOpPostDecrement: outputTriplet(visit, "(", NULL, "--)"); break;
723 case EOpPreIncrement: outputTriplet(visit, "(++", NULL, ")"); break;
724 case EOpPreDecrement: outputTriplet(visit, "(--", NULL, ")"); break;
725 case EOpConvIntToBool:
726 case EOpConvFloatToBool:
727 switch (node->getOperand()->getType().getNominalSize())
728 {
729 case 1: outputTriplet(visit, "bool(", NULL, ")"); break;
730 case 2: outputTriplet(visit, "bool2(", NULL, ")"); break;
731 case 3: outputTriplet(visit, "bool3(", NULL, ")"); break;
732 case 4: outputTriplet(visit, "bool4(", NULL, ")"); break;
733 default: UNREACHABLE();
734 }
735 break;
736 case EOpConvBoolToFloat:
737 case EOpConvIntToFloat:
738 switch (node->getOperand()->getType().getNominalSize())
739 {
740 case 1: outputTriplet(visit, "float(", NULL, ")"); break;
741 case 2: outputTriplet(visit, "float2(", NULL, ")"); break;
742 case 3: outputTriplet(visit, "float3(", NULL, ")"); break;
743 case 4: outputTriplet(visit, "float4(", NULL, ")"); break;
744 default: UNREACHABLE();
745 }
746 break;
747 case EOpConvFloatToInt:
748 case EOpConvBoolToInt:
749 switch (node->getOperand()->getType().getNominalSize())
750 {
751 case 1: outputTriplet(visit, "int(", NULL, ")"); break;
752 case 2: outputTriplet(visit, "int2(", NULL, ")"); break;
753 case 3: outputTriplet(visit, "int3(", NULL, ")"); break;
754 case 4: outputTriplet(visit, "int4(", NULL, ")"); break;
755 default: UNREACHABLE();
756 }
757 break;
758 case EOpRadians: outputTriplet(visit, "radians(", NULL, ")"); break;
759 case EOpDegrees: outputTriplet(visit, "degrees(", NULL, ")"); break;
760 case EOpSin: outputTriplet(visit, "sin(", NULL, ")"); break;
761 case EOpCos: outputTriplet(visit, "cos(", NULL, ")"); break;
762 case EOpTan: outputTriplet(visit, "tan(", NULL, ")"); break;
763 case EOpAsin: outputTriplet(visit, "asin(", NULL, ")"); break;
764 case EOpAcos: outputTriplet(visit, "acos(", NULL, ")"); break;
765 case EOpAtan: outputTriplet(visit, "atan(", NULL, ")"); break;
766 case EOpExp: outputTriplet(visit, "exp(", NULL, ")"); break;
767 case EOpLog: outputTriplet(visit, "log(", NULL, ")"); break;
768 case EOpExp2: outputTriplet(visit, "exp2(", NULL, ")"); break;
769 case EOpLog2: outputTriplet(visit, "log2(", NULL, ")"); break;
770 case EOpSqrt: outputTriplet(visit, "sqrt(", NULL, ")"); break;
771 case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", NULL, ")"); break;
772 case EOpAbs: outputTriplet(visit, "abs(", NULL, ")"); break;
773 case EOpSign: outputTriplet(visit, "sign(", NULL, ")"); break;
774 case EOpFloor: outputTriplet(visit, "floor(", NULL, ")"); break;
775 case EOpCeil: outputTriplet(visit, "ceil(", NULL, ")"); break;
776 case EOpFract: outputTriplet(visit, "frac(", NULL, ")"); break;
777 case EOpLength: outputTriplet(visit, "length(", NULL, ")"); break;
778 case EOpNormalize: outputTriplet(visit, "normalize(", NULL, ")"); break;
daniel@transgaming.com45d03582010-03-11 19:41:29 +0000779// case EOpDPdx: outputTriplet(visit, "ddx(", NULL, ")"); break;
780// case EOpDPdy: outputTriplet(visit, "ddy(", NULL, ")"); break;
781// case EOpFwidth: outputTriplet(visit, "fwidth(", NULL, ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000782 case EOpAny: outputTriplet(visit, "any(", NULL, ")"); break;
783 case EOpAll: outputTriplet(visit, "all(", NULL, ")"); break;
784 default: UNREACHABLE();
785 }
786
787 return true;
788}
789
790bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
791{
792 EShLanguage language = context.language;
793 TInfoSinkBase &out = context.infoSink.obj;
794
795 if (node->getOp() == EOpNull)
796 {
797 out.message(EPrefixError, "node is still EOpNull!");
798 return true;
799 }
800
801 switch (node->getOp())
802 {
803 case EOpSequence: outputTriplet(visit, NULL, ";\n", ";\n"); break;
804 case EOpDeclaration:
805 if (visit == PreVisit)
806 {
807 TIntermSequence &sequence = node->getSequence();
808 TIntermTyped *variable = sequence[0]->getAsTyped();
809 bool visit = true;
810
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000811 if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000812 {
813 out << typeString(variable->getType()) + " ";
814
815 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
816 {
817 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
818
819 if (symbol)
820 {
821 symbol->traverse(this);
822
823 out << arrayString(symbol->getType());
824 }
825 else
826 {
827 (*sit)->traverse(this);
828 }
829
830 if (visit && this->inVisit)
831 {
832 if (*sit != sequence.back())
833 {
834 visit = this->visitAggregate(InVisit, node);
835 }
836 }
837 }
838
839 if (visit && this->postVisit)
840 {
841 this->visitAggregate(PostVisit, node);
842 }
843 }
844
845 return false;
846 }
847 else if (visit == InVisit)
848 {
849 out << ", ";
850 }
851 break;
852 case EOpComma: UNIMPLEMENTED(); /* FIXME */ out << "Comma\n"; return true;
853 case EOpFunction:
854 {
alokp@chromium.org43884872010-03-30 00:08:52 +0000855 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856
857 if (visit == PreVisit)
858 {
859 if (name == "main")
860 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000861 name = "gl_main";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862 }
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000863
864 out << typeString(node->getType()) << " " << name << "(";
865
866 TIntermSequence &sequence = node->getSequence();
867 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
868
869 for (unsigned int i = 0; i < arguments.size(); i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000871 TIntermSymbol *symbol = arguments[i]->getAsSymbolNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000872
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000873 if (symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000874 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000875 const TType &type = symbol->getType();
876 const TString &name = symbol->getSymbol();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000878 out << typeString(type) + " " + name;
879
880 if (i < arguments.size() - 1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000881 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000882 out << ", ";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000883 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000884 }
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000885 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000886 }
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000887
888 sequence.erase(sequence.begin());
889
890 out << ")\n"
891 "{\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892 }
893 else if (visit == PostVisit)
894 {
daniel@transgaming.come78c0c92010-03-28 19:36:06 +0000895 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000896 }
897 }
898 break;
899 case EOpFunctionCall:
900 {
901 if (visit == PreVisit)
902 {
alokp@chromium.org43884872010-03-30 00:08:52 +0000903 TString name = TFunction::unmangleName(node->getName());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904
905 if (node->isUserDefined())
906 {
907 out << name << "(";
908 }
909 else
910 {
911 if (name == "texture2D")
912 {
913 if (node->getSequence().size() == 2)
914 {
915 out << "gl_texture2D(";
916 }
917 else if (node->getSequence().size() == 3)
918 {
919 out << "gl_texture2DBias(";
920 }
921 else UNREACHABLE();
922 }
923 else if (name == "texture2DProj")
924 {
925 out << "gl_texture2DProj(";
926 }
927 else if (name == "texture2DLod")
928 {
929 out << "gl_texture2DLod(";
930 UNIMPLEMENTED(); // FIXME: Move lod to last texture coordinate component
931 }
932 else if (name == "texture2DProjLod")
933 {
934 out << "gl_texture2DProjLod(";
935 UNIMPLEMENTED(); // FIXME: Move lod to last texture coordinate component
936 }
937 else if (name == "textureCube")
938 {
939 out << "gl_textureCube("; // FIXME: Incorrect sampling location
940 }
941 else
942 {
943 UNIMPLEMENTED(); // FIXME
944 }
945 }
946 }
947 else if (visit == InVisit)
948 {
949 out << ", ";
950 }
951 else
952 {
953 out << ")";
954 }
955 }
956 break;
957 case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break;
958 case EOpConstructFloat: outputTriplet(visit, "vec1(", NULL, ")"); break;
959 case EOpConstructVec2: outputTriplet(visit, "vec2(", ", ", ")"); break;
960 case EOpConstructVec3: outputTriplet(visit, "vec3(", ", ", ")"); break;
961 case EOpConstructVec4: outputTriplet(visit, "vec4(", ", ", ")"); break;
962 case EOpConstructBool: UNIMPLEMENTED(); /* FIXME */ out << "Construct bool"; break;
963 case EOpConstructBVec2: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec2"; break;
964 case EOpConstructBVec3: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec3"; break;
965 case EOpConstructBVec4: UNIMPLEMENTED(); /* FIXME */ out << "Construct bvec4"; break;
966 case EOpConstructInt: UNIMPLEMENTED(); /* FIXME */ out << "Construct int"; break;
967 case EOpConstructIVec2: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec2"; break;
968 case EOpConstructIVec3: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec3"; break;
969 case EOpConstructIVec4: UNIMPLEMENTED(); /* FIXME */ out << "Construct ivec4"; break;
970 case EOpConstructMat2: outputTriplet(visit, "float2x2(", ", ", ")"); break;
971 case EOpConstructMat3: outputTriplet(visit, "float3x3(", ", ", ")"); break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +0000972 case EOpConstructMat4: outputTriplet(visit, "float4x4(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973 case EOpConstructStruct: UNIMPLEMENTED(); /* FIXME */ out << "Construct structure"; break;
974 case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break;
975 case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break;
976 case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break;
977 case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break;
978 case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break;
979 case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break;
980 case EOpMod: outputTriplet(visit, "mod(", ", ", ")"); break; // FIXME: Prevent name clashes
981 case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break;
982 case EOpAtan:
983 if (node->getSequence().size() == 1)
984 {
985 outputTriplet(visit, "atan(", ", ", ")");
986 }
987 else if (node->getSequence().size() == 2)
988 {
989 outputTriplet(visit, "atan2(", ", ", ")");
990 }
991 else UNREACHABLE();
992 break;
993 case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break;
994 case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break;
995 case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break;
996 case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break;
997 case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break;
998 case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break;
999 case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break;
1000 case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break;
1001 case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break;
daniel@transgaming.com680553b2010-03-08 21:30:52 +00001002 case EOpFaceForward: outputTriplet(visit, "faceforward(", ", ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001003 case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break;
1004 case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break;
1005 case EOpMul: outputTriplet(visit, "(", " * ", ")"); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001006 default: UNREACHABLE();
1007 }
1008
1009 return true;
1010}
1011
1012bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
1013{
1014 TInfoSinkBase &out = context.infoSink.obj;
1015
alokp@chromium.org60fe4072010-03-29 20:58:29 +00001016 if (node->usesTernaryOperator())
1017 {
1018 out << "(";
1019 node->getCondition()->traverse(this);
1020 out << ") ? (";
1021 node->getTrueBlock()->traverse(this);
1022 out << ") : (";
1023 node->getFalseBlock()->traverse(this);
1024 out << ")\n";
1025 }
1026 else // if/else statement
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001027 {
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001028 out << "if(";
1029
1030 node->getCondition()->traverse(this);
1031
1032 out << ")\n"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001033 "{\n";
1034
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001035 node->getTrueBlock()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001036
1037 out << ";}\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001038
1039 if (node->getFalseBlock())
1040 {
1041 out << "else\n"
1042 "{\n";
1043
1044 node->getFalseBlock()->traverse(this);
1045
1046 out << ";}\n";
1047 }
1048 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001049
1050 return false;
1051}
1052
1053void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
1054{
1055 TInfoSinkBase &out = context.infoSink.obj;
1056
alokp@chromium.orgdd037b22010-03-30 18:47:20 +00001057 const TType &type = node->getType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001058
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001059 if (type.isField())
1060 {
1061 out << type.getFieldName();
1062 }
1063 else
1064 {
1065 int size = type.getObjectSize();
1066 bool matrix = type.isMatrix();
1067 TBasicType basicType = node->getUnionArrayPointer()[0].getType();
1068
1069 switch (basicType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070 {
1071 case EbtBool:
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001072 if (!matrix)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001073 {
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001074 switch (size)
1075 {
1076 case 1: out << "bool("; break;
1077 case 2: out << "bool2("; break;
1078 case 3: out << "bool3("; break;
1079 case 4: out << "bool4("; break;
1080 default: UNREACHABLE();
1081 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001082 }
1083 else
1084 {
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001085 UNIMPLEMENTED();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001086 }
1087 break;
1088 case EbtFloat:
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001089 if (!matrix)
1090 {
1091 switch (size)
1092 {
1093 case 1: out << "float("; break;
1094 case 2: out << "float2("; break;
1095 case 3: out << "float3("; break;
1096 case 4: out << "float4("; break;
1097 default: UNREACHABLE();
1098 }
1099 }
1100 else
1101 {
1102 switch (size)
1103 {
1104 case 4: out << "float2x2("; break;
1105 case 9: out << "float3x3("; break;
1106 case 16: out << "float4x4("; break;
1107 default: UNREACHABLE();
1108 }
1109 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001110 break;
1111 case EbtInt:
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001112 if (!matrix)
1113 {
1114 switch (size)
1115 {
1116 case 1: out << "int("; break;
1117 case 2: out << "int2("; break;
1118 case 3: out << "int3("; break;
1119 case 4: out << "int4("; break;
1120 default: UNREACHABLE();
1121 }
1122 }
1123 else
1124 {
1125 UNIMPLEMENTED();
1126 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001127 break;
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001128 default:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001129 UNIMPLEMENTED(); // FIXME
1130 }
1131
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001132 for (int i = 0; i < size; i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001133 {
daniel@transgaming.com45d03582010-03-11 19:41:29 +00001134 switch (basicType)
1135 {
1136 case EbtBool:
1137 if (node->getUnionArrayPointer()[i].getBConst())
1138 {
1139 out << "true";
1140 }
1141 else
1142 {
1143 out << "false";
1144 }
1145 break;
1146 case EbtFloat:
1147 out << node->getUnionArrayPointer()[i].getFConst();
1148 break;
1149 case EbtInt:
1150 out << node->getUnionArrayPointer()[i].getIConst();
1151 break;
1152 default:
1153 UNIMPLEMENTED(); // FIXME
1154 }
1155
1156 if (i != size - 1)
1157 {
1158 out << ", ";
1159 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001160 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001161
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001162 out << ")";
1163 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001164}
1165
1166bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
1167{
1168 TInfoSinkBase &out = context.infoSink.obj;
1169
1170 if (!node->testFirst())
1171 {
1172 out << "do\n"
1173 "{\n";
1174 }
1175 else
1176 {
1177 out << "for(";
1178
1179 if (node->getInit())
1180 {
1181 node->getInit()->traverse(this);
1182 }
1183
1184 out << "; ";
1185
1186 if (node->getTest())
1187 {
1188 node->getTest()->traverse(this);
1189 }
1190
1191 out << "; ";
1192
1193 if (node->getTerminal())
1194 {
1195 node->getTerminal()->traverse(this);
1196 }
1197
1198 out << ")\n"
1199 "{\n";
1200 }
1201
1202 if (node->getBody())
1203 {
1204 node->getBody()->traverse(this);
1205 }
1206
1207 out << "}\n";
1208
1209 if (!node->testFirst())
1210 {
1211 out << "while(\n";
1212
1213 node->getTest()->traverse(this);
1214
1215 out << ")";
1216 }
1217
1218 out << ";\n";
1219
1220 return false;
1221}
1222
1223bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
1224{
1225 TInfoSinkBase &out = context.infoSink.obj;
1226
1227 switch (node->getFlowOp())
1228 {
daniel@transgaming.comf67f82e2010-03-17 03:58:54 +00001229 case EOpKill: outputTriplet(visit, "discard", NULL, NULL); break;
1230 case EOpBreak: outputTriplet(visit, "break", NULL, NULL); break;
1231 case EOpContinue: outputTriplet(visit, "continue", NULL, NULL); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001232 case EOpReturn:
1233 if (visit == PreVisit)
1234 {
1235 if (node->getExpression())
1236 {
1237 out << "return ";
1238 }
1239 else
1240 {
1241 out << "return;\n";
1242 }
1243 }
1244 else if (visit == PostVisit)
1245 {
1246 out << ";\n";
1247 }
1248 break;
1249 default: UNREACHABLE();
1250 }
1251
1252 return true;
1253}
1254
1255void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString)
1256{
1257 TInfoSinkBase &out = context.infoSink.obj;
1258
1259 if (visit == PreVisit && preString)
1260 {
1261 out << preString;
1262 }
1263 else if (visit == InVisit && inString)
1264 {
1265 out << inString;
1266 }
1267 else if (visit == PostVisit && postString)
1268 {
1269 out << postString;
1270 }
1271}
1272
1273TString OutputHLSL::typeString(const TType &type)
1274{
1275 if (type.isMatrix())
1276 {
1277 switch (type.getNominalSize())
1278 {
1279 case 2: return "float2x2";
1280 case 3: return "float3x3";
1281 case 4: return "float4x4";
1282 }
1283 }
1284 else
1285 {
1286 switch (type.getBasicType())
1287 {
1288 case EbtFloat:
1289 switch (type.getNominalSize())
1290 {
1291 case 1: return "float";
1292 case 2: return "float2";
1293 case 3: return "float3";
1294 case 4: return "float4";
1295 }
1296 case EbtInt:
1297 switch (type.getNominalSize())
1298 {
1299 case 1: return "int";
1300 case 2: return "int2";
1301 case 3: return "int3";
1302 case 4: return "int4";
1303 }
1304 case EbtBool:
1305 switch (type.getNominalSize())
1306 {
1307 case 1: return "bool";
1308 case 2: return "bool2";
1309 case 3: return "bool3";
1310 case 4: return "bool4";
1311 }
1312 case EbtVoid:
1313 return "void";
1314 case EbtSampler2D:
1315 return "sampler2D";
1316 case EbtSamplerCube:
1317 return "samplerCUBE";
1318 }
1319 }
1320
1321 UNIMPLEMENTED(); // FIXME
1322 return "<unknown type>";
1323}
1324
1325TString OutputHLSL::arrayString(const TType &type)
1326{
1327 if (!type.isArray())
1328 {
1329 return "";
1330 }
1331
1332 char buffer[100];
1333 sprintf(buffer, "[%d]", type.getArraySize());
1334
1335 return buffer;
1336}
1337
1338TString OutputHLSL::initializer(const TType &type)
1339{
1340 TString string;
1341
1342 int arraySize = type.isArray() ? type.getArraySize() : 1;
1343
1344 if (type.isArray())
1345 {
1346 string += "{";
1347 }
1348
1349 for (int element = 0; element < arraySize; element++)
1350 {
1351 string += typeString(type) + "(";
1352
1353 for (int component = 0; component < type.getNominalSize(); component++)
1354 {
1355 string += "0";
1356
1357 if (component < type.getNominalSize() - 1)
1358 {
1359 string += ", ";
1360 }
1361 }
1362
1363 string += ")";
1364
1365 if (element < arraySize - 1)
1366 {
1367 string += ", ";
1368 }
1369 }
1370
1371 if (type.isArray())
1372 {
1373 string += "}";
1374 }
1375
1376 return string;
1377}
1378}