-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGLSLGenerator.cs
728 lines (568 loc) · 25.7 KB
/
GLSLGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
// /home/vfpamplona/MonoProjects/hl2glsl/hl2glsl/GLSLGenerator.cs created with MonoDevelop
// User: vfpamplona at 11:15 7/2/2008
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//
using System;
using System.Reflection;
using System.Collections;
using PerCederberg.Grammatica.Parser;
namespace hl2glsl
{
internal class GLSLGenerator : HlslAnalyzer {
// list of externalizable mainFunctions. Every one will generate a new file
private ArrayList mainFunctions;
private string activeMainFunction;
private bool isMain = false;
private bool replaceReturn = false;
private string replaceReturnFor;
// list of global vars found in shader
private ArrayList globalVars;
// to replace indentifiers.
// used for example to replace semantic variables:
// from "out float4 oColor : COLOR" to "glFragColor"
private Hashtable replaceIdentifier = new Hashtable();
private bool removeRegAndPack = false;
// dependency tree
private DependencyGraph dependencyGraph;
// temporary attribute
private DependencyGraphNode functionScope;
private ArrayList scopeVars;
// intrinsic functions that are not supported by GLSL, thus, can not be converted
private string [] nsIntrinsicFunctions = {"asuint","clip","cosh","D3DCOLORtoUBYTE4","determinant","frexp",
"GetRenderTargetSampleCount","GetRenderTargetSamplePosition",
"isfinite","isinf","isnan","ldexp","lit","log10",
"modf","round","sincos","sinh","tanh","tex1Dbias",
"tex1Dgrad","tex2Dbias","tex2Dgrad","tex3Dbias","tex3Dgrad",
"texCUBEbias","texCUBEgrad","transpose","trunc"};
// intrinsic functions that are supported by GLSL but with a different name
private Hashtable eqIntrinsicFunctions = new Hashtable();
private bool CheckAndReplace(Token node, String strHLSL, String strGLSL) {
if (node.GetImage().Equals(strHLSL)) {
node.AddValue(strGLSL);
return true;
}
return false;
}
private bool CheckAndReplace(Token node, Hashtable replacement) {
// Loop through all items of a Hashtable
IDictionaryEnumerator en = replacement.GetEnumerator();
while (en.MoveNext()) {
if (CheckAndReplace(node, en.Key.ToString(), en.Value.ToString()))
return true;
}
return false;
}
private Node replaceIntrinsicFunc(Token node)
{
ArrayList nsif = new ArrayList();
nsif.AddRange(nsIntrinsicFunctions);
if (nsif.Contains(node.GetImage()))
Console.WriteLine("This intrinsic function ({0})is not supported by GLSL", node.GetImage().ToString());
CheckAndReplace(node, eqIntrinsicFunctions);
return node;
}
public DependencyGraphNode SearchInDependencyGraph(string dependantName) {
return dependencyGraph.SearchDependant(dependantName);
}
public void PrintCallTree() {
dependencyGraph.PrintCallTree();
}
public ArrayList GetWhoThisFunctionCalls(string func) {
return dependencyGraph.GetWhoThisFunctionCalls(func);
}
/**
* Class witch replaces HLSL code to GLSL ones.
*
* Override a method Exit<Token> and fix the token for GLSL compiler.
*/
public GLSLGenerator(ArrayList _mainFunctions) {
mainFunctions = _mainFunctions;
globalVars = new ArrayList();
dependencyGraph = new DependencyGraph();
activeMainFunction = mainFunctions[0].ToString();
scopeVars = new ArrayList();
eqIntrinsicFunctions.Add("asfloat","float");
eqIntrinsicFunctions.Add("asint","int");
eqIntrinsicFunctions.Add("atan2","atan");
eqIntrinsicFunctions.Add("ddx","dFdx");
eqIntrinsicFunctions.Add("ddy","dFdy");
eqIntrinsicFunctions.Add("fmod","mod");
eqIntrinsicFunctions.Add("frac", "fract");
eqIntrinsicFunctions.Add("lerp", "mix");
eqIntrinsicFunctions.Add("noise", "noise1");
eqIntrinsicFunctions.Add("rsqrt","inversesqrt");
eqIntrinsicFunctions.Add("tex1D","texture1D");
eqIntrinsicFunctions.Add("tex1Dlod","texture1DLod");
eqIntrinsicFunctions.Add("tex1Dproj","texture1DProj");
eqIntrinsicFunctions.Add("tex2D","texture2D");
eqIntrinsicFunctions.Add("tex2Dlod","texture2DLod");
eqIntrinsicFunctions.Add("tex2Dproj","texture2DProj");
eqIntrinsicFunctions.Add("tex3D","texture3D");
eqIntrinsicFunctions.Add("tex3Dlod","texture3DLod");
eqIntrinsicFunctions.Add("tex3Dproj","texture3DProj");
eqIntrinsicFunctions.Add("texCube", "textureCube");
eqIntrinsicFunctions.Add("texCubelod", "textureCubeLod");
eqIntrinsicFunctions.Add("texCubeproj","textureCubeProj");
}
public override Node ExitPreInclude(Token node)
{
throw new UnsupportedTokenException(node.ToString());
//return node;
}
public override Node ExitPreElseif(Token node)
{
node.AddValue("#elif");
return node;
}
public override Node ExitFloat(Token node) {
CheckAndReplace(node, "float2","vec2");
CheckAndReplace(node, "float3","vec3");
CheckAndReplace(node, "float4","vec4");
CheckAndReplace(node, "float2x2","mat2");
CheckAndReplace(node, "float2x3","mat2x3");
CheckAndReplace(node, "float2x4","mat2x4");
CheckAndReplace(node, "float3x2","mat3x2");
CheckAndReplace(node, "float3x3","mat3");
CheckAndReplace(node, "float3x4","mat3x4");
CheckAndReplace(node, "float4x2","mat4x2");
CheckAndReplace(node, "float4x3","mat4x3");
CheckAndReplace(node, "float4x4","mat4");
return node;
}
public override Node ExitBool(Token node) {
CheckAndReplace(node, "bool2","bvec2");
CheckAndReplace(node, "bool3","bvec3");
CheckAndReplace(node, "bool4","bvec4");
return node;
}
public override Node ExitInt(Token node) {
CheckAndReplace(node, "int2","ivec2");
CheckAndReplace(node, "int3","ivec3");
CheckAndReplace(node, "int4","ivec4");
return node;
}
public override Node ExitHalf(Token node) {
CheckAndReplace(node, "half2", "ivec2");
CheckAndReplace(node, "half3","ivec3");
CheckAndReplace(node, "half4","ivec4");
return node;
}
public override Node ExitIn(Token node) {
node.AddValue("");
return node;
}
public override Node ExitOut(Token node) {
node.AddValue("");
return node;
}
public override Node ExitInout(Token node) {
node.AddValue("");
return node;
}
public override Node ExitRegisterFunc(Production node)
{
Console.WriteLine("GLSL does not support direct access to registers, so, you can't use register.");
removeRegAndPack = true;
return node;
}
public override Node ExitPackoffsetFunc(Production node)
{
Console.WriteLine("GLSL does not support direct access to registers, so, you can't use packoffset.");
removeRegAndPack = true;
return node;
}
public override Node ExitBasicUint(Token node)
{
Console.WriteLine("GLSL does not support 'uint'. Replacing with 'int'.");
CheckAndReplace(node, "uint", "int");
return base.ExitBasicUint(node);
}
/**
* Move the global vars to outside function declaration.
*/
public override Node ExitFile(Production node) {
ArrayList lista = GrammaticaNodeUtils.GetChildren(node);
Node mainFunction = null;
for (int i=0; i<lista.Count; i++) {
mainFunction = (Node)lista[i];
if (mainFunction.GetName().Equals("Function_OR_Variable_Declaration")) {
// move related comment in previous line to inside this function.
GrammaticaNodeUtils.MovePreviousLineRelatedCommentToInsideANode(node, i, (Production)mainFunction);
}
}
// search for the first main function.
for (int i=0; i<lista.Count; i++) {
mainFunction = (Node)lista[i];
if (mainFunction.GetName().Equals("Function_OR_Variable_Declaration")) {
if (mainFunctions.Contains(((Token)GrammaticaNodeUtils.FindChildOf(mainFunction, "IDENTIFIER")).GetImage())) {
break;
}
}
}
// put the global vars before the first MainFunction
foreach (Node var in globalVars) {
lista.Insert(lista.IndexOf(mainFunction), var);
//GrammaticaNodeUtils.SetParent(var, node);
}
return node;
}
public override Node ExitParameters(Production node) {
ArrayList listOfParams = GrammaticaNodeUtils.GetChildren(node);
ArrayList trash = new ArrayList();
for (int i=0; i<listOfParams.Count; i++) {
if ((((Node)listOfParams[i]).GetName().Equals("WS")))
trash.Add(listOfParams[i]);
}
for (int j=0; j<trash.Count; j++) {
listOfParams.Remove(trash[j]);
}
return node;
}
public bool MarkToReplaceIfNodeIsOfTokenType(Production node, string tokenType, string to) {
Node isOfType = GrammaticaNodeUtils.FindChildOf(node,
new string[4] {"Variable_Declaration", "SemanticalParameters", "InOutSemanticalParameters", tokenType});
if (isOfType == null)
isOfType = GrammaticaNodeUtils.FindChildOf(node,
new string[4] {"Variable_Declaration", "SemanticalParameters", "InputSemanticalParameters", tokenType});
if (isOfType == null)
isOfType = GrammaticaNodeUtils.FindChildOf(node,
new string[4] {"Variable_Declaration", "SemanticalParameters", "OutputSemanticalParameters", tokenType});
if (isOfType != null) {
Token name = (Token) GrammaticaNodeUtils.FindChildOf(node, new string[2] {"Variable_Declaration", "IDENTIFIER"});
replaceIdentifier.Add(name.GetImage(), to);
return true;
}
return false;
}
public bool MarkToReplaceIfNodeIsOfTokenType(Production node, string tokenType, string to, bool isIN) {
Node isOfType = GrammaticaNodeUtils.FindChildOf(node,
new string[4] {"Variable_Declaration", "SemanticalParameters", "InOutSemanticalParameters", tokenType});
if (isOfType == null)
isOfType = GrammaticaNodeUtils.FindChildOf(node,
new string[4] {"Variable_Declaration", "SemanticalParameters", "InputSemanticalParameters", tokenType});
if (isOfType == null)
isOfType = GrammaticaNodeUtils.FindChildOf(node,
new string[4] {"Variable_Declaration", "SemanticalParameters", "OutputSemanticalParameters", tokenType});
bool ok = false;
if (isIN) {
if (GrammaticaNodeUtils.FindChildOf(node, new string [2] {"In_out_inout", "INOUT"}) != null
|| GrammaticaNodeUtils.FindChildOf(node, new string [2] {"In_out_inout", "IN"}) != null
|| GrammaticaNodeUtils.FindChildOf(node, new string [2] {"In_out_inout", "OUT"}) == null
|| GrammaticaNodeUtils.FindChildOf(node, new string [1] {"In_out_inout"}) == null
)
ok = true;
} else {
if (GrammaticaNodeUtils.FindChildOf(node, new string [2] {"In_out_inout", "INOUT"}) != null
|| GrammaticaNodeUtils.FindChildOf(node, new string [2] {"In_out_inout", "OUT"}) != null
)
ok = true;
}
if (ok && isOfType != null) {
Token name = (Token) GrammaticaNodeUtils.FindChildOf(node, new string[2] {"Variable_Declaration", "IDENTIFIER"});
replaceIdentifier.Add(name.GetImage(), to);
return true;
}
return false;
}
public Node RemoveTypeFromMain(Production node)
{
Production tempType = (Production) GrammaticaNodeUtils.FindChildOf(node, "Type");
ArrayList tempVoid = GrammaticaNodeUtils.GetChildren(tempType);
if (tempVoid != null)
{
tempVoid.RemoveAt(0);
tempVoid.Insert(0, GrammaticaNodeUtils.CreateVoidToken());
}
return node;
}
public Node RemoveSemanticParams(Production node) {
ArrayList lista = GrammaticaNodeUtils.GetChildren(node);
ArrayList trash = new ArrayList();
for (int i=0; i<lista.Count; i++) {
if ((((Node)lista[i]).GetName().Equals("DOUBLE_DOT")))
trash.Add(lista[i]);
if ((((Node)lista[i]).GetName().Equals("SemanticalParameters")))
trash.Add(lista[i]);
if ((((Node)lista[i]).GetName().Equals("Register_Func")))
trash.Add(lista[i]);
if ((((Node)lista[i]).GetName().Equals("Packoffset_Func")))
trash.Add(lista[i]);
}
for (int j=0; j<trash.Count; j++) {
lista.Remove(trash[j]);
}
trash = new ArrayList();
// Remove spaces left
for (int i=lista.Count-1; i>=0; i--) {
if ((((Node)lista[i]).GetName().Equals("WS")))
trash.Add(lista[i]);
else
break;
}
for (int j=0; j<trash.Count; j++) {
lista.Remove(trash[j]);
}
return node;
}
public Node CheckIfThisNodeIsSemanticAndRemoveIfItIs(Production node) {
if (MarkToReplaceIfNodeIsOfTokenType(node, "COLOR", "gl_FragColor")
|| MarkToReplaceIfNodeIsOfTokenType(node, "NORMAL", "gl_Normal")
|| MarkToReplaceIfNodeIsOfTokenType(node, "POSITION", "gl_Position", false)
|| MarkToReplaceIfNodeIsOfTokenType(node, "POSITION", "gl_Vertex", true)
|| MarkToReplaceIfNodeIsOfTokenType(node, "TEXCOORD0", "gl_MultiTexCoord0", true)
) {
//GrammaticaNodeUtils.GetChildren((Production)GrammaticaNodeUtils.FindChildOf(node, "Variable_Declaration")).;
return node;
}
return RemoveSemanticParams(node);
}
// For each param in every main HLSL function
// Move the uniform and varying params outside the function with his own comments
public override Node ExitListOfParams(Production node) {
ArrayList listOfParams = GrammaticaNodeUtils.GetChildren(node);
// Only main mainFunctions.
// Based on the presence of in, out or inout keywords
if (!isMain) return node;
isMain = false;
// Searching params that will be removed at ExitFunctionParam
for (int i=0; i<listOfParams.Count; i++) {
if (!(listOfParams[i] is Production))
continue;
Production functionParam = (Production)listOfParams[i];
functionParam = (Production)CheckIfThisNodeIsSemanticAndRemoveIfItIs(functionParam);
// Only uniform params are moved
Node n = GrammaticaNodeUtils.FindChildOf(functionParam,
new string[1] {"Variable_Declaration"});
if (n != null) { // Param found
// Getting childrens of it
Production variableDeclaration = (Production) n;
variableDeclaration = (Production)RemoveSemanticParams(variableDeclaration);
ArrayList variableDeclarationChildren = GrammaticaNodeUtils.GetChildren(variableDeclaration);
// add dot_comma and new line
variableDeclarationChildren.Add(GrammaticaNodeUtils.CreateDotCommaToken());
// move related comment in previous line to inside this param.
GrammaticaNodeUtils.MovePreviousLineRelatedCommentToInsideANode(node, i, variableDeclaration);
// move related commnet in the same line after the comma but before \n to this param.
GrammaticaNodeUtils.MoveSameLineRelatedCommentToInsideANode(node, i, variableDeclaration);
// add varying to all non-uniform parameters
Node hasUniform = GrammaticaNodeUtils.FindChildOf(variableDeclaration,
new string[2] {"Storage_Class", "UNIFORM"});
if (hasUniform == null) {
GrammaticaNodeUtils.GetChildren(variableDeclaration).Insert(0, GrammaticaNodeUtils.CreateSpaceToken());
GrammaticaNodeUtils.GetChildren(variableDeclaration).Insert(0, GrammaticaNodeUtils.CreateVaryingToken());
GrammaticaNodeUtils.GetChildren(variableDeclaration).Insert(0, GrammaticaNodeUtils.CreateNewLineToken());
}
// move uniform to outside.
globalVars.Add(variableDeclaration);
// remove from param.
GrammaticaNodeUtils.GetChildren(functionParam).Remove(variableDeclaration);
}
}
ArrayList trash = new ArrayList();
// cleaning the trash: Spaces, commas and newlines before the ) of the function
for (int i=listOfParams.Count-1; i>=0; i--) {
if (listOfParams[i] is Production) {
if (((Production)listOfParams[i]).GetChildCount() == 0
|| ((Production)listOfParams[i]).GetName().Equals("WS") ) {
trash.Add(listOfParams[i]);
} else {
break;
}
}
if (listOfParams[i] is Token) {
if (((Token)listOfParams[i]).GetName().Equals("COMMA")) {
trash.Add(listOfParams[i]);
}
}
}
for (int j=0; j<trash.Count; j++) {
listOfParams.Remove(trash[j]);
}
return node;
}
public override Node ExitFunctionConstructorCallOrVariableDeclaration(Production node) {
Token identifier = (Token) GrammaticaNodeUtils.FindChildOf(node, new string[2] {"Type", "IDENTIFIER"});
if (identifier == null) return node;
replaceIntrinsicFunc(identifier);
// mul(term, term) => term * term
if (identifier.GetImage().Equals("mul")) {
identifier.AddValue("");
Node virgula = GrammaticaNodeUtils.FindChildOf(node, new string[2] {"PartOf_Constructor_Call", "COMMA"});
virgula.AddValue(" * ");
}
// cross(T,N) => cross(N,T).
if (identifier.GetImage().Equals("cross")) {
Production listOfParam = (Production) GrammaticaNodeUtils.FindChildOf(node, "PartOf_Constructor_Call");
Node exp1 = GrammaticaNodeUtils.FindChildOf(listOfParam, "Expression", 1);
Node exp2 = GrammaticaNodeUtils.FindChildOf(listOfParam, "Expression", 2);
GrammaticaNodeUtils.SwapChildrenPosition(listOfParam, exp1, exp2);
}
// saturate(x) => clamp(x,0.0,1.0).
if (identifier.GetImage().Equals("saturate"))
{
identifier.AddValue("clamp");
var listOfParam = (Production)GrammaticaNodeUtils.FindChildOf(node, "PartOf_Constructor_Call");
var children = GrammaticaNodeUtils.GetChildren(listOfParam);
children.Insert(2, GrammaticaNodeUtils.CreateCommaToken());
children.Insert(3, GrammaticaNodeUtils.CreateNumberToken(0.0f));
children.Insert(4, GrammaticaNodeUtils.CreateCommaToken());
children.Insert(5, GrammaticaNodeUtils.CreateNumberToken(1.0f));
}
// add dependent function
Node n = GrammaticaNodeUtils.FindChildOf(node, "PartOf_Constructor_Call");
if (n != null && identifier != null && functionScope != null) {
// function call or constructor call.
dependencyGraph.SearchDependant(identifier.GetImage()).AddCallsBy(functionScope);
}
// Adds only globalVars.
if (!scopeVars.Contains(identifier.GetImage()))
dependencyGraph.SearchDependant(identifier.GetImage()).AddCallsBy(functionScope);
return node;
}
public override Node ExitNumber(Token node)
{
if (node.GetImage().Contains("f"))
{
string temp = node.GetImage();
node.RemoveAllValues();
node.AddValue(temp.Replace("f",""));
}
return base.ExitNumber(node);
}
public override Node ExitAtom(Production node) {
Token identifier = (Token) GrammaticaNodeUtils.FindChildOf(node, new string[2] {"Type", "IDENTIFIER"});
// Adds only globalVars.
if (identifier != null && !scopeVars.Contains(identifier.GetImage()))
dependencyGraph.SearchDependant(identifier.GetImage()).AddCallsBy(functionScope);
ArrayList identifiers = GrammaticaNodeUtils.FindChildrenOf(node, new string[2] {"Identifier_Composed_Required", "IDENTIFIER"});
// Adds only globalVars.
for (int i=0; i<identifiers.Count; i++) {
if (!scopeVars.Contains(((Token)identifiers[i]).GetImage()))
dependencyGraph.SearchDependant(((Token)identifiers[i]).GetImage()).AddCallsBy(functionScope);
}
return node;
}
public override Node ExitPartOfVariableDeclaration(Production node) {
// record all variable declarations inside this function.
Token variableDeclaration = (Token) GrammaticaNodeUtils.FindChildOf(node, new string[1] {"IDENTIFIER"});
if (variableDeclaration != null) {
//Console.WriteLine(variableDeclaration.GetImage());
scopeVars.Add(variableDeclaration.GetImage());
if(removeRegAndPack == true)
RemoveSemanticParams(node);
}
return node;
}
public override Node ExitVariableAssignment(Production node)
{
return base.ExitVariableAssignment(node);
}
public override Node ExitIdentifier(Token node) {
CheckAndReplace(node, "tex2D", "texture2D");
CheckAndReplace(node, replaceIdentifier);
if (node.GetImage().Equals(activeMainFunction))
{
isMain = true;
replaceReturn = true;
}
return node;
}
/**
* Creates a Graph of function calls to decide to what file goes each
* function when the HLSL have more than one main function.
*/
public override void EnterFunctionOrVariableDeclaration(Production node) {
functionScope = new DependencyGraphNode("Nova Função");
dependencyGraph.AddDependant(functionScope);
scopeVars = new ArrayList();
replaceIdentifier = new Hashtable();
}
public override Node ExitFunctionOrVariableDeclaration(Production node) {
DependencyGraphNode antiga = dependencyGraph.SearchDependant(((Token)GrammaticaNodeUtils.FindChildOf(node, "IDENTIFIER")).GetImage());
// se a função já existir
if (antiga != null) {
// trocar a nova pela n em todo o grafo.
dependencyGraph.ReplaceDependant("Nova Função", antiga);
} else {
dependencyGraph.SearchDependant("Nova Função").SetDependencyName(((Token)GrammaticaNodeUtils.FindChildOf(node, "IDENTIFIER")).GetImage());
}
functionScope = null;
if (((Token)GrammaticaNodeUtils.FindChildOf(node, "IDENTIFIER")).GetImage().Equals(activeMainFunction))
{
RemoveTypeFromMain(node);
}
scopeVars = new ArrayList();
replaceIdentifier = new Hashtable();
return node;
}
public override Node ExitStatement(Production node)
{
if (replaceReturn == false) return node;
Node n = GrammaticaNodeUtils.FindChildOf(node, new string[1] { "Return_Statement" });
if (n != null)
{
Node returnStatement = GrammaticaNodeUtils.FindChildOf(n, new string[1] { "RETURN" });
if (returnStatement != null)
{
ArrayList returnStatementChildren = GrammaticaNodeUtils.GetChildren((Production)n);
returnStatementChildren.Insert(1, GrammaticaNodeUtils.CreateEqualToken());
returnStatementChildren.Insert(1, GrammaticaNodeUtils.CreateSpaceToken());
}
}
return node;
}
public bool MarkIfNodeIsFuncSemanticalParameter(Production node, string tokenType, string to)
{
ArrayList lista = GrammaticaNodeUtils.GetChildren(node);
for (int i = 0; i < lista.Count; i++)
{
if ((((Node)lista[i]).GetName().Equals("SemanticalParameters")))
{
Token name = (Token) GrammaticaNodeUtils.FindChildOf(node, new string[3] {"SemanticalParameters", "InOutSemanticalParameters", tokenType});
if (name != null)
{
replaceReturnFor=to;
return true;
}
return false;
}
}
return false;
}
public override Node ExitFunctionPart(Production node)
{
if (MarkIfNodeIsFuncSemanticalParameter(node, "COLOR", "gl_FragColor")
|| MarkIfNodeIsFuncSemanticalParameter(node, "NORMAL", "gl_Normal")
// || MarkIfNodeIsFuncSemanticalParameter(node, "POSITION", "gl_Position", false)
// || MarkIfNodeIsFuncSemanticalParameter(node, "POSITION", "gl_Vertex", true)
// || MarkIfNodeIsFuncSemanticalParameter(node, "TEXCOORD0", "gl_MultiTexCoord0", true)
)
{
RemoveSemanticParams(node);
}
if (replaceReturn == true)
{
replaceReturn = false;
Node funcBody = GrammaticaNodeUtils.FindChildOf(node, "Function_Body");
if (funcBody != null)
{
for (int i = 0; i < funcBody.GetChildCount(); i++)
{
Node tempStatement = GrammaticaNodeUtils.FindChildOf(funcBody.GetChildAt(i), "Return_Statement");
if (tempStatement != null)
{
Token tempReturn = (Token)GrammaticaNodeUtils.FindChildOf(tempStatement, "RETURN");
if (tempReturn != null)
{
tempReturn.RemoveAllValues();
tempReturn.AddValue(replaceReturnFor);
}
}
}
}
}
return base.ExitFunctionPart(node);
}
}
}