GlslGenerator.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions;
  2. using Ryujinx.Graphics.Shader.StructuredIr;
  3. using Ryujinx.Graphics.Shader.Translation;
  4. using System;
  5. using static Ryujinx.Graphics.Shader.CodeGen.Glsl.TypeConversion;
  6. namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
  7. {
  8. static class GlslGenerator
  9. {
  10. private const string MainFunctionName = "main";
  11. public static string Generate(StructuredProgramInfo info, ShaderConfig config)
  12. {
  13. CodeGenContext context = new CodeGenContext(info, config);
  14. Declarations.Declare(context, info);
  15. if (info.Functions.Count != 0)
  16. {
  17. for (int i = 1; i < info.Functions.Count; i++)
  18. {
  19. context.AppendLine($"{GetFunctionSignature(context, info.Functions[i])};");
  20. }
  21. context.AppendLine();
  22. for (int i = 1; i < info.Functions.Count; i++)
  23. {
  24. PrintFunction(context, info, info.Functions[i]);
  25. context.AppendLine();
  26. }
  27. }
  28. PrintFunction(context, info, info.Functions[0], MainFunctionName);
  29. return context.GetCode();
  30. }
  31. private static void PrintFunction(CodeGenContext context, StructuredProgramInfo info, StructuredFunction function, string funcName = null)
  32. {
  33. context.CurrentFunction = function;
  34. context.AppendLine(GetFunctionSignature(context, function, funcName));
  35. context.EnterScope();
  36. Declarations.DeclareLocals(context, function);
  37. PrintBlock(context, function.MainBlock);
  38. context.LeaveScope();
  39. }
  40. private static string GetFunctionSignature(CodeGenContext context, StructuredFunction function, string funcName = null)
  41. {
  42. string[] args = new string[function.InArguments.Length + function.OutArguments.Length];
  43. for (int i = 0; i < function.InArguments.Length; i++)
  44. {
  45. args[i] = $"{Declarations.GetVarTypeName(context, function.InArguments[i])} {OperandManager.GetArgumentName(i)}";
  46. }
  47. for (int i = 0; i < function.OutArguments.Length; i++)
  48. {
  49. int j = i + function.InArguments.Length;
  50. args[j] = $"out {Declarations.GetVarTypeName(context, function.OutArguments[i])} {OperandManager.GetArgumentName(j)}";
  51. }
  52. return $"{Declarations.GetVarTypeName(context, function.ReturnType)} {funcName ?? function.Name}({string.Join(", ", args)})";
  53. }
  54. private static void PrintBlock(CodeGenContext context, AstBlock block)
  55. {
  56. AstBlockVisitor visitor = new AstBlockVisitor(block);
  57. visitor.BlockEntered += (sender, e) =>
  58. {
  59. switch (e.Block.Type)
  60. {
  61. case AstBlockType.DoWhile:
  62. context.AppendLine("do");
  63. break;
  64. case AstBlockType.Else:
  65. context.AppendLine("else");
  66. break;
  67. case AstBlockType.ElseIf:
  68. context.AppendLine($"else if ({GetCondExpr(context, e.Block.Condition)})");
  69. break;
  70. case AstBlockType.If:
  71. context.AppendLine($"if ({GetCondExpr(context, e.Block.Condition)})");
  72. break;
  73. default: throw new InvalidOperationException($"Found unexpected block type \"{e.Block.Type}\".");
  74. }
  75. context.EnterScope();
  76. };
  77. visitor.BlockLeft += (sender, e) =>
  78. {
  79. context.LeaveScope();
  80. if (e.Block.Type == AstBlockType.DoWhile)
  81. {
  82. context.AppendLine($"while ({GetCondExpr(context, e.Block.Condition)});");
  83. }
  84. };
  85. foreach (IAstNode node in visitor.Visit())
  86. {
  87. if (node is AstOperation operation)
  88. {
  89. string expr = InstGen.GetExpression(context, operation);
  90. if (expr != null)
  91. {
  92. context.AppendLine(expr + ";");
  93. }
  94. }
  95. else if (node is AstAssignment assignment)
  96. {
  97. AggregateType dstType = OperandManager.GetNodeDestType(context, assignment.Destination);
  98. AggregateType srcType = OperandManager.GetNodeDestType(context, assignment.Source);
  99. string dest = InstGen.GetExpression(context, assignment.Destination);
  100. string src = ReinterpretCast(context, assignment.Source, srcType, dstType);
  101. context.AppendLine(dest + " = " + src + ";");
  102. }
  103. else if (node is AstComment comment)
  104. {
  105. context.AppendLine("// " + comment.Comment);
  106. }
  107. else
  108. {
  109. throw new InvalidOperationException($"Found unexpected node type \"{node?.GetType().Name ?? "null"}\".");
  110. }
  111. }
  112. }
  113. private static string GetCondExpr(CodeGenContext context, IAstNode cond)
  114. {
  115. AggregateType srcType = OperandManager.GetNodeDestType(context, cond);
  116. return ReinterpretCast(context, cond, srcType, AggregateType.Bool);
  117. }
  118. }
  119. }