GlslGenerator.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions;
  2. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  3. using Ryujinx.Graphics.Shader.StructuredIr;
  4. using Ryujinx.Graphics.Shader.Translation;
  5. using System;
  6. using static Ryujinx.Graphics.Shader.CodeGen.Glsl.TypeConversion;
  7. namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
  8. {
  9. static class GlslGenerator
  10. {
  11. private const string MainFunctionName = "main";
  12. public static string Generate(StructuredProgramInfo info, ShaderConfig config)
  13. {
  14. CodeGenContext context = new CodeGenContext(info, config);
  15. Declarations.Declare(context, info);
  16. if (info.Functions.Count != 0)
  17. {
  18. for (int i = 1; i < info.Functions.Count; i++)
  19. {
  20. context.AppendLine($"{GetFunctionSignature(info.Functions[i])};");
  21. }
  22. context.AppendLine();
  23. for (int i = 1; i < info.Functions.Count; i++)
  24. {
  25. PrintFunction(context, info, info.Functions[i]);
  26. context.AppendLine();
  27. }
  28. }
  29. PrintFunction(context, info, info.Functions[0], MainFunctionName);
  30. return context.GetCode();
  31. }
  32. private static void PrintFunction(CodeGenContext context, StructuredProgramInfo info, StructuredFunction function, string funcName = null)
  33. {
  34. context.CurrentFunction = function;
  35. context.AppendLine(GetFunctionSignature(function, funcName));
  36. context.EnterScope();
  37. Declarations.DeclareLocals(context, function);
  38. if (funcName == MainFunctionName)
  39. {
  40. // Some games will leave some elements of gl_Position uninitialized,
  41. // in those cases, the elements will contain undefined values according
  42. // to the spec, but on NVIDIA they seems to be always initialized to (0, 0, 0, 1),
  43. // so we do explicit initialization to avoid UB on non-NVIDIA gpus.
  44. if (context.Config.Stage == ShaderStage.Vertex)
  45. {
  46. context.AppendLine("gl_Position = vec4(0.0, 0.0, 0.0, 1.0);");
  47. }
  48. // Ensure that unused attributes are set, otherwise the downstream
  49. // compiler may eliminate them.
  50. // (Not needed for fragment shader as it is the last stage).
  51. if (context.Config.Stage != ShaderStage.Compute &&
  52. context.Config.Stage != ShaderStage.Fragment &&
  53. !context.Config.GpPassthrough)
  54. {
  55. for (int attr = 0; attr < Declarations.MaxAttributes; attr++)
  56. {
  57. if (info.OAttributes.Contains(attr))
  58. {
  59. continue;
  60. }
  61. if ((context.Config.Flags & TranslationFlags.Feedback) != 0)
  62. {
  63. context.AppendLine($"{DefaultNames.OAttributePrefix}{attr}_x = 0.0;");
  64. context.AppendLine($"{DefaultNames.OAttributePrefix}{attr}_y = 0.0;");
  65. context.AppendLine($"{DefaultNames.OAttributePrefix}{attr}_z = 0.0;");
  66. context.AppendLine($"{DefaultNames.OAttributePrefix}{attr}_w = 1.0;");
  67. }
  68. else
  69. {
  70. context.AppendLine($"{DefaultNames.OAttributePrefix}{attr} = vec4(0.0, 0.0, 0.0, 1.0);");
  71. }
  72. }
  73. }
  74. }
  75. PrintBlock(context, function.MainBlock);
  76. context.LeaveScope();
  77. }
  78. private static string GetFunctionSignature(StructuredFunction function, string funcName = null)
  79. {
  80. string[] args = new string[function.InArguments.Length + function.OutArguments.Length];
  81. for (int i = 0; i < function.InArguments.Length; i++)
  82. {
  83. args[i] = $"{Declarations.GetVarTypeName(function.InArguments[i])} {OperandManager.GetArgumentName(i)}";
  84. }
  85. for (int i = 0; i < function.OutArguments.Length; i++)
  86. {
  87. int j = i + function.InArguments.Length;
  88. args[j] = $"out {Declarations.GetVarTypeName(function.OutArguments[i])} {OperandManager.GetArgumentName(j)}";
  89. }
  90. return $"{Declarations.GetVarTypeName(function.ReturnType)} {funcName ?? function.Name}({string.Join(", ", args)})";
  91. }
  92. private static void PrintBlock(CodeGenContext context, AstBlock block)
  93. {
  94. AstBlockVisitor visitor = new AstBlockVisitor(block);
  95. visitor.BlockEntered += (sender, e) =>
  96. {
  97. switch (e.Block.Type)
  98. {
  99. case AstBlockType.DoWhile:
  100. context.AppendLine("do");
  101. break;
  102. case AstBlockType.Else:
  103. context.AppendLine("else");
  104. break;
  105. case AstBlockType.ElseIf:
  106. context.AppendLine($"else if ({GetCondExpr(context, e.Block.Condition)})");
  107. break;
  108. case AstBlockType.If:
  109. context.AppendLine($"if ({GetCondExpr(context, e.Block.Condition)})");
  110. break;
  111. default: throw new InvalidOperationException($"Found unexpected block type \"{e.Block.Type}\".");
  112. }
  113. context.EnterScope();
  114. };
  115. visitor.BlockLeft += (sender, e) =>
  116. {
  117. context.LeaveScope();
  118. if (e.Block.Type == AstBlockType.DoWhile)
  119. {
  120. context.AppendLine($"while ({GetCondExpr(context, e.Block.Condition)});");
  121. }
  122. };
  123. foreach (IAstNode node in visitor.Visit())
  124. {
  125. if (node is AstOperation operation)
  126. {
  127. context.AppendLine(InstGen.GetExpression(context, operation) + ";");
  128. }
  129. else if (node is AstAssignment assignment)
  130. {
  131. VariableType srcType = OperandManager.GetNodeDestType(context, assignment.Source);
  132. VariableType dstType = OperandManager.GetNodeDestType(context, assignment.Destination);
  133. string dest;
  134. if (assignment.Destination is AstOperand operand && operand.Type == OperandType.Attribute)
  135. {
  136. dest = OperandManager.GetOutAttributeName(operand, context.Config);
  137. }
  138. else
  139. {
  140. dest = InstGen.GetExpression(context, assignment.Destination);
  141. }
  142. string src = ReinterpretCast(context, assignment.Source, srcType, dstType);
  143. context.AppendLine(dest + " = " + src + ";");
  144. }
  145. else if (node is AstComment comment)
  146. {
  147. context.AppendLine("// " + comment.Comment);
  148. }
  149. else
  150. {
  151. throw new InvalidOperationException($"Found unexpected node type \"{node?.GetType().Name ?? "null"}\".");
  152. }
  153. }
  154. }
  155. private static string GetCondExpr(CodeGenContext context, IAstNode cond)
  156. {
  157. VariableType srcType = OperandManager.GetNodeDestType(context, cond);
  158. return ReinterpretCast(context, cond, srcType, VariableType.Bool);
  159. }
  160. }
  161. }