GlslGenerator.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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}_x = 0;");
  49. context.AppendLine($"{DefaultNames.OAttributePrefix}{attr}_y = 0;");
  50. context.AppendLine($"{DefaultNames.OAttributePrefix}{attr}_z = 0;");
  51. context.AppendLine($"{DefaultNames.OAttributePrefix}{attr}_w = 0;");
  52. }
  53. }
  54. PrintBlock(context, info.MainBlock);
  55. context.LeaveScope();
  56. }
  57. private static void PrintBlock(CodeGenContext context, AstBlock block)
  58. {
  59. AstBlockVisitor visitor = new AstBlockVisitor(block);
  60. visitor.BlockEntered += (sender, e) =>
  61. {
  62. switch (e.Block.Type)
  63. {
  64. case AstBlockType.DoWhile:
  65. context.AppendLine("do");
  66. break;
  67. case AstBlockType.Else:
  68. context.AppendLine("else");
  69. break;
  70. case AstBlockType.ElseIf:
  71. context.AppendLine($"else if ({GetCondExpr(context, e.Block.Condition)})");
  72. break;
  73. case AstBlockType.If:
  74. context.AppendLine($"if ({GetCondExpr(context, e.Block.Condition)})");
  75. break;
  76. default: throw new InvalidOperationException($"Found unexpected block type \"{e.Block.Type}\".");
  77. }
  78. context.EnterScope();
  79. };
  80. visitor.BlockLeft += (sender, e) =>
  81. {
  82. context.LeaveScope();
  83. if (e.Block.Type == AstBlockType.DoWhile)
  84. {
  85. context.AppendLine($"while ({GetCondExpr(context, e.Block.Condition)});");
  86. }
  87. };
  88. foreach (IAstNode node in visitor.Visit())
  89. {
  90. if (node is AstOperation operation)
  91. {
  92. context.AppendLine(InstGen.GetExpression(context, operation) + ";");
  93. }
  94. else if (node is AstAssignment assignment)
  95. {
  96. VariableType srcType = OperandManager.GetNodeDestType(assignment.Source);
  97. VariableType dstType = OperandManager.GetNodeDestType(assignment.Destination);
  98. string dest;
  99. if (assignment.Destination is AstOperand operand && operand.Type == OperandType.Attribute)
  100. {
  101. dest = OperandManager.GetOutAttributeName(operand, context.Config.Stage);
  102. }
  103. else
  104. {
  105. dest = InstGen.GetExpression(context, assignment.Destination);
  106. }
  107. string src = ReinterpretCast(context, assignment.Source, srcType, dstType);
  108. context.AppendLine(dest + " = " + src + ";");
  109. }
  110. else if (node is AstComment comment)
  111. {
  112. context.AppendLine("// " + comment.Comment);
  113. }
  114. else
  115. {
  116. throw new InvalidOperationException($"Found unexpected node type \"{node?.GetType().Name ?? "null"}\".");
  117. }
  118. }
  119. }
  120. private static string GetCondExpr(CodeGenContext context, IAstNode cond)
  121. {
  122. VariableType srcType = OperandManager.GetNodeDestType(cond);
  123. return ReinterpretCast(context, cond, srcType, VariableType.Bool);
  124. }
  125. }
  126. }