GlslGenerator.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. public static GlslProgram Generate(StructuredProgramInfo info, ShaderConfig config)
  12. {
  13. CodeGenContext context = new CodeGenContext(config);
  14. Declarations.Declare(context, info);
  15. PrintMainBlock(context, info);
  16. return new GlslProgram(
  17. context.CBufferDescriptors.ToArray(),
  18. context.SBufferDescriptors.ToArray(),
  19. context.TextureDescriptors.ToArray(),
  20. context.ImageDescriptors.ToArray(),
  21. context.GetCode());
  22. }
  23. private static void PrintMainBlock(CodeGenContext context, StructuredProgramInfo info)
  24. {
  25. context.AppendLine("void main()");
  26. context.EnterScope();
  27. Declarations.DeclareLocals(context, info);
  28. // Some games will leave some elements of gl_Position uninitialized,
  29. // in those cases, the elements will contain undefined values according
  30. // to the spec, but on NVIDIA they seems to be always initialized to (0, 0, 0, 1),
  31. // so we do explicit initialization to avoid UB on non-NVIDIA gpus.
  32. if (context.Config.Stage == ShaderStage.Vertex)
  33. {
  34. context.AppendLine("gl_Position = vec4(0.0, 0.0, 0.0, 1.0);");
  35. }
  36. // Ensure that unused attributes are set, otherwise the downstream
  37. // compiler may eliminate them.
  38. // (Not needed for fragment shader as it is the last stage).
  39. if (context.Config.Stage != ShaderStage.Compute &&
  40. context.Config.Stage != ShaderStage.Fragment)
  41. {
  42. for (int attr = 0; attr < Declarations.MaxAttributes; attr++)
  43. {
  44. if (info.OAttributes.Contains(attr))
  45. {
  46. continue;
  47. }
  48. context.AppendLine($"{DefaultNames.OAttributePrefix}{attr} = vec4(0);");
  49. }
  50. }
  51. PrintBlock(context, info.MainBlock);
  52. context.LeaveScope();
  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. context.AppendLine(InstGen.GetExpression(context, operation) + ";");
  90. }
  91. else if (node is AstAssignment assignment)
  92. {
  93. VariableType srcType = OperandManager.GetNodeDestType(assignment.Source);
  94. VariableType dstType = OperandManager.GetNodeDestType(assignment.Destination);
  95. string dest;
  96. if (assignment.Destination is AstOperand operand && operand.Type == OperandType.Attribute)
  97. {
  98. dest = OperandManager.GetOutAttributeName(operand, context.Config.Stage);
  99. }
  100. else
  101. {
  102. dest = InstGen.GetExpression(context, assignment.Destination);
  103. }
  104. string src = ReinterpretCast(context, assignment.Source, srcType, dstType);
  105. context.AppendLine(dest + " = " + src + ";");
  106. }
  107. else if (node is AstComment comment)
  108. {
  109. context.AppendLine("// " + comment.Comment);
  110. }
  111. else
  112. {
  113. throw new InvalidOperationException($"Found unexpected node type \"{node?.GetType().Name ?? "null"}\".");
  114. }
  115. }
  116. }
  117. private static string GetCondExpr(CodeGenContext context, IAstNode cond)
  118. {
  119. VariableType srcType = OperandManager.GetNodeDestType(cond);
  120. return ReinterpretCast(context, cond, srcType, VariableType.Bool);
  121. }
  122. }
  123. }